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

115 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2019-10-05 10:26:10 -05:00
2017-10-12 03:13:20 -05:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware;
use Fig\Http\Message\RequestMethodInterface;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
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-08-08 10:06:20 -05:00
use Psr\Log\LoggerInterface;
2019-12-31 08:38:37 -06: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;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
2017-03-24 15:38:43 -05:00
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use function Zend\Stratigility\middleware;
class AuthenticationMiddlewareTest extends TestCase
{
private AuthenticationMiddleware $middleware;
private ObjectProphecy $requestToPlugin;
private ObjectProphecy $logger;
2018-03-21 05:13:03 -05:00
2019-02-16 03:53:45 -06:00
public function setUp(): void
{
$this->requestToPlugin = $this->prophesize(RequestToHttpAuthPluginInterface::class);
2019-08-08 10:06:20 -05:00
$this->logger = $this->prophesize(LoggerInterface::class);
$this->middleware = new AuthenticationMiddleware(
$this->requestToPlugin->reveal(),
2019-12-31 08:38:37 -06:00
[HealthAction::class],
2019-08-08 10:06:20 -05:00
$this->logger->reveal()
);
}
/**
* @test
* @dataProvider provideWhitelistedRequests
*/
2019-02-17 13:28:34 -06: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(
$this->prophesize(AuthenticationPluginInterface::class)->reveal()
);
$this->middleware->process($request, $handler->reveal());
2018-11-11 06:18:21 -06:00
$handle->shouldHaveBeenCalledOnce();
$fromRequest->shouldNotHaveBeenCalled();
}
2019-02-17 13:28:34 -06:00
public function provideWhitelistedRequests(): iterable
{
$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,
RouteResult::fromRouteFailure([RequestMethodInterface::METHOD_GET])
)];
yield 'with whitelisted route' => [(new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(
2019-12-31 08:38:37 -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,
RouteResult::fromRoute(new Route('bar', $dummyMiddleware), [])
)->withMethod(RequestMethodInterface::METHOD_OPTIONS)];
}
2019-02-17 13:28:34 -06:00
/** @test */
public function updatedResponseIsReturnedWhenVerificationPasses(): void
{
$newResponse = new Response();
$request = (new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), [])
);
$plugin = $this->prophesize(AuthenticationPluginInterface::class);
$verify = $plugin->verify($request)->will(function () {
});
$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());
$this->assertSame($response, $newResponse);
2018-11-11 06:18:21 -06:00
$verify->shouldHaveBeenCalledOnce();
$update->shouldHaveBeenCalledOnce();
$handle->shouldHaveBeenCalledOnce();
$fromRequest->shouldHaveBeenCalledOnce();
}
private function getDummyMiddleware(): MiddlewareInterface
{
return middleware(fn () => new Response\EmptyResponse());
}
}