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

58 lines
2.0 KiB
PHP
Raw Normal View History

2019-01-26 03:19:20 -06:00
<?php
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Middleware;
2019-01-27 03:54:04 -06:00
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin;
use Shlinkio\Shlink\Rest\Util\RestUtils;
2019-01-26 03:19:20 -06:00
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase;
2019-01-27 03:54:04 -06:00
use function implode;
use function sprintf;
2019-01-26 03:19:20 -06:00
class AuthenticationTest extends ApiTestCase
{
2019-02-17 13:28:34 -06:00
/** @test */
public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void
2019-01-26 03:19:20 -06:00
{
$resp = $this->callApi(self::METHOD_GET, '/short-codes');
['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($resp);
2019-01-26 03:19:20 -06:00
$this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
$this->assertEquals(RestUtils::INVALID_AUTHORIZATION_ERROR, $error);
$this->assertEquals(
sprintf(
'Expected one of the following authentication headers, but none were provided, ["%s"]',
implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
),
$message
);
2019-01-27 03:54:04 -06:00
}
/**
* @test
2019-01-27 05:14:18 -06:00
* @dataProvider provideInvalidApiKeys
2019-01-27 03:54:04 -06:00
*/
2019-02-17 13:28:34 -06:00
public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid(string $apiKey): void
2019-01-27 03:54:04 -06:00
{
$resp = $this->callApi(self::METHOD_GET, '/short-codes', [
'headers' => [
ApiKeyHeaderPlugin::HEADER_NAME => $apiKey,
],
]);
['error' => $error, 'message' => $message] = $this->getJsonResponsePayload($resp);
2019-01-27 03:54:04 -06:00
$this->assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
$this->assertEquals(RestUtils::INVALID_API_KEY_ERROR, $error);
$this->assertEquals('Provided API key does not exist or is invalid.', $message);
2019-01-26 03:19:20 -06:00
}
2019-01-27 05:14:18 -06:00
2019-02-17 13:28:34 -06:00
public function provideInvalidApiKeys(): iterable
2019-01-27 05:14:18 -06:00
{
2019-02-17 13:28:34 -06:00
yield 'key which does not exist' => ['invalid'];
yield 'key which is expired' => ['expired_api_key'];
yield 'key which is disabled' => ['disabled_api_key'];
2019-01-27 05:14:18 -06:00
}
2019-01-26 03:19:20 -06:00
}