1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Middleware; |
6
|
|
|
|
7
|
|
|
use Shlinkio\Shlink\Rest\Authentication\Plugin; |
8
|
|
|
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin; |
9
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase; |
10
|
|
|
|
11
|
|
|
use function implode; |
12
|
|
|
use function sprintf; |
13
|
|
|
|
14
|
|
|
class AuthenticationTest extends ApiTestCase |
15
|
|
|
{ |
16
|
|
|
/** @test */ |
17
|
|
|
public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void |
18
|
|
|
{ |
19
|
|
|
$expectedDetail = sprintf( |
20
|
|
|
'Expected one of the following authentication headers, ["%s"], but none were provided', |
21
|
|
|
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS) |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
$resp = $this->callApi(self::METHOD_GET, '/short-urls'); |
25
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
26
|
|
|
|
27
|
|
|
$this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode()); |
28
|
|
|
$this->assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']); |
29
|
|
|
$this->assertEquals('INVALID_AUTHORIZATION', $payload['type']); |
30
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']); |
31
|
|
|
$this->assertEquals('Invalid authorization', $payload['title']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
* @dataProvider provideInvalidApiKeys |
37
|
|
|
*/ |
38
|
|
|
public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid(string $apiKey): void |
39
|
|
|
{ |
40
|
|
|
$expectedDetail = 'Provided API key does not exist or is invalid.'; |
41
|
|
|
|
42
|
|
|
$resp = $this->callApi(self::METHOD_GET, '/short-urls', [ |
43
|
|
|
'headers' => [ |
44
|
|
|
Plugin\ApiKeyHeaderPlugin::HEADER_NAME => $apiKey, |
45
|
|
|
], |
46
|
|
|
]); |
47
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
48
|
|
|
|
49
|
|
|
$this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode()); |
50
|
|
|
$this->assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']); |
51
|
|
|
$this->assertEquals('INVALID_API_KEY', $payload['type']); |
52
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']); |
53
|
|
|
$this->assertEquals('Invalid API key', $payload['title']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function provideInvalidApiKeys(): iterable |
57
|
|
|
{ |
58
|
|
|
yield 'key which does not exist' => ['invalid']; |
59
|
|
|
yield 'key which is expired' => ['expired_api_key']; |
60
|
|
|
yield 'key which is disabled' => ['disabled_api_key']; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|