. */ declare(strict_types=1); namespace API; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; use App\DataFixtures\APITokenFixtures; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use ApiPlatform\Symfony\Bundle\Test\Client; class APITokenAuthenticationTest extends ApiTestCase { public function testUnauthenticated(): void { self::ensureKernelShutdown(); $client = static::createClient(); $client->request('GET', '/api/parts'); self::assertResponseStatusCodeSame(401); } public function testExpiredToken(): void { self::ensureKernelShutdown(); $client = $this->createClientWithCredentials(APITokenFixtures::TOKEN_EXPIRED); $client->request('GET', '/api/parts'); self::assertResponseStatusCodeSame(401); } public function testReadOnlyToken(): void { self::ensureKernelShutdown(); $client = $this->createClientWithCredentials(APITokenFixtures::TOKEN_READONLY); //Read should be possible $client->request('GET', '/api/parts'); self::assertResponseIsSuccessful(); //Trying to list all users and create a new footprint should fail $client->request('GET', '/api/users'); self::assertResponseStatusCodeSame(403); $client->request('POST', '/api/footprints', ['json' => ['name' => 'post test']]); self::assertResponseStatusCodeSame(403); } public function testEditToken(): void { self::ensureKernelShutdown(); $client = $this->createClientWithCredentials(APITokenFixtures::TOKEN_EDIT); //Read should be possible $client->request('GET', '/api/parts'); self::assertResponseIsSuccessful(); //Trying to list all users $client->request('GET', '/api/users'); self::assertResponseStatusCodeSame(403); $client->request('POST', '/api/footprints', ['json' => ['name' => 'post test']]); self::assertResponseIsSuccessful(); } public function testAdminToken(): void { self::ensureKernelShutdown(); $client = $this->createClientWithCredentials(APITokenFixtures::TOKEN_ADMIN ); //Read should be possible $client->request('GET', '/api/parts'); self::assertResponseIsSuccessful(); //Trying to list all users $client->request('GET', '/api/users'); self::assertResponseIsSuccessful(); $client->request('POST', '/api/footprints', ['json' => ['name' => 'post test']]); self::assertResponseIsSuccessful(); } public function testWithAuthorizationToken(): void { //For the KICAD API it should also work with Authorization: Token header instead of Bearer self::ensureKernelShutdown(); $client = static::createClient([], ['headers' => ['authorization' => 'Token '.APITokenFixtures::TOKEN_ADMIN]]);; //Read should be possible $client->request('GET', '/api/parts'); self::assertResponseIsSuccessful(); //Trying to list all users $client->request('GET', '/api/users'); self::assertResponseIsSuccessful(); $client->request('POST', '/api/footprints', ['json' => ['name' => 'post test']]); self::assertResponseIsSuccessful(); } protected function createClientWithCredentials(string $token): Client { return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]); } }