From 4a122e0209d95deaf6f3f90fbb415dd40c6473cd Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 14 Aug 2022 10:51:12 +0200 Subject: [PATCH] Added remaining API tests covering error type convertions --- module/Rest/test-api/Action/UpdateTagTest.php | 38 +++++++++++++++---- .../Middleware/AuthenticationTest.php | 38 +++++++++++++------ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/module/Rest/test-api/Action/UpdateTagTest.php b/module/Rest/test-api/Action/UpdateTagTest.php index 262789d7..414e7670 100644 --- a/module/Rest/test-api/Action/UpdateTagTest.php +++ b/module/Rest/test-api/Action/UpdateTagTest.php @@ -7,6 +7,8 @@ namespace ShlinkioApiTest\Shlink\Rest\Action; use GuzzleHttp\RequestOptions; use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase; +use function sprintf; + class UpdateTagTest extends ApiTestCase { /** @@ -34,12 +36,15 @@ class UpdateTagTest extends ApiTestCase yield [['newName' => 'foo']]; } - /** @test */ - public function tryingToRenameInvalidTagReturnsNotFound(): void + /** + * @test + * @dataProvider provideTagNotFoundApiVersions + */ + public function tryingToRenameInvalidTagReturnsNotFound(string $version, string $expectedType): void { $expectedDetail = 'Tag with name "invalid_tag" could not be found'; - $resp = $this->callApiWithKey(self::METHOD_PUT, '/tags', [RequestOptions::JSON => [ + $resp = $this->callApiWithKey(self::METHOD_PUT, sprintf('/rest/v%s/tags', $version), [RequestOptions::JSON => [ 'oldName' => 'invalid_tag', 'newName' => 'foo', ]]); @@ -47,17 +52,27 @@ class UpdateTagTest extends ApiTestCase self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); self::assertEquals(self::STATUS_NOT_FOUND, $payload['status']); - self::assertEquals('TAG_NOT_FOUND', $payload['type']); + self::assertEquals($expectedType, $payload['type']); self::assertEquals($expectedDetail, $payload['detail']); self::assertEquals('Tag not found', $payload['title']); } - /** @test */ - public function errorIsThrownWhenTryingToRenameTagToAnotherTagName(): void + public function provideTagNotFoundApiVersions(): iterable + { + yield 'version 1' => ['1', 'TAG_NOT_FOUND']; + yield 'version 2' => ['2', 'TAG_NOT_FOUND']; + yield 'version 3' => ['3', 'https://shlink.io/api/error/tag-not-found']; + } + + /** + * @test + * @dataProvider provideTagConflictsApiVersions + */ + public function errorIsThrownWhenTryingToRenameTagToAnotherTagName(string $version, string $expectedType): void { $expectedDetail = 'You cannot rename tag foo to bar, because it already exists'; - $resp = $this->callApiWithKey(self::METHOD_PUT, '/tags', [RequestOptions::JSON => [ + $resp = $this->callApiWithKey(self::METHOD_PUT, sprintf('/rest/v%s/tags', $version), [RequestOptions::JSON => [ 'oldName' => 'foo', 'newName' => 'bar', ]]); @@ -65,11 +80,18 @@ class UpdateTagTest extends ApiTestCase self::assertEquals(self::STATUS_CONFLICT, $resp->getStatusCode()); self::assertEquals(self::STATUS_CONFLICT, $payload['status']); - self::assertEquals('TAG_CONFLICT', $payload['type']); + self::assertEquals($expectedType, $payload['type']); self::assertEquals($expectedDetail, $payload['detail']); self::assertEquals('Tag conflict', $payload['title']); } + public function provideTagConflictsApiVersions(): iterable + { + yield 'version 1' => ['1', 'TAG_CONFLICT']; + yield 'version 2' => ['2', 'TAG_CONFLICT']; + yield 'version 3' => ['3', 'https://shlink.io/api/error/tag-conflict']; + } + /** @test */ public function tagIsProperlyRenamedWhenRenamingToItself(): void { diff --git a/module/Rest/test-api/Middleware/AuthenticationTest.php b/module/Rest/test-api/Middleware/AuthenticationTest.php index 61dbd2c5..51128079 100644 --- a/module/Rest/test-api/Middleware/AuthenticationTest.php +++ b/module/Rest/test-api/Middleware/AuthenticationTest.php @@ -6,32 +6,47 @@ namespace ShlinkioApiTest\Shlink\Rest\Middleware; use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase; +use function sprintf; + class AuthenticationTest extends ApiTestCase { - /** @test */ - public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void + /** + * @test + * @dataProvider provideApiVersions + */ + public function authorizationErrorIsReturnedIfNoApiKeyIsSent(string $version, string $expectedType): void { $expectedDetail = 'Expected one of the following authentication headers, ["X-Api-Key"], but none were provided'; - $resp = $this->callApi(self::METHOD_GET, '/short-urls'); + $resp = $this->callApi(self::METHOD_GET, sprintf('/rest/v%s/short-urls', $version)); $payload = $this->getJsonResponsePayload($resp); self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode()); self::assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']); - self::assertEquals('INVALID_AUTHORIZATION', $payload['type']); + self::assertEquals($expectedType, $payload['type']); self::assertEquals($expectedDetail, $payload['detail']); self::assertEquals('Invalid authorization', $payload['title']); } + public function provideApiVersions(): iterable + { + yield 'version 1' => ['1', 'INVALID_AUTHORIZATION']; + yield 'version 2' => ['2', 'INVALID_AUTHORIZATION']; + yield 'version 3' => ['3', 'https://shlink.io/api/error/missing-authentication']; + } + /** * @test * @dataProvider provideInvalidApiKeys */ - public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid(string $apiKey): void - { + public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid( + string $apiKey, + string $version, + string $expectedType, + ): void { $expectedDetail = 'Provided API key does not exist or is invalid.'; - $resp = $this->callApi(self::METHOD_GET, '/short-urls', [ + $resp = $this->callApi(self::METHOD_GET, sprintf('/rest/v%s/short-urls', $version), [ 'headers' => [ 'X-Api-Key' => $apiKey, ], @@ -40,15 +55,16 @@ class AuthenticationTest extends ApiTestCase self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode()); self::assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']); - self::assertEquals('INVALID_API_KEY', $payload['type']); + self::assertEquals($expectedType, $payload['type']); self::assertEquals($expectedDetail, $payload['detail']); self::assertEquals('Invalid API key', $payload['title']); } public function provideInvalidApiKeys(): iterable { - yield 'key which does not exist' => ['invalid']; - yield 'key which is expired' => ['expired_api_key']; - yield 'key which is disabled' => ['disabled_api_key']; + yield 'key which does not exist' => ['invalid', '2', 'INVALID_API_KEY']; + yield 'key which is expired' => ['expired_api_key', '2', 'INVALID_API_KEY']; + yield 'key which is disabled' => ['disabled_api_key', '2', 'INVALID_API_KEY']; + yield 'version 3' => ['disabled_api_key', '3', 'https://shlink.io/api/error/invalid-api-key']; } }