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
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Authentication;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Rest\Authentication\AuthenticationPluginManager;
use Shlinkio\Shlink\Rest\Authentication\AuthenticationPluginManagerFactory;
use Zend\ServiceManager\ServiceManager;
class AuthenticationPluginManagerFactoryTest extends TestCase
{
/**
* @var AuthenticationPluginManagerFactory
*/
private $factory;
public function setUp()
{
$this->factory = new AuthenticationPluginManagerFactory();
}
/**
* @test
*/
public function serviceIsProperlyCreated()
{
$instance = $this->factory->__invoke(new ServiceManager(['services' => [
'config' => [],
]]), '');
$this->assertInstanceOf(AuthenticationPluginManager::class, $instance);
}
}
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Authentication;
namespace ShlinkioTest\Shlink\Rest\Authentication\Plugin;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Authentication;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Rest\Authentication\AuthenticationPluginManagerInterface;
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthenticationPluginInterface;
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin;
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin;
use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException;
use Zend\Diactoros\ServerRequestFactory;
class RequestToAuthPluginTest extends TestCase
{
/**
* @var RequestToHttpAuthPlugin
*/
private $requestToPlugin;
/**
* @var ObjectProphecy
*/
private $pluginManager;
public function setUp()
{
$this->pluginManager = $this->prophesize(AuthenticationPluginManagerInterface::class);
$this->requestToPlugin = new RequestToHttpAuthPlugin($this->pluginManager->reveal());
}
/**
* @test
*/
public function exceptionIsFoundWhenNoneOfTheSupportedMethodsIsFound()
{
$request = ServerRequestFactory::fromGlobals();
$this->expectException(NoAuthenticationException::class);
$this->expectExceptionMessage(sprintf(
'None of the valid authentication mechanisms where provided. Expected one of ["%s"]',
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
));
$this->requestToPlugin->fromRequest($request);
}
/**
* @test
* @dataProvider provideHeaders
*/
public function properPluginIsFetchedWhenAnyAuthTypeIsFound(array $headers, string $expectedHeader)
{
$request = ServerRequestFactory::fromGlobals();
foreach ($headers as $header => $value) {
$request = $request->withHeader($header, $value);
}
$plugin = $this->prophesize(AuthenticationPluginInterface::class);
$getPlugin = $this->pluginManager->get($expectedHeader)->willReturn($plugin->reveal());
$this->requestToPlugin->fromRequest($request);
$getPlugin->shouldHaveBeenCalledTimes(1);
}
public function provideHeaders(): array
{
return [
'API key header only' => [[
ApiKeyHeaderPlugin::HEADER_NAME => 'foobar',
], ApiKeyHeaderPlugin::HEADER_NAME],
'Authorization header only' => [[
AuthorizationHeaderPlugin::HEADER_NAME => 'foobar',
], AuthorizationHeaderPlugin::HEADER_NAME],
'Both headers' => [[
AuthorizationHeaderPlugin::HEADER_NAME => 'foobar',
ApiKeyHeaderPlugin::HEADER_NAME => 'foobar',
], ApiKeyHeaderPlugin::HEADER_NAME],
];
}
}
@@ -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());