shlink/module/Rest/test-api/Middleware/AuthenticationTest.php

67 lines
2.8 KiB
PHP
Raw Normal View History

2019-01-26 12:19:20 +03:00
<?php
2019-10-05 18:26:10 +03:00
2019-01-26 12:19:20 +03:00
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Middleware;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
2019-08-11 17:30:46 +03:00
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
use function sprintf;
2019-01-26 12:19:20 +03:00
class AuthenticationTest extends ApiTestCase
{
#[Test, DataProvider('provideApiVersions')]
public function authorizationErrorIsReturnedIfNoApiKeyIsSent(string $version, string $expectedType): void
2019-01-26 12:19:20 +03:00
{
$expectedDetail = 'Expected one of the following authentication headers, ["X-Api-Key"], but none were provided';
$resp = $this->callApi(self::METHOD_GET, sprintf('/rest/v%s/short-urls', $version));
$payload = $this->getJsonResponsePayload($resp);
2019-01-26 12:19:20 +03:00
2020-10-04 01:35:14 +03:00
self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
self::assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']);
self::assertEquals($expectedType, $payload['type']);
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedDetail, $payload['detail']);
self::assertEquals('Invalid authorization', $payload['title']);
2019-01-27 12:54:04 +03:00
}
2023-02-09 11:32:38 +03:00
public static function provideApiVersions(): iterable
{
2024-02-12 22:29:40 +03:00
yield 'version 1' => ['1', 'https://shlink.io/api/error/missing-authentication'];
yield 'version 2' => ['2', 'https://shlink.io/api/error/missing-authentication'];
yield 'version 3' => ['3', 'https://shlink.io/api/error/missing-authentication'];
}
#[Test, DataProvider('provideInvalidApiKeys')]
public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid(
string $apiKey,
string $version,
string $expectedType,
): void {
$expectedDetail = 'Provided API key does not exist or is invalid.';
$resp = $this->callApi(self::METHOD_GET, sprintf('/rest/v%s/short-urls', $version), [
'headers' => [
'X-Api-Key' => $apiKey,
],
]);
$payload = $this->getJsonResponsePayload($resp);
2019-01-27 12:54:04 +03:00
2020-10-04 01:35:14 +03:00
self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
self::assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']);
self::assertEquals($expectedType, $payload['type']);
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedDetail, $payload['detail']);
self::assertEquals('Invalid API key', $payload['title']);
2019-01-26 12:19:20 +03:00
}
2019-01-27 14:14:18 +03:00
2023-02-09 11:32:38 +03:00
public static function provideInvalidApiKeys(): iterable
2019-01-27 14:14:18 +03:00
{
2024-02-12 22:29:40 +03:00
yield 'key which does not exist' => ['invalid', '2', 'https://shlink.io/api/error/invalid-api-key'];
yield 'key which is expired' => ['expired_api_key', '2', 'https://shlink.io/api/error/invalid-api-key'];
yield 'key which is disabled' => ['disabled_api_key', '2', 'https://shlink.io/api/error/invalid-api-key'];
yield 'version 3' => ['disabled_api_key', '3', 'https://shlink.io/api/error/invalid-api-key'];
2019-01-27 14:14:18 +03:00
}
2019-01-26 12:19:20 +03:00
}