2020-04-12 15:19:51 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-12 19:01:13 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action;
|
2020-04-12 15:19:51 +03:00
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2020-04-12 15:19:51 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use RuntimeException;
|
|
|
|
use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Action\MercureInfoAction;
|
|
|
|
use Shlinkio\Shlink\Rest\Exception\MercureException;
|
|
|
|
|
|
|
|
class MercureInfoActionTest extends TestCase
|
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2020-04-12 15:19:51 +03:00
|
|
|
private ObjectProphecy $provider;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->provider = $this->prophesize(JwtProviderInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideNoHostConfigs
|
|
|
|
*/
|
|
|
|
public function throwsExceptionWhenConfigDoesNotHavePublicHost(array $mercureConfig): void
|
|
|
|
{
|
|
|
|
$buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willReturn('abc.123');
|
|
|
|
|
|
|
|
$action = new MercureInfoAction($this->provider->reveal(), $mercureConfig);
|
|
|
|
|
|
|
|
$this->expectException(MercureException::class);
|
|
|
|
$buildToken->shouldNotBeCalled();
|
|
|
|
|
|
|
|
$action->handle(ServerRequestFactory::fromGlobals());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideNoHostConfigs(): iterable
|
|
|
|
{
|
|
|
|
yield 'host not defined' => [[]];
|
|
|
|
yield 'host is null' => [['public_hub_url' => null]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideValidConfigs
|
|
|
|
*/
|
|
|
|
public function throwsExceptionWhenBuildingTokenFails(array $mercureConfig): void
|
|
|
|
{
|
|
|
|
$buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willThrow(
|
|
|
|
new RuntimeException('Error'),
|
|
|
|
);
|
|
|
|
|
|
|
|
$action = new MercureInfoAction($this->provider->reveal(), $mercureConfig);
|
|
|
|
|
|
|
|
$this->expectException(MercureException::class);
|
|
|
|
$buildToken->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$action->handle(ServerRequestFactory::fromGlobals());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideValidConfigs(): iterable
|
|
|
|
{
|
|
|
|
yield 'days not defined' => [['public_hub_url' => 'http://foobar.com']];
|
|
|
|
yield 'days defined' => [['public_hub_url' => 'http://foobar.com', 'jwt_days_duration' => 20]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDays
|
|
|
|
*/
|
|
|
|
public function returnsExpectedInfoWhenEverythingIsOk(?int $days): void
|
|
|
|
{
|
|
|
|
$buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willReturn('abc.123');
|
|
|
|
|
|
|
|
$action = new MercureInfoAction($this->provider->reveal(), [
|
|
|
|
'public_hub_url' => 'http://foobar.com',
|
|
|
|
'jwt_days_duration' => $days,
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
$resp = $action->handle(ServerRequestFactory::fromGlobals());
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertArrayHasKey('mercureHubUrl', $payload);
|
|
|
|
self::assertEquals('http://foobar.com/.well-known/mercure', $payload['mercureHubUrl']);
|
|
|
|
self::assertArrayHasKey('token', $payload);
|
|
|
|
self::assertArrayHasKey('jwtExpiration', $payload);
|
|
|
|
self::assertEquals(
|
2020-04-18 12:29:49 +03:00
|
|
|
Chronos::now()->addDays($days ?? 1)->startOfDay(),
|
2020-04-12 15:19:51 +03:00
|
|
|
Chronos::parse($payload['jwtExpiration'])->startOfDay(),
|
|
|
|
);
|
|
|
|
$buildToken->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideDays(): iterable
|
|
|
|
{
|
|
|
|
yield 'days not defined' => [null];
|
|
|
|
yield 'days defined' => [10];
|
|
|
|
}
|
|
|
|
}
|