|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin; |
|
8
|
|
|
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin; |
|
9
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
10
|
|
|
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase; |
|
11
|
|
|
use function implode; |
|
12
|
|
|
use function Shlinkio\Shlink\Common\json_decode; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
|
|
15
|
|
|
class AuthenticationTest extends ApiTestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @test |
|
19
|
|
|
*/ |
|
20
|
|
|
public function authorizationErrorIsReturnedIfNoApiKeyIsSent() |
|
21
|
|
|
{ |
|
22
|
|
|
try { |
|
23
|
|
|
$this->callApi(self::METHOD_GET, '/short-codes'); |
|
24
|
|
|
} catch (ClientException $e) { |
|
25
|
|
|
['error' => $error, 'message' => $message] = json_decode((string) $e->getResponse()->getBody()); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertEquals(self::STATUS_UNAUTHORIZED, $e->getCode()); |
|
28
|
|
|
$this->assertEquals(RestUtils::INVALID_AUTHORIZATION_ERROR, $error); |
|
29
|
|
|
$this->assertEquals( |
|
30
|
|
|
sprintf( |
|
31
|
|
|
'Expected one of the following authentication headers, but none were provided, ["%s"]', |
|
32
|
|
|
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS) |
|
33
|
|
|
), |
|
34
|
|
|
$message |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
*/ |
|
42
|
|
|
public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid() |
|
43
|
|
|
{ |
|
44
|
|
|
try { |
|
45
|
|
|
$this->callApi(self::METHOD_GET, '/short-codes', [ |
|
46
|
|
|
'headers' => [ |
|
47
|
|
|
ApiKeyHeaderPlugin::HEADER_NAME => 'invalid', |
|
48
|
|
|
], |
|
49
|
|
|
]); |
|
50
|
|
|
} catch (ClientException $e) { |
|
51
|
|
|
['error' => $error, 'message' => $message] = json_decode((string) $e->getResponse()->getBody()); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEquals(self::STATUS_UNAUTHORIZED, $e->getCode()); |
|
54
|
|
|
$this->assertEquals(RestUtils::INVALID_API_KEY_ERROR, $error); |
|
55
|
|
|
$this->assertEquals('Provided API key does not exist or is invalid.', $message); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|