0

Users can only log into the system when their account is active. I use a factory to create a user (initially their status is set to 'pending') and then try to post to the login form.

From here, I'm not too sure what assertions I need to do. I have already written the code that does this functionality (I have a custom Validator in the validateLogin form) and then it throws an error message something along the lines of 'your account is not active'.

What's the best way to approach this? What I have so far:

/** @test */
public function it_can_only_login_if_the_status_is_active()
{
    $user = factory(User::class)->create();
    $this->post('login', ['email' => $user->email, 'password' => $user->password, '_token' => csrf_token()]);
}

1 Answer 1

1

With the build in Laravel testing methods you could do this:

    $user = factory(User::class)->create();

    $this->visit('/login')
        ->type($user->email, 'email')
        ->type($user->password, 'password')
        ->press('Login')
        ->see('your account is not active');

I can recommend the Codeception library if you need to test some complicated features, because with it you can verify results in the DB, create acceptance and functional tests.

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome thanks. Just one little problem, it looks like phpunit is trying to post to localhost/login. Am I missing a setting in my tests somewhere?
In the TestCase file there is protected $baseUrl = 'http://localhost'; you can change it with your domain.
Thanks, I still have some errors but I think that's to do with my code. Thanks for your help!
@TheFallen in the code $user->password should return hash, so this test case should fail. I think you should use something like this $user = factory(App\User::class)->create([ 'password' => \Hash::make($this->password) ]);
@UmeshMishra, yes, you're absolutely right. Your suggestion would work or you can also hardcode same password in the factory for all users and use it in the tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.