diff options
Diffstat (limited to 'tests/lib/Authentication/TwoFactorAuth')
5 files changed, 100 insertions, 60 deletions
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 52792c29ed0..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; @@ -204,7 +205,7 @@ class ManagerTest extends TestCase { $this->assertTrue($this->manager->isTwoFactorAuthenticated($this->user)); } - public function providerStatesFixData(): array { + public static function providerStatesFixData(): array { return [ [false, false], [true, true], @@ -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') @@ -356,12 +356,18 @@ class ManagerTest extends TestCase { ->method('get') ->with('two_factor_remember_login') ->willReturn(false); + + $calls = [ + ['two_factor_auth_uid'], + ['two_factor_remember_login'], + ]; $this->session->expects($this->exactly(2)) ->method('remove') - ->withConsecutive( - ['two_factor_auth_uid'], - ['two_factor_remember_login'] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); + $this->session->expects($this->once()) ->method('set') ->with(Manager::SESSION_UID_DONE, 'jos'); @@ -398,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); @@ -474,18 +480,23 @@ class ManagerTest extends TestCase { public function testNeedsSecondFactor(): void { $user = $this->createMock(IUser::class); + + $calls = [ + ['app_password'], + ['two_factor_auth_uid'], + [Manager::SESSION_UID_DONE], + ]; $this->session->expects($this->exactly(3)) ->method('exists') - ->withConsecutive( - ['app_password'], - ['two_factor_auth_uid'], - [Manager::SESSION_UID_DONE], - ) - ->willReturn(false); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + return false; + }); $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); @@ -513,7 +524,7 @@ class ManagerTest extends TestCase { $this->timeFactory, $this->dispatcher, ]) - ->setMethods(['loadTwoFactorApp', 'isTwoFactorAuthenticated'])// Do not actually load the apps + ->onlyMethods(['isTwoFactorAuthenticated'])// Do not actually load the apps ->getMock(); $manager->method('isTwoFactorAuthenticated') @@ -550,16 +561,20 @@ class ManagerTest extends TestCase { $this->user->method('getUID') ->willReturn('ferdinand'); + $calls = [ + ['two_factor_auth_uid', 'ferdinand'], + ['two_factor_remember_login', true], + ]; $this->session->expects($this->exactly(2)) ->method('set') - ->withConsecutive( - ['two_factor_auth_uid', 'ferdinand'], - ['two_factor_remember_login', true] - ); + ->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); @@ -580,16 +595,20 @@ class ManagerTest extends TestCase { $this->user->method('getUID') ->willReturn('ferdinand'); + $calls = [ + ['two_factor_auth_uid', 'ferdinand'], + ['two_factor_remember_login', false], + ]; $this->session->expects($this->exactly(2)) ->method('set') - ->withConsecutive( - ['two_factor_auth_uid', 'ferdinand'], - ['two_factor_remember_login', false] - ); + ->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); @@ -650,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); @@ -685,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([]); @@ -710,21 +729,29 @@ class ManagerTest extends TestCase { '42', '43', '44' ]); + $deleteUserValueCalls = [ + ['theUserId', 'login_token_2fa', '42'], + ['theUserId', 'login_token_2fa', '43'], + ['theUserId', 'login_token_2fa', '44'], + ]; $this->config->expects($this->exactly(3)) ->method('deleteUserValue') - ->withConsecutive( - ['theUserId', 'login_token_2fa', '42'], - ['theUserId', 'login_token_2fa', '43'], - ['theUserId', 'login_token_2fa', '44'], - ); + ->willReturnCallback(function () use (&$deleteUserValueCalls): void { + $expected = array_shift($deleteUserValueCalls); + $this->assertEquals($expected, func_get_args()); + }); + $invalidateCalls = [ + ['theUserId', 42], + ['theUserId', 43], + ['theUserId', 44], + ]; $this->tokenProvider->expects($this->exactly(3)) ->method('invalidateTokenById') - ->withConsecutive( - ['theUserId', 42], - ['theUserId', 43], - ['theUserId', 44], - ); + ->willReturnCallback(function () use (&$invalidateCalls): void { + $expected = array_shift($invalidateCalls); + $this->assertEquals($expected, func_get_args()); + }); $this->manager->clearTwoFactorPending('theUserId'); } @@ -736,22 +763,28 @@ class ManagerTest extends TestCase { '42', '43', '44' ]); + $deleteUserValueCalls = [ + ['theUserId', 'login_token_2fa', '42'], + ['theUserId', 'login_token_2fa', '43'], + ['theUserId', 'login_token_2fa', '44'], + ]; $this->config->expects($this->exactly(3)) ->method('deleteUserValue') - ->withConsecutive( - ['theUserId', 'login_token_2fa', '42'], - ['theUserId', 'login_token_2fa', '43'], - ['theUserId', 'login_token_2fa', '44'], - ); + ->willReturnCallback(function () use (&$deleteUserValueCalls): void { + $expected = array_shift($deleteUserValueCalls); + $this->assertEquals($expected, func_get_args()); + }); + $invalidateCalls = [ + ['theUserId', 42], + ['theUserId', 43], + ['theUserId', 44], + ]; $this->tokenProvider->expects($this->exactly(3)) ->method('invalidateTokenById') - ->withConsecutive( - ['theUserId', 42], - ['theUserId', 43], - ['theUserId', 44], - ) - ->willReturnCallback(function ($user, $tokenId) { + ->willReturnCallback(function ($user, $tokenId) use (&$invalidateCalls): void { + $expected = array_shift($invalidateCalls); + $this->assertEquals($expected, func_get_args()); if ($tokenId === 43) { throw new DoesNotExistException('token does not exist'); } 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 77e619d03a2..2018dc1a634 100644 --- a/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php @@ -119,12 +119,17 @@ class RegistryTest extends TestCase { 'provider_id' => 'twofactor_u2f', ] ]); + + $calls = [ + [new TwoFactorProviderDisabled('twofactor_u2f')], + [new TwoFactorProviderUserDeleted($user, 'twofactor_u2f')], + ]; $this->dispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->withConsecutive( - [new TwoFactorProviderDisabled('twofactor_u2f')], - [new TwoFactorProviderUserDeleted($user, 'twofactor_u2f')], - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->registry->deleteUserData($user); } |