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);
}
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);
}
Share
Improve this question
edited Jan 20 at 12:48
matiaslauriti
8,0824 gold badges36 silver badges47 bronze badges
asked Jan 20 at 10:34
Eduardo CorralesEduardo Corrales
231 silver badge5 bronze badges
2 Answers
Reset to default 0try 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);
});
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 worked.
$this->withoutMiddleware(LogApiAccess::class);