From 4a5cc9a986ad15bf8bde7e96c0b858304d348a2b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 21 Jan 2021 19:43:34 +0100 Subject: [PATCH] Added API test for single-step short URL creation action --- CHANGELOG.md | 1 + .../Action/SingleStepCreateShortUrlTest.php | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 module/Rest/test-api/Action/SingleStepCreateShortUrlTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5376959a..578d78df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ### Fixed * [#968](https://github.com/shlinkio/shlink/issues/968) Fixed index error in MariaDB while updating to v2.5.0. +* [#972](https://github.com/shlinkio/shlink/issues/972) Fixed 500 error when calling single-step short URL creation endpoint. ## [2.5.0] - 2021-01-17 diff --git a/module/Rest/test-api/Action/SingleStepCreateShortUrlTest.php b/module/Rest/test-api/Action/SingleStepCreateShortUrlTest.php new file mode 100644 index 00000000..0c5ab1b4 --- /dev/null +++ b/module/Rest/test-api/Action/SingleStepCreateShortUrlTest.php @@ -0,0 +1,56 @@ +createShortUrl($format, 'valid_api_key'); + + self::assertEquals(self::STATUS_OK, $resp->getStatusCode()); + self::assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type')); + } + + public function provideFormats(): iterable + { + yield 'txt format' => ['txt', 'text/plain']; + yield 'json format' => ['json', 'application/json']; + yield ' format' => [null, 'application/json']; + } + + /** @test */ + public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void + { + $expectedDetail = 'Expected authentication to be provided in "apiKey" query param'; + + $resp = $this->createShortUrl(); + $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($expectedDetail, $payload['detail']); + self::assertEquals('Invalid authorization', $payload['title']); + } + + private function createShortUrl(?string $format = 'json', ?string $apiKey = null): ResponseInterface + { + $query = [ + 'longUrl' => 'https://app.shlink.io', + 'apiKey' => $apiKey, + 'format' => $format, + ]; + return $this->callApi(self::METHOD_GET, '/short-urls/shorten', [RequestOptions::QUERY => $query]); + } +}