From c56d56d38c7af5962b711264aa95672e3bdb2ff9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 10 Jan 2021 09:09:56 +0100 Subject: [PATCH] Added api tests to cover implicit domain when creating short URLs with proper API key --- .../Action/CreateShortUrlActionTest.php | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/module/Rest/test-api/Action/CreateShortUrlActionTest.php b/module/Rest/test-api/Action/CreateShortUrlActionTest.php index c9bf6fe5..5e388b0d 100644 --- a/module/Rest/test-api/Action/CreateShortUrlActionTest.php +++ b/module/Rest/test-api/Action/CreateShortUrlActionTest.php @@ -244,18 +244,40 @@ class CreateShortUrlActionTest extends ApiTestCase self::assertNull($payload['domain']); } + /** + * @test + * @dataProvider provideDomains + */ + public function apiKeyDomainIsEnforced(?string $providedDomain): void + { + [$statusCode, ['domain' => $returnedDomain]] = $this->createShortUrl( + ['domain' => $providedDomain], + 'domain_api_key', + ); + + self::assertEquals(self::STATUS_OK, $statusCode); + self::assertEquals('example.com', $returnedDomain); + } + + public function provideDomains(): iterable + { + yield 'no domain' => [null]; + yield 'invalid domain' => ['this-will-be-overwritten.com']; + yield 'example domain' => ['example.com']; + } + /** * @return array { * @var int $statusCode * @var array $payload * } */ - private function createShortUrl(array $body = []): array + private function createShortUrl(array $body = [], string $apiKey = 'valid_api_key'): array { if (! isset($body['longUrl'])) { $body['longUrl'] = 'https://app.shlink.io'; } - $resp = $this->callApiWithKey(self::METHOD_POST, '/short-urls', [RequestOptions::JSON => $body]); + $resp = $this->callApiWithKey(self::METHOD_POST, '/short-urls', [RequestOptions::JSON => $body], $apiKey); $payload = $this->getJsonResponsePayload($resp); return [$resp->getStatusCode(), $payload];