Skip to content

Commit

Permalink
Update all endpoints for new oauth-api base url and use the appropria…
Browse files Browse the repository at this point in the history
…te OAuthAPIKernel
  • Loading branch information
jolelievre committed Mar 25, 2024
1 parent e3b3fa1 commit 10c2160
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 68 deletions.
30 changes: 15 additions & 15 deletions tests/Integration/ApiPlatform/ApiClientEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,29 @@ public function getProtectedEndpoints(): iterable
{
yield 'get endpoint' => [
'GET',
'/api/api-client/1',
'/api-client/1',
];

yield 'create endpoint' => [
'POST',
'/api/api-client',
'/api-client',
];

yield 'update endpoint' => [
'PATCH',
'/api/api-client/1',
'/api-client/1',
];

yield 'delete endpoint' => [
'DELETE',
'/api/api-client/1',
'/api-client/1',
];
}

public function testAddApiClient(): int
{
$bearerToken = $this->getBearerToken(['api_client_write']);
$response = static::createClient()->request('POST', '/api/api-client', [
$response = static::createClient()->request('POST', '/api-client', [
'auth_bearer' => $bearerToken,
'json' => [
'clientId' => 'client_id_test',
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testAddApiClient(): int
public function testGetApiClient(int $apiClientId): int
{
$bearerToken = $this->getBearerToken(['api_client_read']);
$response = static::createClient()->request('GET', '/api/api-client/' . $apiClientId, [
$response = static::createClient()->request('GET', '/api-client/' . $apiClientId, [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(200);
Expand Down Expand Up @@ -142,7 +142,7 @@ public function testUpdateApiClient(int $apiClientId): int
$bearerToken = $this->getBearerToken(['api_client_write']);

// Update API client
$response = static::createClient()->request('PATCH', '/api/api-client/' . $apiClientId, [
$response = static::createClient()->request('PATCH', '/api-client/' . $apiClientId, [
'auth_bearer' => $bearerToken,
'json' => [
'clientId' => 'client_id_test_updated',
Expand Down Expand Up @@ -178,7 +178,7 @@ public function testUpdateApiClient(int $apiClientId): int
);

// Update partially API client
$response = static::createClient()->request('PATCH', '/api/api-client/' . $apiClientId, [
$response = static::createClient()->request('PATCH', '/api-client/' . $apiClientId, [
'auth_bearer' => $bearerToken,
'json' => [
'description' => 'Client description test partially updated',
Expand Down Expand Up @@ -219,7 +219,7 @@ public function testUpdateApiClient(int $apiClientId): int
public function testGetUpdatedApiClient(int $apiClientId): int
{
$bearerToken = $this->getBearerToken(['api_client_read']);
$response = static::createClient()->request('GET', '/api/api-client/' . $apiClientId, [
$response = static::createClient()->request('GET', '/api-client/' . $apiClientId, [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(200);
Expand Down Expand Up @@ -253,37 +253,37 @@ public function testGetUpdatedApiClient(int $apiClientId): int
public function testDeleteApiClient(int $apiClientId): void
{
// Delete API client without token
static::createClient()->request('DELETE', '/api/api-client/' . $apiClientId);
static::createClient()->request('DELETE', '/api-client/' . $apiClientId);
self::assertResponseStatusCodeSame(401);
// Delete API client without token
static::createClient()->request('DELETE', '/api/api-client/' . $apiClientId, [
static::createClient()->request('DELETE', '/api-client/' . $apiClientId, [
'auth_bearer' => 'toto',
]);
self::assertResponseStatusCodeSame(401);

// Try to delete with a token with only read scope
$readBearerToken = $this->getBearerToken(['api_client_read']);
$response = static::createClient()->request('DELETE', '/api/api-client/' . $apiClientId, [
$response = static::createClient()->request('DELETE', '/api-client/' . $apiClientId, [
'auth_bearer' => $readBearerToken,
]);
$this->assertEquals(403, $response->getStatusCode());
self::assertResponseStatusCodeSame(403);

// Check that API client was not deleted
static::createClient()->request('GET', '/api/api-client/' . $apiClientId, [
static::createClient()->request('GET', '/api-client/' . $apiClientId, [
'auth_bearer' => $readBearerToken,
]);
self::assertResponseStatusCodeSame(200);

// Delete API client with valid token
$writeBearerToken = $this->getBearerToken(['api_client_write']);
$response = static::createClient()->request('DELETE', '/api/api-client/' . $apiClientId, [
$response = static::createClient()->request('DELETE', '/api-client/' . $apiClientId, [
'auth_bearer' => $writeBearerToken,
]);
self::assertResponseStatusCodeSame(204);
$this->assertEmpty($response->getContent());

static::createClient()->request('GET', '/api/api-client/' . $apiClientId, [
static::createClient()->request('GET', '/api-client/' . $apiClientId, [
'auth_bearer' => $readBearerToken,
]);
self::assertResponseStatusCodeSame(404);
Expand Down
14 changes: 12 additions & 2 deletions tests/Integration/ApiPlatform/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public static function tearDownAfterClass(): void
self::$clientSecret = null;
}

/**
* API endpoints are only available in the AdminApi application so we force using the proper kernel here.
*
* @return string
*/
protected static function getKernelClass(): string
{
return \AdminAPIKernel::class;
}

/**
* @dataProvider getProtectedEndpoints
*
Expand Down Expand Up @@ -88,7 +98,7 @@ public function testProtectedEndpoints(string $method, string $uri, string $cont
*
* yield 'get endpoint' => [
* 'GET',
* '/api/product/1',
* '/product/1',
* ];
*
* Since all Api Platform resources should likely have some protected endpoints this provider
Expand Down Expand Up @@ -130,7 +140,7 @@ protected function getBearerToken(array $scopes = []): string
'content-type' => 'application/x-www-form-urlencoded',
],
];
$response = static::createClient()->request('POST', '/api/oauth2/token', $options);
$response = static::createClient()->request('POST', '/access_token', $options);

return json_decode($response->getContent())->access_token;
}
Expand Down
18 changes: 9 additions & 9 deletions tests/Integration/ApiPlatform/CustomerGroupApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ public function getProtectedEndpoints(): iterable
{
yield 'get endpoint' => [
'GET',
'/api/customers/group/1',
'/customers/group/1',
];

yield 'create endpoint' => [
'POST',
'/api/customers/group',
'/customers/group',
];

yield 'update endpoint' => [
'PUT',
'/api/customers/group/1',
'/customers/group/1',
];

yield 'delete endpoint' => [
'DELETE',
'/api/customers/group/1',
'/customers/group/1',
];
}

Expand All @@ -75,7 +75,7 @@ public function testAddCustomerGroup(): int
$numberOfGroups = count(\Group::getGroups(\Context::getContext()->language->id));

$bearerToken = $this->getBearerToken(['customer_group_write']);
$response = static::createClient()->request('POST', '/api/customers/group', [
$response = static::createClient()->request('POST', '/customers/group', [
'auth_bearer' => $bearerToken,
'json' => [
'localizedNames' => [
Expand Down Expand Up @@ -124,7 +124,7 @@ public function testUpdateCustomerGroup(int $customerGroupId): int

$bearerToken = $this->getBearerToken(['customer_group_write']);
// Update customer group with partial data
$response = static::createClient()->request('PUT', '/api/customers/group/' . $customerGroupId, [
$response = static::createClient()->request('PUT', '/customers/group/' . $customerGroupId, [
'auth_bearer' => $bearerToken,
'json' => [
'localizedNames' => [
Expand Down Expand Up @@ -168,7 +168,7 @@ public function testUpdateCustomerGroup(int $customerGroupId): int
public function testGetCustomerGroup(int $customerGroupId): int
{
$bearerToken = $this->getBearerToken(['customer_group_read']);
$response = static::createClient()->request('GET', '/api/customers/group/' . $customerGroupId, [
$response = static::createClient()->request('GET', '/customers/group/' . $customerGroupId, [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(200);
Expand Down Expand Up @@ -204,13 +204,13 @@ public function testDeleteCustomerGroup(int $customerGroupId): void
{
$bearerToken = $this->getBearerToken(['customer_group_read', 'customer_group_write']);
// Update customer group with partial data
$response = static::createClient()->request('DELETE', '/api/customers/group/' . $customerGroupId, [
$response = static::createClient()->request('DELETE', '/customers/group/' . $customerGroupId, [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(204);
$this->assertEmpty($response->getContent());

static::createClient()->request('GET', '/api/customers/group/' . $customerGroupId, [
static::createClient()->request('GET', '/customers/group/' . $customerGroupId, [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(404);
Expand Down
20 changes: 10 additions & 10 deletions tests/Integration/ApiPlatform/GetHookStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public function getProtectedEndpoints(): iterable
{
yield 'get endpoint' => [
'GET',
'/api/hook-status/1',
'/hook-status/1',
];

yield 'put endpoint' => [
'PUT',
'/api/hook-status',
'/hook-status',
];
}

Expand All @@ -73,18 +73,18 @@ public function testGetHookStatus(): void
'hook_read',
'hook_write',
]);
$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $inactiveHook->id, ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hook-status/' . (int) $inactiveHook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, $inactiveHook->active);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $activeHook->id, ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hook-status/' . (int) $activeHook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, $activeHook->active);
self::assertResponseStatusCodeSame(200);

static::createClient()->request('GET', '/api/hook-status/' . 9999, ['auth_bearer' => $bearerToken]);
static::createClient()->request('GET', '/hook-status/' . 9999, ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(404);

static::createClient()->request('GET', '/api/hook-status/' . $activeHook->id);
static::createClient()->request('GET', '/hook-status/' . $activeHook->id);
self::assertResponseStatusCodeSame(401);

$inactiveHook->delete();
Expand All @@ -102,13 +102,13 @@ public function testDisableHook(): void
'hook_read',
'hook_write',
]);
static::createClient()->request('PUT', '/api/hook-status', [
static::createClient()->request('PUT', '/hook-status', [
'auth_bearer' => $bearerToken,
'json' => ['id' => (int) $hook->id, 'active' => false],
]);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hook-status/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, false);
self::assertResponseStatusCodeSame(200);
}
Expand All @@ -124,13 +124,13 @@ public function testEnableHook(): void
'hook_read',
'hook_write',
]);
static::createClient()->request('PUT', '/api/hook-status', [
static::createClient()->request('PUT', '/hook-status', [
'auth_bearer' => $bearerToken,
'json' => ['id' => (int) $hook->id, 'active' => true],
]);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hook-status/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, true);
self::assertResponseStatusCodeSame(200);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/Integration/ApiPlatform/GetHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getProtectedEndpoints(): iterable
{
yield 'get endpoint' => [
'GET',
'/api/hooks/1',
'/hooks/1',
];
}

Expand All @@ -65,14 +65,14 @@ public function testGetHook(): void
'hook_write',
]);

$response = static::createClient()->request('GET', '/api/hooks/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hooks/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, $hook->active);
self::assertResponseStatusCodeSame(200);

static::createClient()->request('GET', '/api/hooks/' . 9999, ['auth_bearer' => $bearerToken]);
static::createClient()->request('GET', '/hooks/' . 9999, ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(404);

static::createClient()->request('GET', '/api/hooks/' . $hook->id);
static::createClient()->request('GET', '/hooks/' . $hook->id);
self::assertResponseStatusCodeSame(401);

$hook->delete();
Expand All @@ -86,16 +86,16 @@ public function testListHooks(): void
'hook_write',
]);

$response = static::createClient()->request('GET', '/api/hooks', ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hooks', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertCount(50, json_decode($response->getContent())->items);
$totalItems = json_decode($response->getContent())->totalItems;

$response = static::createClient()->request('GET', '/api/hooks?limit=10', ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hooks?limit=10', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertCount(10, json_decode($response->getContent())->items);

$response = static::createClient()->request('GET', '/api/hooks?limit=1&orderBy=id_hook&sortOrder=desc', ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hooks?limit=1&orderBy=id_hook&sortOrder=desc', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertCount(1, json_decode($response->getContent())->items);
$returnedHook = json_decode($response->getContent());
Expand All @@ -106,7 +106,7 @@ public function testListHooks(): void
self::assertEquals('testHook50', $returnedHook->items[0]->name);
self::assertTrue($returnedHook->items[0]->active);

$response = static::createClient()->request('GET', '/api/hooks?filters[name]=testHook', ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hooks?filters[name]=testHook', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertCount(50, json_decode($response->getContent())->items);
foreach (json_decode($response->getContent())->items as $key => $item) {
Expand All @@ -119,11 +119,11 @@ public function testListHooks(): void
$newHook->add();
$hooks[] = $newHook;

$response = static::createClient()->request('GET', '/api/hooks', ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hooks', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertEquals($totalItems + 1, json_decode($response->getContent())->totalItems);

static::createClient()->request('GET', '/api/hooks');
static::createClient()->request('GET', '/hooks');
self::assertResponseStatusCodeSame(401);

foreach ($hooks as $hook) {
Expand Down
Loading

0 comments on commit 10c2160

Please sign in to comment.