LexikJWTAuthenticationBundle
LexikJWTAuthenticationBundle copied to clipboard
Randomly "Unable to find the controller for path '/api/auth'" in Tests
Hello,
I'm using the bundle in a API platform project for auth. In my API tests, I use the configured login endpoint /api/tests
.
Bundle configuration:
lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
api_platform:
check_path: /api/auth
username_path: email
password_path: password
The URL is also defined in my routes.yaml
:
api_auth:
path: /api/auth
methods: ['POST']
This is my test class with 3 test cases:
<?php
declare(strict_types=1);
namespace App\Tests\IntegrationTests\Api\XY;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use Spatie\Snapshots\MatchesSnapshots;
class CanConnectTest extends ApiTestCase
{
use MatchesSnapshots;
private Client $client;
private string $token;
protected function setUp(): void
{
$client = self::createClient();
$response1 = $client->request('POST', '/api/auth', [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => [
'email' => '[email protected]',
'password' => 'apiuser',
],
]);
$json = $response1->toArray();
self::assertResponseIsSuccessful();
self::assertArrayHasKey('token', $json);
$this->token = $json['token'];
$this->client = $client;
}
public function testCanConnectTrue(): void
{
$response = $this->client->request(
'GET',
'http://localhost/api/audiosus/can_connect/store/7',
[
'auth_bearer' => $this->token,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
],
);
self::assertResponseIsSuccessful();
self::assertMatchesJsonSnapshot($response->toArray());
}
public function testCanConnectFalse(): void
{
$response = $this->client->request(
'GET',
'http://localhost/api/audiosus/can_connect/store/6',
[
'auth_bearer' => $this->token,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
],
);
self::assertResponseIsSuccessful();
self::assertMatchesJsonSnapshot($response->toArray());
}
public function testCanConnectNotFound(): void
{
$this->client->request(
'GET',
'http://localhost/api/audiosus/can_connect/store/12312313213',
[
'auth_bearer' => $this->token,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
],
);
self::assertResponseStatusCodeSame(404);
}
}
When I run the tests, I get randomly errors like this:
1) App\Tests\IntegrationTests\Api\Audiosus\CanConnectTest::testCanConnectFalse
Symfony\Component\HttpClient\Exception\ClientException: An error occurred
Unable to find the controller for path "/api/auth". The route is wrongly configured.
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:80
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:94
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:125
/srv/app/tests/IntegrationTests/Api/XY/CanConnectTest.php:33
2) App\Tests\IntegrationTests\Api\XY\CanConnectTest::testCanConnectTrue
Symfony\Component\HttpClient\Exception\ClientException: An error occurred
Unable to find the controller for path "/api/auth". The route is wrongly configured.
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:80
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:94
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:125
/srv/app/tests/IntegrationTests/Api/XY/CanConnectTest.php:33
or sometimes all the test cases are green, or only one test fails.
Any ideas?