mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added remaining API tests covering error type convertions
This commit is contained in:
parent
ce4bf62d75
commit
4a122e0209
@ -7,6 +7,8 @@ namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|||||||
use GuzzleHttp\RequestOptions;
|
use GuzzleHttp\RequestOptions;
|
||||||
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
class UpdateTagTest extends ApiTestCase
|
class UpdateTagTest extends ApiTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -34,12 +36,15 @@ class UpdateTagTest extends ApiTestCase
|
|||||||
yield [['newName' => 'foo']];
|
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';
|
$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',
|
'oldName' => 'invalid_tag',
|
||||||
'newName' => 'foo',
|
'newName' => 'foo',
|
||||||
]]);
|
]]);
|
||||||
@ -47,17 +52,27 @@ class UpdateTagTest extends ApiTestCase
|
|||||||
|
|
||||||
self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
||||||
self::assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
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($expectedDetail, $payload['detail']);
|
||||||
self::assertEquals('Tag not found', $payload['title']);
|
self::assertEquals('Tag not found', $payload['title']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
public function provideTagNotFoundApiVersions(): iterable
|
||||||
public function errorIsThrownWhenTryingToRenameTagToAnotherTagName(): void
|
{
|
||||||
|
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';
|
$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',
|
'oldName' => 'foo',
|
||||||
'newName' => 'bar',
|
'newName' => 'bar',
|
||||||
]]);
|
]]);
|
||||||
@ -65,11 +80,18 @@ class UpdateTagTest extends ApiTestCase
|
|||||||
|
|
||||||
self::assertEquals(self::STATUS_CONFLICT, $resp->getStatusCode());
|
self::assertEquals(self::STATUS_CONFLICT, $resp->getStatusCode());
|
||||||
self::assertEquals(self::STATUS_CONFLICT, $payload['status']);
|
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($expectedDetail, $payload['detail']);
|
||||||
self::assertEquals('Tag conflict', $payload['title']);
|
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 */
|
/** @test */
|
||||||
public function tagIsProperlyRenamedWhenRenamingToItself(): void
|
public function tagIsProperlyRenamedWhenRenamingToItself(): void
|
||||||
{
|
{
|
||||||
|
@ -6,32 +6,47 @@ namespace ShlinkioApiTest\Shlink\Rest\Middleware;
|
|||||||
|
|
||||||
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
class AuthenticationTest extends ApiTestCase
|
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';
|
$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);
|
$payload = $this->getJsonResponsePayload($resp);
|
||||||
|
|
||||||
self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
|
self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
|
||||||
self::assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']);
|
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($expectedDetail, $payload['detail']);
|
||||||
self::assertEquals('Invalid authorization', $payload['title']);
|
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
|
* @test
|
||||||
* @dataProvider provideInvalidApiKeys
|
* @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.';
|
$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' => [
|
'headers' => [
|
||||||
'X-Api-Key' => $apiKey,
|
'X-Api-Key' => $apiKey,
|
||||||
],
|
],
|
||||||
@ -40,15 +55,16 @@ class AuthenticationTest extends ApiTestCase
|
|||||||
|
|
||||||
self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
|
self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
|
||||||
self::assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']);
|
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($expectedDetail, $payload['detail']);
|
||||||
self::assertEquals('Invalid API key', $payload['title']);
|
self::assertEquals('Invalid API key', $payload['title']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideInvalidApiKeys(): iterable
|
public function provideInvalidApiKeys(): iterable
|
||||||
{
|
{
|
||||||
yield 'key which does not exist' => ['invalid'];
|
yield 'key which does not exist' => ['invalid', '2', 'INVALID_API_KEY'];
|
||||||
yield 'key which is expired' => ['expired_api_key'];
|
yield 'key which is expired' => ['expired_api_key', '2', 'INVALID_API_KEY'];
|
||||||
yield 'key which is disabled' => ['disabled_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'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user