skip to Main Content

I’ve a Laravel app with api routes using Sanctum authentication. When I try to write a feature test for any api route, I always receive an error:

"message": "SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed (SQL: insert into "personal_access_token_logs" ("personal_access_token_id", "controller", "action", "request_ip", "request_uri", "request_method", "request_body", "response_body", "response_status", "updated_at", "created_at") values (0, {hidden_controller}, show, ?, http://localhost/api/{hidden_route}, GET, [], {"id":84,"personal_acc"…

I think it’s related to the personal_access_token_id field having value 0, but I can’t realize why this is happening.

I’m using sqlite with in_memory database, but changed to mysql with a testing database (and without cleaning before tests) to try to find what is happening. Same problem, user and token are created, but the error appears when try to log the connection into personal_access_token_logs.

The test code for references:

it('can show {hidden_route}', function () {
    $user = User::factory()->create(['is_admin' => true]);
    Sanctum::actingAs($user, ['*']);
    $response = $this->getJson('/api/{hidden_route}');
    $response->assertStatus(200);
}

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution. There was a middleware "LogApiAccess" trying to log the request and response to database. I just disable it and the test case worked. $this->withoutMiddleware(LogApiAccess::class);


  2. try to create token first

      $token = $user->createToken('Test Token')->plainTextToken;
    

    in your code

    it('can show {hidden_route}', function () {
    $user = User::factory()->create(['is_admin' => true]);
    $token = $user->createToken('Test Token')->plainTextToken;
    Sanctum::actingAs($user, ['*']); 
    $response = $this->getJson('/api/{hidden_route}');
    $response->assertStatus(200);
    

    });

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search