anchour's avatar

Login cest not working in Codecept test, but works in the browser.

I'm not quite sure what I'm missing here. My test seems simple enough - I'm just not sure why it's failing on the it_logs_the_user_in test.

Here's my test:

<?php 
// ...
public function it_logs_the_user_in(FunctionalTester $I)
    {
        $I->am('guest');
        $I->amGoingTo('log into the dashboard');
        $I->haveEnabledFilters();
        $I->dontSeeAuthentication();

        $I->amOnRoute('auth.login');
        $I->seeCurrentUrlEquals('/auth/login');

        $I->seeElement('form');
        $I->seeElement('input[type="email"]');
        $I->seeElement('input[type="password"]');

        $I->fillField('email',    'email@domain.com');
        $I->fillField('password', 'test');
        $I->click('Log In');

        $I->seeCurrentUrlEquals('/dashboard');
        $I->seeAuthentication();
    }

Note: I've got some extra calls in there just to test/practice, etc. so there are some calls that aren't needed.

I'm running using my development database right now - no testing database. Have configured and rebuilt the codecept config as follows:

class_name: FunctionalTester
modules:
    enabled: [Filesystem, FunctionalHelper, Laravel4, Asserts, Db]
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=anc_clients_development'
            user: 'homestead'
            password: 'secret'
            dump: tests/_data/dump.sql
            preload: true
            cleanup: true

Not sure what I'm missing. When the test is run, it just doesn't work, but there aren't any errors, etc. being reported.

0 likes
7 replies
seb7's avatar

So codeception has died before finishing its job, I guess. You should find where it stop adding stupid die('step1') in your test to find where codeception fails.

Anything in codeception logs ?

anchour's avatar

Nothing in the logs. It seems like a configuration issue, is all, but I'm not sure what is wrong with the configuration.

I put a dd() call right after the button was clicked - no problem. It fails when it tries to assert the URL equals '/dashboard' - it's being redirected to /auth/login (as intended on a failed login attempt, but it shouldn't be. Database seeder adds a user with the correct username/password, as well. Very confused.

dimitriacosta's avatar

If you're using codeception maybe this could helpl, it works for me

<?php
$I = new FunctionalTester($scenario);
$I->am('registered common user');
$I->wantTo('perform authentication actions');
// When
$I->amOnPage('login');
// Then
$I->see('Log in', 'h2');
$I->see('Username', 'label');
$I->see('Password', 'label');
$I->see('Remember me', 'label');
$I->seeElement('input', ['value' => 'Log in', 'class' => 'btn btn-primary btn-block']);
$I->amGoingTo('fill an invalid user in order to see the errors');
// When
$I->fillField('username', 'admin');
$I->fillField('password', '12345');
// And
$I->click('Log in', 'input[type=submit]');
// Then
$I->expectTo('see an error message');
$I->seeCurrentUrlEquals('/login');
$I->seeInField('username', 'admin');
$I->see('Invalid data', '.alert-danger');
$I->haveRecord('users', [
    'first_name'     => 'System',
    'last_name'      => 'Administrator',
    'username'       => 'admin',
    'password'       => Hash::make('secret'),
    'email'          => 'email@demo.com',
    'remember_token' => null
]);
$I->amGoingTo('fill a valid user and see the result');
// When
$I->fillField('password', 'secret');
// And
$I->click('Log in', 'input[type=submit]');
// Then
$I->seeCurrentUrlEquals('');
$I->see('You have arrived.', 'h1');
$I->see('Log out', 'a');
$I->seeAuthentication();
$I->amGoingTo('log out');
// When
$I->click('Log out', 'a');
// Then
$I->seeCurrentUrlEquals('/login');
$I->see('Logged out correctly', '.alert-danger');
$I->dontSeeAuthentication();
1 like
hasokeric's avatar

If you are using Laravel 5 i am experiencing the same issue where my logins do not work, but not just through a test even through a form - they used to.

MikeHopley's avatar

Dumb question, maybe, but...

Does the DB dump exists at tests/_data/dump.sql? Is this a copy of your DB, containing the user you are trying to log in?

When running acceptance tests, Codeception will not use your "main" database. It will look for the dump file, which can be used to rebuild the database after each test.

Codeception does not create this dump file for you. You have to make it "manually".

anchour's avatar

@Mike Yes, I've tried a few times to dump my database and put it in tests/_data/dump.sql. Still not sure why.

@hasokeric at the moment it's a Laravel 4 application, but at the same time I'm tempted to just wait it out and use Laravel 5 when it's release. Or, I'll start the development process over in Laravel 5 since I'm not too far into it to begin with.

@dimitriacosta Thanks for the response. I still wound up having the same issue when I copied some of your code over and ran my tests again, but the code you posted is helpful - made me think of additional things to test in my functional/acceptance tests in the future.

MikeHopley's avatar

Debugging idea:

Rename tests/_data/dump.sql to tests/_data/dump.sql.bak (or just delete it). Then try running the tests. Do you get a different error?

This might at least tell you whether Codeception is trying to use that file.

Please or to participate in this conversation.