2021-01-10 22:24:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\CLI\ApiKey;
|
|
|
|
|
2022-10-12 13:47:58 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-01-10 22:24:13 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\CLI\ApiKey\RoleResolver;
|
2022-02-19 21:23:36 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException;
|
2021-01-10 22:24:13 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2021-01-10 22:24:13 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
2022-10-12 13:47:58 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
2021-01-10 22:24:13 +03:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
2022-10-12 13:47:58 +03:00
|
|
|
use function Functional\map;
|
|
|
|
|
2021-01-10 22:24:13 +03:00
|
|
|
class RoleResolverTest extends TestCase
|
|
|
|
{
|
|
|
|
private RoleResolver $resolver;
|
2022-10-12 13:47:58 +03:00
|
|
|
private MockObject $domainService;
|
2021-01-10 22:24:13 +03:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-12 13:47:58 +03:00
|
|
|
$this->domainService = $this->createMock(DomainServiceInterface::class);
|
|
|
|
$this->resolver = new RoleResolver($this->domainService, 'default.com');
|
2021-01-10 22:24:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideRoles
|
|
|
|
*/
|
|
|
|
public function properRolesAreResolvedBasedOnInput(
|
|
|
|
InputInterface $input,
|
|
|
|
array $expectedRoles,
|
2021-05-23 13:31:10 +03:00
|
|
|
int $expectedDomainCalls,
|
2021-01-10 22:24:13 +03:00
|
|
|
): void {
|
2022-10-12 13:47:58 +03:00
|
|
|
$this->domainService
|
|
|
|
->expects($this->exactly($expectedDomainCalls))
|
|
|
|
->method('getOrCreate')
|
|
|
|
->with($this->equalTo('example.com'))
|
|
|
|
->willReturn(Domain::withAuthority('example.com')->setId('1'));
|
2021-01-10 22:24:13 +03:00
|
|
|
|
|
|
|
$result = $this->resolver->determineRoles($input);
|
|
|
|
|
|
|
|
self::assertEquals($expectedRoles, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRoles(): iterable
|
|
|
|
{
|
2021-07-22 21:48:58 +03:00
|
|
|
$domain = Domain::withAuthority('example.com')->setId('1');
|
2021-01-10 22:24:13 +03:00
|
|
|
$buildInput = function (array $definition): InputInterface {
|
2022-10-12 13:47:58 +03:00
|
|
|
$input = $this->createStub(InputInterface::class);
|
|
|
|
$input->method('getOption')->willReturnMap(
|
|
|
|
map($definition, static fn (mixed $returnValue, string $param) => [$param, $returnValue]),
|
|
|
|
);
|
2021-01-10 22:24:13 +03:00
|
|
|
|
2022-10-12 13:47:58 +03:00
|
|
|
return $input;
|
2021-01-10 22:24:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
yield 'no roles' => [
|
2022-10-12 13:47:58 +03:00
|
|
|
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => false]),
|
2021-01-10 22:24:13 +03:00
|
|
|
[],
|
|
|
|
0,
|
|
|
|
];
|
|
|
|
yield 'domain role only' => [
|
2022-10-12 13:47:58 +03:00
|
|
|
$buildInput(
|
|
|
|
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => false],
|
|
|
|
),
|
2021-01-11 18:32:59 +03:00
|
|
|
[RoleDefinition::forDomain($domain)],
|
2021-01-10 22:24:13 +03:00
|
|
|
1,
|
|
|
|
];
|
2021-07-17 21:58:24 +03:00
|
|
|
yield 'false domain role' => [
|
2022-10-12 13:47:58 +03:00
|
|
|
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => false]),
|
2021-07-17 21:58:24 +03:00
|
|
|
[],
|
|
|
|
0,
|
|
|
|
];
|
|
|
|
yield 'true domain role' => [
|
2022-10-12 13:47:58 +03:00
|
|
|
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => true]),
|
2021-07-17 21:58:24 +03:00
|
|
|
[],
|
|
|
|
0,
|
|
|
|
];
|
|
|
|
yield 'string array domain role' => [
|
2022-10-12 13:47:58 +03:00
|
|
|
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => ['foo', 'bar']]),
|
2021-07-17 21:58:24 +03:00
|
|
|
[],
|
|
|
|
0,
|
|
|
|
];
|
2021-01-10 22:24:13 +03:00
|
|
|
yield 'author role only' => [
|
2022-10-12 13:47:58 +03:00
|
|
|
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => true]),
|
2021-01-10 22:24:13 +03:00
|
|
|
[RoleDefinition::forAuthoredShortUrls()],
|
|
|
|
0,
|
|
|
|
];
|
|
|
|
yield 'both roles' => [
|
2022-10-12 13:47:58 +03:00
|
|
|
$buildInput(
|
|
|
|
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => true],
|
|
|
|
),
|
2021-01-11 18:32:59 +03:00
|
|
|
[RoleDefinition::forAuthoredShortUrls(), RoleDefinition::forDomain($domain)],
|
2021-01-10 22:24:13 +03:00
|
|
|
1,
|
|
|
|
];
|
|
|
|
}
|
2022-02-19 21:23:36 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function exceptionIsThrownWhenTryingToAddDomainOnlyLinkedToDefaultDomain(): void
|
|
|
|
{
|
2022-10-12 13:47:58 +03:00
|
|
|
$input = $this->createStub(InputInterface::class);
|
|
|
|
$input
|
|
|
|
->method('getOption')
|
|
|
|
->willReturnMap([
|
|
|
|
[Role::DOMAIN_SPECIFIC->paramName(), 'default.com'],
|
|
|
|
[Role::AUTHORED_SHORT_URLS->paramName(), null],
|
|
|
|
]);
|
2022-02-19 21:23:36 +03:00
|
|
|
|
|
|
|
$this->expectException(InvalidRoleConfigException::class);
|
|
|
|
|
2022-10-12 13:47:58 +03:00
|
|
|
$this->resolver->determineRoles($input);
|
2022-02-19 21:23:36 +03:00
|
|
|
}
|
2021-01-10 22:24:13 +03:00
|
|
|
}
|