diff options
Diffstat (limited to 'tests/lib/Authentication')
21 files changed, 198 insertions, 120 deletions
diff --git a/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php b/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php index f91371a86b7..1861a5b2150 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): void { + $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/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 4fde9817d09..300517fab4a 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): 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..c6c4ec4198f 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(); } @@ -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'); } 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/ManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php index 52792c29ed0..c68d1de60c5 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], @@ -356,12 +357,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 +405,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 +481,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 +525,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 +562,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 +596,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 +670,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 +705,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 +730,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 +764,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..6004e67603b 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; @@ -43,7 +44,7 @@ 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); @@ -89,7 +90,7 @@ class ProviderManagerTest extends TestCase { 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); } |