2021-01-04 13:15:42 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
|
|
|
|
|
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use PHPUnit\Framework\Assert;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 16:00:49 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-01-04 13:15:42 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2022-09-23 11:30:07 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter;
|
2021-01-04 13:15:42 -06:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\OverrideDomainMiddleware;
|
|
|
|
|
|
|
|
class OverrideDomainMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
private OverrideDomainMiddleware $middleware;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & DomainServiceInterface $domainService;
|
|
|
|
private MockObject & ApiKey $apiKey;
|
|
|
|
private MockObject & RequestHandlerInterface $handler;
|
2021-01-04 13:15:42 -06:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-23 16:00:49 -05:00
|
|
|
$this->apiKey = $this->createMock(ApiKey::class);
|
|
|
|
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
2021-01-04 13:15:42 -06:00
|
|
|
|
2022-10-23 16:00:49 -05:00
|
|
|
$this->domainService = $this->createMock(DomainServiceInterface::class);
|
|
|
|
$this->middleware = new OverrideDomainMiddleware($this->domainService);
|
2021-01-04 13:15:42 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-01-04 13:15:42 -06:00
|
|
|
public function nextMiddlewareIsCalledWhenApiKeyDoesNotHaveProperRole(): void
|
|
|
|
{
|
|
|
|
$request = $this->requestWithApiKey();
|
|
|
|
$response = new Response();
|
2022-10-23 16:00:49 -05:00
|
|
|
$this->apiKey->expects($this->once())->method('hasRole')->with(Role::DOMAIN_SPECIFIC)->willReturn(false);
|
|
|
|
$this->handler->expects($this->once())->method('handle')->with($request)->willReturn($response);
|
|
|
|
$this->domainService->expects($this->never())->method('getDomain');
|
2021-01-04 13:15:42 -06:00
|
|
|
|
2022-10-23 16:00:49 -05:00
|
|
|
$result = $this->middleware->process($request, $this->handler);
|
2021-01-04 13:15:42 -06:00
|
|
|
|
|
|
|
self::assertSame($response, $result);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideBodies')]
|
2021-01-04 13:15:42 -06:00
|
|
|
public function overwritesRequestBodyWhenMethodIsPost(Domain $domain, array $body, array $expectedBody): void
|
|
|
|
{
|
|
|
|
$request = $this->requestWithApiKey()->withMethod('POST')->withParsedBody($body);
|
2022-10-23 16:00:49 -05:00
|
|
|
$this->apiKey->expects($this->once())->method('hasRole')->with(Role::DOMAIN_SPECIFIC)->willReturn(true);
|
|
|
|
$this->apiKey->expects($this->once())->method('getRoleMeta')->with(Role::DOMAIN_SPECIFIC)->willReturn(
|
|
|
|
['domain_id' => '123'],
|
|
|
|
);
|
|
|
|
$this->domainService->expects($this->once())->method('getDomain')->with('123')->willReturn($domain);
|
|
|
|
$this->handler->expects($this->once())->method('handle')->with($this->callback(
|
2021-01-04 13:15:42 -06:00
|
|
|
function (ServerRequestInterface $req) use ($expectedBody): bool {
|
|
|
|
Assert::assertEquals($req->getParsedBody(), $expectedBody);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
))->willReturn(new Response());
|
|
|
|
|
2022-10-23 16:00:49 -05:00
|
|
|
$this->middleware->process($request, $this->handler);
|
2021-01-04 13:15:42 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideBodies(): iterable
|
2021-01-04 13:15:42 -06:00
|
|
|
{
|
2021-07-22 13:48:58 -05:00
|
|
|
yield 'no domain provided' => [
|
|
|
|
Domain::withAuthority('foo.com'),
|
|
|
|
[],
|
|
|
|
[ShortUrlInputFilter::DOMAIN => 'foo.com'],
|
|
|
|
];
|
2021-01-04 13:15:42 -06:00
|
|
|
yield 'other domain provided' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
Domain::withAuthority('bar.com'),
|
2021-01-31 00:44:46 -06:00
|
|
|
[ShortUrlInputFilter::DOMAIN => 'foo.com'],
|
|
|
|
[ShortUrlInputFilter::DOMAIN => 'bar.com'],
|
2021-01-04 13:15:42 -06:00
|
|
|
];
|
|
|
|
yield 'same domain provided' => [
|
2021-07-22 13:48:58 -05:00
|
|
|
Domain::withAuthority('baz.com'),
|
2021-01-31 00:44:46 -06:00
|
|
|
[ShortUrlInputFilter::DOMAIN => 'baz.com'],
|
|
|
|
[ShortUrlInputFilter::DOMAIN => 'baz.com'],
|
2021-01-04 13:15:42 -06:00
|
|
|
];
|
|
|
|
yield 'more body params' => [
|
2023-01-19 02:05:52 -06:00
|
|
|
Domain::withAuthority('s.test'),
|
2021-01-31 00:44:46 -06:00
|
|
|
[ShortUrlInputFilter::DOMAIN => 'baz.com', 'something' => 'else', 'foo' => 123],
|
2023-01-19 02:05:52 -06:00
|
|
|
[ShortUrlInputFilter::DOMAIN => 's.test', 'something' => 'else', 'foo' => 123],
|
2021-01-04 13:15:42 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideMethods')]
|
2021-01-04 13:15:42 -06:00
|
|
|
public function setsRequestAttributeWhenMethodIsNotPost(string $method): void
|
|
|
|
{
|
2021-07-22 13:48:58 -05:00
|
|
|
$domain = Domain::withAuthority('something.com');
|
2021-01-04 13:15:42 -06:00
|
|
|
$request = $this->requestWithApiKey()->withMethod($method);
|
2022-10-23 16:00:49 -05:00
|
|
|
$this->apiKey->expects($this->once())->method('hasRole')->with(Role::DOMAIN_SPECIFIC)->willReturn(true);
|
|
|
|
$this->apiKey->expects($this->once())->method('getRoleMeta')->with(Role::DOMAIN_SPECIFIC)->willReturn(
|
|
|
|
['domain_id' => '123'],
|
|
|
|
);
|
|
|
|
$this->domainService->expects($this->once())->method('getDomain')->with('123')->willReturn($domain);
|
|
|
|
$this->handler->expects($this->once())->method('handle')->with($this->callback(
|
2021-01-04 13:15:42 -06:00
|
|
|
function (ServerRequestInterface $req): bool {
|
2021-01-31 00:44:46 -06:00
|
|
|
Assert::assertEquals($req->getAttribute(ShortUrlInputFilter::DOMAIN), 'something.com');
|
2021-01-04 13:15:42 -06:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
))->willReturn(new Response());
|
|
|
|
|
2022-10-23 16:00:49 -05:00
|
|
|
$this->middleware->process($request, $this->handler);
|
2021-01-04 13:15:42 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideMethods(): iterable
|
2021-01-04 13:15:42 -06:00
|
|
|
{
|
|
|
|
yield 'GET' => ['GET'];
|
|
|
|
yield 'PUT' => ['PUT'];
|
|
|
|
yield 'PATCH' => ['PATCH'];
|
|
|
|
yield 'DELETE' => ['DELETE'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function requestWithApiKey(): ServerRequestInterface
|
|
|
|
{
|
2022-10-23 16:00:49 -05:00
|
|
|
return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $this->apiKey);
|
2021-01-04 13:15:42 -06:00
|
|
|
}
|
|
|
|
}
|