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