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-01-26 12:21:51 -06:00
|
|
|
use function sprintf;
|
|
|
|
|
2019-11-20 13:44:03 -06:00
|
|
|
class ResolveShortUrlActionTest extends ApiTestCase
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode());
|
|
|
|
$this->assertEquals(self::STATUS_OK, $fetchResp->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
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]];
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:44:03 -06:00
|
|
|
/** @test */
|
|
|
|
public function tryingToResolveInvalidUrlReturnsNotFoundError(): void
|
|
|
|
{
|
2019-11-27 13:48:35 -06:00
|
|
|
$expectedDetail = 'No URL found with short code "invalid"';
|
|
|
|
|
2019-11-20 13:44:03 -06:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/short-urls/invalid');
|
2019-11-27 13:48:35 -06:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 13:44:03 -06:00
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
2019-11-27 13:48:35 -06:00
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
|
|
|
$this->assertEquals('INVALID_SHORTCODE', $payload['type']);
|
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
$this->assertEquals('Short URL not found', $payload['title']);
|
|
|
|
$this->assertEquals('invalid', $payload['shortCode']);
|
2019-11-20 13:44:03 -06:00
|
|
|
}
|
|
|
|
}
|