GimmeMylanta's avatar

Logout test using sanctum not returning as expected

Hey All,

I am in the process of writing Sanctum tests, and I have come to the Logout method, but it seems to be doing some strange things.

My Logout route is;

Route::post('logout', [Auth\AuthenticationController::class, 'logout'])->middleware('auth:sanctum');

Notice it has the auth.sanctum middleware attached to it which should cause a unauthorized if someone tries to hit it without having a bearer token.

My test looks like so;

class LogoutTest extends TestCase
{
    use RefreshDatabase;

    public function testLoggingOutUser(): void
    {
        $user = User::factory()->create();

        $response =  $this->postJson('/api/login', [
            'email' => $user->email,
            'password' => 'password'
        ]);

        $response->assertOk()
            ->assertJson([
                'access_token' => true,
            ]);

        $testResponse = $this->postJson('/api/logout');
        dd($testResponse);
    }
}

The top bit works perfect, but its the $testResponse which is causing me issues.

As it is a new 'request' and im not passing any token, it should throw the unauthenticated error ... But instead if i inspect the response, it is giving me the 'User logged out successfully' which should only be returned if the token is passed with that response.

It's a bit confusing. If I hit that /api/logout endpoint in postman, it does exactly what it should.

Anyone else had this issue? How did you solve it?

0 likes
0 replies

Please or to participate in this conversation.