|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Rest\Authentication; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
8
|
|
|
use Shlinkio\Shlink\Rest\Authentication\AuthenticationPluginManagerInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin; |
|
10
|
|
|
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthenticationPluginInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin; |
|
12
|
|
|
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin; |
|
13
|
|
|
use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException; |
|
14
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
15
|
|
|
|
|
16
|
|
|
class RequestToAuthPluginTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var RequestToHttpAuthPlugin |
|
20
|
|
|
*/ |
|
21
|
|
|
private $requestToPlugin; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ObjectProphecy |
|
24
|
|
|
*/ |
|
25
|
|
|
private $pluginManager; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->pluginManager = $this->prophesize(AuthenticationPluginManagerInterface::class); |
|
30
|
|
|
$this->requestToPlugin = new RequestToHttpAuthPlugin($this->pluginManager->reveal()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @test |
|
35
|
|
|
*/ |
|
36
|
|
|
public function exceptionIsFoundWhenNoneOfTheSupportedMethodsIsFound() |
|
37
|
|
|
{ |
|
38
|
|
|
$request = ServerRequestFactory::fromGlobals(); |
|
39
|
|
|
|
|
40
|
|
|
$this->expectException(NoAuthenticationException::class); |
|
41
|
|
|
$this->expectExceptionMessage(sprintf( |
|
42
|
|
|
'None of the valid authentication mechanisms where provided. Expected one of ["%s"]', |
|
43
|
|
|
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS) |
|
44
|
|
|
)); |
|
45
|
|
|
|
|
46
|
|
|
$this->requestToPlugin->fromRequest($request); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @test |
|
51
|
|
|
* @dataProvider provideHeaders |
|
52
|
|
|
*/ |
|
53
|
|
|
public function properPluginIsFetchedWhenAnyAuthTypeIsFound(array $headers, string $expectedHeader) |
|
54
|
|
|
{ |
|
55
|
|
|
$request = ServerRequestFactory::fromGlobals(); |
|
56
|
|
|
foreach ($headers as $header => $value) { |
|
57
|
|
|
$request = $request->withHeader($header, $value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$plugin = $this->prophesize(AuthenticationPluginInterface::class); |
|
61
|
|
|
$getPlugin = $this->pluginManager->get($expectedHeader)->willReturn($plugin->reveal()); |
|
62
|
|
|
|
|
63
|
|
|
$this->requestToPlugin->fromRequest($request); |
|
64
|
|
|
|
|
65
|
|
|
$getPlugin->shouldHaveBeenCalledTimes(1); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function provideHeaders(): array |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
71
|
|
|
'API key header only' => [[ |
|
72
|
|
|
ApiKeyHeaderPlugin::HEADER_NAME => 'foobar', |
|
73
|
|
|
], ApiKeyHeaderPlugin::HEADER_NAME], |
|
74
|
|
|
'Authorization header only' => [[ |
|
75
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME => 'foobar', |
|
76
|
|
|
], AuthorizationHeaderPlugin::HEADER_NAME], |
|
77
|
|
|
'Both headers' => [[ |
|
78
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME => 'foobar', |
|
79
|
|
|
ApiKeyHeaderPlugin::HEADER_NAME => 'foobar', |
|
80
|
|
|
], ApiKeyHeaderPlugin::HEADER_NAME], |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|