Skip to content

Commit

Permalink
add uuid validation in read, update, delete requestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Mar 11, 2020
1 parent fad43bb commit 127e8cf
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/RequestHandler/Api/Crud/DeleteRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Ramsey\Uuid\Uuid;

final class DeleteRequestHandler implements RequestHandlerInterface
{
Expand All @@ -36,7 +37,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$id = $request->getAttribute('id');
$accept = $request->getAttribute('accept');

if (null === $model = $this->repository->findById($id)) {
if (!Uuid::isValid($id) || null === $model = $this->repository->findById($id)) {
return $this->responseManager->createFromApiProblem(new NotFound(), $accept);
}

Expand Down
3 changes: 2 additions & 1 deletion app/RequestHandler/Api/Crud/ReadRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Ramsey\Uuid\Uuid;

final class ReadRequestHandler implements RequestHandlerInterface
{
Expand All @@ -37,7 +38,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$id = $request->getAttribute('id');
$accept = $request->getAttribute('accept');

if (null === $model = $this->repository->findById($id)) {
if (!Uuid::isValid($id) || null === $model = $this->repository->findById($id)) {
return $this->responseManager->createFromApiProblem(new NotFound(), $accept);
}

Expand Down
3 changes: 2 additions & 1 deletion app/RequestHandler/Api/Crud/UpdateRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Ramsey\Uuid\Uuid;

final class UpdateRequestHandler implements RequestHandlerInterface
{
Expand Down Expand Up @@ -60,7 +61,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$accept = $request->getAttribute('accept');
$contentType = $request->getAttribute('contentType');

if (null === $model = $this->repository->findById($id)) {
if (!Uuid::isValid($id) || null === $model = $this->repository->findById($id)) {
/** @var ModelInterface $model */
return $this->responseManager->createFromApiProblem(new NotFound(), $accept);
}
Expand Down
32 changes: 31 additions & 1 deletion tests/Unit/RequestHandler/Api/Crud/DeleteRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,37 @@ final class DeleteRequestHandlerTest extends TestCase
{
use MockByCallsTrait;

public function testCreateResourceNotFound(): void
public function testCreateResourceNotFoundInvalidUuid(): void
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->getMockByCalls(ServerRequestInterface::class, [
Call::create('getAttribute')->with('id', null)->willReturn('1234'),
Call::create('getAttribute')->with('accept', null)->willReturn('application/json'),
]);

/** @var ResponseInterface|MockObject $response */
$response = $this->getMockByCalls(ResponseInterface::class);

/** @var RepositoryInterface|MockObject $repository */
$repository = $this->getMockByCalls(RepositoryInterface::class);

/** @var ResponseManagerInterface|MockObject $responseManager */
$responseManager = $this->getMockByCalls(ResponseManagerInterface::class, [
Call::create('createFromApiProblem')
->with(
new ArgumentCallback(function (NotFound $apiProblem): void {}),
'application/json',
null
)
->willReturn($response),
]);

$requestHandler = new DeleteRequestHandler($repository, $responseManager);

self::assertSame($response, $requestHandler->handle($request));
}

public function testCreateResourceNotFoundMissingModel(): void
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->getMockByCalls(ServerRequestInterface::class, [
Expand Down
32 changes: 31 additions & 1 deletion tests/Unit/RequestHandler/Api/Crud/ReadRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,37 @@ final class ReadRequestHandlerTest extends TestCase
{
use MockByCallsTrait;

public function testCreateResourceNotFound(): void
public function testCreateResourceNotFoundInvalidUuid(): void
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->getMockByCalls(ServerRequestInterface::class, [
Call::create('getAttribute')->with('id', null)->willReturn('1234'),
Call::create('getAttribute')->with('accept', null)->willReturn('application/json'),
]);

/** @var ResponseInterface|MockObject $response */
$response = $this->getMockByCalls(ResponseInterface::class);

/** @var RepositoryInterface|MockObject $repository */
$repository = $this->getMockByCalls(RepositoryInterface::class);

/** @var ResponseManagerInterface|MockObject $responseManager */
$responseManager = $this->getMockByCalls(ResponseManagerInterface::class, [
Call::create('createFromApiProblem')
->with(
new ArgumentCallback(function (NotFound $apiProblem): void {}),
'application/json',
null
)
->willReturn($response),
]);

$requestHandler = new ReadRequestHandler($repository, $responseManager);

self::assertSame($response, $requestHandler->handle($request));
}

public function testCreateResourceNotFoundMissingModel(): void
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->getMockByCalls(ServerRequestInterface::class, [
Expand Down
44 changes: 43 additions & 1 deletion tests/Unit/RequestHandler/Api/Crud/UpdateRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,49 @@ final class UpdateRequestHandlerTest extends TestCase
{
use MockByCallsTrait;

public function testCreateResourceNotFound(): void
public function testCreateResourceNotFoundInvalidUuid(): void
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->getMockByCalls(ServerRequestInterface::class, [
Call::create('getAttribute')->with('id', null)->willReturn('1234'),
Call::create('getAttribute')->with('accept', null)->willReturn('application/json'),
Call::create('getAttribute')->with('contentType', null)->willReturn('application/json'),
]);

/** @var ResponseInterface|MockObject $response */
$response = $this->getMockByCalls(ResponseInterface::class);

/** @var RepositoryInterface|MockObject $repository */
$repository = $this->getMockByCalls(RepositoryInterface::class);

/** @var RequestManagerInterface|MockObject $requestManager */
$requestManager = $this->getMockByCalls(RequestManagerInterface::class);

/** @var ResponseManagerInterface|MockObject $responseManager */
$responseManager = $this->getMockByCalls(ResponseManagerInterface::class, [
Call::create('createFromApiProblem')
->with(
new ArgumentCallback(function (NotFound $apiProblem): void {}),
'application/json',
null
)
->willReturn($response),
]);

/** @var ValidatorInterface|MockObject $validator */
$validator = $this->getMockByCalls(ValidatorInterface::class);

$requestHandler = new UpdateRequestHandler(
$repository,
$requestManager,
$responseManager,
$validator
);

self::assertSame($response, $requestHandler->handle($request));
}

public function testCreateResourceNotFoundMissingModel(): void
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->getMockByCalls(ServerRequestInterface::class, [
Expand Down

0 comments on commit 127e8cf

Please sign in to comment.