Added remaining API tests covering error type convertions

This commit is contained in:
Alejandro Celaya 2022-08-14 10:51:12 +02:00
parent ce4bf62d75
commit 4a122e0209
2 changed files with 57 additions and 19 deletions

View file

@ -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
{

View file

@ -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'];
}
}