diff options
Diffstat (limited to 'tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php')
-rw-r--r-- | tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php index 6097c1f482d..7e7f949965f 100644 --- a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php +++ b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php @@ -23,6 +23,7 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\Security\ICrypto; use OCP\Security\IHasher; +use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -53,8 +54,8 @@ class PublicKeyTokenProviderTest extends TestCase { parent::setUp(); $this->mapper = $this->createMock(PublicKeyTokenMapper::class); - $this->hasher = \OC::$server->get(IHasher::class); - $this->crypto = \OC::$server->getCrypto(); + $this->hasher = Server::get(IHasher::class); + $this->crypto = Server::get(ICrypto::class); $this->config = $this->createMock(IConfig::class); $this->config->method('getSystemValue') ->willReturnMap([ @@ -232,7 +233,7 @@ class PublicKeyTokenProviderTest extends TestCase { public function testGetPasswordPasswordLessToken(): void { - $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class); + $this->expectException(PasswordlessTokenException::class); $token = 'token1234'; $tk = new PublicKeyToken(); @@ -243,7 +244,7 @@ class PublicKeyTokenProviderTest extends TestCase { public function testGetPasswordInvalidToken(): void { - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); + $this->expectException(InvalidTokenException::class); $token = 'tokentokentokentokentoken'; $uid = 'user'; @@ -294,7 +295,7 @@ class PublicKeyTokenProviderTest extends TestCase { public function testSetPasswordInvalidToken(): void { - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); + $this->expectException(InvalidTokenException::class); $token = $this->createMock(IToken::class); $tokenId = 'token123'; @@ -304,12 +305,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): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->tokenProvider->invalidateToken('token7'); } @@ -336,14 +342,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): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->tokenProvider->invalidateOldTokens(); } @@ -375,12 +386,12 @@ class PublicKeyTokenProviderTest extends TestCase { ->expects($this->once()) ->method('insert') ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) { - return $token->getUID() === $uid && - $token->getLoginName() === $user && - $token->getName() === $name && - $token->getType() === IToken::DO_NOT_REMEMBER && - $token->getLastActivity() === $this->time && - $token->getPassword() === null; + return $token->getUID() === $uid + && $token->getLoginName() === $user + && $token->getName() === $name + && $token->getType() === IToken::DO_NOT_REMEMBER + && $token->getLastActivity() === $this->time + && $token->getPassword() === null; })); $this->mapper ->expects($this->once()) @@ -415,13 +426,13 @@ class PublicKeyTokenProviderTest extends TestCase { ->expects($this->once()) ->method('insert') ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name): bool { - return $token->getUID() === $uid && - $token->getLoginName() === $user && - $token->getName() === $name && - $token->getType() === IToken::DO_NOT_REMEMBER && - $token->getLastActivity() === $this->time && - $token->getPassword() !== null && - $this->tokenProvider->getPassword($token, 'newIdtokentokentokentoken') === 'password'; + return $token->getUID() === $uid + && $token->getLoginName() === $user + && $token->getName() === $name + && $token->getType() === IToken::DO_NOT_REMEMBER + && $token->getLastActivity() === $this->time + && $token->getPassword() !== null + && $this->tokenProvider->getPassword($token, 'newIdtokentokentokentoken') === 'password'; })); $this->mapper ->expects($this->once()) @@ -453,16 +464,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): void { + $expected = array_shift($calls); + $this->assertEquals(hash('sha512', $expected), $token); + throw new DoesNotExistException('nope'); + }); $this->tokenProvider->getToken('unhashedTokentokentokentokentoken'); } |