From de81e81ecb7c99c87f01f21833271ab2fb889eef Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 3 Aug 2021 19:43:30 +0200 Subject: [PATCH] Created API test for Domain redirects --- .../test-api/Action/DomainRedirectsTest.php | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 module/Rest/test-api/Action/DomainRedirectsTest.php diff --git a/module/Rest/test-api/Action/DomainRedirectsTest.php b/module/Rest/test-api/Action/DomainRedirectsTest.php new file mode 100644 index 00000000..987c09d6 --- /dev/null +++ b/module/Rest/test-api/Action/DomainRedirectsTest.php @@ -0,0 +1,100 @@ +callApiWithKey(self::METHOD_PATCH, '/domains/redirects', [ + RequestOptions::JSON => ['domain' => 'doma.in'], + ]); + $payload = $this->getJsonResponsePayload($resp); + + self::assertEquals(self::STATUS_FORBIDDEN, $resp->getStatusCode()); + self::assertEquals(self::STATUS_FORBIDDEN, $payload['status']); + self::assertEquals('INVALID_DOMAIN', $payload['type']); + self::assertEquals( + 'You cannot configure default domain\'s redirects this way. Use the configuration or env vars.', + $payload['detail'], + ); + self::assertEquals('Invalid domain', $payload['title']); + } + + /** + * @test + * @dataProvider provideInvalidDomains + */ + public function anErrorIsReturnedWhenTryingToEditAnInvalidDomain(array $request): void + { + $resp = $this->callApiWithKey(self::METHOD_PATCH, '/domains/redirects', [ + RequestOptions::JSON => $request, + ]); + $payload = $this->getJsonResponsePayload($resp); + + self::assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode()); + self::assertEquals(self::STATUS_BAD_REQUEST, $payload['status']); + self::assertEquals('INVALID_ARGUMENT', $payload['type']); + self::assertEquals('Provided data is not valid', $payload['detail']); + self::assertEquals('Invalid data', $payload['title']); + } + + public function provideInvalidDomains(): iterable + { + yield 'no domain' => [[]]; + yield 'empty domain' => [['domain' => '']]; + yield 'null domain' => [['domain' => null]]; + yield 'invalid domain' => [['domain' => '192.168.1.1']]; + } + + /** + * @test + * @dataProvider provideRequests + */ + public function allowsToEditDomainRedirects(array $request, array $expectedResponse): void + { + $resp = $this->callApiWithKey(self::METHOD_PATCH, '/domains/redirects', [ + RequestOptions::JSON => $request, + ]); + $payload = $this->getJsonResponsePayload($resp); + + self::assertEquals(self::STATUS_OK, $resp->getStatusCode()); + self::assertEquals($expectedResponse, $payload); + } + + public function provideRequests(): iterable + { + yield 'new domain' => [[ + 'domain' => 'my-new-domain.com', + 'regular404Redirect' => 'foo.com', + ], [ + 'baseUrlRedirect' => null, + 'regular404Redirect' => 'foo.com', + 'invalidShortUrlRedirect' => null, + ]]; + yield 'existing domain with redirects' => [[ + 'domain' => 'detached-with-redirects.com', + 'baseUrlRedirect' => null, + 'invalidShortUrlRedirect' => 'foo.com', + ], [ + 'baseUrlRedirect' => null, + 'regular404Redirect' => 'bar.com', + 'invalidShortUrlRedirect' => 'foo.com', + ]]; + yield 'existing domain with no redirects' => [[ + 'domain' => 'example.com', + 'baseUrlRedirect' => null, + 'invalidShortUrlRedirect' => 'foo.com', + ], [ + 'baseUrlRedirect' => null, + 'regular404Redirect' => null, + 'invalidShortUrlRedirect' => 'foo.com', + ]]; + } +}