2019-11-20 22:31:18 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2020-01-26 11:29:04 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2021-05-23 13:31:10 +03:00
|
|
|
use GuzzleHttp\Psr7\Query;
|
2019-11-20 22:31:18 +03:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2020-02-02 11:51:17 +03:00
|
|
|
use Laminas\Diactoros\Uri;
|
2019-11-20 22:31:18 +03:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
2020-02-02 15:15:08 +03:00
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait;
|
2020-02-02 21:19:35 +03:00
|
|
|
|
2020-01-26 11:29:04 +03:00
|
|
|
use function sprintf;
|
|
|
|
|
2021-01-24 11:25:36 +03:00
|
|
|
class EditShortUrlTest extends ApiTestCase
|
2019-11-20 22:31:18 +03:00
|
|
|
{
|
2020-01-26 11:29:04 +03:00
|
|
|
use ArraySubsetAsserts;
|
2020-02-02 15:15:08 +03:00
|
|
|
use NotFoundUrlHelpersTrait;
|
2020-01-26 11:29:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2020-01-26 21:21:51 +03:00
|
|
|
* @dataProvider provideMeta
|
2020-01-26 11:29:04 +03:00
|
|
|
*/
|
|
|
|
public function metadataCanBeReset(array $meta): void
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$url = sprintf('/short-urls/%s', $shortCode);
|
|
|
|
$resetMeta = [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$editWithProvidedMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $meta]);
|
|
|
|
$metaAfterEditing = $this->findShortUrlMetaByShortCode($shortCode);
|
|
|
|
|
|
|
|
$editWithResetMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [
|
|
|
|
RequestOptions::JSON => $resetMeta,
|
|
|
|
]);
|
|
|
|
$metaAfterResetting = $this->findShortUrlMetaByShortCode($shortCode);
|
|
|
|
|
2021-01-31 15:21:23 +03:00
|
|
|
self::assertEquals(self::STATUS_OK, $editWithProvidedMeta->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_OK, $editWithResetMeta->getStatusCode());
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($resetMeta, $metaAfterResetting);
|
2020-01-26 11:29:04 +03:00
|
|
|
self::assertArraySubset($meta, $metaAfterEditing);
|
|
|
|
}
|
|
|
|
|
2020-01-26 21:21:51 +03:00
|
|
|
public function provideMeta(): iterable
|
2020-01-26 11:29:04 +03:00
|
|
|
{
|
|
|
|
$now = Chronos::now();
|
|
|
|
|
|
|
|
yield [['validSince' => $now->addMonth()->toAtomString()]];
|
|
|
|
yield [['validUntil' => $now->subMonth()->toAtomString()]];
|
|
|
|
yield [['maxVisits' => 20]];
|
|
|
|
yield [['validUntil' => $now->addYear()->toAtomString(), 'maxVisits' => 100]];
|
|
|
|
yield [[
|
|
|
|
'validSince' => $now->subYear()->toAtomString(),
|
|
|
|
'validUntil' => $now->addYear()->toAtomString(),
|
|
|
|
'maxVisits' => 100,
|
|
|
|
]];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findShortUrlMetaByShortCode(string $shortCode): ?array
|
|
|
|
{
|
2020-02-02 11:51:17 +03:00
|
|
|
$matchingShortUrl = $this->getJsonResponsePayload(
|
|
|
|
$this->callApiWithKey(self::METHOD_GET, '/short-urls/' . $shortCode),
|
|
|
|
);
|
2020-01-26 11:29:04 +03:00
|
|
|
|
|
|
|
return $matchingShortUrl['meta'] ?? null;
|
|
|
|
}
|
|
|
|
|
2020-03-22 19:30:01 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideLongUrls
|
|
|
|
*/
|
|
|
|
public function longUrlCanBeEditedIfItIsValid(string $longUrl, int $expectedStatus, ?string $expectedError): void
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$url = sprintf('/short-urls/%s', $shortCode);
|
|
|
|
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => [
|
|
|
|
'longUrl' => $longUrl,
|
|
|
|
]]);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($expectedStatus, $resp->getStatusCode());
|
2020-03-22 19:30:01 +03:00
|
|
|
if ($expectedError !== null) {
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($expectedError, $payload['type']);
|
2020-03-22 19:30:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideLongUrls(): iterable
|
|
|
|
{
|
2021-01-31 15:21:23 +03:00
|
|
|
yield 'valid URL' => ['https://shlink.io', self::STATUS_OK, null];
|
2020-03-22 19:30:01 +03:00
|
|
|
yield 'invalid URL' => ['htt:foo', self::STATUS_BAD_REQUEST, 'INVALID_URL'];
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:15:08 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidUrls
|
|
|
|
*/
|
|
|
|
public function tryingToEditInvalidUrlReturnsNotFoundError(
|
|
|
|
string $shortCode,
|
|
|
|
?string $domain,
|
2021-01-10 11:02:05 +03:00
|
|
|
string $expectedDetail,
|
2021-05-23 13:31:10 +03:00
|
|
|
string $apiKey,
|
2020-02-02 15:15:08 +03:00
|
|
|
): void {
|
|
|
|
$url = $this->buildShortUrlPath($shortCode, $domain);
|
2021-01-10 11:02:05 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => []], $apiKey);
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:31:18 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
|
|
|
self::assertEquals('INVALID_SHORTCODE', $payload['type']);
|
|
|
|
self::assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
self::assertEquals('Short URL not found', $payload['title']);
|
|
|
|
self::assertEquals($shortCode, $payload['shortCode']);
|
|
|
|
self::assertEquals($domain, $payload['domain'] ?? null);
|
2019-11-20 22:31:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function providingInvalidDataReturnsBadRequest(): void
|
|
|
|
{
|
2019-11-27 22:48:35 +03:00
|
|
|
$expectedDetail = 'Provided data is not valid';
|
|
|
|
|
2019-11-20 22:31:18 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/invalid', [RequestOptions::JSON => [
|
|
|
|
'maxVisits' => 'not_a_number',
|
|
|
|
]]);
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:31:18 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_BAD_REQUEST, $payload['status']);
|
|
|
|
self::assertEquals('INVALID_ARGUMENT', $payload['type']);
|
|
|
|
self::assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
self::assertEquals('Invalid data', $payload['title']);
|
2019-11-20 22:31:18 +03:00
|
|
|
}
|
2020-02-02 11:51:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDomains
|
|
|
|
*/
|
|
|
|
public function metadataIsEditedOnProperShortUrlBasedOnDomain(?string $domain, string $expectedUrl): void
|
|
|
|
{
|
|
|
|
$shortCode = 'ghi789';
|
|
|
|
$url = new Uri(sprintf('/short-urls/%s', $shortCode));
|
|
|
|
|
|
|
|
if ($domain !== null) {
|
2021-05-23 13:31:10 +03:00
|
|
|
$url = $url->withQuery(Query::build(['domain' => $domain]));
|
2020-02-02 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$editResp = $this->callApiWithKey(self::METHOD_PATCH, (string) $url, [RequestOptions::JSON => [
|
|
|
|
'maxVisits' => 100,
|
|
|
|
]]);
|
|
|
|
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url));
|
|
|
|
|
2021-01-31 15:21:23 +03:00
|
|
|
self::assertEquals(self::STATUS_OK, $editResp->getStatusCode());
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($domain, $editedShortUrl['domain']);
|
|
|
|
self::assertEquals($expectedUrl, $editedShortUrl['longUrl']);
|
|
|
|
self::assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null);
|
2020-02-02 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideDomains(): iterable
|
|
|
|
{
|
2020-02-02 12:46:38 +03:00
|
|
|
yield 'domain' => [
|
2020-02-02 11:51:17 +03:00
|
|
|
'example.com',
|
|
|
|
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/',
|
|
|
|
];
|
2020-02-02 12:46:38 +03:00
|
|
|
yield 'no domain' => [null, 'https://shlink.io/documentation/'];
|
2020-02-02 11:51:17 +03:00
|
|
|
}
|
2019-11-20 22:31:18 +03:00
|
|
|
}
|