|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Rest\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use Doctrine\ORM\EntityManager; |
|
9
|
|
|
use Doctrine\ORM\EntityRepository; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Prophecy\Argument; |
|
12
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
13
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; |
|
14
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey; |
|
15
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService; |
|
16
|
|
|
|
|
17
|
|
|
class ApiKeyServiceTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ApiKeyService */ |
|
20
|
|
|
private $service; |
|
21
|
|
|
/** @var ObjectProphecy */ |
|
22
|
|
|
private $em; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->em = $this->prophesize(EntityManager::class); |
|
27
|
|
|
$this->service = new ApiKeyService($this->em->reveal()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @test |
|
32
|
|
|
* @dataProvider provideCreationDate |
|
33
|
|
|
*/ |
|
34
|
|
|
public function apiKeyIsProperlyCreated(?Chronos $date): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->em->flush()->shouldBeCalledOnce(); |
|
37
|
|
|
$this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledOnce(); |
|
38
|
|
|
|
|
39
|
|
|
$key = $this->service->create($date); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEquals($date, $key->getExpirationDate()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function provideCreationDate(): iterable |
|
45
|
|
|
{ |
|
46
|
|
|
yield 'no expiration date' => [null]; |
|
47
|
|
|
yield 'expiration date' => [Chronos::parse('2030-01-01')]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @test |
|
52
|
|
|
* @dataProvider provideInvalidApiKeys |
|
53
|
|
|
*/ |
|
54
|
|
|
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void |
|
55
|
|
|
{ |
|
56
|
|
|
$repo = $this->prophesize(EntityRepository::class); |
|
57
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn($invalidKey) |
|
58
|
|
|
->shouldBeCalledOnce(); |
|
59
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertFalse($this->service->check('12345')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function provideInvalidApiKeys(): iterable |
|
65
|
|
|
{ |
|
66
|
|
|
yield 'non-existent api key' => [null]; |
|
67
|
|
|
yield 'disabled api key' => [(new ApiKey())->disable()]; |
|
68
|
|
|
yield 'expired api key' => [new ApiKey(Chronos::now()->subDay())]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @test */ |
|
72
|
|
|
public function checkReturnsTrueWhenConditionsAreFavorable(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$repo = $this->prophesize(EntityRepository::class); |
|
75
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn(new ApiKey()) |
|
76
|
|
|
->shouldBeCalledOnce(); |
|
77
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertTrue($this->service->check('12345')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @test */ |
|
83
|
|
|
public function disableThrowsExceptionWhenNoApiKeyIsFound(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$repo = $this->prophesize(EntityRepository::class); |
|
86
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn(null) |
|
87
|
|
|
->shouldBeCalledOnce(); |
|
88
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); |
|
89
|
|
|
|
|
90
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
91
|
|
|
|
|
92
|
|
|
$this->service->disable('12345'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** @test */ |
|
96
|
|
|
public function disableReturnsDisabledApiKeyWhenFound(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$key = new ApiKey(); |
|
99
|
|
|
$repo = $this->prophesize(EntityRepository::class); |
|
100
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn($key) |
|
101
|
|
|
->shouldBeCalledOnce(); |
|
102
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); |
|
103
|
|
|
|
|
104
|
|
|
$this->em->flush()->shouldBeCalledOnce(); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertTrue($key->isEnabled()); |
|
107
|
|
|
$returnedKey = $this->service->disable('12345'); |
|
108
|
|
|
$this->assertFalse($key->isEnabled()); |
|
109
|
|
|
$this->assertSame($key, $returnedKey); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** @test */ |
|
113
|
|
|
public function listFindsAllApiKeys(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()]; |
|
116
|
|
|
|
|
117
|
|
|
$repo = $this->prophesize(EntityRepository::class); |
|
118
|
|
|
$repo->findBy([])->willReturn($expectedApiKeys) |
|
119
|
|
|
->shouldBeCalledOnce(); |
|
120
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); |
|
121
|
|
|
|
|
122
|
|
|
$result = $this->service->listKeys(); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals($expectedApiKeys, $result); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** @test */ |
|
128
|
|
|
public function listEnabledFindsOnlyEnabledApiKeys(): void |
|
129
|
|
|
{ |
|
130
|
|
|
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()]; |
|
131
|
|
|
|
|
132
|
|
|
$repo = $this->prophesize(EntityRepository::class); |
|
133
|
|
|
$repo->findBy(['enabled' => true])->willReturn($expectedApiKeys) |
|
134
|
|
|
->shouldBeCalledOnce(); |
|
135
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); |
|
136
|
|
|
|
|
137
|
|
|
$result = $this->service->listKeys(true); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertEquals($expectedApiKeys, $result); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|