2018-09-29 09:16:40 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-09-29 09:16:40 +03:00
|
|
|
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;
|
2019-11-26 23:43:29 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:34:02 +03:00
|
|
|
use function implode;
|
|
|
|
use function sprintf;
|
2018-09-29 09:16:40 +03:00
|
|
|
|
|
|
|
class RequestToAuthPluginTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
private RequestToHttpAuthPlugin $requestToPlugin;
|
|
|
|
private ObjectProphecy $pluginManager;
|
2018-09-29 09:16:40 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-09-29 09:16:40 +03:00
|
|
|
{
|
|
|
|
$this->pluginManager = $this->prophesize(AuthenticationPluginManagerInterface::class);
|
|
|
|
$this->requestToPlugin = new RequestToHttpAuthPlugin($this->pluginManager->reveal());
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
|
|
|
public function exceptionIsFoundWhenNoneOfTheSupportedMethodsIsFound(): void
|
2018-09-29 09:16:40 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = new ServerRequest();
|
2018-09-29 09:16:40 +03:00
|
|
|
|
2019-11-26 23:43:29 +03:00
|
|
|
$this->expectException(MissingAuthenticationException::class);
|
2018-09-29 09:16:40 +03:00
|
|
|
$this->expectExceptionMessage(sprintf(
|
2019-12-01 12:14:29 +03:00
|
|
|
'Expected one of the following authentication headers, ["%s"], but none were provided',
|
2018-09-29 09:16:40 +03:00
|
|
|
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->requestToPlugin->fromRequest($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideHeaders
|
|
|
|
*/
|
2019-02-17 22:28:34 +03:00
|
|
|
public function properPluginIsFetchedWhenAnyAuthTypeIsFound(array $headers, string $expectedHeader): void
|
2018-09-29 09:16:40 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = new ServerRequest();
|
2018-09-29 09:16:40 +03:00
|
|
|
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);
|
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$getPlugin->shouldHaveBeenCalledOnce();
|
2018-09-29 09:16:40 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
public function provideHeaders(): iterable
|
2018-09-29 09:16:40 +03:00
|
|
|
{
|
2019-02-17 22:28:34 +03:00
|
|
|
yield 'API key header only' => [[
|
|
|
|
ApiKeyHeaderPlugin::HEADER_NAME => 'foobar',
|
|
|
|
], ApiKeyHeaderPlugin::HEADER_NAME];
|
|
|
|
yield 'Authorization header only' => [[
|
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME => 'foobar',
|
|
|
|
], AuthorizationHeaderPlugin::HEADER_NAME];
|
|
|
|
yield 'Both headers' => [[
|
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME => 'foobar',
|
|
|
|
ApiKeyHeaderPlugin::HEADER_NAME => 'foobar',
|
|
|
|
], ApiKeyHeaderPlugin::HEADER_NAME];
|
2018-09-29 09:16:40 +03:00
|
|
|
}
|
|
|
|
}
|