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-*
phpstan.neon
php*xml*
infection.json
infection*
**/test*
build*
**/.*

View File

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

View File

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

View File

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

View File

@ -11,6 +11,7 @@ use Shlinkio\Shlink\Core\Exception\ValidationException;
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 EditShortUrlTagsAction extends AbstractRestAction
{
@ -35,8 +36,9 @@ class EditShortUrlTagsAction extends AbstractRestAction
}
['tags' => $tags] = $bodyParams;
$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()]);
}
}

View File

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