diff options
Diffstat (limited to 'tests/lib/Authentication')
17 files changed, 166 insertions, 92 deletions
diff --git a/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php b/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php index f91371a86b7..a7590cdd244 100644 --- a/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php +++ b/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php @@ -83,13 +83,18 @@ class UserDeletedTokenCleanupListenerTest extends TestCase { $token2, $token3, ]); + + $calls = [ + ['user123', 1], + ['user123', 2], + ['user123', 3], + ]; $this->manager->expects($this->exactly(3)) ->method('invalidateTokenById') - ->withConsecutive( - ['user123', 1], - ['user123', 2], - ['user123', 3] - ); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->logger->expects($this->never()) ->method('error'); diff --git a/tests/lib/Authentication/Login/ALoginCommandTest.php b/tests/lib/Authentication/Login/ALoginTestCommand.php index 2ab59835bfe..502380bc3fe 100644 --- a/tests/lib/Authentication/Login/ALoginCommandTest.php +++ b/tests/lib/Authentication/Login/ALoginTestCommand.php @@ -8,14 +8,13 @@ declare(strict_types=1); namespace Test\Authentication\Login; -use OC\Authentication\Login\ALoginCommand; use OC\Authentication\Login\LoginData; use OCP\IRequest; use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; -abstract class ALoginCommandTest extends TestCase { +abstract class ALoginTestCommand extends TestCase { /** @var IRequest|MockObject */ protected $request; @@ -36,7 +35,7 @@ abstract class ALoginCommandTest extends TestCase { /** @var IUser|MockObject */ protected $user; - /** @var ALoginCommand */ + /** @var ALoginTestCommand */ protected $cmd; protected function setUp(): void { diff --git a/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php b/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php index 025612e963e..5ff2da28946 100644 --- a/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php +++ b/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php @@ -13,7 +13,7 @@ use OC\Authentication\Login\ClearLostPasswordTokensCommand; use OCP\IConfig; use PHPUnit\Framework\MockObject\MockObject; -class ClearLostPasswordTokensCommandTest extends ALoginCommandTest { +class ClearLostPasswordTokensCommandTest extends ALoginTestCommand { /** @var IConfig|MockObject */ private $config; diff --git a/tests/lib/Authentication/Login/CompleteLoginCommandTest.php b/tests/lib/Authentication/Login/CompleteLoginCommandTest.php index 4ddb26e7648..5b08368671f 100644 --- a/tests/lib/Authentication/Login/CompleteLoginCommandTest.php +++ b/tests/lib/Authentication/Login/CompleteLoginCommandTest.php @@ -13,7 +13,7 @@ use OC\Authentication\Login\CompleteLoginCommand; use OC\User\Session; use PHPUnit\Framework\MockObject\MockObject; -class CompleteLoginCommandTest extends ALoginCommandTest { +class CompleteLoginCommandTest extends ALoginTestCommand { /** @var Session|MockObject */ private $session; diff --git a/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php b/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php index fd6a154ef5d..668c0a6d6ea 100644 --- a/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php +++ b/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php @@ -15,7 +15,7 @@ use OC\User\Session; use OCP\IConfig; use PHPUnit\Framework\MockObject\MockObject; -class CreateSessionTokenCommandTest extends ALoginCommandTest { +class CreateSessionTokenCommandTest extends ALoginTestCommand { /** @var IConfig|MockObject */ private $config; diff --git a/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php b/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php index 4eb4f94055f..499abafa6e3 100644 --- a/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php +++ b/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php @@ -14,7 +14,7 @@ use OC\User\Session; use OCP\IConfig; use PHPUnit\Framework\MockObject\MockObject; -class FinishRememberedLoginCommandTest extends ALoginCommandTest { +class FinishRememberedLoginCommandTest extends ALoginTestCommand { /** @var Session|MockObject */ private $userSession; /** @var IConfig|MockObject */ diff --git a/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php b/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php index ce783050921..7b011d70673 100644 --- a/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php +++ b/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php @@ -15,7 +15,7 @@ use OCP\EventDispatcher\IEventDispatcher; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; -class LoggedInCheckCommandTest extends ALoginCommandTest { +class LoggedInCheckCommandTest extends ALoginTestCommand { /** @var LoggerInterface|MockObject */ private $logger; diff --git a/tests/lib/Authentication/Login/PreLoginHookCommandTest.php b/tests/lib/Authentication/Login/PreLoginHookCommandTest.php index b6d826f1c21..0e5096baf55 100644 --- a/tests/lib/Authentication/Login/PreLoginHookCommandTest.php +++ b/tests/lib/Authentication/Login/PreLoginHookCommandTest.php @@ -14,7 +14,7 @@ use OC\User\Manager; use OCP\IUserManager; use PHPUnit\Framework\MockObject\MockObject; -class PreLoginHookCommandTest extends ALoginCommandTest { +class PreLoginHookCommandTest extends ALoginTestCommand { /** @var IUserManager|MockObject */ private $userManager; diff --git a/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php index d4e15f68257..fb8240c4b1e 100644 --- a/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php +++ b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php @@ -14,7 +14,7 @@ use OCP\IConfig; use OCP\ISession; use PHPUnit\Framework\MockObject\MockObject; -class SetUserTimezoneCommandTest extends ALoginCommandTest { +class SetUserTimezoneCommandTest extends ALoginTestCommand { /** @var IConfig|MockObject */ private $config; diff --git a/tests/lib/Authentication/Login/TwoFactorCommandTest.php b/tests/lib/Authentication/Login/TwoFactorCommandTest.php index af7d7446bfb..a95e4b50cbc 100644 --- a/tests/lib/Authentication/Login/TwoFactorCommandTest.php +++ b/tests/lib/Authentication/Login/TwoFactorCommandTest.php @@ -18,7 +18,7 @@ use OCP\Authentication\TwoFactorAuth\IProvider as ITwoFactorAuthProvider; use OCP\IURLGenerator; use PHPUnit\Framework\MockObject\MockObject; -class TwoFactorCommandTest extends ALoginCommandTest { +class TwoFactorCommandTest extends ALoginTestCommand { /** @var Manager|MockObject */ private $twoFactorManager; diff --git a/tests/lib/Authentication/Login/UidLoginCommandTest.php b/tests/lib/Authentication/Login/UidLoginCommandTest.php index 3073aac1859..daae34e2212 100644 --- a/tests/lib/Authentication/Login/UidLoginCommandTest.php +++ b/tests/lib/Authentication/Login/UidLoginCommandTest.php @@ -13,7 +13,7 @@ use OC\Authentication\Login\UidLoginCommand; use OC\User\Manager; use PHPUnit\Framework\MockObject\MockObject; -class UidLoginCommandTest extends ALoginCommandTest { +class UidLoginCommandTest extends ALoginTestCommand { /** @var Manager|MockObject */ private $userManager; diff --git a/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php b/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php index f5760f3f232..1a845a05c23 100644 --- a/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php +++ b/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php @@ -13,7 +13,7 @@ use OC\Authentication\Login\UpdateLastPasswordConfirmCommand; use OCP\ISession; use PHPUnit\Framework\MockObject\MockObject; -class UpdateLastPasswordConfirmCommandTest extends ALoginCommandTest { +class UpdateLastPasswordConfirmCommandTest extends ALoginTestCommand { /** @var ISession|MockObject */ private $session; diff --git a/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php b/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php index 84b1018e5fa..ee4e171d443 100644 --- a/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php +++ b/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php @@ -15,7 +15,7 @@ use OCP\IUserManager; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; -class UserDisabledCheckCommandTest extends ALoginCommandTest { +class UserDisabledCheckCommandTest extends ALoginTestCommand { /** @var IUserManager|MockObject */ private $userManager; 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'); } diff --git a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php index 52792c29ed0..75691627ce7 100644 --- a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php @@ -204,7 +204,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], @@ -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) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); + $this->session->expects($this->once()) ->method('set') ->with(Manager::SESSION_UID_DONE, 'jos'); @@ -474,14 +480,19 @@ 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'); @@ -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,12 +561,16 @@ 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) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->session->method('getId') ->willReturn('mysessionid'); @@ -580,12 +595,16 @@ 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) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->session->method('getId') ->willReturn('mysessionid'); @@ -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) { + $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) { + $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) { + $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) { + $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/RegistryTest.php b/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php index 77e619d03a2..252b57e7983 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) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->registry->deleteUserData($user); } |