From 25ee9b5dafe8425257bcfd224120aae99bef4994 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 3 Jan 2021 16:50:47 +0100 Subject: [PATCH] Applied API role specs to single short URL tags edition --- .dockerignore | 2 +- module/Core/src/Service/ShortUrlService.php | 4 ++-- .../src/Service/ShortUrlServiceInterface.php | 2 +- .../Core/test/Service/ShortUrlServiceTest.php | 4 ++-- .../ShortUrl/EditShortUrlTagsAction.php | 4 +++- .../ShortUrl/EditShortUrlTagsActionTest.php | 24 ++++++++++++++----- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.dockerignore b/.dockerignore index 9a48c84c..2080adcf 100644 --- a/.dockerignore +++ b/.dockerignore @@ -17,7 +17,7 @@ indocker docker-* phpstan.neon php*xml* -infection.json +infection* **/test* build* **/.* diff --git a/module/Core/src/Service/ShortUrlService.php b/module/Core/src/Service/ShortUrlService.php index b2691de2..06b39f08 100644 --- a/module/Core/src/Service/ShortUrlService.php +++ b/module/Core/src/Service/ShortUrlService.php @@ -55,9 +55,9 @@ class ShortUrlService implements ShortUrlServiceInterface * @param string[] $tags * @throws ShortUrlNotFoundException */ - public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags = []): ShortUrl + public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags, ?ApiKey $apiKey = null): ShortUrl { - $shortUrl = $this->urlResolver->resolveShortUrl($identifier); + $shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey); $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags)); $this->em->flush(); diff --git a/module/Core/src/Service/ShortUrlServiceInterface.php b/module/Core/src/Service/ShortUrlServiceInterface.php index 63867045..5f6b9b30 100644 --- a/module/Core/src/Service/ShortUrlServiceInterface.php +++ b/module/Core/src/Service/ShortUrlServiceInterface.php @@ -24,7 +24,7 @@ interface ShortUrlServiceInterface * @param string[] $tags * @throws ShortUrlNotFoundException */ - public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags = []): ShortUrl; + public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags, ?ApiKey $apiKey = null): ShortUrl; /** * @throws ShortUrlNotFoundException diff --git a/module/Core/test/Service/ShortUrlServiceTest.php b/module/Core/test/Service/ShortUrlServiceTest.php index b290ceb5..19c92b6f 100644 --- a/module/Core/test/Service/ShortUrlServiceTest.php +++ b/module/Core/test/Service/ShortUrlServiceTest.php @@ -74,8 +74,8 @@ class ShortUrlServiceTest extends TestCase $shortUrl = $this->prophesize(ShortUrl::class); $shortUrl->setTags(Argument::any())->shouldBeCalledOnce(); $shortCode = 'abc123'; - $this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode))->willReturn($shortUrl->reveal()) - ->shouldBeCalledOnce(); + $this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode), null)->willReturn($shortUrl->reveal()) + ->shouldBeCalledOnce(); $tagRepo = $this->prophesize(EntityRepository::class); $tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag('foo'))->shouldBeCalledOnce(); diff --git a/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php b/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php index def36d6c..7d115765 100644 --- a/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php +++ b/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php @@ -11,6 +11,7 @@ use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; +use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware; class EditShortUrlTagsAction extends AbstractRestAction { @@ -35,8 +36,9 @@ class EditShortUrlTagsAction extends AbstractRestAction } ['tags' => $tags] = $bodyParams; $identifier = ShortUrlIdentifier::fromApiRequest($request); + $apiKey = AuthenticationMiddleware::apiKeyFromRequest($request); - $shortUrl = $this->shortUrlService->setTagsByShortCode($identifier, $tags); + $shortUrl = $this->shortUrlService->setTagsByShortCode($identifier, $tags, $apiKey); return new JsonResponse(['tags' => $shortUrl->getTags()->toArray()]); } } diff --git a/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php b/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php index 2fa6f456..9c72dd91 100644 --- a/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php +++ b/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php @@ -4,15 +4,18 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl; -use Laminas\Diactoros\ServerRequest; +use Laminas\Diactoros\ServerRequestFactory; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlTagsAction; +use Shlinkio\Shlink\Rest\Entity\ApiKey; class EditShortUrlTagsActionTest extends TestCase { @@ -31,20 +34,29 @@ class EditShortUrlTagsActionTest extends TestCase public function notProvidingTagsReturnsError(): void { $this->expectException(ValidationException::class); - $this->action->handle((new ServerRequest())->withAttribute('shortCode', 'abc123')); + $this->action->handle($this->createRequestWithAPiKey()->withAttribute('shortCode', 'abc123')); } /** @test */ public function tagsListIsReturnedIfCorrectShortCodeIsProvided(): void { $shortCode = 'abc123'; - $this->shortUrlService->setTagsByShortCode(new ShortUrlIdentifier($shortCode), [])->willReturn(new ShortUrl('')) - ->shouldBeCalledOnce(); + $this->shortUrlService->setTagsByShortCode( + new ShortUrlIdentifier($shortCode), + [], + Argument::type(ApiKey::class), + )->willReturn(new ShortUrl('')) + ->shouldBeCalledOnce(); $response = $this->action->handle( - (new ServerRequest())->withAttribute('shortCode', 'abc123') - ->withParsedBody(['tags' => []]), + $this->createRequestWithAPiKey()->withAttribute('shortCode', 'abc123') + ->withParsedBody(['tags' => []]), ); self::assertEquals(200, $response->getStatusCode()); } + + private function createRequestWithAPiKey(): ServerRequestInterface + { + return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, new ApiKey()); + } }