|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Common\Mercure; |
|
6
|
|
|
|
|
7
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
|
10
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
11
|
|
|
use Psr\Container\ContainerInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Common\Exception\MercureException; |
|
13
|
|
|
use Shlinkio\Shlink\Common\Mercure\JwtConfigFactory; |
|
14
|
|
|
|
|
15
|
|
|
class JwtConfigFactoryTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
use ProphecyTrait; |
|
18
|
|
|
|
|
19
|
|
|
private JwtConfigFactory $factory; |
|
20
|
|
|
private ObjectProphecy $container; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->factory = new JwtConfigFactory(); |
|
25
|
|
|
$this->container = $this->prophesize(ContainerInterface::class); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @test |
|
30
|
|
|
* @dataProvider provideInvalidConfigs |
|
31
|
|
|
*/ |
|
32
|
|
|
public function throwsExceptionWhenProperConfigCouldNotBeFound(array $config): void |
|
33
|
|
|
{ |
|
34
|
|
|
$getConfig = $this->container->get('config')->willReturn($config); |
|
35
|
|
|
|
|
36
|
|
|
$this->expectException(MercureException::class); |
|
37
|
|
|
$this->expectExceptionMessage( |
|
38
|
|
|
'You have to provide a secret key for the JWT generation, under mercure.jwt_secret', |
|
39
|
|
|
); |
|
40
|
|
|
$getConfig->shouldBeCalledOnce(); |
|
41
|
|
|
|
|
42
|
|
|
($this->factory)($this->container->reveal()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function provideInvalidConfigs(): iterable |
|
46
|
|
|
{ |
|
47
|
|
|
yield 'empty config' => [[]]; |
|
48
|
|
|
yield 'empty mercure config' => [['mercure' => []]]; |
|
49
|
|
|
yield 'empty jwt secret' => [['mercure' => [ |
|
50
|
|
|
'jwt_secret' => null, |
|
51
|
|
|
]]]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @test */ |
|
55
|
|
|
public function returnsExpectedObjectWhenProperConfigIsFound(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$secret = 'the _super_secure_secret'; |
|
58
|
|
|
|
|
59
|
|
|
$getConfig = $this->container->get('config')->willReturn(['mercure' => [ |
|
60
|
|
|
'jwt_secret' => $secret, |
|
61
|
|
|
]]); |
|
62
|
|
|
|
|
63
|
|
|
$jwtConfig = ($this->factory)($this->container->reveal()); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertInstanceOf(Sha256::class, $jwtConfig->getSigner()); |
|
66
|
|
|
$this->assertEquals($secret, $jwtConfig->getSigningKey()->getContent()); |
|
67
|
|
|
$getConfig->shouldHaveBeenCalledOnce(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|