Converted AuthenticationpluginManager in a plain plugin manager and encasulated in new service adding extra behavior

This commit is contained in:
Alejandro Celaya
2018-09-29 08:16:40 +02:00
parent 8e61639598
commit 0f5fb066d1
10 changed files with 226 additions and 85 deletions

View File

@@ -14,9 +14,9 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
use Shlinkio\Shlink\Rest\Authentication\AuthenticationPluginManager;
use Shlinkio\Shlink\Rest\Authentication\AuthenticationPluginManagerInterface;
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthenticationPluginInterface;
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin;
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPluginInterface;
use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException;
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
@@ -39,7 +39,7 @@ class AuthenticationMiddlewareTest extends TestCase
/**
* @var ObjectProphecy
*/
protected $pluginManager;
protected $requestToPlugin;
/**
* @var callable
@@ -48,8 +48,8 @@ class AuthenticationMiddlewareTest extends TestCase
public function setUp()
{
$this->pluginManager = $this->prophesize(AuthenticationPluginManagerInterface::class);
$this->middleware = new AuthenticationMiddleware($this->pluginManager->reveal(), Translator::factory([]), [
$this->requestToPlugin = $this->prophesize(RequestToHttpAuthPluginInterface::class);
$this->middleware = new AuthenticationMiddleware($this->requestToPlugin->reveal(), Translator::factory([]), [
AuthenticateAction::class,
]);
}
@@ -62,7 +62,7 @@ class AuthenticationMiddlewareTest extends TestCase
{
$handler = $this->prophesize(RequestHandlerInterface::class);
$handle = $handler->handle($request)->willReturn(new Response());
$fromRequest = $this->pluginManager->fromRequest(Argument::any())->willReturn(
$fromRequest = $this->requestToPlugin->fromRequest(Argument::any())->willReturn(
$this->prophesize(AuthenticationPluginInterface::class)->reveal()
);
@@ -101,12 +101,11 @@ class AuthenticationMiddlewareTest extends TestCase
*/
public function errorIsReturnedWhenNoValidAuthIsProvided($e)
{
$authToken = 'ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), [])
)->withHeader(AuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
$fromRequest = $this->pluginManager->fromRequest(Argument::any())->willThrow($e);
);
$fromRequest = $this->requestToPlugin->fromRequest(Argument::any())->willThrow($e);
/** @var Response\JsonResponse $response */
$response = $this->middleware->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
@@ -115,7 +114,7 @@ class AuthenticationMiddlewareTest extends TestCase
$this->assertEquals(RestUtils::INVALID_AUTHORIZATION_ERROR, $payload['error']);
$this->assertEquals(sprintf(
'Expected one of the following authentication headers, but none were provided, ["%s"]',
implode('", "', AuthenticationPluginManager::SUPPORTED_AUTH_HEADERS)
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
), $payload['message']);
$fromRequest->shouldHaveBeenCalledTimes(1);
}
@@ -134,17 +133,16 @@ class AuthenticationMiddlewareTest extends TestCase
*/
public function errorIsReturnedWhenVerificationFails()
{
$authToken = 'ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), [])
)->withHeader(AuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
);
$plugin = $this->prophesize(AuthenticationPluginInterface::class);
$verify = $plugin->verify($request)->willThrow(
VerifyAuthenticationException::withError('the_error', 'the_message')
);
$fromRequest = $this->pluginManager->fromRequest(Argument::any())->willReturn($plugin->reveal());
$fromRequest = $this->requestToPlugin->fromRequest(Argument::any())->willReturn($plugin->reveal());
/** @var Response\JsonResponse $response */
$response = $this->middleware->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
@@ -161,18 +159,17 @@ class AuthenticationMiddlewareTest extends TestCase
*/
public function updatedResponseIsReturnedWhenVerificationPasses()
{
$authToken = 'ABC-abc';
$newResponse = new Response();
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), [])
)->withHeader(AuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
);
$plugin = $this->prophesize(AuthenticationPluginInterface::class);
$verify = $plugin->verify($request)->will(function () {
});
$update = $plugin->update($request, Argument::type(ResponseInterface::class))->willReturn($newResponse);
$fromRequest = $this->pluginManager->fromRequest(Argument::any())->willReturn($plugin->reveal());
$fromRequest = $this->requestToPlugin->fromRequest(Argument::any())->willReturn($plugin->reveal());
$handler = $this->prophesize(RequestHandlerInterface::class);
$handle = $handler->handle($request)->willReturn(new Response());