Переглянути джерело

Applied API role specs to single short URL deletion

Alejandro Celaya 3 роки тому
батько
коміт
65797b61a0

+ 7 - 3
module/Core/src/Service/ShortUrl/DeleteShortUrlService.php

@@ -9,6 +9,7 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
 use Shlinkio\Shlink\Core\Exception;
 use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
 use Shlinkio\Shlink\Core\Options\DeleteShortUrlsOptions;
+use Shlinkio\Shlink\Rest\Entity\ApiKey;
 
 class DeleteShortUrlService implements DeleteShortUrlServiceInterface
 {
@@ -30,9 +31,12 @@ class DeleteShortUrlService implements DeleteShortUrlServiceInterface
      * @throws Exception\ShortUrlNotFoundException
      * @throws Exception\DeleteShortUrlException
      */
-    public function deleteByShortCode(ShortUrlIdentifier $identifier, bool $ignoreThreshold = false): void
-    {
-        $shortUrl = $this->urlResolver->resolveShortUrl($identifier);
+    public function deleteByShortCode(
+        ShortUrlIdentifier $identifier,
+        bool $ignoreThreshold = false,
+        ?ApiKey $apiKey = null
+    ): void {
+        $shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
         if (! $ignoreThreshold && $this->isThresholdReached($shortUrl)) {
             throw Exception\DeleteShortUrlException::fromVisitsThreshold(
                 $this->deleteShortUrlsOptions->getVisitsThreshold(),

+ 6 - 1
module/Core/src/Service/ShortUrl/DeleteShortUrlServiceInterface.php

@@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Service\ShortUrl;
 
 use Shlinkio\Shlink\Core\Exception;
 use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
+use Shlinkio\Shlink\Rest\Entity\ApiKey;
 
 interface DeleteShortUrlServiceInterface
 {
@@ -13,5 +14,9 @@ interface DeleteShortUrlServiceInterface
      * @throws Exception\ShortUrlNotFoundException
      * @throws Exception\DeleteShortUrlException
      */
-    public function deleteByShortCode(ShortUrlIdentifier $identifier, bool $ignoreThreshold = false): void;
+    public function deleteByShortCode(
+        ShortUrlIdentifier $identifier,
+        bool $ignoreThreshold = false,
+        ?ApiKey $apiKey = null
+    ): void;
 }

+ 5 - 1
module/Rest/src/Action/ShortUrl/DeleteShortUrlAction.php

@@ -10,6 +10,7 @@ use Psr\Http\Message\ServerRequestInterface;
 use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
 use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
 use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
+use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
 
 class DeleteShortUrlAction extends AbstractRestAction
 {
@@ -26,7 +27,10 @@ class DeleteShortUrlAction extends AbstractRestAction
     public function handle(ServerRequestInterface $request): ResponseInterface
     {
         $identifier = ShortUrlIdentifier::fromApiRequest($request);
-        $this->deleteShortUrlService->deleteByShortCode($identifier);
+        $apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
+
+        $this->deleteShortUrlService->deleteByShortCode($identifier, false, $apiKey);
+
         return new EmptyResponse();
     }
 }

+ 8 - 4
module/Rest/test/Action/ShortUrl/DeleteShortUrlActionTest.php

@@ -4,13 +4,14 @@ 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 Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
 use Shlinkio\Shlink\Rest\Action\ShortUrl\DeleteShortUrlAction;
+use Shlinkio\Shlink\Rest\Entity\ApiKey;
 
 class DeleteShortUrlActionTest extends TestCase
 {
@@ -28,10 +29,13 @@ class DeleteShortUrlActionTest extends TestCase
     /** @test */
     public function emptyResponseIsReturnedIfProperlyDeleted(): void
     {
-        $deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->will(function (): void {
-        });
+        $apiKey = new ApiKey();
+        $deleteByShortCode = $this->service->deleteByShortCode(Argument::any(), false, $apiKey)->will(
+            function (): void {
+            },
+        );
 
-        $resp = $this->action->handle(new ServerRequest());
+        $resp = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $apiKey));
 
         self::assertEquals(204, $resp->getStatusCode());
         $deleteByShortCode->shouldHaveBeenCalledOnce();