Applied API role specs to single short URL tags edition

This commit is contained in:
Alejandro Celaya 2021-01-03 16:50:47 +01:00
parent fff10ebee4
commit 25ee9b5daf
6 changed files with 27 additions and 13 deletions

View File

@ -17,7 +17,7 @@ indocker
docker-* docker-*
phpstan.neon phpstan.neon
php*xml* php*xml*
infection.json infection*
**/test* **/test*
build* build*
**/.* **/.*

View File

@ -55,9 +55,9 @@ class ShortUrlService implements ShortUrlServiceInterface
* @param string[] $tags * @param string[] $tags
* @throws ShortUrlNotFoundException * @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)); $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
$this->em->flush(); $this->em->flush();

View File

@ -24,7 +24,7 @@ interface ShortUrlServiceInterface
* @param string[] $tags * @param string[] $tags
* @throws ShortUrlNotFoundException * @throws ShortUrlNotFoundException
*/ */
public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags = []): ShortUrl; public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags, ?ApiKey $apiKey = null): ShortUrl;
/** /**
* @throws ShortUrlNotFoundException * @throws ShortUrlNotFoundException

View File

@ -74,7 +74,7 @@ class ShortUrlServiceTest extends TestCase
$shortUrl = $this->prophesize(ShortUrl::class); $shortUrl = $this->prophesize(ShortUrl::class);
$shortUrl->setTags(Argument::any())->shouldBeCalledOnce(); $shortUrl->setTags(Argument::any())->shouldBeCalledOnce();
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode))->willReturn($shortUrl->reveal()) $this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode), null)->willReturn($shortUrl->reveal())
->shouldBeCalledOnce(); ->shouldBeCalledOnce();
$tagRepo = $this->prophesize(EntityRepository::class); $tagRepo = $this->prophesize(EntityRepository::class);

View File

@ -11,6 +11,7 @@ use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
class EditShortUrlTagsAction extends AbstractRestAction class EditShortUrlTagsAction extends AbstractRestAction
{ {
@ -35,8 +36,9 @@ class EditShortUrlTagsAction extends AbstractRestAction
} }
['tags' => $tags] = $bodyParams; ['tags' => $tags] = $bodyParams;
$identifier = ShortUrlIdentifier::fromApiRequest($request); $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()]); return new JsonResponse(['tags' => $shortUrl->getTags()->toArray()]);
} }
} }

View File

@ -4,15 +4,18 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use Laminas\Diactoros\ServerRequest; use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlTagsAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlTagsAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class EditShortUrlTagsActionTest extends TestCase class EditShortUrlTagsActionTest extends TestCase
{ {
@ -31,20 +34,29 @@ class EditShortUrlTagsActionTest extends TestCase
public function notProvidingTagsReturnsError(): void public function notProvidingTagsReturnsError(): void
{ {
$this->expectException(ValidationException::class); $this->expectException(ValidationException::class);
$this->action->handle((new ServerRequest())->withAttribute('shortCode', 'abc123')); $this->action->handle($this->createRequestWithAPiKey()->withAttribute('shortCode', 'abc123'));
} }
/** @test */ /** @test */
public function tagsListIsReturnedIfCorrectShortCodeIsProvided(): void public function tagsListIsReturnedIfCorrectShortCodeIsProvided(): void
{ {
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->shortUrlService->setTagsByShortCode(new ShortUrlIdentifier($shortCode), [])->willReturn(new ShortUrl('')) $this->shortUrlService->setTagsByShortCode(
new ShortUrlIdentifier($shortCode),
[],
Argument::type(ApiKey::class),
)->willReturn(new ShortUrl(''))
->shouldBeCalledOnce(); ->shouldBeCalledOnce();
$response = $this->action->handle( $response = $this->action->handle(
(new ServerRequest())->withAttribute('shortCode', 'abc123') $this->createRequestWithAPiKey()->withAttribute('shortCode', 'abc123')
->withParsedBody(['tags' => []]), ->withParsedBody(['tags' => []]),
); );
self::assertEquals(200, $response->getStatusCode()); self::assertEquals(200, $response->getStatusCode());
} }
private function createRequestWithAPiKey(): ServerRequestInterface
{
return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, new ApiKey());
}
} }