From ea05259bbe12939d7cdd0aa5642cba3146f7b3c7 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 10 Jan 2021 09:02:05 +0100 Subject: [PATCH] Improved api tests where a short URL needs to be resolved, covering cases where API key lacks permissions --- .../Action/DeleteShortUrlActionTest.php | 5 +++-- .../Action/EditShortUrlActionTest.php | 5 +++-- .../Action/EditShortUrlTagsActionTest.php | 5 +++-- .../Action/ResolveShortUrlActionTest.php | 5 +++-- .../Action/ShortUrlVisitsActionTest.php | 10 ++++++++-- .../Utils/NotFoundUrlHelpersTrait.php | 20 ++++++++++++++++--- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/module/Rest/test-api/Action/DeleteShortUrlActionTest.php b/module/Rest/test-api/Action/DeleteShortUrlActionTest.php index 7c66ff0b..76968cbd 100644 --- a/module/Rest/test-api/Action/DeleteShortUrlActionTest.php +++ b/module/Rest/test-api/Action/DeleteShortUrlActionTest.php @@ -18,9 +18,10 @@ class DeleteShortUrlActionTest extends ApiTestCase public function notFoundErrorIsReturnWhenDeletingInvalidUrl( string $shortCode, ?string $domain, - string $expectedDetail + string $expectedDetail, + string $apiKey ): void { - $resp = $this->callApiWithKey(self::METHOD_DELETE, $this->buildShortUrlPath($shortCode, $domain)); + $resp = $this->callApiWithKey(self::METHOD_DELETE, $this->buildShortUrlPath($shortCode, $domain), [], $apiKey); $payload = $this->getJsonResponsePayload($resp); self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); diff --git a/module/Rest/test-api/Action/EditShortUrlActionTest.php b/module/Rest/test-api/Action/EditShortUrlActionTest.php index e6b37eba..a909130a 100644 --- a/module/Rest/test-api/Action/EditShortUrlActionTest.php +++ b/module/Rest/test-api/Action/EditShortUrlActionTest.php @@ -104,10 +104,11 @@ class EditShortUrlActionTest extends ApiTestCase public function tryingToEditInvalidUrlReturnsNotFoundError( string $shortCode, ?string $domain, - string $expectedDetail + string $expectedDetail, + string $apiKey ): void { $url = $this->buildShortUrlPath($shortCode, $domain); - $resp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => []]); + $resp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => []], $apiKey); $payload = $this->getJsonResponsePayload($resp); self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); diff --git a/module/Rest/test-api/Action/EditShortUrlTagsActionTest.php b/module/Rest/test-api/Action/EditShortUrlTagsActionTest.php index 84d2af80..7fe45c73 100644 --- a/module/Rest/test-api/Action/EditShortUrlTagsActionTest.php +++ b/module/Rest/test-api/Action/EditShortUrlTagsActionTest.php @@ -34,12 +34,13 @@ class EditShortUrlTagsActionTest extends ApiTestCase public function providingInvalidShortCodeReturnsBadRequest( string $shortCode, ?string $domain, - string $expectedDetail + string $expectedDetail, + string $apiKey ): void { $url = $this->buildShortUrlPath($shortCode, $domain, '/tags'); $resp = $this->callApiWithKey(self::METHOD_PUT, $url, [RequestOptions::JSON => [ 'tags' => ['foo', 'bar'], - ]]); + ]], $apiKey); $payload = $this->getJsonResponsePayload($resp); self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); diff --git a/module/Rest/test-api/Action/ResolveShortUrlActionTest.php b/module/Rest/test-api/Action/ResolveShortUrlActionTest.php index cf1a7212..7996e459 100644 --- a/module/Rest/test-api/Action/ResolveShortUrlActionTest.php +++ b/module/Rest/test-api/Action/ResolveShortUrlActionTest.php @@ -50,9 +50,10 @@ class ResolveShortUrlActionTest extends ApiTestCase public function tryingToResolveInvalidUrlReturnsNotFoundError( string $shortCode, ?string $domain, - string $expectedDetail + string $expectedDetail, + string $apiKey ): void { - $resp = $this->callApiWithKey(self::METHOD_GET, $this->buildShortUrlPath($shortCode, $domain)); + $resp = $this->callApiWithKey(self::METHOD_GET, $this->buildShortUrlPath($shortCode, $domain), [], $apiKey); $payload = $this->getJsonResponsePayload($resp); self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); diff --git a/module/Rest/test-api/Action/ShortUrlVisitsActionTest.php b/module/Rest/test-api/Action/ShortUrlVisitsActionTest.php index 6e2463a2..22864108 100644 --- a/module/Rest/test-api/Action/ShortUrlVisitsActionTest.php +++ b/module/Rest/test-api/Action/ShortUrlVisitsActionTest.php @@ -22,9 +22,15 @@ class ShortUrlVisitsActionTest extends ApiTestCase public function tryingToGetVisitsForInvalidUrlReturnsNotFoundError( string $shortCode, ?string $domain, - string $expectedDetail + string $expectedDetail, + string $apiKey ): void { - $resp = $this->callApiWithKey(self::METHOD_GET, $this->buildShortUrlPath($shortCode, $domain, '/visits')); + $resp = $this->callApiWithKey( + self::METHOD_GET, + $this->buildShortUrlPath($shortCode, $domain, '/visits'), + [], + $apiKey, + ); $payload = $this->getJsonResponsePayload($resp); self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); diff --git a/module/Rest/test-api/Utils/NotFoundUrlHelpersTrait.php b/module/Rest/test-api/Utils/NotFoundUrlHelpersTrait.php index 3cf2ad30..1c415208 100644 --- a/module/Rest/test-api/Utils/NotFoundUrlHelpersTrait.php +++ b/module/Rest/test-api/Utils/NotFoundUrlHelpersTrait.php @@ -4,25 +4,39 @@ declare(strict_types=1); namespace ShlinkioApiTest\Shlink\Rest\Utils; +use GuzzleHttp\Psr7\Query; use Laminas\Diactoros\Uri; -use function GuzzleHttp\Psr7\build_query; use function sprintf; trait NotFoundUrlHelpersTrait { public function provideInvalidUrls(): iterable { - yield 'invalid shortcode' => ['invalid', null, 'No URL found with short code "invalid"']; + yield 'invalid shortcode' => ['invalid', null, 'No URL found with short code "invalid"', 'valid_api_key']; yield 'invalid shortcode without domain' => [ 'abc123', 'example.com', 'No URL found with short code "abc123" for domain "example.com"', + 'valid_api_key', ]; yield 'invalid shortcode + domain' => [ 'custom-with-domain', 'example.com', 'No URL found with short code "custom-with-domain" for domain "example.com"', + 'valid_api_key', + ]; + yield 'valid shortcode with invalid API key' => [ + 'ghi789', + null, + 'No URL found with short code "ghi789"', + 'author_api_key', + ]; + yield 'valid shortcode + domain with invalid API key' => [ + 'custom-with-domain', + 'some-domain.com', + 'No URL found with short code "custom-with-domain" for domain "some-domain.com"', + 'domain_api_key', ]; } @@ -30,7 +44,7 @@ trait NotFoundUrlHelpersTrait { $url = new Uri(sprintf('/short-urls/%s%s', $shortCode, $suffix)); if ($domain !== null) { - $url = $url->withQuery(build_query(['domain' => $domain])); + $url = $url->withQuery(Query::build(['domain' => $domain])); } return (string) $url;