diff options
Diffstat (limited to 'tests/lib/Authentication')
11 files changed, 67 insertions, 70 deletions
diff --git a/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php b/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php index a7590cdd244..1861a5b2150 100644 --- a/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php +++ b/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php @@ -91,7 +91,7 @@ class UserDeletedTokenCleanupListenerTest extends TestCase { ]; $this->manager->expects($this->exactly(3)) ->method('invalidateTokenById') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); diff --git a/tests/lib/Authentication/Login/ALoginTestCommand.php b/tests/lib/Authentication/Login/ALoginTestCommand.php index 502380bc3fe..b955b20beba 100644 --- a/tests/lib/Authentication/Login/ALoginTestCommand.php +++ b/tests/lib/Authentication/Login/ALoginTestCommand.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/lib/Authentication/LoginCredentials/StoreTest.php b/tests/lib/Authentication/LoginCredentials/StoreTest.php index 072ec2ab571..aca586b91ec 100644 --- a/tests/lib/Authentication/LoginCredentials/StoreTest.php +++ b/tests/lib/Authentication/LoginCredentials/StoreTest.php @@ -111,7 +111,7 @@ class StoreTest extends TestCase { public function testGetLoginCredentialsSessionNotAvailable(): void { $this->session->expects($this->once()) ->method('getId') - ->will($this->throwException(new SessionNotAvailableException())); + ->willThrowException(new SessionNotAvailableException()); $this->expectException(CredentialsUnavailableException::class); $this->store->getLoginCredentials(); @@ -124,7 +124,7 @@ class StoreTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new InvalidTokenException())); + ->willThrowException(new InvalidTokenException()); $this->expectException(CredentialsUnavailableException::class); $this->store->getLoginCredentials(); @@ -141,7 +141,7 @@ class StoreTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new InvalidTokenException())); + ->willThrowException(new InvalidTokenException()); $this->session->expects($this->once()) ->method('exists') ->with($this->equalTo('login_credentials')) @@ -181,7 +181,7 @@ class StoreTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new InvalidTokenException())); + ->willThrowException(new InvalidTokenException()); $this->session->expects($this->once()) ->method('exists') ->with($this->equalTo('login_credentials')) @@ -222,7 +222,7 @@ class StoreTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new InvalidTokenException())); + ->willThrowException(new InvalidTokenException()); $this->session->expects($this->once()) ->method('exists') ->with($this->equalTo('login_credentials')) @@ -248,7 +248,7 @@ class StoreTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new PasswordlessTokenException())); + ->willThrowException(new PasswordlessTokenException()); $this->expectException(CredentialsUnavailableException::class); $this->store->getLoginCredentials(); @@ -276,7 +276,7 @@ class StoreTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new PasswordlessTokenException())); + ->willThrowException(new PasswordlessTokenException()); $this->session->expects($this->once()) ->method('exists') diff --git a/tests/lib/Authentication/Token/ManagerTest.php b/tests/lib/Authentication/Token/ManagerTest.php index 0f95d1d2f2c..58bbe236248 100644 --- a/tests/lib/Authentication/Token/ManagerTest.php +++ b/tests/lib/Authentication/Token/ManagerTest.php @@ -157,9 +157,7 @@ class ManagerTest extends TestCase { } } - /** - * @dataProvider tokenData - */ + #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')] public function testUpdateToken(IToken|string $token): void { if (is_string($token)) { $token = $this->createMock($token); @@ -172,9 +170,7 @@ class ManagerTest extends TestCase { $this->manager->updateToken($token); } - /** - * @dataProvider tokenData - */ + #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')] public function testUpdateTokenActivity(IToken|string $token): void { if (is_string($token)) { $token = $this->createMock($token); @@ -187,9 +183,7 @@ class ManagerTest extends TestCase { $this->manager->updateTokenActivity($token); } - /** - * @dataProvider tokenData - */ + #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')] public function testGetPassword(IToken|string $token): void { if (is_string($token)) { $token = $this->createMock($token); @@ -204,9 +198,7 @@ class ManagerTest extends TestCase { $this->assertSame('password', $result); } - /** - * @dataProvider tokenData - */ + #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')] public function testSetPassword(IToken|string $token): void { if (is_string($token)) { $token = $this->createMock($token); @@ -382,7 +374,7 @@ class ManagerTest extends TestCase { $this->publicKeyTokenProvider ->expects($this->exactly(2)) ->method('invalidateTokenById') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); 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 dc6ec7c7f3e..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'; @@ -311,7 +312,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->mapper->expects($this->exactly(2)) ->method('invalidate') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); @@ -350,7 +351,7 @@ class PublicKeyTokenProviderTest extends TestCase { ]; $this->mapper->expects($this->exactly(4)) ->method('invalidateOld') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); @@ -385,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()) @@ -425,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()) @@ -469,7 +470,7 @@ class PublicKeyTokenProviderTest extends TestCase { ]; $this->mapper->expects($this->exactly(2)) ->method('getToken') - ->willReturnCallback(function (string $token) use (&$calls) { + ->willReturnCallback(function (string $token) use (&$calls): void { $expected = array_shift($calls); $this->assertEquals(hash('sha512', $expected), $token); throw new DoesNotExistException('nope'); diff --git a/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php b/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php index 7a1ea64ca9a..b59ef876ffd 100644 --- a/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php @@ -9,9 +9,9 @@ declare(strict_types=1); namespace Test\Authentication\TwoFactorAuth\Db; -use OC; use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao; use OCP\IDBConnection; +use OCP\Server; use Test\TestCase; /** @@ -27,7 +27,7 @@ class ProviderUserAssignmentDaoTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->dbConn = OC::$server->getDatabaseConnection(); + $this->dbConn = Server::get(IDBConnection::class); $qb = $this->dbConn->getQueryBuilder(); $q = $qb->delete(ProviderUserAssignmentDao::TABLE_NAME); $q->execute(); diff --git a/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php b/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php index 5323a36867a..f1d38c10801 100644 --- a/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php index 75691627ce7..a2bed8a3652 100644 --- a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php @@ -8,8 +8,9 @@ namespace Test\Authentication\TwoFactorAuth; -use OC; +use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Token\IProvider as TokenProvider; +use OC\Authentication\Token\IToken; use OC\Authentication\TwoFactorAuth\Manager; use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor; use OC\Authentication\TwoFactorAuth\ProviderLoader; @@ -217,9 +218,8 @@ class ManagerTest extends TestCase { * enabled providers. * * If any of these providers is active, 2FA is enabled - * - * @dataProvider providerStatesFixData */ + #[\PHPUnit\Framework\Attributes\DataProvider('providerStatesFixData')] public function testIsTwoFactorAuthenticatedFixesProviderStates(bool $providerEnabled, bool $expected): void { $this->providerRegistry->expects($this->once()) ->method('getProviderStates') @@ -363,7 +363,7 @@ class ManagerTest extends TestCase { ]; $this->session->expects($this->exactly(2)) ->method('remove') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); @@ -404,7 +404,7 @@ class ManagerTest extends TestCase { 'provider' => 'Fake 2FA', ])) ->willReturnSelf(); - $token = $this->createMock(OC\Authentication\Token\IToken::class); + $token = $this->createMock(IToken::class); $this->tokenProvider->method('getToken') ->with('mysessionid') ->willReturn($token); @@ -496,7 +496,7 @@ class ManagerTest extends TestCase { $this->session->method('getId') ->willReturn('mysessionid'); - $token = $this->createMock(OC\Authentication\Token\IToken::class); + $token = $this->createMock(IToken::class); $this->tokenProvider->method('getToken') ->with('mysessionid') ->willReturn($token); @@ -567,14 +567,14 @@ class ManagerTest extends TestCase { ]; $this->session->expects($this->exactly(2)) ->method('set') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); $this->session->method('getId') ->willReturn('mysessionid'); - $token = $this->createMock(OC\Authentication\Token\IToken::class); + $token = $this->createMock(IToken::class); $this->tokenProvider->method('getToken') ->with('mysessionid') ->willReturn($token); @@ -601,14 +601,14 @@ class ManagerTest extends TestCase { ]; $this->session->expects($this->exactly(2)) ->method('set') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); $this->session->method('getId') ->willReturn('mysessionid'); - $token = $this->createMock(OC\Authentication\Token\IToken::class); + $token = $this->createMock(IToken::class); $this->tokenProvider->method('getToken') ->with('mysessionid') ->willReturn($token); @@ -669,7 +669,7 @@ class ManagerTest extends TestCase { $this->session->method('getId') ->willReturn('mysessionid'); - $token = $this->createMock(OC\Authentication\Token\IToken::class); + $token = $this->createMock(IToken::class); $token->method('getId') ->willReturn(40); @@ -704,7 +704,7 @@ class ManagerTest extends TestCase { $this->tokenProvider->method('getToken') ->with('mysessionid') - ->willThrowException(new OC\Authentication\Exceptions\InvalidTokenException()); + ->willThrowException(new InvalidTokenException()); $this->config->method('getUserKeys')->willReturn([]); @@ -736,7 +736,7 @@ class ManagerTest extends TestCase { ]; $this->config->expects($this->exactly(3)) ->method('deleteUserValue') - ->willReturnCallback(function () use (&$deleteUserValueCalls) { + ->willReturnCallback(function () use (&$deleteUserValueCalls): void { $expected = array_shift($deleteUserValueCalls); $this->assertEquals($expected, func_get_args()); }); @@ -748,7 +748,7 @@ class ManagerTest extends TestCase { ]; $this->tokenProvider->expects($this->exactly(3)) ->method('invalidateTokenById') - ->willReturnCallback(function () use (&$invalidateCalls) { + ->willReturnCallback(function () use (&$invalidateCalls): void { $expected = array_shift($invalidateCalls); $this->assertEquals($expected, func_get_args()); }); @@ -770,7 +770,7 @@ class ManagerTest extends TestCase { ]; $this->config->expects($this->exactly(3)) ->method('deleteUserValue') - ->willReturnCallback(function () use (&$deleteUserValueCalls) { + ->willReturnCallback(function () use (&$deleteUserValueCalls): void { $expected = array_shift($deleteUserValueCalls); $this->assertEquals($expected, func_get_args()); }); @@ -782,7 +782,7 @@ class ManagerTest extends TestCase { ]; $this->tokenProvider->expects($this->exactly(3)) ->method('invalidateTokenById') - ->willReturnCallback(function ($user, $tokenId) use (&$invalidateCalls) { + ->willReturnCallback(function ($user, $tokenId) use (&$invalidateCalls): void { $expected = array_shift($invalidateCalls); $this->assertEquals($expected, func_get_args()); if ($tokenId === 43) { diff --git a/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php index 34248f11f21..a1f2a6fa69a 100644 --- a/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace lib\Authentication\TwoFactorAuth; +use OC\Authentication\Exceptions\InvalidProviderException; use OC\Authentication\TwoFactorAuth\ProviderLoader; use OC\Authentication\TwoFactorAuth\ProviderManager; use OCP\Authentication\TwoFactorAuth\IActivatableByAdmin; @@ -41,9 +42,9 @@ class ProviderManagerTest extends TestCase { ); } - + public function testTryEnableInvalidProvider(): void { - $this->expectException(\OC\Authentication\Exceptions\InvalidProviderException::class); + $this->expectException(InvalidProviderException::class); $user = $this->createMock(IUser::class); $this->providerManager->tryEnableProviderFor('none', $user); @@ -87,9 +88,9 @@ class ProviderManagerTest extends TestCase { $this->assertTrue($res); } - + public function testTryDisableInvalidProvider(): void { - $this->expectException(\OC\Authentication\Exceptions\InvalidProviderException::class); + $this->expectException(InvalidProviderException::class); $user = $this->createMock(IUser::class); $this->providerManager->tryDisableProviderFor('none', $user); diff --git a/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php b/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php index 252b57e7983..2018dc1a634 100644 --- a/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php @@ -126,7 +126,7 @@ class RegistryTest extends TestCase { ]; $this->dispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); |