From fff10ebee414e1cdbf2ffd27f6612d4592ff70f1 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 3 Jan 2021 16:41:44 +0100 Subject: [PATCH] Applied API role specs to single short URL edition --- composer.json | 15 ++++-------- infection-db.json | 23 +++++++++++++++++++ infection.json | 8 +++---- module/Core/src/Service/ShortUrlService.php | 9 +++++--- .../src/Service/ShortUrlServiceInterface.php | 6 ++++- .../Core/test/Service/ShortUrlServiceTest.php | 17 +++++++++----- .../Action/ShortUrl/EditShortUrlAction.php | 4 +++- .../ShortUrl/EditShortUrlActionTest.php | 2 ++ 8 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 infection-db.json diff --git a/composer.json b/composer.json index 09b26c42..97cdf152 100644 --- a/composer.json +++ b/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" diff --git a/infection-db.json b/infection-db.json new file mode 100644 index 00000000..a429c995 --- /dev/null +++ b/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 + } +} diff --git a/infection.json b/infection.json index 44fdf228..b182bddf 100644 --- a/infection.json +++ b/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": "." }, diff --git a/module/Core/src/Service/ShortUrlService.php b/module/Core/src/Service/ShortUrlService.php index 410e0f16..b2691de2 100644 --- a/module/Core/src/Service/ShortUrlService.php +++ b/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(); diff --git a/module/Core/src/Service/ShortUrlServiceInterface.php b/module/Core/src/Service/ShortUrlServiceInterface.php index b3582ac2..63867045 100644 --- a/module/Core/src/Service/ShortUrlServiceInterface.php +++ b/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; } diff --git a/module/Core/test/Service/ShortUrlServiceTest.php b/module/Core/test/Service/ShortUrlServiceTest.php index fc2de22b..b290ceb5 100644 --- a/module/Core/test/Service/ShortUrlServiceTest.php +++ b/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]; } } diff --git a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php b/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php index 30d95ae1..32d95b2d 100644 --- a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php +++ b/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(); } } diff --git a/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php b/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php index 087b4298..5e9eadf7 100644 --- a/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php +++ b/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, ]);