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