|
4 | 4 |
|
5 | 5 | use ItkDev\OpenIdConnectBundle\Exception\CacheException; |
6 | 6 | use ItkDev\OpenIdConnectBundle\Exception\OpenIdConnectBundleExceptionInterface; |
| 7 | +use ItkDev\OpenIdConnectBundle\Exception\TokenNotFoundException; |
7 | 8 | use ItkDev\OpenIdConnectBundle\Util\CliLoginHelper; |
8 | 9 | use PHPUnit\Framework\TestCase; |
9 | 10 | use Psr\Cache\CacheItemInterface; |
@@ -40,7 +41,10 @@ public function testDecodeKeyReturnsInputWhenNotValidBase64(): void |
40 | 41 |
|
41 | 42 | public function testThrowExceptionIfTokenDoesNotExist(): void |
42 | 43 | { |
43 | | - $this->expectException(OpenIdConnectBundleExceptionInterface::class); |
| 44 | + // TokenNotFoundException (not just the marker interface) is part of |
| 45 | + // the public contract: CliLoginTokenAuthenticator catches it |
| 46 | + // specifically to distinguish "no such token" from cache failures. |
| 47 | + $this->expectException(TokenNotFoundException::class); |
44 | 48 |
|
45 | 49 | $cache = new ArrayAdapter(); |
46 | 50 |
|
@@ -79,6 +83,37 @@ public function testTokenIsRemovedAfterUse(): void |
79 | 83 | $cliHelper->getUsername($token); |
80 | 84 | } |
81 | 85 |
|
| 86 | + public function testBothCacheEntriesAreRemovedAfterUse(): void |
| 87 | + { |
| 88 | + $cache = new ArrayAdapter(); |
| 89 | + |
| 90 | + $cliHelper = new CliLoginHelper($cache); |
| 91 | + |
| 92 | + $testUser = 'test_user'; |
| 93 | + $token = $cliHelper->createToken($testUser); |
| 94 | + |
| 95 | + $this->assertEquals($testUser, $cliHelper->getUsername($token)); |
| 96 | + |
| 97 | + // The reverse entry (username => token) must be gone too; otherwise |
| 98 | + // createToken() would hand out the already-redeemed token again. |
| 99 | + $this->assertFalse($cache->hasItem($cliHelper->encodeKey($testUser))); |
| 100 | + |
| 101 | + $newToken = $cliHelper->createToken($testUser); |
| 102 | + $this->assertNotSame($token, $newToken); |
| 103 | + $this->assertEquals($testUser, $cliHelper->getUsername($newToken)); |
| 104 | + } |
| 105 | + |
| 106 | + public function testEncodeKeyPrependsNamespace(): void |
| 107 | + { |
| 108 | + $cache = new ArrayAdapter(); |
| 109 | + $cliHelper = new CliLoginHelper($cache); |
| 110 | + |
| 111 | + // Assert the exact encoding, not just an encode/decode roundtrip: |
| 112 | + // the namespace prefix guards against cache key collisions with the |
| 113 | + // consuming application, and a roundtrip is blind to losing it. |
| 114 | + $this->assertSame(base64_encode('itk-dev-cli-logintest_user'), $cliHelper->encodeKey('test_user')); |
| 115 | + } |
| 116 | + |
82 | 117 | public function testCreateTokenAndGetUsername(): void |
83 | 118 | { |
84 | 119 | $cache = new ArrayAdapter(); |
|
0 commit comments