hattonc120@gmail.com's avatar

User authentication problem within phpunit test

When the test below is run I get the following error...
The user is not authenticated -- Failed asserting that false is true. 

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\Teacher;

public function test_teacher_can_log_in_using_login_form()
    {
        $teacher = Teacher::find(1);
        $credentials = [
            'email'     => $teacher->email,
            'password'  => 'password'
        ];
        $response = $this->post('/teacherlogin', $credentials);
        $this->assertAuthenticated();
        $response->assertRedirect('/teacher/home');
    }

Can anyone help me with this please?```
0 likes
4 replies
hattonc120@gmail.com's avatar

Lol. 57 seconds after I posted this request for help I solved the problem as below...

I replaced $response = $this->post('/teacherlogin', $credentials); with $response = $this->actingAs($teacher)->post('/teacherlogin');

:)

Tray2's avatar

I take it that your Teacher works with the users table supplied with Laravel? If not you really should consider a user is a user regardless if it's a teacher, student or whatever. You use roles to determine what kind of user it is with authorization.

Please use three backticks ` to wrap your code to make it readable.

Not the solution but more readable for others who may help you.

use Illuminate\Foundation\Testing\RefreshDatabase; 
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase; 
use App\Models\Teacher;

public function test_teacher_can_log_in_using_login_form()
 { 
		$teacher = Teacher::find(1); 
		$credentials = [ 'email' => $teacher->email, 'password' => 'password' ];
	    $response = $this->post('/teacherlogin', $credentials);
        $this->assertAuthenticated(); 
        $response->assertRedirect('/teacher/home'); 
}
hattonc120@gmail.com's avatar

Thanks so much for the response Tray2. Much appreciated. I have created separate models for student, teacher and used different guards to control access for each user group. Annoyingly I am now unsure if I have solved the problem.

public function test_teacher_can_log_in_using_login_form()
    {
        $teacher = Teacher::find(1);
        $credentials = [
            'email'     => $teacher->email,
            'password'  => 'password'
        ];
        $response = $this->actingAs($teacher)->post('/teacherlogin');
        $this->assertAuthenticated();
        $response->assertRedirect('/teacher/home');
    }

The test is authenticating even if I put 'post('/sdkfghsghsgshg'). Any ideas?

hattonc120@gmail.com's avatar

Sorted now.

public function test_teacher_can_log_in_using_teacher_login_form()
    {
        $teacher = Teacher::find(1);
        $credentials = [
            'email'     => $teacher->email,
            'password'  => 'password'
        ];
        $response = $this->actingAs($teacher)->post('/teacherlogin', $credentials);
        /* ---------------------------------------------------------------------- */
        $this->assertAuthenticated();
        $response->assertRedirect('/teacher/home');
    }

Please or to participate in this conversation.