1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Common\Mercure; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use Lcobucci\JWT\Configuration; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Shlinkio\Shlink\Common\Mercure\LcobucciJwtProvider; |
11
|
|
|
|
12
|
|
|
class LcobucciJwtProviderTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private Configuration $jwtConfig; |
15
|
|
|
|
16
|
|
|
public function setUp(): void |
17
|
|
|
{ |
18
|
|
|
$this->jwtConfig = Configuration::forUnsecuredSigner(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @test |
23
|
|
|
* @dataProvider provideMercureConfigs |
24
|
|
|
*/ |
25
|
|
|
public function expectedPublishTokenIsCreated(array $mercureConfig, string $expectedIssuer): void |
26
|
|
|
{ |
27
|
|
|
$token = $this->jwtConfig->getParser()->parse( |
28
|
|
|
(new LcobucciJwtProvider($this->jwtConfig, $mercureConfig))(), |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$this->assertTrue($token->hasBeenIssuedBy($expectedIssuer)); |
32
|
|
|
$this->assertTrue($token->isExpired(Chronos::now()->addMinutes(10)->addSeconds(5))); |
33
|
|
|
$this->assertEquals(['publish' => []], $token->claims()->get('mercure')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function provideMercureConfigs(): iterable |
37
|
|
|
{ |
38
|
|
|
yield 'without issuer' => [[], 'Shlink']; |
39
|
|
|
yield 'with issuer' => [['jwt_issuer' => $issuer = 'foobar'], $issuer]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
* @dataProvider provideExpirationDates |
45
|
|
|
*/ |
46
|
|
|
public function expectedSubscriptionTokenIsCreated(?Chronos $expiresAt, Chronos $expectedExpiresAt): void |
47
|
|
|
{ |
48
|
|
|
$token = $this->jwtConfig->getParser()->parse( |
49
|
|
|
(new LcobucciJwtProvider($this->jwtConfig, []))->buildSubscriptionToken($expiresAt), |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->assertTrue($token->isExpired($expectedExpiresAt->addSeconds(5))); |
53
|
|
|
$this->assertEquals(['subscribe' => []], $token->claims()->get('mercure')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function provideExpirationDates(): iterable |
57
|
|
|
{ |
58
|
|
|
yield 'default expiration' => [null, Chronos::now()->addDays(3)]; |
59
|
|
|
yield 'explicit expiration' => [$expires = Chronos::now()->addMonths(5), $expires]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|