mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 09:43:13 +08:00
Improved API tests and added test for short URLs creation
This commit is contained in:
121
module/Rest/test-api/Action/CreateShortUrlActionTest.php
Normal file
121
module/Rest/test-api/Action/CreateShortUrlActionTest.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase;
|
||||
|
||||
class CreateShortUrlActionTest extends ApiTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function createsNewShortUrlWhenOnlyLongUrlIsProvided()
|
||||
{
|
||||
$expectedKeys = ['shortCode', 'shortUrl', 'longUrl', 'dateCreated', 'visitsCount', 'tags'];
|
||||
[$statusCode, $payload] = $this->createShortUrl();
|
||||
|
||||
$this->assertEquals(self::STATUS_OK, $statusCode);
|
||||
foreach ($expectedKeys as $key) {
|
||||
$this->assertArrayHasKey($key, $payload);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function createsNewShortUrlWithCustomSlug()
|
||||
{
|
||||
[$statusCode, $payload] = $this->createShortUrl(['customSlug' => 'my cool slug']);
|
||||
|
||||
$this->assertEquals(self::STATUS_OK, $statusCode);
|
||||
$this->assertEquals('my-cool-slug', $payload['shortCode']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function createsNewShortUrlWithTags()
|
||||
{
|
||||
[$statusCode, $payload] = $this->createShortUrl(['tags' => ['foo', 'bar', 'baz']]);
|
||||
|
||||
$this->assertEquals(self::STATUS_OK, $statusCode);
|
||||
$this->assertEquals(['foo', 'bar', 'baz'], $payload['tags']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideMaxVisits
|
||||
*/
|
||||
public function createsNewShortUrlWithVisitsLimit(int $maxVisits)
|
||||
{
|
||||
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl(['maxVisits' => $maxVisits]);
|
||||
|
||||
$this->assertEquals(self::STATUS_OK, $statusCode);
|
||||
|
||||
// Last request to the short URL will return a 404, and the rest, a 302
|
||||
for ($i = 0; $i < $maxVisits; $i++) {
|
||||
$this->assertEquals(self::STATUS_FOUND, $this->callShortUrl($shortCode)->getStatusCode());
|
||||
}
|
||||
$lastResp = $this->callShortUrl($shortCode);
|
||||
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode());
|
||||
}
|
||||
|
||||
public function provideMaxVisits(): array
|
||||
{
|
||||
return [
|
||||
[1],
|
||||
[5],
|
||||
[3],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function createsShortUrlWithValidSince()
|
||||
{
|
||||
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl([
|
||||
'validSince' => Chronos::now()->addDay()->toAtomString(),
|
||||
]);
|
||||
|
||||
$this->assertEquals(self::STATUS_OK, $statusCode);
|
||||
|
||||
// Request to the short URL will return a 404 since ist' not valid yet
|
||||
$lastResp = $this->callShortUrl($shortCode);
|
||||
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function createsShortUrlWithValidUntil()
|
||||
{
|
||||
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl([
|
||||
'validUntil' => Chronos::now()->subDay()->toAtomString(),
|
||||
]);
|
||||
|
||||
$this->assertEquals(self::STATUS_OK, $statusCode);
|
||||
|
||||
// Request to the short URL will return a 404 since it's no longer valid
|
||||
$lastResp = $this->callShortUrl($shortCode);
|
||||
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array {
|
||||
* @var int $statusCode
|
||||
* @var array $payload
|
||||
* }
|
||||
*/
|
||||
private function createShortUrl(array $body = []): array
|
||||
{
|
||||
$body['longUrl'] = 'https://app.shlink.io';
|
||||
$resp = $this->callApiWithKey(self::METHOD_POST, '/short-urls', [RequestOptions::JSON => $body]);
|
||||
$payload = $this->getJsonResponsePayload($resp);
|
||||
|
||||
return [$resp->getStatusCode(), $payload];
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Middleware;
|
||||
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
|
||||
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin;
|
||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
||||
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase;
|
||||
use function implode;
|
||||
use function Shlinkio\Shlink\Common\json_decode;
|
||||
use function sprintf;
|
||||
|
||||
class AuthenticationTest extends ApiTestCase
|
||||
@@ -19,21 +17,18 @@ class AuthenticationTest extends ApiTestCase
|
||||
*/
|
||||
public function authorizationErrorIsReturnedIfNoApiKeyIsSent()
|
||||
{
|
||||
try {
|
||||
$this->callApi(self::METHOD_GET, '/short-codes');
|
||||
} catch (ClientException $e) {
|
||||
['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($e->getResponse());
|
||||
$resp = $this->callApi(self::METHOD_GET, '/short-codes');
|
||||
['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($resp);
|
||||
|
||||
$this->assertEquals(self::STATUS_UNAUTHORIZED, $e->getCode());
|
||||
$this->assertEquals(RestUtils::INVALID_AUTHORIZATION_ERROR, $error);
|
||||
$this->assertEquals(
|
||||
sprintf(
|
||||
'Expected one of the following authentication headers, but none were provided, ["%s"]',
|
||||
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
|
||||
),
|
||||
$message
|
||||
);
|
||||
}
|
||||
$this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
|
||||
$this->assertEquals(RestUtils::INVALID_AUTHORIZATION_ERROR, $error);
|
||||
$this->assertEquals(
|
||||
sprintf(
|
||||
'Expected one of the following authentication headers, but none were provided, ["%s"]',
|
||||
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
|
||||
),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,19 +37,16 @@ class AuthenticationTest extends ApiTestCase
|
||||
*/
|
||||
public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid(string $apiKey)
|
||||
{
|
||||
try {
|
||||
$this->callApi(self::METHOD_GET, '/short-codes', [
|
||||
'headers' => [
|
||||
ApiKeyHeaderPlugin::HEADER_NAME => $apiKey,
|
||||
],
|
||||
]);
|
||||
} catch (ClientException $e) {
|
||||
['error' => $error, 'message' => $message] = json_decode((string) $e->getResponse()->getBody());
|
||||
$resp = $this->callApi(self::METHOD_GET, '/short-codes', [
|
||||
'headers' => [
|
||||
ApiKeyHeaderPlugin::HEADER_NAME => $apiKey,
|
||||
],
|
||||
]);
|
||||
['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($resp);
|
||||
|
||||
$this->assertEquals(self::STATUS_UNAUTHORIZED, $e->getCode());
|
||||
$this->assertEquals(RestUtils::INVALID_API_KEY_ERROR, $error);
|
||||
$this->assertEquals('Provided API key does not exist or is invalid.', $message);
|
||||
}
|
||||
$this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
|
||||
$this->assertEquals(RestUtils::INVALID_API_KEY_ERROR, $error);
|
||||
$this->assertEquals('Provided API key does not exist or is invalid.', $message);
|
||||
}
|
||||
|
||||
public function provideInvalidApiKeys(): array
|
||||
|
||||
Reference in New Issue
Block a user