Passed
Pull Request — master (#554)
by Alejandro
05:37
created

AuthenticationTest::provideInvalidAuthorizations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 17
rs 9.8333
c 1
b 0
f 0
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
        $resp = $this->callApi(self::METHOD_GET, '/short-codes');
20
        ['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($resp);
21
22
        $this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
23
        $this->assertEquals('INVALID_AUTHORIZATION', $error);
24
        $this->assertEquals(
25
            sprintf(
26
                'Expected one of the following authentication headers, but none were provided, ["%s"]',
27
                implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
28
            ),
29
            $message
30
        );
31
    }
32
33
    /**
34
     * @test
35
     * @dataProvider provideInvalidApiKeys
36
     */
37
    public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid(string $apiKey): void
38
    {
39
        $resp = $this->callApi(self::METHOD_GET, '/short-codes', [
40
            'headers' => [
41
                Plugin\ApiKeyHeaderPlugin::HEADER_NAME => $apiKey,
42
            ],
43
        ]);
44
        ['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($resp);
45
46
        $this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
47
        $this->assertEquals('INVALID_API_KEY', $error);
48
        $this->assertEquals('Provided API key does not exist or is invalid.', $message);
49
    }
50
51
    public function provideInvalidApiKeys(): iterable
52
    {
53
        yield 'key which does not exist' => ['invalid'];
54
        yield 'key which is expired' => ['expired_api_key'];
55
        yield 'key which is disabled' => ['disabled_api_key'];
56
    }
57
58
    /**
59
     * @test
60
     * @dataProvider provideInvalidAuthorizations
61
     */
62
    public function authorizationErrorIsReturnedIfInvalidDataIsProvided(
63
        string $authValue,
64
        string $expectedMessage,
65
        string $expectedError
66
    ): void {
67
        $resp = $this->callApi(self::METHOD_GET, '/short-codes', [
68
            'headers' => [
69
                Plugin\AuthorizationHeaderPlugin::HEADER_NAME => $authValue,
70
            ],
71
        ]);
72
        ['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($resp);
73
74
        $this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
75
        $this->assertEquals($expectedError, $error);
76
        $this->assertEquals($expectedMessage, $message);
77
    }
78
79
    public function provideInvalidAuthorizations(): iterable
80
    {
81
        yield 'no type' => [
82
            'invalid',
83
            'You need to provide the Bearer type in the Authorization header.',
84
            'INVALID_AUTHORIZATION',
85
        ];
86
        yield 'invalid type' => [
87
            'Basic invalid',
88
            'Provided authorization type Basic is not supported. Use Bearer instead.',
89
            'INVALID_AUTHORIZATION',
90
        ];
91
        yield 'invalid JWT' => [
92
            'Bearer invalid',
93
            'Missing or invalid auth token provided. Perform a new authentication request and send provided '
94
            . 'token on every new request on the Authorization header',
95
            'INVALID_AUTH_TOKEN',
96
        ];
97
    }
98
}
99