bwrigley's avatar

Feature test fails while Dusk and manual test pass

I am having a curious issue with a feature test not working as expected.

With Sentinel you can set login throttling, which I have set to 5 attempts, on the 6th attempt a ThrottlingException is thrown which I catch and set a different error message. This works fine manually in the browser, or in a Dusk test.

The last assertion always fails while the others in the foreach all pass no matter how high I set $loginAttempts to.

However, if I run the test again immediately it gets the throttling message on the first attempt.

All the records that Sentinel captures for throttling are in the DB and present and correct.

All my own log files show that the correct final error is never triggered by the test.

    public function test_wrong_login_locks_account()
    {

        $loginCredentials = [
            'email' => '[email protected]',
            'password' => 'wrong_password'
        ];

        $loginAttempts=5;

        foreach (range(0, $loginAttempts) as $i) {
            $this->fromUrl(route('login'))
            ->post('/login', $loginCredentials)
            ->assertEIMErrorExists('login_fail')
            ->followRedirects();
        }

        $this->fromUrl(route('login'))
        ->post('/login', $loginCredentials)
        ->assertEIMErrorExists('login_locked');

    }

assertEIMErrorExists() is a macro to check the session for the existence of a custom message

followRedirect() is a macro to follow any redirect commands (needed to trigger the session flash)

(these are both used in many other tests and are working fine.)

I appreciate this is not a straightforward one to explain, so please ask me any questions you have, Thanks!

0 likes
1 reply
bwrigley's avatar

More info:

I have managed to replicate this in tinker

if I run

Sentinel::authenticate(['email' => '[email protected]', 'password' => 'wrong-password'])

I get false as expected

If I run it 10 times, I still get false each time

if I then restart tinker and run the command again, I get the ThrottlingException

So what is that changes whentinker is restarted or phpunit is rerun?

Please or to participate in this conversation.