aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Authentication/Token
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Authentication/Token')
-rw-r--r--tests/lib/Authentication/Token/ManagerTest.php57
-rw-r--r--tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php11
-rw-r--r--tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php84
3 files changed, 89 insertions, 63 deletions
diff --git a/tests/lib/Authentication/Token/ManagerTest.php b/tests/lib/Authentication/Token/ManagerTest.php
index 4fde9817d09..58bbe236248 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],
];
}
@@ -157,10 +157,12 @@ class ManagerTest extends TestCase {
}
}
- /**
- * @dataProvider tokenData
- */
- public function testUpdateToken(IToken $token): void {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ 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);
@@ -168,10 +170,12 @@ class ManagerTest extends TestCase {
$this->manager->updateToken($token);
}
- /**
- * @dataProvider tokenData
- */
- public function testUpdateTokenActivity(IToken $token): void {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ 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);
@@ -179,10 +183,12 @@ class ManagerTest extends TestCase {
$this->manager->updateTokenActivity($token);
}
- /**
- * @dataProvider tokenData
- */
- public function testGetPassword(IToken $token): void {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ 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);
@@ -192,10 +198,12 @@ class ManagerTest extends TestCase {
$this->assertSame('password', $result);
}
- /**
- * @dataProvider tokenData
- */
- public function testSetPassword(IToken $token): void {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ 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 +366,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): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
$this->manager->invalidateTokensOfUser('theUser', null);
}
diff --git a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php
index 7cc4e77ecb2..d1585dadc26 100644
--- a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php
+++ b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php
@@ -8,13 +8,14 @@ declare(strict_types=1);
namespace Test\Authentication\Token;
-use OC;
use OC\Authentication\Token\PublicKeyToken;
use OC\Authentication\Token\PublicKeyTokenMapper;
+use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Authentication\Token\IToken;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;
+use OCP\Server;
use Test\TestCase;
/**
@@ -33,7 +34,7 @@ class PublicKeyTokenMapperTest extends TestCase {
protected function setUp(): void {
parent::setUp();
- $this->dbConnection = OC::$server->getDatabaseConnection();
+ $this->dbConnection = Server::get(IDBConnection::class);
$this->time = time();
$this->resetDatabase();
@@ -178,7 +179,7 @@ class PublicKeyTokenMapperTest extends TestCase {
public function testGetInvalidToken(): void {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
+ $this->expectException(DoesNotExistException::class);
$token = 'thisisaninvalidtokenthatisnotinthedatabase';
@@ -210,14 +211,14 @@ class PublicKeyTokenMapperTest extends TestCase {
public function testGetTokenByIdNotFound(): void {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
+ $this->expectException(DoesNotExistException::class);
$this->mapper->getTokenById(-1);
}
public function testGetInvalidTokenById(): void {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
+ $this->expectException(DoesNotExistException::class);
$id = '42';
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');
}