Passed
Pull Request — master (#339)
by Alejandro
05:38
created

AuthenticationTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid() 0 14 2
A authorizationErrorIsReturnedIfNoApiKeyIsSent() 0 15 2
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