0

I am trying to test if authenticated manager will redirect to dashboard. (When the manager is already authenticated and goes to login page, he is redirected to dashboard)

public function testIfBackendIsLoggingAuthenticatedUser()
{
    $manager = \App\Manager::where('email', 'test@email.com')->first();
    Auth::attempt(['email' => $manager->email, 'password' => $manager->password]);

    $response = $this->actingAs($manager)
        ->get(route('backend.login.show'))
        ->assertRedirect(route('backend.dashboard.index'));
}

But I get the result

Response status code [200] is not a redirect status code.
Failed asserting that false is true.

How can I make authentication test?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Thelambofgoat
  • 609
  • 1
  • 11
  • 24
  • So are you testing action of *logging in* or that auth/guest middleware works? – Kyslik Jul 24 '18 at 15:40
  • I am trying to test situation when manager who is already logged in tries goes to login page – Thelambofgoat Jul 24 '18 at 16:05
  • Obviously `$manager->password` is hashed and should blow up (in another words not work). Besides that delete line starting with `Auth::` and that should be it. – Kyslik Jul 24 '18 at 16:06
  • there is no need to mess with the guard directly at all ... just call `$this->actingAs(...)` that is what it is for. Also did you think that perhaps this is following redirects, so it ends up on the dashboard page, which would be a 200 response? – lagbox Jul 24 '18 at 20:34
  • @lagbox I thank you. Really nothing to do with password hashing. But working version shows redirect, not follows) – Thelambofgoat Jul 25 '18 at 10:41

2 Answers2

0

The attempt method hashed the password itself. You are passing hashed password, so it double hashed the password so it is incorrect.

Use:

Auth::login($manager);

instead of:

Auth::attempt(['email' => $manager->email, 'password' => $manager->password]);
Hamidreza Bayat
  • 103
  • 1
  • 10
0

Sorry, guys. Really the mistake was that I forgot to mention guard. So the working code is

$response = $this->actingAs($manager, 'manager')
        ->get(route('backend.login.show'))
        ->assertRedirect(route('backend.dashboard.index'));
Thelambofgoat
  • 609
  • 1
  • 11
  • 24