From f067d0e831ca49f8c512c743767973564206d3ce Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 2 Oct 2019 20:13:25 +0200 Subject: [PATCH] Allowed to provide the domain when creating a short URL --- .../Action/ShortUrl/CreateShortUrlAction.php | 3 +- .../Action/CreateShortUrlActionTest.php | 29 +++++++++++++++++-- .../test-api/Action/ListShortUrlsTest.php | 18 ++++++++++-- .../test-api/Fixtures/ShortUrlsFixture.php | 6 ++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php index ff79818b..17d7eab1 100644 --- a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php @@ -36,7 +36,8 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction $this->getOptionalDate($postData, 'validUntil'), $postData['customSlug'] ?? null, $postData['maxVisits'] ?? null, - $postData['findIfExists'] ?? null + $postData['findIfExists'] ?? null, + $postData['domain'] ?? null ) ); } diff --git a/module/Rest/test-api/Action/CreateShortUrlActionTest.php b/module/Rest/test-api/Action/CreateShortUrlActionTest.php index 099eb9c4..1970cd7a 100644 --- a/module/Rest/test-api/Action/CreateShortUrlActionTest.php +++ b/module/Rest/test-api/Action/CreateShortUrlActionTest.php @@ -5,6 +5,7 @@ namespace ShlinkioApiTest\Shlink\Rest\Action; use Cake\Chronos\Chronos; use GuzzleHttp\RequestOptions; +use Shlinkio\Shlink\Rest\Util\RestUtils; use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase; use function Functional\map; @@ -33,6 +34,18 @@ class CreateShortUrlActionTest extends ApiTestCase $this->assertEquals('my-cool-slug', $payload['shortCode']); } + /** + * @test + * @dataProvider provideConflictingSlugs + */ + public function failsToCreateShortUrlWithDuplicatedSlug(string $slug, ?string $domain): void + { + [$statusCode, $payload] = $this->createShortUrl(['customSlug' => $slug, 'domain' => $domain]); + + $this->assertEquals(self::STATUS_BAD_REQUEST, $statusCode); + $this->assertEquals(RestUtils::INVALID_SLUG_ERROR, $payload['error']); + } + /** @test */ public function createsNewShortUrlWithTags(): void { @@ -126,22 +139,32 @@ class CreateShortUrlActionTest extends ApiTestCase ]]; } - /** @test */ - public function returnsErrorWhenRequestingReturnExistingButCustomSlugIsInUse(): void + /** + * @test + * @dataProvider provideConflictingSlugs + */ + public function returnsErrorWhenRequestingReturnExistingButCustomSlugIsInUse(string $slug, ?string $domain): void { $longUrl = 'https://www.alejandrocelaya.com'; [$firstStatusCode] = $this->createShortUrl(['longUrl' => $longUrl]); [$secondStatusCode] = $this->createShortUrl([ 'longUrl' => $longUrl, - 'customSlug' => 'custom', + 'customSlug' => $slug, 'findIfExists' => true, + 'domain' => $domain, ]); $this->assertEquals(self::STATUS_OK, $firstStatusCode); $this->assertEquals(self::STATUS_BAD_REQUEST, $secondStatusCode); } + public function provideConflictingSlugs(): iterable + { + yield 'without domain' => ['custom', null]; + yield 'with domain' => ['custom-with-domain', 'some-domain.com']; + } + /** @test */ public function createsNewShortUrlIfRequestedToFindButThereIsNoMatch(): void { diff --git a/module/Rest/test-api/Action/ListShortUrlsTest.php b/module/Rest/test-api/Action/ListShortUrlsTest.php index 8264c4ee..02d3b404 100644 --- a/module/Rest/test-api/Action/ListShortUrlsTest.php +++ b/module/Rest/test-api/Action/ListShortUrlsTest.php @@ -81,13 +81,27 @@ class ListShortUrlsTest extends ApiTestCase 'https://blog.alejandrocelaya.com/2019/04/27' . '/considerations-to-properly-use-open-source-software-projects/', ], + [ + 'shortCode' => 'custom-with-domain', + 'shortUrl' => 'http://some-domain.com/custom-with-domain', + 'longUrl' => 'https://google.com', + 'dateCreated' => '2019-01-01T00:00:00+00:00', + 'visitsCount' => 0, + 'tags' => [], + 'meta' => [ + 'validSince' => null, + 'validUntil' => null, + 'maxVisits' => null, + ], + 'originalUrl' => 'https://google.com', + ], ], 'pagination' => [ 'currentPage' => 1, 'pagesCount' => 1, 'itemsPerPage' => 10, - 'itemsInCurrentPage' => 4, - 'totalItems' => 4, + 'itemsInCurrentPage' => 5, + 'totalItems' => 5, ], ], ], $respPayload); diff --git a/module/Rest/test-api/Fixtures/ShortUrlsFixture.php b/module/Rest/test-api/Fixtures/ShortUrlsFixture.php index 0313c5cf..5e7d98ba 100644 --- a/module/Rest/test-api/Fixtures/ShortUrlsFixture.php +++ b/module/Rest/test-api/Fixtures/ShortUrlsFixture.php @@ -40,6 +40,12 @@ class ShortUrlsFixture extends AbstractFixture ))->setShortCode('ghi789'); $manager->persist($withDomainShortUrl); + $withDomainAndSlugShortUrl = $this->setShortUrlDate(new ShortUrl( + 'https://google.com', + ShortUrlMeta::createFromRawData(['domain' => 'some-domain.com']) + ))->setShortCode('custom-with-domain'); + $manager->persist($withDomainAndSlugShortUrl); + $manager->flush(); $this->addReference('abc123_short_url', $abcShortUrl);