Added api tests to cover implicit domain when creating short URLs with proper API key

This commit is contained in:
Alejandro Celaya 2021-01-10 09:09:56 +01:00
parent ea05259bbe
commit c56d56d38c

View file

@ -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];