mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Created MercureInfoActionTest
This commit is contained in:
parent
2ffbf03cf8
commit
31db97228d
4 changed files with 111 additions and 4 deletions
|
@ -21,7 +21,7 @@ return [
|
|||
ApiKeyService::class => ConfigAbstractFactory::class,
|
||||
|
||||
Action\HealthAction::class => ConfigAbstractFactory::class,
|
||||
Action\MercureAction::class => ConfigAbstractFactory::class,
|
||||
Action\MercureInfoAction::class => ConfigAbstractFactory::class,
|
||||
Action\ShortUrl\CreateShortUrlAction::class => ConfigAbstractFactory::class,
|
||||
Action\ShortUrl\SingleStepCreateShortUrlAction::class => ConfigAbstractFactory::class,
|
||||
Action\ShortUrl\EditShortUrlAction::class => ConfigAbstractFactory::class,
|
||||
|
@ -48,7 +48,7 @@ return [
|
|||
ApiKeyService::class => ['em'],
|
||||
|
||||
Action\HealthAction::class => [Connection::class, AppOptions::class, 'Logger_Shlink'],
|
||||
Action\MercureAction::class => [LcobucciJwtProvider::class, 'config.mercure', 'Logger_Shlink'],
|
||||
Action\MercureInfoAction::class => [LcobucciJwtProvider::class, 'config.mercure', 'Logger_Shlink'],
|
||||
Action\ShortUrl\CreateShortUrlAction::class => [
|
||||
Service\UrlShortener::class,
|
||||
'config.url_shortener.domain',
|
||||
|
|
|
@ -34,7 +34,7 @@ return [
|
|||
Action\Tag\CreateTagsAction::getRouteDef(),
|
||||
Action\Tag\UpdateTagAction::getRouteDef(),
|
||||
|
||||
Action\MercureAction::getRouteDef(),
|
||||
Action\MercureInfoAction::getRouteDef(),
|
||||
],
|
||||
|
||||
];
|
||||
|
|
|
@ -13,7 +13,7 @@ use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
|
|||
use Shlinkio\Shlink\Rest\Exception\MercureException;
|
||||
use Throwable;
|
||||
|
||||
class MercureAction extends AbstractRestAction
|
||||
class MercureInfoAction extends AbstractRestAction
|
||||
{
|
||||
protected const ROUTE_PATH = '/mercure-info';
|
||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
107
module/Rest/test/Action/MercureInfoActionTest.php
Normal file
107
module/Rest/test/Action/MercureInfoActionTest.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Argument\ArgumentsWildcard;
|
||||
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
|
||||
{
|
||||
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();
|
||||
|
||||
$this->assertArrayHasKey('mercureHubUrl', $payload);
|
||||
$this->assertEquals('http://foobar.com', $payload['mercureHubUrl']);
|
||||
$this->assertArrayHasKey('token', $payload);
|
||||
$this->assertArrayHasKey('jwtExpiration', $payload);
|
||||
$this->assertEquals(
|
||||
Chronos::now()->addDays($days ?? 3)->startOfDay(),
|
||||
Chronos::parse($payload['jwtExpiration'])->startOfDay(),
|
||||
);
|
||||
$buildToken->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideDays(): iterable
|
||||
{
|
||||
yield 'days not defined' => [null];
|
||||
yield 'days defined' => [10];
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue