Skip to content

Commit

Permalink
Applied API role specs to single short URL edition
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jan 3, 2021
1 parent 65797b6 commit fff10eb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 25 deletions.
15 changes: 5 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions infection-db.json
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 4 additions & 4 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "."
},
Expand Down
9 changes: 6 additions & 3 deletions module/Core/src/Service/ShortUrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags =
* @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();
Expand Down
6 changes: 5 additions & 1 deletion module/Core/src/Service/ShortUrlServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags =
* @throws ShortUrlNotFoundException
* @throws InvalidUrlException
*/
public function updateMetadataByShortCode(ShortUrlIdentifier $identifier, ShortUrlEdit $shortUrlEdit): ShortUrl;
public function updateMetadataByShortCode(
ShortUrlIdentifier $identifier,
ShortUrlEdit $shortUrlEdit,
?ApiKey $apiKey = null
): ShortUrl;
}
17 changes: 11 additions & 6 deletions module/Core/test/Service/ShortUrlServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;

Expand Down Expand Up @@ -90,15 +91,19 @@ public function providedTagsAreGetFromRepoAndSetToTheShortUrl(): void
*/
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());
Expand All @@ -121,19 +126,19 @@ public function provideShortUrlEdits(): iterable
'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];
}
}
4 changes: 3 additions & 1 deletion module/Rest/src/Action/ShortUrl/EditShortUrlAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
{
Expand All @@ -28,8 +29,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$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 changes: 2 additions & 0 deletions module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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
{
Expand Down Expand Up @@ -43,6 +44,7 @@ public function invalidDataThrowsError(): void
public function correctShortCodeReturnsSuccess(): void
{
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
->withAttribute(ApiKey::class, new ApiKey())
->withParsedBody([
'maxVisits' => 5,
]);
Expand Down

0 comments on commit fff10eb

Please sign in to comment.