diff options
Diffstat (limited to 'tests/lib/Authentication/Token')
-rw-r--r-- | tests/lib/Authentication/Token/ManagerTest.php | 41 | ||||
-rw-r--r-- | tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php | 47 |
2 files changed, 60 insertions, 28 deletions
diff --git a/tests/lib/Authentication/Token/ManagerTest.php b/tests/lib/Authentication/Token/ManagerTest.php index 4fde9817d09..0f95d1d2f2c 100644 --- a/tests/lib/Authentication/Token/ManagerTest.php +++ b/tests/lib/Authentication/Token/ManagerTest.php @@ -128,10 +128,10 @@ class ManagerTest extends TestCase { $this->assertSame(121, mb_strlen($actual->getName())); } - public function tokenData(): array { + public static function tokenData(): array { return [ [new PublicKeyToken()], - [$this->createMock(IToken::class)], + [IToken::class], ]; } @@ -160,7 +160,11 @@ class ManagerTest extends TestCase { /** * @dataProvider tokenData */ - public function testUpdateToken(IToken $token): void { + public function testUpdateToken(IToken|string $token): void { + if (is_string($token)) { + $token = $this->createMock($token); + } + $this->setNoCall($token); $this->setCall($token, 'updateToken'); $this->setException($token); @@ -171,7 +175,11 @@ class ManagerTest extends TestCase { /** * @dataProvider tokenData */ - public function testUpdateTokenActivity(IToken $token): void { + public function testUpdateTokenActivity(IToken|string $token): void { + if (is_string($token)) { + $token = $this->createMock($token); + } + $this->setNoCall($token); $this->setCall($token, 'updateTokenActivity'); $this->setException($token); @@ -182,7 +190,11 @@ class ManagerTest extends TestCase { /** * @dataProvider tokenData */ - public function testGetPassword(IToken $token): void { + public function testGetPassword(IToken|string $token): void { + if (is_string($token)) { + $token = $this->createMock($token); + } + $this->setNoCall($token); $this->setCall($token, 'getPassword', 'password'); $this->setException($token); @@ -195,7 +207,11 @@ class ManagerTest extends TestCase { /** * @dataProvider tokenData */ - public function testSetPassword(IToken $token): void { + public function testSetPassword(IToken|string $token): void { + if (is_string($token)) { + $token = $this->createMock($token); + } + $this->setNoCall($token); $this->setCall($token, 'setPassword'); $this->setException($token); @@ -358,13 +374,18 @@ class ManagerTest extends TestCase { ->method('getTokenByUser') ->with('theUser') ->willReturn([$t1, $t2]); + + $calls = [ + ['theUser', 123], + ['theUser', 456], + ]; $this->publicKeyTokenProvider ->expects($this->exactly(2)) ->method('invalidateTokenById') - ->withConsecutive( - ['theUser', 123], - ['theUser', 456], - ); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->manager->invalidateTokensOfUser('theUser', null); } diff --git a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php index 6097c1f482d..dc6ec7c7f3e 100644 --- a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php +++ b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php @@ -304,12 +304,17 @@ class PublicKeyTokenProviderTest extends TestCase { } public function testInvalidateToken(): void { + $calls = [ + [hash('sha512', 'token7' . '1f4h9s')], + [hash('sha512', 'token7')] + ]; + $this->mapper->expects($this->exactly(2)) ->method('invalidate') - ->withConsecutive( - [hash('sha512', 'token7' . '1f4h9s')], - [hash('sha512', 'token7')] - ); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->tokenProvider->invalidateToken('token7'); } @@ -336,14 +341,19 @@ class PublicKeyTokenProviderTest extends TestCase { ['token_auth_wipe_token_retention', $wipeTokenLifetime, 500], ['token_auth_token_retention', 60 * 60 * 24 * 365, 800], ]); + + $calls = [ + [$this->time - 150, IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER], + [$this->time - 300, IToken::TEMPORARY_TOKEN, IToken::REMEMBER], + [$this->time - 500, IToken::WIPE_TOKEN, null], + [$this->time - 800, IToken::PERMANENT_TOKEN, null], + ]; $this->mapper->expects($this->exactly(4)) ->method('invalidateOld') - ->withConsecutive( - [$this->time - 150, IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER], - [$this->time - 300, IToken::TEMPORARY_TOKEN, IToken::REMEMBER], - [$this->time - 500, IToken::WIPE_TOKEN, null], - [$this->time - 800, IToken::PERMANENT_TOKEN, null], - ); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->tokenProvider->invalidateOldTokens(); } @@ -453,16 +463,17 @@ class PublicKeyTokenProviderTest extends TestCase { public function testGetInvalidToken(): void { $this->expectException(InvalidTokenException::class); + $calls = [ + 'unhashedTokentokentokentokentoken' . '1f4h9s', + 'unhashedTokentokentokentokentoken', + ]; $this->mapper->expects($this->exactly(2)) ->method('getToken') - ->withConsecutive( - [$this->callback(function (string $token): bool { - return hash('sha512', 'unhashedTokentokentokentokentoken' . '1f4h9s') === $token; - })], - [$this->callback(function (string $token): bool { - return hash('sha512', 'unhashedTokentokentokentokentoken') === $token; - })] - )->willThrowException(new DoesNotExistException('nope')); + ->willReturnCallback(function (string $token) use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals(hash('sha512', $expected), $token); + throw new DoesNotExistException('nope'); + }); $this->tokenProvider->getToken('unhashedTokentokentokentokentoken'); } |