2021-01-09 05:38:06 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\ApiKey;
|
|
|
|
|
|
|
|
use Happyr\DoctrineSpecification\Spec;
|
|
|
|
use Happyr\DoctrineSpecification\Specification\Specification;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2021-01-09 05:38:06 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToApiKey;
|
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToApiKeyInlined;
|
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToDomain;
|
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToDomainInlined;
|
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKeyRole;
|
|
|
|
|
|
|
|
class RoleTest extends TestCase
|
|
|
|
{
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideRoles')]
|
2022-01-18 13:06:32 -06:00
|
|
|
public function returnsExpectedSpec(ApiKeyRole $apiKeyRole, Specification $expected): void
|
2021-01-09 05:38:06 -06:00
|
|
|
{
|
2022-01-18 13:06:32 -06:00
|
|
|
self::assertEquals($expected, Role::toSpec($apiKeyRole));
|
2021-01-09 05:38:06 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideRoles(): iterable
|
2021-01-09 05:38:06 -06:00
|
|
|
{
|
2021-03-14 03:59:35 -05:00
|
|
|
$apiKey = ApiKey::create();
|
2021-01-09 05:38:06 -06:00
|
|
|
|
2022-01-18 13:06:32 -06:00
|
|
|
yield 'author role' => [
|
2021-01-09 05:38:06 -06:00
|
|
|
new ApiKeyRole(Role::AUTHORED_SHORT_URLS, [], $apiKey),
|
2022-01-18 13:06:32 -06:00
|
|
|
new BelongsToApiKey($apiKey),
|
|
|
|
];
|
|
|
|
yield 'domain role' => [
|
|
|
|
new ApiKeyRole(Role::DOMAIN_SPECIFIC, ['domain_id' => '456'], $apiKey),
|
|
|
|
new BelongsToDomain('456'),
|
2021-01-09 05:38:06 -06:00
|
|
|
];
|
2022-01-18 13:06:32 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideInlinedRoles')]
|
2022-01-18 13:06:32 -06:00
|
|
|
public function returnsExpectedInlinedSpec(ApiKeyRole $apiKeyRole, Specification $expected): void
|
|
|
|
{
|
|
|
|
self::assertEquals($expected, Role::toInlinedSpec($apiKeyRole));
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideInlinedRoles(): iterable
|
2022-01-18 13:06:32 -06:00
|
|
|
{
|
|
|
|
$apiKey = ApiKey::create();
|
|
|
|
|
|
|
|
yield 'author role' => [
|
2021-01-09 05:38:06 -06:00
|
|
|
new ApiKeyRole(Role::AUTHORED_SHORT_URLS, [], $apiKey),
|
2022-01-18 13:06:32 -06:00
|
|
|
Spec::andX(new BelongsToApiKeyInlined($apiKey)),
|
2021-01-09 05:38:06 -06:00
|
|
|
];
|
2022-01-18 13:06:32 -06:00
|
|
|
yield 'domain role' => [
|
2021-01-09 05:38:06 -06:00
|
|
|
new ApiKeyRole(Role::DOMAIN_SPECIFIC, ['domain_id' => '123'], $apiKey),
|
2021-02-26 13:27:41 -06:00
|
|
|
Spec::andX(new BelongsToDomainInlined('123')),
|
2021-01-09 05:38:06 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideMetasWithDomainId')]
|
2021-01-09 05:38:06 -06:00
|
|
|
public function getsExpectedDomainIdFromMeta(array $meta, string $expectedDomainId): void
|
|
|
|
{
|
|
|
|
self::assertEquals($expectedDomainId, Role::domainIdFromMeta($meta));
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideMetasWithDomainId(): iterable
|
2021-01-09 05:38:06 -06:00
|
|
|
{
|
|
|
|
yield 'empty meta' => [[], '-1'];
|
|
|
|
yield 'meta without domain_id' => [['foo' => 'bar'], '-1'];
|
|
|
|
yield 'meta with domain_id' => [['domain_id' => '123'], '123'];
|
|
|
|
}
|
2021-01-11 10:01:01 -06:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideMetasWithAuthority')]
|
2021-01-11 10:01:01 -06:00
|
|
|
public function getsExpectedAuthorityFromMeta(array $meta, string $expectedAuthority): void
|
|
|
|
{
|
|
|
|
self::assertEquals($expectedAuthority, Role::domainAuthorityFromMeta($meta));
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideMetasWithAuthority(): iterable
|
2021-01-11 10:01:01 -06:00
|
|
|
{
|
|
|
|
yield 'empty meta' => [[], ''];
|
|
|
|
yield 'meta without authority' => [['foo' => 'bar'], ''];
|
|
|
|
yield 'meta with authority' => [['authority' => 'example.com'], 'example.com'];
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideRoleNames')]
|
2023-05-30 02:12:46 -05:00
|
|
|
public function getsExpectedRoleFriendlyName(Role $role, array $meta, string $expectedFriendlyName): void
|
2021-01-11 10:01:01 -06:00
|
|
|
{
|
2023-05-30 02:12:46 -05:00
|
|
|
self::assertEquals($expectedFriendlyName, $role->toFriendlyName($meta));
|
2021-01-11 10:01:01 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideRoleNames(): iterable
|
2021-01-11 10:01:01 -06:00
|
|
|
{
|
2023-05-30 02:12:46 -05:00
|
|
|
yield Role::AUTHORED_SHORT_URLS->value => [Role::AUTHORED_SHORT_URLS, [], 'Author only'];
|
|
|
|
yield Role::DOMAIN_SPECIFIC->value => [Role::DOMAIN_SPECIFIC, ['authority' => 's.test'], 'Domain only: s.test'];
|
|
|
|
yield Role::NO_ORPHAN_VISITS->value => [Role::NO_ORPHAN_VISITS, [], 'No orphan visits'];
|
2021-01-11 10:01:01 -06:00
|
|
|
}
|
2021-01-09 05:38:06 -06:00
|
|
|
}
|