-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliLoginHelperTest.php
More file actions
258 lines (194 loc) · 8.99 KB
/
Copy pathCliLoginHelperTest.php
File metadata and controls
258 lines (194 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace ItkDev\OpenIdConnectBundle\Tests\Util;
use ItkDev\OpenIdConnectBundle\Exception\CacheException;
use ItkDev\OpenIdConnectBundle\Exception\OpenIdConnectBundleExceptionInterface;
use ItkDev\OpenIdConnectBundle\Exception\TokenNotFoundException;
use ItkDev\OpenIdConnectBundle\Util\CliLoginHelper;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Uid\Uuid;
class CliLoginHelperTest extends TestCase
{
public function testEncodeAndDecode(): void
{
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
$randomUsername = Uuid::v4()->toBase32();
$encodedUsername = $cliHelper->encodeKey($randomUsername);
$decodedUsername = $cliHelper->decodeKey($encodedUsername);
$this->assertEquals($randomUsername, $decodedUsername);
}
public function testDecodeKeyReturnsInputWhenNotValidBase64(): void
{
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
// Strict base64_decode() rejects input with characters outside the
// base64 alphabet; decodeKey() then returns the value unchanged.
$notBase64 = 'not valid base64 !!@@';
$this->assertSame($notBase64, $cliHelper->decodeKey($notBase64));
}
public function testThrowExceptionIfTokenDoesNotExist(): void
{
// TokenNotFoundException (not just the marker interface) is part of
// the public contract: CliLoginTokenAuthenticator catches it
// specifically to distinguish "no such token" from cache failures.
$this->expectException(TokenNotFoundException::class);
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
$cliHelper->getUsername('random_gibberish_token');
}
public function testReuseSetTokenRatherThanRemake(): void
{
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
$testUser = 'test_user';
$token = $cliHelper->createToken($testUser);
$token2 = $cliHelper->createToken($testUser);
$this->assertEquals($token, $token2);
}
public function testTokenIsRemovedAfterUse(): void
{
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
$testUser = 'test_user';
$token = $cliHelper->createToken($testUser);
$username = $cliHelper->getUsername($token);
$this->assertEquals($testUser, $username);
$this->expectException(OpenIdConnectBundleExceptionInterface::class);
$cliHelper->getUsername($token);
}
public function testBothCacheEntriesAreRemovedAfterUse(): void
{
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
$testUser = 'test_user';
$token = $cliHelper->createToken($testUser);
$this->assertEquals($testUser, $cliHelper->getUsername($token));
// The reverse entry (username => token) must be gone too; otherwise
// createToken() would hand out the already-redeemed token again.
$this->assertFalse($cache->hasItem($cliHelper->encodeKey($testUser)));
$newToken = $cliHelper->createToken($testUser);
$this->assertNotSame($token, $newToken);
$this->assertEquals($testUser, $cliHelper->getUsername($newToken));
}
public function testEncodeKeyPrependsNamespace(): void
{
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
// Assert the exact encoding, not just an encode/decode roundtrip:
// the namespace prefix guards against cache key collisions with the
// consuming application, and a roundtrip is blind to losing it.
$this->assertSame(base64_encode('itk-dev-cli-logintest_user'), $cliHelper->encodeKey('test_user'));
}
public function testCreateTokenAndGetUsername(): void
{
$cache = new ArrayAdapter();
$cliHelper = new CliLoginHelper($cache);
$testUser = 'test_user';
$token = $cliHelper->createToken($testUser);
$username = $cliHelper->getUsername($token);
$this->assertEquals($testUser, $username);
}
public function testCreateTokenThrowsCacheExceptionOnGetItem(): void
{
$cause = new TestInvalidArgumentException('Cache error');
$stubCache = $this->createStub(CacheItemPoolInterface::class);
$stubCache->method('getItem')->willThrowException($cause);
$cliHelper = new CliLoginHelper($stubCache);
try {
$cliHelper->createToken('test_user');
} catch (CacheException $thrown) {
$this->assertSame('Cache error', $thrown->getMessage());
$this->assertSame($cause, $thrown->getPrevious(), 'Original cause must be chained');
return;
}
$this->fail('Expected CacheException');
}
public function testCreateTokenThrowsCacheExceptionOnSecondGetItem(): void
{
$stubCacheItem = $this->createStub(CacheItemInterface::class);
$stubCacheItem->method('isHit')->willReturn(false);
$stubCacheItem->method('get')->willReturn(null);
$cause = new TestInvalidArgumentException('Second cache error');
$stubCache = $this->createStub(CacheItemPoolInterface::class);
$callCount = 0;
$stubCache->method('getItem')
->willReturnCallback(function () use ($stubCacheItem, $cause, &$callCount) {
++$callCount;
if (1 === $callCount) {
return $stubCacheItem;
}
throw $cause;
});
$stubCache->method('save')->willReturn(true);
$cliHelper = new CliLoginHelper($stubCache);
try {
$cliHelper->createToken('another_user');
} catch (CacheException $thrown) {
$this->assertSame('Second cache error', $thrown->getMessage());
$this->assertSame($cause, $thrown->getPrevious(), 'Original cause must be chained');
return;
}
$this->fail('Expected CacheException');
}
public function testGetUsernameThrowsCacheExceptionOnGetItem(): void
{
$cause = new TestInvalidArgumentException('Cache error');
$stubCache = $this->createStub(CacheItemPoolInterface::class);
$stubCache->method('getItem')->willThrowException($cause);
$cliHelper = new CliLoginHelper($stubCache);
try {
$cliHelper->getUsername('some-token');
} catch (CacheException $thrown) {
$this->assertSame('Cache error', $thrown->getMessage());
$this->assertSame($cause, $thrown->getPrevious(), 'Original cause must be chained');
return;
}
$this->fail('Expected CacheException');
}
public function testCreateTokenThrowsCacheExceptionOnNonStringCachedToken(): void
{
$stubCacheItem = $this->createStub(CacheItemInterface::class);
$stubCacheItem->method('isHit')->willReturn(true);
$stubCacheItem->method('get')->willReturn(42);
$stubCache = $this->createStub(CacheItemPoolInterface::class);
$stubCache->method('getItem')->willReturn($stubCacheItem);
$cliHelper = new CliLoginHelper($stubCache);
$this->expectException(CacheException::class);
$this->expectExceptionMessage('Cached token is not a string');
$cliHelper->createToken('test_user');
}
public function testGetUsernameThrowsCacheExceptionOnNonStringCachedUsername(): void
{
$stubCacheItem = $this->createStub(CacheItemInterface::class);
$stubCacheItem->method('isHit')->willReturn(true);
$stubCacheItem->method('get')->willReturn(42);
$stubCache = $this->createStub(CacheItemPoolInterface::class);
$stubCache->method('getItem')->willReturn($stubCacheItem);
$cliHelper = new CliLoginHelper($stubCache);
$this->expectException(CacheException::class);
$this->expectExceptionMessage('Cached username is not a string');
$cliHelper->getUsername('some-token');
}
public function testGetUsernameThrowsCacheExceptionOnDeleteItem(): void
{
$stubCacheItem = $this->createStub(CacheItemInterface::class);
$stubCacheItem->method('isHit')->willReturn(true);
$stubCacheItem->method('get')->willReturn('encoded_username');
$cause = new TestInvalidArgumentException('Delete error');
$stubCache = $this->createStub(CacheItemPoolInterface::class);
$stubCache->method('getItem')->willReturn($stubCacheItem);
$stubCache->method('deleteItem')->willThrowException($cause);
$cliHelper = new CliLoginHelper($stubCache);
try {
$cliHelper->getUsername('some-token');
} catch (CacheException $thrown) {
$this->assertSame('Delete error', $thrown->getMessage());
$this->assertSame($cause, $thrown->getPrevious(), 'Original cause must be chained');
return;
}
$this->fail('Expected CacheException');
}
}