shlink/module/Rest/test/Authentication/RequestToAuthPluginTest.php

70 lines
2.3 KiB
PHP
Raw Normal View History

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