2019-11-20 13:44:03 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2020-01-26 12:21:51 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use GuzzleHttp\RequestOptions;
|
2019-11-20 13:44:03 -06:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
2020-02-02 06:15:08 -06:00
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait;
|
2019-11-20 13:44:03 -06:00
|
|
|
|
2020-01-26 12:21:51 -06:00
|
|
|
use function sprintf;
|
|
|
|
|
2019-11-20 13:44:03 -06:00
|
|
|
class ResolveShortUrlActionTest extends ApiTestCase
|
|
|
|
{
|
2020-02-02 06:15:08 -06:00
|
|
|
use NotFoundUrlHelpersTrait;
|
|
|
|
|
2020-01-26 12:21:51 -06:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDisabledMeta
|
|
|
|
*/
|
|
|
|
public function shortUrlIsProperlyResolvedEvenWhenNotEnabled(array $disabledMeta): void
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$url = sprintf('/short-urls/%s', $shortCode);
|
|
|
|
$this->callShortUrl($shortCode);
|
|
|
|
|
|
|
|
$editResp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $disabledMeta]);
|
|
|
|
$visitResp = $this->callShortUrl($shortCode);
|
|
|
|
$fetchResp = $this->callApiWithKey(self::METHOD_GET, $url);
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_OK, $fetchResp->getStatusCode());
|
2020-01-26 12:21:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideDisabledMeta(): iterable
|
|
|
|
{
|
|
|
|
$now = Chronos::now();
|
|
|
|
|
|
|
|
yield 'future validSince' => [['validSince' => $now->addMonth()->toAtomString()]];
|
|
|
|
yield 'past validUntil' => [['validUntil' => $now->subMonth()->toAtomString()]];
|
|
|
|
yield 'maxVisits reached' => [['maxVisits' => 1]];
|
|
|
|
}
|
|
|
|
|
2020-02-02 06:15:08 -06:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidUrls
|
|
|
|
*/
|
|
|
|
public function tryingToResolveInvalidUrlReturnsNotFoundError(
|
|
|
|
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 {
|
2021-01-10 02:02:05 -06:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, $this->buildShortUrlPath($shortCode, $domain), [], $apiKey);
|
2019-11-27 13:48:35 -06:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 13:44:03 -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:44:03 -06:00
|
|
|
}
|
|
|
|
}
|