shlink/module/Rest/test/Middleware/AuthenticationMiddlewareTest.php

110 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware;
use Fig\Http\Message\RequestMethodInterface;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
2018-03-26 20:02:41 +03:00
use Psr\Http\Server\RequestHandlerInterface;
2019-12-31 17:38:37 +03:00
use Shlinkio\Shlink\Rest\Action\HealthAction;
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthenticationPluginInterface;
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPluginInterface;
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
2020-01-01 23:11:53 +03:00
use function Laminas\Stratigility\middleware;
class AuthenticationMiddlewareTest extends TestCase
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
private AuthenticationMiddleware $middleware;
private ObjectProphecy $requestToPlugin;
2018-03-21 13:13:03 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
{
$this->requestToPlugin = $this->prophesize(RequestToHttpAuthPluginInterface::class);
2020-11-02 13:50:19 +03:00
$this->middleware = new AuthenticationMiddleware($this->requestToPlugin->reveal(), [HealthAction::class]);
}
/**
* @test
* @dataProvider provideWhitelistedRequests
*/
2019-02-17 22:28:34 +03:00
public function someWhiteListedSituationsFallbackToNextMiddleware(ServerRequestInterface $request): void
{
$handler = $this->prophesize(RequestHandlerInterface::class);
$handle = $handler->handle($request)->willReturn(new Response());
$fromRequest = $this->requestToPlugin->fromRequest(Argument::any())->willReturn(
2020-01-01 22:48:31 +03:00
$this->prophesize(AuthenticationPluginInterface::class)->reveal(),
);
$this->middleware->process($request, $handler->reveal());
2018-11-11 15:18:21 +03:00
$handle->shouldHaveBeenCalledOnce();
$fromRequest->shouldNotHaveBeenCalled();
}
2019-02-17 22:28:34 +03:00
public function provideWhitelistedRequests(): iterable
{
$dummyMiddleware = $this->getDummyMiddleware();
2019-02-17 22:28:34 +03:00
yield 'with no route result' => [new ServerRequest()];
yield 'with failure route result' => [(new ServerRequest())->withAttribute(
RouteResult::class,
2020-01-01 22:48:31 +03:00
RouteResult::fromRouteFailure([RequestMethodInterface::METHOD_GET]),
2019-02-17 22:28:34 +03:00
)];
yield 'with whitelisted route' => [(new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(
2020-01-01 22:48:31 +03:00
new Route('foo', $dummyMiddleware, Route::HTTP_METHOD_ANY, HealthAction::class),
),
2019-02-17 22:28:34 +03:00
)];
yield 'with OPTIONS method' => [(new ServerRequest())->withAttribute(
RouteResult::class,
2020-01-01 22:48:31 +03:00
RouteResult::fromRoute(new Route('bar', $dummyMiddleware), []),
2019-02-17 22:28:34 +03:00
)->withMethod(RequestMethodInterface::METHOD_OPTIONS)];
}
2019-02-17 22:28:34 +03:00
/** @test */
public function updatedResponseIsReturnedWhenVerificationPasses(): void
{
$newResponse = new Response();
$request = (new ServerRequest())->withAttribute(
RouteResult::class,
2020-01-01 22:48:31 +03:00
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), []),
);
$plugin = $this->prophesize(AuthenticationPluginInterface::class);
2020-01-01 22:48:31 +03:00
$verify = $plugin->verify($request)->will(function (): void {
});
$update = $plugin->update($request, Argument::type(ResponseInterface::class))->willReturn($newResponse);
$fromRequest = $this->requestToPlugin->fromRequest(Argument::any())->willReturn($plugin->reveal());
$handler = $this->prophesize(RequestHandlerInterface::class);
$handle = $handler->handle($request)->willReturn(new Response());
$response = $this->middleware->process($request, $handler->reveal());
2020-10-04 01:35:14 +03:00
self::assertSame($response, $newResponse);
2018-11-11 15:18:21 +03:00
$verify->shouldHaveBeenCalledOnce();
$update->shouldHaveBeenCalledOnce();
$handle->shouldHaveBeenCalledOnce();
$fromRequest->shouldHaveBeenCalledOnce();
}
private function getDummyMiddleware(): MiddlewareInterface
{
return middleware(fn () => new Response\EmptyResponse());
}
}