cartalyst / sentinel

A framework agnostic authentication & authorization system.
BSD 3-Clause "New" or "Revised" License
1.51k stars 238 forks source link

[Question] How do we unit test Sentinel 5.x with Laravel 8.0? #540

Closed digitalformula closed 3 years ago

digitalformula commented 3 years ago

Your Environment

Expected behaviour

Expect to use Sentinel::login($user) with a valid $user object and then assert HTTP 200 on a page that requires authentication.

It appears this used to work in previous versions.

Actual behaviour

Page consistently returns 302 with an attempt to redirect to my login page. Can't use actingAs() due to:

Argument 1 passed to Illuminate\Foundation\Testing\TestCase::actingAs() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given, called in /srv/app/html/tests/Feature/LoggedInTest.php on line 21

Steps to reproduce

Any test that requires a user to be authenticated.

digitalformula commented 3 years ago

Not sure what I was doing wrong but this just started working as expected above. These both work as expected:

$user = Sentinel::findByCredentials( [ 'login' => 'no-reply@acme.com' ] );
Sentinel::login( $user );
$response = $this->get('/auth/logout');
$response->assertRedirect('/auth/login');
$user = Sentinel::findByCredentials( [ 'login' => 'no-reply@acme.com' ] );
Sentinel::login( $user );
$response = $this->get('/projects');
$response->assertStatus(200)->assertSee('OHAI');