Browse Source

Applied API role specs to single short URL edition

Alejandro Celaya 3 years ago
parent
commit
fff10ebee4

+ 5 - 10
composer.json

@@ -134,17 +134,12 @@
         "test:db:ms": "DB_DRIVER=mssql composer test:db:sqlite",
         "test:api": "bin/test/run-api-tests.sh",
         "test:unit:pretty": "@php vendor/bin/phpunit --order-by=random --colors=always --coverage-html build/coverage-unit-html",
-        "infect": "infection --threads=4 --min-msi=80 --log-verbosity=default --only-covered",
-        "infect:ci:base": "@infect --skip-initial-tests",
-        "infect:ci:unit": "@infect:ci:base --coverage=build/coverage-unit",
-        "infect:ci:db": "@infect:ci:base --coverage=build/coverage-db --min-msi=95 --test-framework-options=--configuration=phpunit-db.xml",
-        "infect:ci": [
-            "@infect:ci:unit",
-            "@infect:ci:db"
-        ],
+        "infect:ci:base": "infection --threads=4 --log-verbosity=default --only-covered --skip-initial-tests",
+        "infect:ci:unit": "@infect:ci:base --coverage=build/coverage-unit --min-msi=80",
+        "infect:ci:db": "@infect:ci:base --coverage=build/coverage-db --min-msi=95 --configuration=infection-db.json",
+        "infect:ci": "@parallel infect:ci:unit infect:ci:db",
         "infect:test": [
-            "@test:unit:ci",
-            "@test:db:sqlite:ci",
+            "@parallel test:unit:ci test:db:sqlite:ci",
             "@infect:ci"
         ],
         "clean:dev": "rm -f data/database.sqlite && rm -f config/params/generated_config.php"

+ 23 - 0
infection-db.json

@@ -0,0 +1,23 @@
+{
+    "source": {
+        "directories": [
+            "module/*/src"
+        ]
+    },
+    "timeout": 5,
+    "logs": {
+        "text": "build/infection-db/infection-log.txt",
+        "summary": "build/infection-db/summary-log.txt",
+        "debug": "build/infection-db/debug-log.txt"
+    },
+    "tmpDir": "build/infection-db/temp",
+    "phpUnit": {
+        "configDir": "."
+    },
+    "testFrameworkOptions": "--configuration=phpunit-db.xml",
+    "mutators": {
+        "@default": true,
+        "IdenticalEqual": false,
+        "NotIdenticalNotEqual": false
+    }
+}

+ 4 - 4
infection.json

@@ -6,11 +6,11 @@
     },
     "timeout": 5,
     "logs": {
-        "text": "build/infection/infection-log.txt",
-        "summary": "build/infection/summary-log.txt",
-        "debug": "build/infection/debug-log.txt"
+        "text": "build/infection-unit/infection-log.txt",
+        "summary": "build/infection-unit/summary-log.txt",
+        "debug": "build/infection-unit/debug-log.txt"
     },
-    "tmpDir": "build/infection/temp",
+    "tmpDir": "build/infection-unit/temp",
     "phpUnit": {
         "configDir": "."
     },

+ 6 - 3
module/Core/src/Service/ShortUrlService.php

@@ -69,13 +69,16 @@ class ShortUrlService implements ShortUrlServiceInterface
      * @throws ShortUrlNotFoundException
      * @throws InvalidUrlException
      */
-    public function updateMetadataByShortCode(ShortUrlIdentifier $identifier, ShortUrlEdit $shortUrlEdit): ShortUrl
-    {
+    public function updateMetadataByShortCode(
+        ShortUrlIdentifier $identifier,
+        ShortUrlEdit $shortUrlEdit,
+        ?ApiKey $apiKey = null
+    ): ShortUrl {
         if ($shortUrlEdit->hasLongUrl()) {
             $this->urlValidator->validateUrl($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl());
         }
 
-        $shortUrl = $this->urlResolver->resolveShortUrl($identifier);
+        $shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
         $shortUrl->update($shortUrlEdit);
 
         $this->em->flush();

+ 5 - 1
module/Core/src/Service/ShortUrlServiceInterface.php

@@ -30,5 +30,9 @@ interface ShortUrlServiceInterface
      * @throws ShortUrlNotFoundException
      * @throws InvalidUrlException
      */
-    public function updateMetadataByShortCode(ShortUrlIdentifier $identifier, ShortUrlEdit $shortUrlEdit): ShortUrl;
+    public function updateMetadataByShortCode(
+        ShortUrlIdentifier $identifier,
+        ShortUrlEdit $shortUrlEdit,
+        ?ApiKey $apiKey = null
+    ): ShortUrl;
 }

+ 11 - 6
module/Core/test/Service/ShortUrlServiceTest.php

@@ -20,6 +20,7 @@ use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
 use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
 use Shlinkio\Shlink\Core\Service\ShortUrlService;
 use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
+use Shlinkio\Shlink\Rest\Entity\ApiKey;
 
 use function count;
 
@@ -90,15 +91,19 @@ class ShortUrlServiceTest extends TestCase
      */
     public function updateMetadataByShortCodeUpdatesProvidedData(
         int $expectedValidateCalls,
-        ShortUrlEdit $shortUrlEdit
+        ShortUrlEdit $shortUrlEdit,
+        ?ApiKey $apiKey
     ): void {
         $originalLongUrl = 'originalLongUrl';
         $shortUrl = new ShortUrl($originalLongUrl);
 
-        $findShortUrl = $this->urlResolver->resolveShortUrl(new ShortUrlIdentifier('abc123'))->willReturn($shortUrl);
+        $findShortUrl = $this->urlResolver->resolveShortUrl(
+            new ShortUrlIdentifier('abc123'),
+            $apiKey,
+        )->willReturn($shortUrl);
         $flush = $this->em->flush()->willReturn(null);
 
-        $result = $this->service->updateMetadataByShortCode(new ShortUrlIdentifier('abc123'), $shortUrlEdit);
+        $result = $this->service->updateMetadataByShortCode(new ShortUrlIdentifier('abc123'), $shortUrlEdit, $apiKey);
 
         self::assertSame($shortUrl, $result);
         self::assertEquals($shortUrlEdit->validSince(), $shortUrl->getValidSince());
@@ -121,19 +126,19 @@ class ShortUrlServiceTest extends TestCase
                 'validUntil' => Chronos::parse('2017-01-05 00:00:00')->toAtomString(),
                 'maxVisits' => 5,
             ],
-        )];
+        ), null];
         yield 'long URL' => [1, ShortUrlEdit::fromRawData(
             [
                 'validSince' => Chronos::parse('2017-01-01 00:00:00')->toAtomString(),
                 'maxVisits' => 10,
                 'longUrl' => 'modifiedLongUrl',
             ],
-        )];
+        ), new ApiKey()];
         yield 'long URL with validation' => [1, ShortUrlEdit::fromRawData(
             [
                 'longUrl' => 'modifiedLongUrl',
                 'validateUrl' => true,
             ],
-        )];
+        ), null];
     }
 }

+ 3 - 1
module/Rest/src/Action/ShortUrl/EditShortUrlAction.php

@@ -11,6 +11,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
 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 EditShortUrlAction extends AbstractRestAction
 {
@@ -28,8 +29,9 @@ class EditShortUrlAction extends AbstractRestAction
     {
         $shortUrlEdit = ShortUrlEdit::fromRawData((array) $request->getParsedBody());
         $identifier = ShortUrlIdentifier::fromApiRequest($request);
+        $apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
 
-        $this->shortUrlService->updateMetadataByShortCode($identifier, $shortUrlEdit);
+        $this->shortUrlService->updateMetadataByShortCode($identifier, $shortUrlEdit, $apiKey);
         return new EmptyResponse();
     }
 }

+ 2 - 0
module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php

@@ -13,6 +13,7 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
 use Shlinkio\Shlink\Core\Exception\ValidationException;
 use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
 use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
+use Shlinkio\Shlink\Rest\Entity\ApiKey;
 
 class EditShortUrlActionTest extends TestCase
 {
@@ -43,6 +44,7 @@ class EditShortUrlActionTest extends TestCase
     public function correctShortCodeReturnsSuccess(): void
     {
         $request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
+                                        ->withAttribute(ApiKey::class, new ApiKey())
                                         ->withParsedBody([
                                             'maxVisits' => 5,
                                         ]);