2020-09-27 03:11:41 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Domain;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-10-21 12:01:41 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-09-27 03:11:41 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-12-09 05:32:02 -06:00
|
|
|
use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig;
|
2021-07-22 10:49:37 -05:00
|
|
|
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
2020-09-27 03:11:41 -05:00
|
|
|
use Shlinkio\Shlink\Core\Domain\DomainService;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2021-01-04 08:02:37 -06:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
2020-09-27 03:11:41 -05:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface;
|
2021-01-04 13:15:42 -06:00
|
|
|
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
2021-03-14 03:59:35 -05:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
2021-01-09 05:38:06 -06:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2020-09-27 03:11:41 -05:00
|
|
|
|
|
|
|
class DomainServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
private DomainService $domainService;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & EntityManagerInterface $em;
|
2020-09-27 03:11:41 -05:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2020-09-27 03:11:41 -05:00
|
|
|
{
|
2022-10-21 12:01:41 -05:00
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$this->domainService = new DomainService($this->em, 'default.com');
|
2020-09-27 03:11:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideExcludedDomains
|
|
|
|
*/
|
2021-01-09 05:38:06 -06:00
|
|
|
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ?ApiKey $apiKey): void
|
2020-09-27 03:11:41 -05:00
|
|
|
{
|
2022-10-21 12:01:41 -05:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 11:15:57 -05:00
|
|
|
$repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains);
|
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
2020-09-27 03:11:41 -05:00
|
|
|
|
2021-01-09 05:38:06 -06:00
|
|
|
$result = $this->domainService->listDomains($apiKey);
|
2020-09-27 03:11:41 -05:00
|
|
|
|
|
|
|
self::assertEquals($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExcludedDomains(): iterable
|
|
|
|
{
|
2021-12-09 05:32:02 -06:00
|
|
|
$default = DomainItem::forDefaultDomain('default.com', new EmptyNotFoundRedirectConfig());
|
2021-03-14 03:59:35 -05:00
|
|
|
$adminApiKey = ApiKey::create();
|
2022-10-24 13:11:25 -05:00
|
|
|
$domain = Domain::withAuthority('');
|
|
|
|
$domain->setId('123');
|
2021-03-14 03:59:35 -05:00
|
|
|
$domainSpecificApiKey = ApiKey::fromMeta(
|
2022-10-24 13:11:25 -05:00
|
|
|
ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)),
|
2021-03-14 03:59:35 -05:00
|
|
|
);
|
2021-01-09 05:38:06 -06:00
|
|
|
|
|
|
|
yield 'empty list without API key' => [[], [$default], null];
|
|
|
|
yield 'one item without API key' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
[Domain::withAuthority('bar.com')],
|
2021-12-09 05:32:02 -06:00
|
|
|
[$default, DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com'))],
|
2021-01-09 05:38:06 -06:00
|
|
|
null,
|
|
|
|
];
|
|
|
|
yield 'multiple items without API key' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
2021-07-21 14:09:33 -05:00
|
|
|
[
|
|
|
|
$default,
|
2021-12-09 05:32:02 -06:00
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('foo.com')),
|
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com')),
|
2021-07-21 14:09:33 -05:00
|
|
|
],
|
2021-01-09 05:38:06 -06:00
|
|
|
null,
|
|
|
|
];
|
2021-01-04 08:02:37 -06:00
|
|
|
|
2021-01-09 05:38:06 -06:00
|
|
|
yield 'empty list with admin API key' => [[], [$default], $adminApiKey];
|
|
|
|
yield 'one item with admin API key' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
[Domain::withAuthority('bar.com')],
|
2021-12-09 05:32:02 -06:00
|
|
|
[$default, DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com'))],
|
2021-01-09 05:38:06 -06:00
|
|
|
$adminApiKey,
|
|
|
|
];
|
|
|
|
yield 'multiple items with admin API key' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
2021-07-21 14:09:33 -05:00
|
|
|
[
|
|
|
|
$default,
|
2021-12-09 05:32:02 -06:00
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('foo.com')),
|
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com')),
|
2021-07-21 14:09:33 -05:00
|
|
|
],
|
2021-01-09 05:38:06 -06:00
|
|
|
$adminApiKey,
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'empty list with domain-specific API key' => [[], [], $domainSpecificApiKey];
|
|
|
|
yield 'one item with domain-specific API key' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
[Domain::withAuthority('bar.com')],
|
2021-12-09 05:32:02 -06:00
|
|
|
[DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com'))],
|
2021-01-09 05:38:06 -06:00
|
|
|
$domainSpecificApiKey,
|
|
|
|
];
|
|
|
|
yield 'multiple items with domain-specific API key' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
2021-07-21 14:09:33 -05:00
|
|
|
[
|
2021-12-09 05:32:02 -06:00
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('foo.com')),
|
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com')),
|
2021-07-21 14:09:33 -05:00
|
|
|
],
|
2021-01-09 05:38:06 -06:00
|
|
|
$domainSpecificApiKey,
|
2021-01-04 08:02:37 -06:00
|
|
|
];
|
2020-09-27 03:11:41 -05:00
|
|
|
}
|
2021-01-04 13:15:42 -06:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function getDomainThrowsExceptionWhenDomainIsNotFound(): void
|
|
|
|
{
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Domain::class, '123')->willReturn(null);
|
2021-01-04 13:15:42 -06:00
|
|
|
|
|
|
|
$this->expectException(DomainNotFoundException::class);
|
|
|
|
|
|
|
|
$this->domainService->getDomain('123');
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function getDomainReturnsEntityWhenFound(): void
|
|
|
|
{
|
2021-07-22 13:48:58 -05:00
|
|
|
$domain = Domain::withAuthority('');
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Domain::class, '123')->willReturn($domain);
|
2021-01-04 13:15:42 -06:00
|
|
|
|
|
|
|
$result = $this->domainService->getDomain('123');
|
|
|
|
|
|
|
|
self::assertSame($domain, $result);
|
|
|
|
}
|
2021-01-10 13:05:14 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFoundDomains
|
|
|
|
*/
|
2021-07-29 12:08:29 -05:00
|
|
|
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
2021-01-10 13:05:14 -06:00
|
|
|
{
|
|
|
|
$authority = 'example.com';
|
2022-10-21 12:01:41 -05:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 11:15:57 -05:00
|
|
|
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
2022-10-21 12:01:41 -05:00
|
|
|
$foundDomain,
|
|
|
|
);
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
|
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
2022-10-21 12:01:41 -05:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
2021-01-10 13:05:14 -06:00
|
|
|
|
2021-07-29 12:08:29 -05:00
|
|
|
$result = $this->domainService->getOrCreate($authority, $apiKey);
|
2021-01-10 13:05:14 -06:00
|
|
|
|
|
|
|
if ($foundDomain !== null) {
|
|
|
|
self::assertSame($result, $foundDomain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 12:08:29 -05:00
|
|
|
/** @test */
|
|
|
|
public function getOrCreateThrowsExceptionForApiKeysWithDomainRole(): void
|
|
|
|
{
|
|
|
|
$authority = 'example.com';
|
2022-10-24 13:11:25 -05:00
|
|
|
$domain = Domain::withAuthority($authority);
|
|
|
|
$domain->setId('1');
|
2021-07-29 12:08:29 -05:00
|
|
|
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
2022-10-21 12:01:41 -05:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 11:15:57 -05:00
|
|
|
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null);
|
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
2022-10-21 12:01:41 -05:00
|
|
|
$this->em->expects($this->never())->method('persist');
|
|
|
|
$this->em->expects($this->never())->method('flush');
|
2021-07-29 12:08:29 -05:00
|
|
|
|
|
|
|
$this->expectException(DomainNotFoundException::class);
|
|
|
|
|
|
|
|
$this->domainService->getOrCreate($authority, $apiKey);
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:49:37 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFoundDomains
|
|
|
|
*/
|
2021-07-29 12:08:29 -05:00
|
|
|
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
2021-07-22 10:49:37 -05:00
|
|
|
{
|
|
|
|
$authority = 'example.com';
|
2022-10-21 12:01:41 -05:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 11:15:57 -05:00
|
|
|
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn($foundDomain);
|
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
|
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
2022-10-21 12:01:41 -05:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
2021-07-22 10:49:37 -05:00
|
|
|
|
2021-08-03 07:08:36 -05:00
|
|
|
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
|
2021-07-22 10:49:37 -05:00
|
|
|
'foo.com',
|
|
|
|
'bar.com',
|
|
|
|
'baz.com',
|
2021-07-29 12:08:29 -05:00
|
|
|
), $apiKey);
|
2021-07-22 10:49:37 -05:00
|
|
|
|
|
|
|
if ($foundDomain !== null) {
|
|
|
|
self::assertSame($result, $foundDomain);
|
|
|
|
}
|
|
|
|
self::assertEquals('foo.com', $result->baseUrlRedirect());
|
|
|
|
self::assertEquals('bar.com', $result->regular404Redirect());
|
|
|
|
self::assertEquals('baz.com', $result->invalidShortUrlRedirect());
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:05:14 -06:00
|
|
|
public function provideFoundDomains(): iterable
|
|
|
|
{
|
2021-07-29 12:08:29 -05:00
|
|
|
$domain = Domain::withAuthority('');
|
|
|
|
$adminApiKey = ApiKey::create();
|
|
|
|
$authorApiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()));
|
|
|
|
|
|
|
|
yield 'domain not found and no API key' => [null, null];
|
|
|
|
yield 'domain found and no API key' => [$domain, null];
|
|
|
|
yield 'domain not found and admin API key' => [null, $adminApiKey];
|
|
|
|
yield 'domain found and admin API key' => [$domain, $adminApiKey];
|
|
|
|
yield 'domain not found and author API key' => [null, $authorApiKey];
|
|
|
|
yield 'domain found and author API key' => [$domain, $authorApiKey];
|
2021-01-10 13:05:14 -06:00
|
|
|
}
|
2020-09-27 03:11:41 -05:00
|
|
|
}
|