diff options
Diffstat (limited to 'tests/lib/Authentication')
34 files changed, 742 insertions, 1150 deletions
diff --git a/tests/lib/Authentication/Events/RemoteWipeFinishedTest.php b/tests/lib/Authentication/Events/RemoteWipeFinishedTest.php index f28f0ef78bb..c89b1e4108f 100644 --- a/tests/lib/Authentication/Events/RemoteWipeFinishedTest.php +++ b/tests/lib/Authentication/Events/RemoteWipeFinishedTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Events; @@ -30,7 +14,7 @@ use OC\Authentication\Token\IToken; use Test\TestCase; class RemoteWipeFinishedTest extends TestCase { - public function testGetToken() { + public function testGetToken(): void { $token = $this->createMock(IToken::class); $event = new RemoteWipeFinished($token); diff --git a/tests/lib/Authentication/Events/RemoteWipeStartedTest.php b/tests/lib/Authentication/Events/RemoteWipeStartedTest.php index d6b0ef7019e..fc297f7c087 100644 --- a/tests/lib/Authentication/Events/RemoteWipeStartedTest.php +++ b/tests/lib/Authentication/Events/RemoteWipeStartedTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Events; @@ -30,7 +14,7 @@ use OC\Authentication\Token\IToken; use Test\TestCase; class RemoteWipeStartedTest extends TestCase { - public function testGetToken() { + public function testGetToken(): void { $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); diff --git a/tests/lib/Authentication/Listeners/RemoteWipeActivityListenerTest.php b/tests/lib/Authentication/Listeners/RemoteWipeActivityListenerTest.php index daf8d272c2f..e2f957ab69a 100644 --- a/tests/lib/Authentication/Listeners/RemoteWipeActivityListenerTest.php +++ b/tests/lib/Authentication/Listeners/RemoteWipeActivityListenerTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Events; @@ -59,7 +43,7 @@ class RemoteWipeActivityListenerTest extends TestCase { ); } - public function testHandleUnrelated() { + public function testHandleUnrelated(): void { $event = new Event(); $this->listener->handle($event); @@ -67,7 +51,7 @@ class RemoteWipeActivityListenerTest extends TestCase { $this->addToAssertionCount(1); } - public function testHandleRemoteWipeStarted() { + public function testHandleRemoteWipeStarted(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); @@ -103,7 +87,7 @@ class RemoteWipeActivityListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeStartedCanNotPublish() { + public function testHandleRemoteWipeStartedCanNotPublish(): void { $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); $this->activityManager->expects($this->once()) @@ -115,7 +99,7 @@ class RemoteWipeActivityListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeFinished() { + public function testHandleRemoteWipeFinished(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeFinished($token); diff --git a/tests/lib/Authentication/Listeners/RemoteWipeEmailListenerTest.php b/tests/lib/Authentication/Listeners/RemoteWipeEmailListenerTest.php index 7a715dd3f0c..1c45add4e31 100644 --- a/tests/lib/Authentication/Listeners/RemoteWipeEmailListenerTest.php +++ b/tests/lib/Authentication/Listeners/RemoteWipeEmailListenerTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace lib\Authentication\Listeners; @@ -82,14 +66,14 @@ class RemoteWipeEmailListenerTest extends TestCase { } - public function testHandleUnrelated() { + public function testHandleUnrelated(): void { $event = new Event(); $this->mailer->expects($this->never())->method('send'); $this->listener->handle($event); } - public function testHandleRemoteWipeStartedInvalidUser() { + public function testHandleRemoteWipeStartedInvalidUser(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); @@ -103,7 +87,7 @@ class RemoteWipeEmailListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeStartedNoEmailSet() { + public function testHandleRemoteWipeStartedNoEmailSet(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); @@ -119,7 +103,7 @@ class RemoteWipeEmailListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeStartedTransmissionError() { + public function testHandleRemoteWipeStartedTransmissionError(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); @@ -139,7 +123,7 @@ class RemoteWipeEmailListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeStarted() { + public function testHandleRemoteWipeStarted(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); @@ -164,7 +148,7 @@ class RemoteWipeEmailListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeFinishedInvalidUser() { + public function testHandleRemoteWipeFinishedInvalidUser(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeFinished($token); @@ -178,7 +162,7 @@ class RemoteWipeEmailListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeFinishedNoEmailSet() { + public function testHandleRemoteWipeFinishedNoEmailSet(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeFinished($token); @@ -194,7 +178,7 @@ class RemoteWipeEmailListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeFinishedTransmissionError() { + public function testHandleRemoteWipeFinishedTransmissionError(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeFinished($token); @@ -214,7 +198,7 @@ class RemoteWipeEmailListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeFinished() { + public function testHandleRemoteWipeFinished(): void { /** @var IToken|MockObject $token */ $token = $this->createMock(IToken::class); $event = new RemoteWipeFinished($token); diff --git a/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php b/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php index e5a329a4a7f..53fa502b199 100644 --- a/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php +++ b/tests/lib/Authentication/Listeners/RemoteWipeNotificationsListenerTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Events; @@ -60,7 +44,7 @@ class RemoteWipeNotificationsListenerTest extends TestCase { ); } - public function testHandleUnrelated() { + public function testHandleUnrelated(): void { $event = new Event(); $this->listener->handle($event); @@ -68,7 +52,7 @@ class RemoteWipeNotificationsListenerTest extends TestCase { $this->addToAssertionCount(1); } - public function testHandleRemoteWipeStarted() { + public function testHandleRemoteWipeStarted(): void { $token = $this->createMock(IToken::class); $event = new RemoteWipeStarted($token); $notification = $this->createMock(INotification::class); @@ -108,7 +92,7 @@ class RemoteWipeNotificationsListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleRemoteWipeFinished() { + public function testHandleRemoteWipeFinished(): void { $token = $this->createMock(IToken::class); $event = new RemoteWipeFinished($token); $notification = $this->createMock(INotification::class); diff --git a/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php b/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php index 20b85a293d4..1861a5b2150 100644 --- a/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php +++ b/tests/lib/Authentication/Listeners/UserDeletedTokenCleanupListenerTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Listeners; @@ -99,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 7d56a76e10c..b955b20beba 100644 --- a/tests/lib/Authentication/Login/ALoginCommandTest.php +++ b/tests/lib/Authentication/Login/ALoginTestCommand.php @@ -1,37 +1,21 @@ <?php + /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ 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; @@ -52,7 +36,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 1d6250415a9..5ff2da28946 100644 --- a/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php +++ b/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -29,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; @@ -43,7 +27,7 @@ class ClearLostPasswordTokensCommandTest extends ALoginCommandTest { ); } - public function testProcess() { + public function testProcess(): void { $data = $this->getLoggedInLoginData(); $this->user->expects($this->once()) ->method('getUID') diff --git a/tests/lib/Authentication/Login/CompleteLoginCommandTest.php b/tests/lib/Authentication/Login/CompleteLoginCommandTest.php index 3f7f9e4fe73..5b08368671f 100644 --- a/tests/lib/Authentication/Login/CompleteLoginCommandTest.php +++ b/tests/lib/Authentication/Login/CompleteLoginCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -29,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; @@ -43,7 +27,7 @@ class CompleteLoginCommandTest extends ALoginCommandTest { ); } - public function testProcess() { + public function testProcess(): void { $data = $this->getLoggedInLoginData(); $this->session->expects($this->once()) ->method('completeLogin') diff --git a/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php b/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php index eb48463402d..668c0a6d6ea 100644 --- a/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php +++ b/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -31,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; @@ -50,7 +34,7 @@ class CreateSessionTokenCommandTest extends ALoginCommandTest { ); } - public function testProcess() { + public function testProcess(): void { $data = $this->getLoggedInLoginData(); $this->config->expects($this->once()) ->method('getSystemValueInt') @@ -83,7 +67,7 @@ class CreateSessionTokenCommandTest extends ALoginCommandTest { $this->assertTrue($result->isSuccess()); } - public function testProcessDoNotRemember() { + public function testProcessDoNotRemember(): void { $data = $this->getLoggedInLoginData(); $this->config->expects($this->once()) ->method('getSystemValueInt') diff --git a/tests/lib/Authentication/Login/EmailLoginCommandTest.php b/tests/lib/Authentication/Login/EmailLoginCommandTest.php deleted file mode 100644 index 4544aedec11..00000000000 --- a/tests/lib/Authentication/Login/EmailLoginCommandTest.php +++ /dev/null @@ -1,164 +0,0 @@ -<?php - -/** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -declare(strict_types=1); - -namespace Test\Authentication\Login; - -use OC\Authentication\Login\EmailLoginCommand; -use OCP\IUser; -use OCP\IUserManager; -use PHPUnit\Framework\MockObject\MockObject; - -class EmailLoginCommandTest extends ALoginCommandTest { - /** @var IUserManager|MockObject */ - private $userManager; - - protected function setUp(): void { - parent::setUp(); - - $this->userManager = $this->createMock(IUserManager::class); - - $this->cmd = new EmailLoginCommand( - $this->userManager - ); - } - - public function testProcessAlreadyLoggedIn() { - $data = $this->getLoggedInLoginData(); - - $result = $this->cmd->process($data); - - $this->assertTrue($result->isSuccess()); - } - - public function testProcessNotAnEmailLogin() { - $data = $this->getFailedLoginData(); - $this->userManager->expects($this->never()) - ->method('getByEmail') - ->with($this->username) - ->willReturn([]); - - $result = $this->cmd->process($data); - - $this->assertTrue($result->isSuccess()); - } - - public function testProcessDuplicateEmailLogin() { - $data = $this->getFailedLoginData(); - $data->setUsername('user@example.com'); - $this->userManager->expects($this->once()) - ->method('getByEmail') - ->with('user@example.com') - ->willReturn([ - $this->createMock(IUser::class), - $this->createMock(IUser::class), - ]); - - $result = $this->cmd->process($data); - - $this->assertTrue($result->isSuccess()); - } - - public function testProcessUidIsEmail() { - $email = 'user@domain.com'; - $data = $this->getFailedLoginData(); - $data->setUsername($email); - $emailUser = $this->createMock(IUser::class); - $emailUser->expects($this->any()) - ->method('getUID') - ->willReturn($email); - $this->userManager->expects($this->once()) - ->method('getByEmail') - ->with($email) - ->willReturn([ - $emailUser, - ]); - $this->userManager->expects($this->never()) - ->method('checkPassword'); - - $result = $this->cmd->process($data); - - $this->assertTrue($result->isSuccess()); - $this->assertFalse($data->getUser()); - $this->assertEquals($email, $data->getUsername()); - } - - public function testProcessWrongPassword() { - $email = 'user@domain.com'; - $data = $this->getFailedLoginData(); - $data->setUsername($email); - $emailUser = $this->createMock(IUser::class); - $emailUser->expects($this->any()) - ->method('getUID') - ->willReturn('user2'); - $this->userManager->expects($this->once()) - ->method('getByEmail') - ->with($email) - ->willReturn([ - $emailUser, - ]); - $this->userManager->expects($this->once()) - ->method('checkPassword') - ->with( - 'user2', - $this->password - ) - ->willReturn(false); - - $result = $this->cmd->process($data); - - $this->assertTrue($result->isSuccess()); - $this->assertFalse($data->getUser()); - $this->assertEquals($email, $data->getUsername()); - } - - public function testProcess() { - $email = 'user@domain.com'; - $data = $this->getFailedLoginData(); - $data->setUsername($email); - $emailUser = $this->createMock(IUser::class); - $emailUser->expects($this->any()) - ->method('getUID') - ->willReturn('user2'); - $this->userManager->expects($this->once()) - ->method('getByEmail') - ->with($email) - ->willReturn([ - $emailUser, - ]); - $this->userManager->expects($this->once()) - ->method('checkPassword') - ->with( - 'user2', - $this->password - ) - ->willReturn($emailUser); - - $result = $this->cmd->process($data); - - $this->assertTrue($result->isSuccess()); - $this->assertEquals($emailUser, $data->getUser()); - $this->assertEquals('user2', $data->getUsername()); - } -} diff --git a/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php b/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php index d6f18ad9eff..499abafa6e3 100644 --- a/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php +++ b/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -30,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 */ @@ -48,7 +32,7 @@ class FinishRememberedLoginCommandTest extends ALoginCommandTest { ); } - public function testProcessNotRememberedLogin() { + public function testProcessNotRememberedLogin(): void { $data = $this->getLoggedInLoginData(); $data->setRememberLogin(false); $this->userSession->expects($this->never()) @@ -59,10 +43,10 @@ class FinishRememberedLoginCommandTest extends ALoginCommandTest { $this->assertTrue($result->isSuccess()); } - public function testProcess() { + public function testProcess(): void { $data = $this->getLoggedInLoginData(); $this->config->expects($this->once()) - ->method('getSystemValue') + ->method('getSystemValueBool') ->with('auto_logout', false) ->willReturn(false); $this->userSession->expects($this->once()) @@ -74,10 +58,10 @@ class FinishRememberedLoginCommandTest extends ALoginCommandTest { $this->assertTrue($result->isSuccess()); } - public function testProcessNotRemeberedLoginWithAutologout() { + public function testProcessNotRemeberedLoginWithAutologout(): void { $data = $this->getLoggedInLoginData(); $this->config->expects($this->once()) - ->method('getSystemValue') + ->method('getSystemValueBool') ->with('auto_logout', false) ->willReturn(true); $this->userSession->expects($this->never()) diff --git a/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php b/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php index d3ebf205943..7b011d70673 100644 --- a/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php +++ b/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -31,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; @@ -50,7 +34,7 @@ class LoggedInCheckCommandTest extends ALoginCommandTest { ); } - public function testProcessSuccessfulLogin() { + public function testProcessSuccessfulLogin(): void { $data = $this->getLoggedInLoginData(); $result = $this->cmd->process($data); @@ -58,7 +42,7 @@ class LoggedInCheckCommandTest extends ALoginCommandTest { $this->assertTrue($result->isSuccess()); } - public function testProcessFailedLogin() { + public function testProcessFailedLogin(): void { $data = $this->getFailedLoginData(); $this->logger->expects($this->once()) ->method('warning'); diff --git a/tests/lib/Authentication/Login/PreLoginHookCommandTest.php b/tests/lib/Authentication/Login/PreLoginHookCommandTest.php index 2e83ef51ffa..0e5096baf55 100644 --- a/tests/lib/Authentication/Login/PreLoginHookCommandTest.php +++ b/tests/lib/Authentication/Login/PreLoginHookCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -30,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; @@ -44,7 +28,7 @@ class PreLoginHookCommandTest extends ALoginCommandTest { ); } - public function testProcess() { + public function testProcess(): void { $data = $this->getBasicLoginData(); $this->userManager->expects($this->once()) ->method('emit') diff --git a/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php index ae08b6205d5..fb8240c4b1e 100644 --- a/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php +++ b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -30,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; @@ -49,7 +33,7 @@ class SetUserTimezoneCommandTest extends ALoginCommandTest { ); } - public function testProcessNoTimezoneSet() { + public function testProcessNoTimezoneSet(): void { $data = $this->getLoggedInLoginData(); $this->config->expects($this->never()) ->method('setUserValue'); @@ -61,7 +45,7 @@ class SetUserTimezoneCommandTest extends ALoginCommandTest { $this->assertTrue($result->isSuccess()); } - public function testProcess() { + public function testProcess(): void { $data = $this->getLoggedInLoginDataWithTimezone(); $this->user->expects($this->once()) ->method('getUID') diff --git a/tests/lib/Authentication/Login/TwoFactorCommandTest.php b/tests/lib/Authentication/Login/TwoFactorCommandTest.php index 679bae07351..a95e4b50cbc 100644 --- a/tests/lib/Authentication/Login/TwoFactorCommandTest.php +++ b/tests/lib/Authentication/Login/TwoFactorCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -34,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; @@ -58,7 +42,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { ); } - public function testNotTwoFactorAuthenticated() { + public function testNotTwoFactorAuthenticated(): void { $data = $this->getLoggedInLoginData(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') @@ -71,7 +55,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { $this->assertTrue($result->isSuccess()); } - public function testProcessOneActiveProvider() { + public function testProcessOneActiveProvider(): void { $data = $this->getLoggedInLoginData(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') @@ -115,7 +99,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { $this->assertEquals('two/factor/url', $result->getRedirectUrl()); } - public function testProcessMissingProviders() { + public function testProcessMissingProviders(): void { $data = $this->getLoggedInLoginData(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') @@ -156,7 +140,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { $this->assertEquals('two/factor/url', $result->getRedirectUrl()); } - public function testProcessTwoActiveProviders() { + public function testProcessTwoActiveProviders(): void { $data = $this->getLoggedInLoginData(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') @@ -202,7 +186,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { $this->assertEquals('two/factor/url', $result->getRedirectUrl()); } - public function testProcessFailingProviderAndEnforcedButNoSetupProviders() { + public function testProcessFailingProviderAndEnforcedButNoSetupProviders(): void { $data = $this->getLoggedInLoginData(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') @@ -237,7 +221,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { $this->assertEquals('two/factor/url', $result->getRedirectUrl()); } - public function testProcessFailingProviderAndEnforced() { + public function testProcessFailingProviderAndEnforced(): void { $data = $this->getLoggedInLoginData(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') @@ -275,7 +259,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { $this->assertEquals('two/factor/url', $result->getRedirectUrl()); } - public function testProcessNoProvidersButEnforced() { + public function testProcessNoProvidersButEnforced(): void { $data = $this->getLoggedInLoginData(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') @@ -310,7 +294,7 @@ class TwoFactorCommandTest extends ALoginCommandTest { $this->assertEquals('two/factor/url', $result->getRedirectUrl()); } - public function testProcessWithRedirectUrl() { + public function testProcessWithRedirectUrl(): void { $data = $this->getLoggedInLoginDataWithRedirectUrl(); $this->twoFactorManager->expects($this->once()) ->method('isTwoFactorAuthenticated') diff --git a/tests/lib/Authentication/Login/UidLoginCommandTest.php b/tests/lib/Authentication/Login/UidLoginCommandTest.php index 812215f266e..daae34e2212 100644 --- a/tests/lib/Authentication/Login/UidLoginCommandTest.php +++ b/tests/lib/Authentication/Login/UidLoginCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -29,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; @@ -43,7 +27,7 @@ class UidLoginCommandTest extends ALoginCommandTest { ); } - public function testProcessFailingLogin() { + public function testProcessFailingLogin(): void { $data = $this->getBasicLoginData(); $this->userManager->expects($this->once()) ->method('checkPasswordNoLogging') @@ -59,7 +43,7 @@ class UidLoginCommandTest extends ALoginCommandTest { $this->assertFalse($data->getUser()); } - public function testProcess() { + public function testProcess(): void { $data = $this->getBasicLoginData(); $this->userManager->expects($this->once()) ->method('checkPasswordNoLogging') diff --git a/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php b/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php index 07b32daa065..1a845a05c23 100644 --- a/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php +++ b/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -29,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; @@ -43,7 +27,7 @@ class UpdateLastPasswordConfirmCommandTest extends ALoginCommandTest { ); } - public function testProcess() { + public function testProcess(): void { $data = $this->getLoggedInLoginData(); $this->user->expects($this->once()) ->method('getLastLogin') diff --git a/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php b/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php index fe30d3b4fea..ee4e171d443 100644 --- a/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php +++ b/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php @@ -1,24 +1,8 @@ <?php /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ declare(strict_types=1); @@ -31,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; @@ -50,7 +34,7 @@ class UserDisabledCheckCommandTest extends ALoginCommandTest { ); } - public function testProcessNonExistingUser() { + public function testProcessNonExistingUser(): void { $data = $this->getBasicLoginData(); $this->userManager->expects($this->once()) ->method('get') @@ -62,7 +46,7 @@ class UserDisabledCheckCommandTest extends ALoginCommandTest { $this->assertTrue($result->isSuccess()); } - public function testProcessDisabledUser() { + public function testProcessDisabledUser(): void { $data = $this->getBasicLoginData(); $this->userManager->expects($this->once()) ->method('get') @@ -78,7 +62,7 @@ class UserDisabledCheckCommandTest extends ALoginCommandTest { $this->assertSame(LoginController::LOGIN_MSG_USERDISABLED, $result->getErrorMessage()); } - public function testProcess() { + public function testProcess(): void { $data = $this->getBasicLoginData(); $this->userManager->expects($this->once()) ->method('get') diff --git a/tests/lib/Authentication/LoginCredentials/CredentialsTest.php b/tests/lib/Authentication/LoginCredentials/CredentialsTest.php index f550325a6dc..a64ad6c9b76 100644 --- a/tests/lib/Authentication/LoginCredentials/CredentialsTest.php +++ b/tests/lib/Authentication/LoginCredentials/CredentialsTest.php @@ -1,25 +1,8 @@ <?php /** - * @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2016 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\LoginCredentials; @@ -50,15 +33,15 @@ class CredentialsTest extends TestCase { $this->credentials = new Credentials($this->uid, $this->user, $this->password); } - public function testGetUID() { + public function testGetUID(): void { $this->assertEquals($this->uid, $this->credentials->getUID()); } - public function testGetUserName() { + public function testGetUserName(): void { $this->assertEquals($this->user, $this->credentials->getLoginName()); } - public function testGetPassword() { + public function testGetPassword(): void { $this->assertEquals($this->password, $this->credentials->getPassword()); } } diff --git a/tests/lib/Authentication/LoginCredentials/StoreTest.php b/tests/lib/Authentication/LoginCredentials/StoreTest.php index 80d64d5466f..aca586b91ec 100644 --- a/tests/lib/Authentication/LoginCredentials/StoreTest.php +++ b/tests/lib/Authentication/LoginCredentials/StoreTest.php @@ -1,25 +1,8 @@ <?php /** - * @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2016 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\LoginCredentials; @@ -32,6 +15,7 @@ use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; use OCP\Authentication\Exceptions\CredentialsUnavailableException; use OCP\ISession; +use OCP\Security\ICrypto; use OCP\Session\Exceptions\SessionNotAvailableException; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -46,6 +30,8 @@ class StoreTest extends TestCase { /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ private $logger; + /** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */ + private $crypto; /** @var Store */ private $store; @@ -56,40 +42,44 @@ class StoreTest extends TestCase { $this->session = $this->createMock(ISession::class); $this->tokenProvider = $this->createMock(IProvider::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->crypto = $this->createMock(ICrypto::class); - $this->store = new Store($this->session, $this->logger, $this->tokenProvider); + $this->store = new Store($this->session, $this->logger, $this->crypto, $this->tokenProvider); } - public function testAuthenticate() { + public function testAuthenticate(): void { $params = [ 'run' => true, 'uid' => 'user123', - 'password' => 123456, + 'password' => '123456', ]; $this->session->expects($this->once()) ->method('set') ->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params))); + $this->crypto->expects($this->once()) + ->method('encrypt') + ->willReturn('123456'); $this->store->authenticate($params); } - public function testSetSession() { + public function testSetSession(): void { $session = $this->createMock(ISession::class); $this->store->setSession($session); $this->addToAssertionCount(1); } - public function testGetLoginCredentialsNoTokenProvider() { - $this->store = new Store($this->session, $this->logger, null); + public function testGetLoginCredentialsNoTokenProvider(): void { + $this->store = new Store($this->session, $this->logger, $this->crypto, null); $this->expectException(CredentialsUnavailableException::class); $this->store->getLoginCredentials(); } - public function testGetLoginCredentials() { + public function testGetLoginCredentials(): void { $uid = 'uid'; $user = 'user123'; $password = 'passme'; @@ -118,29 +108,29 @@ class StoreTest extends TestCase { $this->assertEquals($expected, $creds); } - public function testGetLoginCredentialsSessionNotAvailable() { + 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(); } - public function testGetLoginCredentialsInvalidToken() { + public function testGetLoginCredentialsInvalidToken(): void { $this->session->expects($this->once()) ->method('getId') ->willReturn('sess2233'); $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new InvalidTokenException())); + ->willThrowException(new InvalidTokenException()); $this->expectException(CredentialsUnavailableException::class); $this->store->getLoginCredentials(); } - public function testGetLoginCredentialsPartialCredentialsAndSessionName() { + public function testGetLoginCredentialsPartialCredentialsAndSessionName(): void { $uid = 'id987'; $user = 'user987'; $password = '7389374'; @@ -151,11 +141,14 @@ 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')) ->willReturn(true); + $this->crypto->expects($this->once()) + ->method('decrypt') + ->willReturn($password); $this->session->expects($this->exactly(2)) ->method('get') ->willReturnMap([ @@ -178,7 +171,7 @@ class StoreTest extends TestCase { $this->assertEquals($expected, $actual); } - public function testGetLoginCredentialsPartialCredentials() { + public function testGetLoginCredentialsPartialCredentials(): void { $uid = 'id987'; $password = '7389374'; @@ -188,11 +181,14 @@ 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')) ->willReturn(true); + $this->crypto->expects($this->once()) + ->method('decrypt') + ->willReturn($password); $this->session->expects($this->exactly(2)) ->method('get') ->willReturnMap([ @@ -215,7 +211,7 @@ class StoreTest extends TestCase { $this->assertEquals($expected, $actual); } - public function testGetLoginCredentialsInvalidTokenLoginCredentials() { + public function testGetLoginCredentialsInvalidTokenLoginCredentials(): void { $uid = 'id987'; $user = 'user987'; $password = '7389374'; @@ -226,11 +222,14 @@ 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')) ->willReturn(true); + $this->crypto->expects($this->once()) + ->method('decrypt') + ->willReturn($password); $this->session->expects($this->once()) ->method('get') ->with($this->equalTo('login_credentials')) @@ -242,16 +241,56 @@ class StoreTest extends TestCase { $this->assertEquals($expected, $actual); } - public function testGetLoginCredentialsPasswordlessToken() { + public function testGetLoginCredentialsPasswordlessToken(): void { $this->session->expects($this->once()) ->method('getId') ->willReturn('sess2233'); $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sess2233') - ->will($this->throwException(new PasswordlessTokenException())); + ->willThrowException(new PasswordlessTokenException()); $this->expectException(CredentialsUnavailableException::class); $this->store->getLoginCredentials(); } + + public function testAuthenticatePasswordlessToken(): void { + $user = 'user987'; + $password = null; + + $params = [ + 'run' => true, + 'loginName' => $user, + 'uid' => $user, + 'password' => $password, + ]; + + $this->session->expects($this->once()) + ->method('set') + ->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params))); + + + $this->session->expects($this->once()) + ->method('getId') + ->willReturn('sess2233'); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sess2233') + ->willThrowException(new PasswordlessTokenException()); + + $this->session->expects($this->once()) + ->method('exists') + ->with($this->equalTo('login_credentials')) + ->willReturn(true); + $this->session->expects($this->once()) + ->method('get') + ->with($this->equalTo('login_credentials')) + ->willReturn(json_encode($params)); + + $this->store->authenticate($params); + $actual = $this->store->getLoginCredentials(); + + $expected = new Credentials($user, $user, $password); + $this->assertEquals($expected, $actual); + } } diff --git a/tests/lib/Authentication/Token/ManagerTest.php b/tests/lib/Authentication/Token/ManagerTest.php index 5f024bb1d43..58bbe236248 100644 --- a/tests/lib/Authentication/Token/ManagerTest.php +++ b/tests/lib/Authentication/Token/ManagerTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Token; @@ -50,7 +33,7 @@ class ManagerTest extends TestCase { ); } - public function testGenerateToken() { + public function testGenerateToken(): void { $token = new PublicKeyToken(); $this->publicKeyTokenProvider->expects($this->once()) @@ -78,7 +61,7 @@ class ManagerTest extends TestCase { $this->assertSame($token, $actual); } - public function testGenerateConflictingToken() { + public function testGenerateConflictingToken(): void { /** @var MockObject|UniqueConstraintViolationException $exception */ $exception = $this->createMock(UniqueConstraintViolationException::class); @@ -114,7 +97,7 @@ class ManagerTest extends TestCase { $this->assertSame($token, $actual); } - public function testGenerateTokenTooLongName() { + public function testGenerateTokenTooLongName(): void { $token = $this->createMock(IToken::class); $token->method('getName') ->willReturn(str_repeat('a', 120) . '…'); @@ -145,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], ]; } @@ -174,10 +157,12 @@ class ManagerTest extends TestCase { } } - /** - * @dataProvider tokenData - */ - public function testUpdateToken(IToken $token) { + #[\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); @@ -185,10 +170,12 @@ class ManagerTest extends TestCase { $this->manager->updateToken($token); } - /** - * @dataProvider tokenData - */ - public function testUpdateTokenActivity(IToken $token) { + #[\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); @@ -196,10 +183,12 @@ class ManagerTest extends TestCase { $this->manager->updateTokenActivity($token); } - /** - * @dataProvider tokenData - */ - public function testGetPassword(IToken $token) { + #[\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); @@ -209,10 +198,12 @@ class ManagerTest extends TestCase { $this->assertSame('password', $result); } - /** - * @dataProvider tokenData - */ - public function testSetPassword(IToken $token) { + #[\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); @@ -220,7 +211,7 @@ class ManagerTest extends TestCase { $this->manager->setPassword($token, 'tokenId', 'password'); } - public function testInvalidateTokens() { + public function testInvalidateTokens(): void { $this->publicKeyTokenProvider->expects($this->once()) ->method('invalidateToken') ->with('token'); @@ -228,7 +219,7 @@ class ManagerTest extends TestCase { $this->manager->invalidateToken('token'); } - public function testInvalidateTokenById() { + public function testInvalidateTokenById(): void { $this->publicKeyTokenProvider->expects($this->once()) ->method('invalidateTokenById') ->with('uid', 42); @@ -236,14 +227,22 @@ class ManagerTest extends TestCase { $this->manager->invalidateTokenById('uid', 42); } - public function testInvalidateOldTokens() { + public function testInvalidateOldTokens(): void { $this->publicKeyTokenProvider->expects($this->once()) ->method('invalidateOldTokens'); $this->manager->invalidateOldTokens(); } - public function testGetTokenByUser() { + public function testInvalidateLastUsedBefore(): void { + $this->publicKeyTokenProvider->expects($this->once()) + ->method('invalidateLastUsedBefore') + ->with('user', 946684800); + + $this->manager->invalidateLastUsedBefore('user', 946684800); + } + + public function testGetTokenByUser(): void { $t1 = new PublicKeyToken(); $t2 = new PublicKeyToken(); @@ -256,7 +255,7 @@ class ManagerTest extends TestCase { $this->assertEquals([$t1, $t2], $result); } - public function testRenewSessionTokenPublicKey() { + public function testRenewSessionTokenPublicKey(): void { $this->publicKeyTokenProvider->expects($this->once()) ->method('renewSessionToken') ->with('oldId', 'newId'); @@ -264,7 +263,7 @@ class ManagerTest extends TestCase { $this->manager->renewSessionToken('oldId', 'newId'); } - public function testRenewSessionInvalid() { + public function testRenewSessionInvalid(): void { $this->publicKeyTokenProvider->expects($this->once()) ->method('renewSessionToken') ->with('oldId', 'newId') @@ -274,7 +273,7 @@ class ManagerTest extends TestCase { $this->manager->renewSessionToken('oldId', 'newId'); } - public function testGetTokenByIdPublicKey() { + public function testGetTokenByIdPublicKey(): void { $token = $this->createMock(IToken::class); $this->publicKeyTokenProvider->expects($this->once()) @@ -285,7 +284,7 @@ class ManagerTest extends TestCase { $this->assertSame($token, $this->manager->getTokenById(42)); } - public function testGetTokenByIdInvalid() { + public function testGetTokenByIdInvalid(): void { $this->publicKeyTokenProvider->expects($this->once()) ->method('getTokenById') ->with(42) @@ -295,7 +294,7 @@ class ManagerTest extends TestCase { $this->manager->getTokenById(42); } - public function testGetTokenPublicKey() { + public function testGetTokenPublicKey(): void { $token = new PublicKeyToken(); $this->publicKeyTokenProvider @@ -306,7 +305,7 @@ class ManagerTest extends TestCase { $this->assertSame($token, $this->manager->getToken('tokenId')); } - public function testGetTokenInvalid() { + public function testGetTokenInvalid(): void { $this->publicKeyTokenProvider ->method('getToken') ->with('tokenId') @@ -316,12 +315,12 @@ class ManagerTest extends TestCase { $this->manager->getToken('tokenId'); } - public function testRotateInvalid() { + public function testRotateInvalid(): void { $this->expectException(InvalidTokenException::class); $this->manager->rotate($this->createMock(IToken::class), 'oldId', 'newId'); } - public function testRotatePublicKey() { + public function testRotatePublicKey(): void { $token = new PublicKeyToken(); $this->publicKeyTokenProvider @@ -332,7 +331,7 @@ class ManagerTest extends TestCase { $this->assertSame($token, $this->manager->rotate($token, 'oldId', 'newId')); } - public function testMarkPasswordInvalidPublicKey() { + public function testMarkPasswordInvalidPublicKey(): void { $token = $this->createMock(PublicKeyToken::class); $this->publicKeyTokenProvider->expects($this->once()) @@ -342,17 +341,66 @@ class ManagerTest extends TestCase { $this->manager->markPasswordInvalid($token, 'tokenId'); } - public function testMarkPasswordInvalidInvalidToken() { + public function testMarkPasswordInvalidInvalidToken(): void { $this->expectException(InvalidTokenException::class); $this->manager->markPasswordInvalid($this->createMock(IToken::class), 'tokenId'); } - public function testUpdatePasswords() { + public function testUpdatePasswords(): void { $this->publicKeyTokenProvider->expects($this->once()) ->method('updatePasswords') ->with('uid', 'pass'); $this->manager->updatePasswords('uid', 'pass'); } + + public function testInvalidateTokensOfUserNoClientName(): void { + $t1 = new PublicKeyToken(); + $t2 = new PublicKeyToken(); + $t1->setId(123); + $t2->setId(456); + + $this->publicKeyTokenProvider + ->expects($this->once()) + ->method('getTokenByUser') + ->with('theUser') + ->willReturn([$t1, $t2]); + + $calls = [ + ['theUser', 123], + ['theUser', 456], + ]; + $this->publicKeyTokenProvider + ->expects($this->exactly(2)) + ->method('invalidateTokenById') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); + $this->manager->invalidateTokensOfUser('theUser', null); + } + + public function testInvalidateTokensOfUserClientNameGiven(): void { + $t1 = new PublicKeyToken(); + $t2 = new PublicKeyToken(); + $t3 = new PublicKeyToken(); + $t1->setId(123); + $t1->setName('Firefox session'); + $t2->setId(456); + $t2->setName('My Client Name'); + $t3->setId(789); + $t3->setName('mobile client'); + + $this->publicKeyTokenProvider + ->expects($this->once()) + ->method('getTokenByUser') + ->with('theUser') + ->willReturn([$t1, $t2, $t3]); + $this->publicKeyTokenProvider + ->expects($this->once()) + ->method('invalidateTokenById') + ->with('theUser', 456); + $this->manager->invalidateTokensOfUser('theUser', 'My Client Name'); + } } diff --git a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php index 27646f19888..d1585dadc26 100644 --- a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php +++ b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php @@ -2,36 +2,20 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Token; -use OC; -use OC\Authentication\Token\IToken; 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; /** @@ -50,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(); @@ -113,6 +97,20 @@ class PublicKeyTokenMapperTest extends TestCase { 'version' => $qb->createNamedParameter(2), 'password_invalid' => $qb->createNamedParameter(1), ])->execute(); + $qb->insert('authtoken')->values([ + 'uid' => $qb->createNamedParameter('user3'), + 'login_name' => $qb->createNamedParameter('User3'), + 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'), + 'name' => $qb->createNamedParameter('Iceweasel on Linux'), + 'token' => $qb->createNamedParameter('84c5808c6445b6d65b8aa5b03840f09b27de603f0fb970906fb14ea4b115b7bf5ec53fada5c093fe46afdcd7bbc9617253a4d105f7dfb32719f9973d72412f31'), + 'type' => $qb->createNamedParameter(IToken::PERMANENT_TOKEN), + 'last_activity' => $qb->createNamedParameter($this->time - 60 * 3, IQueryBuilder::PARAM_INT), // Three minutes ago + 'last_check' => $this->time - 60 * 10, // 10mins ago + 'public_key' => $qb->createNamedParameter('public key'), + 'private_key' => $qb->createNamedParameter('private key'), + 'version' => $qb->createNamedParameter(2), + 'password_invalid' => $qb->createNamedParameter(1), + ])->execute(); } private function getNumberOfTokens() { @@ -121,34 +119,42 @@ class PublicKeyTokenMapperTest extends TestCase { ->from('authtoken') ->execute() ->fetch(); - return (int) $result['count']; + return (int)$result['count']; } - public function testInvalidate() { + public function testInvalidate(): void { $token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'; $this->mapper->invalidate($token); - $this->assertSame(3, $this->getNumberOfTokens()); + $this->assertSame(4, $this->getNumberOfTokens()); } - public function testInvalidateInvalid() { + public function testInvalidateInvalid(): void { $token = 'youwontfindthisoneinthedatabase'; $this->mapper->invalidate($token); - $this->assertSame(4, $this->getNumberOfTokens()); + $this->assertSame(5, $this->getNumberOfTokens()); } - public function testInvalidateOld() { + public function testInvalidateOld(): void { $olderThan = $this->time - 60 * 60; // One hour $this->mapper->invalidateOld($olderThan); - $this->assertSame(3, $this->getNumberOfTokens()); + $this->assertSame(4, $this->getNumberOfTokens()); } - public function testGetToken() { + public function testInvalidateLastUsedBefore(): void { + $before = $this->time - 60 * 2; // Two minutes + + $this->mapper->invalidateLastUsedBefore('user3', $before); + + $this->assertSame(4, $this->getNumberOfTokens()); + } + + public function testGetToken(): void { $token = new PublicKeyToken(); $token->setUid('user2'); $token->setLoginName('User2'); @@ -172,15 +178,15 @@ class PublicKeyTokenMapperTest extends TestCase { } - public function testGetInvalidToken() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); + public function testGetInvalidToken(): void { + $this->expectException(DoesNotExistException::class); $token = 'thisisaninvalidtokenthatisnotinthedatabase'; $this->mapper->getToken($token); } - public function testGetTokenById() { + public function testGetTokenById(): void { $token = new PublicKeyToken(); $token->setUid('user2'); $token->setLoginName('User2'); @@ -204,30 +210,30 @@ class PublicKeyTokenMapperTest extends TestCase { } - public function testGetTokenByIdNotFound() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); + public function testGetTokenByIdNotFound(): void { + $this->expectException(DoesNotExistException::class); $this->mapper->getTokenById(-1); } - public function testGetInvalidTokenById() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); + public function testGetInvalidTokenById(): void { + $this->expectException(DoesNotExistException::class); $id = '42'; $this->mapper->getToken($id); } - public function testGetTokenByUser() { + public function testGetTokenByUser(): void { $this->assertCount(2, $this->mapper->getTokenByUser('user1')); } - public function testGetTokenByUserNotFound() { + public function testGetTokenByUserNotFound(): void { $this->assertCount(0, $this->mapper->getTokenByUser('user1000')); } - public function testDeleteById() { + public function testGetById(): void { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $qb = $this->dbConnection->getQueryBuilder(); @@ -237,20 +243,11 @@ class PublicKeyTokenMapperTest extends TestCase { $result = $qb->execute(); $id = $result->fetch()['id']; - $this->mapper->deleteById('user1', (int)$id); - $this->assertEquals(3, $this->getNumberOfTokens()); + $token = $this->mapper->getTokenById((int)$id); + $this->assertEquals('user1', $token->getUID()); } - public function testDeleteByIdWrongUser() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ - $user = $this->createMock(IUser::class); - $id = 33; - - $this->mapper->deleteById('user1000', $id); - $this->assertEquals(4, $this->getNumberOfTokens()); - } - - public function testDeleteByName() { + public function testDeleteByName(): void { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('name') ->from('authtoken') @@ -258,10 +255,10 @@ class PublicKeyTokenMapperTest extends TestCase { $result = $qb->execute(); $name = $result->fetch()['name']; $this->mapper->deleteByName($name); - $this->assertEquals(3, $this->getNumberOfTokens()); + $this->assertEquals(4, $this->getNumberOfTokens()); } - public function testHasExpiredTokens() { + public function testHasExpiredTokens(): void { $this->assertFalse($this->mapper->hasExpiredTokens('user1')); $this->assertTrue($this->mapper->hasExpiredTokens('user3')); } diff --git a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php index ca7618dfd6d..7e7f949965f 100644 --- a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php +++ b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Token; @@ -29,15 +12,18 @@ namespace Test\Authentication\Token; use OC\Authentication\Exceptions\ExpiredTokenException; use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Exceptions\PasswordlessTokenException; -use OC\Authentication\Token\IToken; use OC\Authentication\Token\PublicKeyToken; use OC\Authentication\Token\PublicKeyTokenMapper; use OC\Authentication\Token\PublicKeyTokenProvider; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Authentication\Token\IToken; +use OCP\ICacheFactory; 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; @@ -47,6 +33,8 @@ class PublicKeyTokenProviderTest extends TestCase { private $tokenProvider; /** @var PublicKeyTokenMapper|\PHPUnit\Framework\MockObject\MockObject */ private $mapper; + /** @var IHasher|\PHPUnit\Framework\MockObject\MockObject */ + private $hasher; /** @var ICrypto */ private $crypto; /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ @@ -57,6 +45,8 @@ class PublicKeyTokenProviderTest extends TestCase { private $logger; /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ private $timeFactory; + /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ + private $cacheFactory; /** @var int */ private $time; @@ -64,22 +54,24 @@ class PublicKeyTokenProviderTest extends TestCase { parent::setUp(); $this->mapper = $this->createMock(PublicKeyTokenMapper::class); - $this->hasher = \OC::$server->getHasher(); - $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([ - ['session_lifetime', 60 * 60 * 24, 150], - ['remember_login_cookie_lifetime', 60 * 60 * 24 * 15, 300], - ['secret', '', '1f4h9s'], ['openssl', [], []], ]); + $this->config->method('getSystemValueString') + ->willReturnMap([ + ['secret', '', '1f4h9s'], + ]); $this->db = $this->createMock(IDBConnection::class); $this->logger = $this->createMock(LoggerInterface::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->time = 1313131; $this->timeFactory->method('getTime') ->willReturn($this->time); + $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->tokenProvider = new PublicKeyTokenProvider( $this->mapper, @@ -89,11 +81,12 @@ class PublicKeyTokenProviderTest extends TestCase { $this->logger, $this->timeFactory, $this->hasher, + $this->cacheFactory, ); } - public function testGenerateToken() { - $token = 'token'; + public function testGenerateToken(): void { + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'passme'; @@ -115,7 +108,7 @@ class PublicKeyTokenProviderTest extends TestCase { } public function testGenerateTokenNoPassword(): void { - $token = 'token'; + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'passme'; @@ -137,8 +130,8 @@ class PublicKeyTokenProviderTest extends TestCase { $this->tokenProvider->getPassword($actual, $token); } - public function testGenerateTokenLongPassword() { - $token = 'token'; + public function testGenerateTokenLongPassword(): void { + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = ''; @@ -156,8 +149,8 @@ class PublicKeyTokenProviderTest extends TestCase { $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); } - public function testGenerateTokenInvalidName() { - $token = 'token'; + public function testGenerateTokenInvalidName(): void { + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'passme'; @@ -181,7 +174,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token)); } - public function testUpdateToken() { + public function testUpdateToken(): void { $tk = new PublicKeyToken(); $this->mapper->expects($this->once()) ->method('updateActivity') @@ -197,7 +190,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->assertEquals($this->time, $tk->getLastActivity()); } - public function testUpdateTokenDebounce() { + public function testUpdateTokenDebounce(): void { $tk = new PublicKeyToken(); $this->config->method('getSystemValueInt') ->willReturnCallback(function ($value, $default) { @@ -212,7 +205,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->tokenProvider->updateTokenActivity($tk); } - public function testGetTokenByUser() { + public function testGetTokenByUser(): void { $this->mapper->expects($this->once()) ->method('getTokenByUser') ->with('uid') @@ -221,8 +214,8 @@ class PublicKeyTokenProviderTest extends TestCase { $this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid')); } - public function testGetPassword() { - $token = 'token'; + public function testGetPassword(): void { + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'passme'; @@ -239,8 +232,8 @@ class PublicKeyTokenProviderTest extends TestCase { } - public function testGetPasswordPasswordLessToken() { - $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class); + public function testGetPasswordPasswordLessToken(): void { + $this->expectException(PasswordlessTokenException::class); $token = 'token1234'; $tk = new PublicKeyToken(); @@ -250,10 +243,10 @@ class PublicKeyTokenProviderTest extends TestCase { } - public function testGetPasswordInvalidToken() { - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); + public function testGetPasswordInvalidToken(): void { + $this->expectException(InvalidTokenException::class); - $token = 'token'; + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'passme'; @@ -269,8 +262,8 @@ class PublicKeyTokenProviderTest extends TestCase { $this->tokenProvider->getPassword($actual, 'wrongtoken'); } - public function testSetPassword() { - $token = 'token'; + public function testSetPassword(): void { + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'passme'; @@ -291,18 +284,18 @@ class PublicKeyTokenProviderTest extends TestCase { $this->mapper->expects($this->once()) ->method('update') ->with($this->callback(function ($token) use ($newpass) { - return $newpass === $this->tokenProvider->getPassword($token, 'token'); + return $newpass === $this->tokenProvider->getPassword($token, 'tokentokentokentokentoken'); })); $this->tokenProvider->setPassword($actual, $token, $newpass); - $this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'token')); + $this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'tokentokentokentokentoken')); } - public function testSetPasswordInvalidToken() { - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); + public function testSetPasswordInvalidToken(): void { + $this->expectException(InvalidTokenException::class); $token = $this->createMock(IToken::class); $tokenId = 'token123'; @@ -311,48 +304,71 @@ class PublicKeyTokenProviderTest extends TestCase { $this->tokenProvider->setPassword($token, $tokenId, $password); } - public function testInvalidateToken() { - $this->mapper->expects($this->at(0)) - ->method('invalidate') - ->with(hash('sha512', 'token7'.'1f4h9s')); - $this->mapper->expects($this->at(1)) + public function testInvalidateToken(): void { + $calls = [ + [hash('sha512', 'token7' . '1f4h9s')], + [hash('sha512', 'token7')] + ]; + + $this->mapper->expects($this->exactly(2)) ->method('invalidate') - ->with(hash('sha512', 'token7')); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->tokenProvider->invalidateToken('token7'); } - public function testInvaildateTokenById() { + public function testInvalidateTokenById(): void { $id = 123; $this->mapper->expects($this->once()) - ->method('deleteById') - ->with('uid', $id); + ->method('getTokenById') + ->with($id); $this->tokenProvider->invalidateTokenById('uid', $id); } - public function testInvalidateOldTokens() { + public function testInvalidateOldTokens(): void { $defaultSessionLifetime = 60 * 60 * 24; $defaultRememberMeLifetime = 60 * 60 * 24 * 15; - $this->config->expects($this->exactly(2)) - ->method('getSystemValue') + $wipeTokenLifetime = 60 * 60 * 24 * 60; + $this->config->expects($this->exactly(4)) + ->method('getSystemValueInt') ->willReturnMap([ ['session_lifetime', $defaultSessionLifetime, 150], ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300], + ['token_auth_wipe_token_retention', $wipeTokenLifetime, 500], + ['token_auth_token_retention', 60 * 60 * 24 * 365, 800], ]); - $this->mapper->expects($this->exactly(2)) + + $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], - [$this->time - 300] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->tokenProvider->invalidateOldTokens(); } - public function testRenewSessionTokenWithoutPassword() { - $token = 'oldId'; + public function testInvalidateLastUsedBefore(): void { + $this->mapper->expects($this->once()) + ->method('invalidateLastUsedBefore') + ->with('user', 946684800); + + $this->tokenProvider->invalidateLastUsedBefore('user', 946684800); + } + + public function testRenewSessionTokenWithoutPassword(): void { + $token = 'oldIdtokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = null; @@ -364,18 +380,18 @@ class PublicKeyTokenProviderTest extends TestCase { $this->mapper ->expects($this->once()) ->method('getToken') - ->with(hash('sha512', 'oldId' . '1f4h9s')) + ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) ->willReturn($oldToken); $this->mapper ->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()) @@ -384,11 +400,11 @@ class PublicKeyTokenProviderTest extends TestCase { return $token === $oldToken; })); - $this->tokenProvider->renewSessionToken('oldId', 'newId'); + $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken'); } public function testRenewSessionTokenWithPassword(): void { - $token = 'oldId'; + $token = 'oldIdtokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'password'; @@ -404,19 +420,19 @@ class PublicKeyTokenProviderTest extends TestCase { $this->mapper ->expects($this->once()) ->method('getToken') - ->with(hash('sha512', 'oldId' . '1f4h9s')) + ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) ->willReturn($oldToken); $this->mapper ->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, 'newId') === '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()) @@ -425,7 +441,7 @@ class PublicKeyTokenProviderTest extends TestCase { return $token === $oldToken; })); - $this->tokenProvider->renewSessionToken('oldId', 'newId'); + $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken'); } public function testGetToken(): void { @@ -438,37 +454,33 @@ class PublicKeyTokenProviderTest extends TestCase { $this->mapper->method('getToken') ->with( $this->callback(function (string $token) { - return hash('sha512', 'unhashedToken'.'1f4h9s') === $token; + return hash('sha512', 'unhashedTokentokentokentokentoken' . '1f4h9s') === $token; }) )->willReturn($token); - $this->assertSame($token, $this->tokenProvider->getToken('unhashedToken')); + $this->assertSame($token, $this->tokenProvider->getToken('unhashedTokentokentokentokentoken')); } - public function testGetInvalidToken() { + public function testGetInvalidToken(): void { $this->expectException(InvalidTokenException::class); - $this->mapper->expects($this->at(0)) - ->method('getToken') - ->with( - $this->callback(function (string $token): bool { - return hash('sha512', 'unhashedToken'.'1f4h9s') === $token; - }) - )->willThrowException(new DoesNotExistException('nope')); - - $this->mapper->expects($this->at(1)) + $calls = [ + 'unhashedTokentokentokentokentoken' . '1f4h9s', + 'unhashedTokentokentokentokentoken', + ]; + $this->mapper->expects($this->exactly(2)) ->method('getToken') - ->with( - $this->callback(function (string $token): bool { - return hash('sha512', 'unhashedToken') === $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('unhashedToken'); + $this->tokenProvider->getToken('unhashedTokentokentokentokentoken'); } - public function testGetExpiredToken() { - $token = 'token'; + public function testGetExpiredToken(): void { + $token = 'tokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'passme'; @@ -481,19 +493,19 @@ class PublicKeyTokenProviderTest extends TestCase { $this->mapper->method('getToken') ->with( $this->callback(function (string $token) { - return hash('sha512', 'token'.'1f4h9s') === $token; + return hash('sha512', 'tokentokentokentokentoken' . '1f4h9s') === $token; }) )->willReturn($actual); try { - $this->tokenProvider->getToken('token'); + $this->tokenProvider->getToken('tokentokentokentokentoken'); $this->fail(); } catch (ExpiredTokenException $e) { $this->assertSame($actual, $e->getToken()); } } - public function testGetTokenById() { + public function testGetTokenById(): void { $token = $this->createMock(PublicKeyToken::class); $this->mapper->expects($this->once()) @@ -504,7 +516,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->assertSame($token, $this->tokenProvider->getTokenById(42)); } - public function testGetInvalidTokenById() { + public function testGetInvalidTokenById(): void { $this->expectException(InvalidTokenException::class); $this->mapper->expects($this->once()) @@ -515,7 +527,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->tokenProvider->getTokenById(42); } - public function testGetExpiredTokenById() { + public function testGetExpiredTokenById(): void { $token = new PublicKeyToken(); $token->setExpires(42); @@ -532,8 +544,8 @@ class PublicKeyTokenProviderTest extends TestCase { } } - public function testRotate() { - $token = 'oldtoken'; + public function testRotate(): void { + $token = 'oldtokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = 'password'; @@ -546,13 +558,13 @@ class PublicKeyTokenProviderTest extends TestCase { ]); $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); - $new = $this->tokenProvider->rotate($actual, 'oldtoken', 'newtoken'); + $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken'); - $this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtoken')); + $this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtokentokentokentokentoken')); } - public function testRotateNoPassword() { - $token = 'oldtoken'; + public function testRotateNoPassword(): void { + $token = 'oldtokentokentokentokentoken'; $uid = 'user'; $user = 'User'; $password = null; @@ -563,7 +575,7 @@ class PublicKeyTokenProviderTest extends TestCase { $oldPrivate = $actual->getPrivateKey(); - $new = $this->tokenProvider->rotate($actual, 'oldtoken', 'newtoken'); + $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken'); $newPrivate = $new->getPrivateKey(); @@ -571,7 +583,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->assertNull($new->getPassword()); } - public function testMarkPasswordInvalidInvalidToken() { + public function testMarkPasswordInvalidInvalidToken(): void { $token = $this->createMock(IToken::class); $this->expectException(InvalidTokenException::class); @@ -579,7 +591,7 @@ class PublicKeyTokenProviderTest extends TestCase { $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); } - public function testMarkPasswordInvalid() { + public function testMarkPasswordInvalid(): void { $token = $this->createMock(PublicKeyToken::class); $token->expects($this->once()) @@ -592,10 +604,10 @@ class PublicKeyTokenProviderTest extends TestCase { $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); } - public function testUpdatePasswords() { + public function testUpdatePasswords(): void { $uid = 'myUID'; $token1 = $this->tokenProvider->generateToken( - 'foo', + 'foobetokentokentokentoken', $uid, $uid, 'bar', @@ -603,7 +615,7 @@ class PublicKeyTokenProviderTest extends TestCase { IToken::PERMANENT_TOKEN, IToken::REMEMBER); $token2 = $this->tokenProvider->generateToken( - 'foobar', + 'foobartokentokentokentoken', $uid, $uid, 'bar', diff --git a/tests/lib/Authentication/Token/PublicKeyTokenTest.php b/tests/lib/Authentication/Token/PublicKeyTokenTest.php index 35c2e0ece39..5f5f29c865f 100644 --- a/tests/lib/Authentication/Token/PublicKeyTokenTest.php +++ b/tests/lib/Authentication/Token/PublicKeyTokenTest.php @@ -2,43 +2,27 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Token; use OC\Authentication\Token\PublicKeyToken; +use OCP\Authentication\Token\IToken; use Test\TestCase; class PublicKeyTokenTest extends TestCase { - public function testSetScopeAsArray() { - $scope = ['filesystem' => false]; + public function testSetScopeAsArray(): void { + $scope = [IToken::SCOPE_FILESYSTEM => false]; $token = new PublicKeyToken(); $token->setScope($scope); $this->assertEquals(json_encode($scope), $token->getScope()); $this->assertEquals($scope, $token->getScopeAsArray()); } - public function testDefaultScope() { - $scope = ['filesystem' => true]; + public function testDefaultScope(): void { + $scope = [IToken::SCOPE_FILESYSTEM => true]; $token = new PublicKeyToken(); $this->assertEquals($scope, $token->getScopeAsArray()); } diff --git a/tests/lib/Authentication/Token/RemoteWipeTest.php b/tests/lib/Authentication/Token/RemoteWipeTest.php index 843dd4aec5c..ca09767c759 100644 --- a/tests/lib/Authentication/Token/RemoteWipeTest.php +++ b/tests/lib/Authentication/Token/RemoteWipeTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Token; @@ -122,7 +106,7 @@ class RemoteWipeTest extends TestCase { $this->assertTrue($result); } - public function testStartWipingNotAWipeToken() { + public function testStartWipingNotAWipeToken(): void { $token = $this->createMock(IToken::class); $this->tokenProvider->expects($this->once()) ->method('getToken') @@ -136,7 +120,7 @@ class RemoteWipeTest extends TestCase { $this->assertFalse($result); } - public function testStartWiping() { + public function testStartWiping(): void { $token = $this->createMock(IToken::class); $this->tokenProvider->expects($this->once()) ->method('getToken') @@ -153,7 +137,7 @@ class RemoteWipeTest extends TestCase { $this->assertTrue($result); } - public function testFinishWipingNotAWipeToken() { + public function testFinishWipingNotAWipeToken(): void { $token = $this->createMock(IToken::class); $this->tokenProvider->expects($this->once()) ->method('getToken') diff --git a/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php b/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php index 4f1b8d25b58..b59ef876ffd 100644 --- a/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDaoTest.php @@ -3,32 +3,15 @@ declare(strict_types=1); /** - * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\TwoFactorAuth\Db; -use OC; use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao; use OCP\IDBConnection; +use OCP\Server; use Test\TestCase; /** @@ -44,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(); @@ -52,7 +35,7 @@ class ProviderUserAssignmentDaoTest extends TestCase { $this->dao = new ProviderUserAssignmentDao($this->dbConn); } - public function testGetState() { + public function testGetState(): void { $qb = $this->dbConn->getQueryBuilder(); $q1 = $qb->insert(ProviderUserAssignmentDao::TABLE_NAME)->values([ 'provider_id' => $qb->createNamedParameter('twofactor_u2f'), @@ -76,7 +59,7 @@ class ProviderUserAssignmentDaoTest extends TestCase { $this->assertEquals($expected, $state); } - public function testPersist() { + public function testPersist(): void { $qb = $this->dbConn->getQueryBuilder(); $this->dao->persist('twofactor_totp', 'user123', 0); @@ -93,7 +76,7 @@ class ProviderUserAssignmentDaoTest extends TestCase { $this->assertCount(1, $data); } - public function testPersistTwice() { + public function testPersistTwice(): void { $qb = $this->dbConn->getQueryBuilder(); $this->dao->persist('twofactor_totp', 'user123', 0); @@ -112,7 +95,7 @@ class ProviderUserAssignmentDaoTest extends TestCase { $this->assertCount(1, $data); } - public function testPersistSameStateTwice() { + public function testPersistSameStateTwice(): void { $qb = $this->dbConn->getQueryBuilder(); $this->dao->persist('twofactor_totp', 'user123', 1); @@ -131,7 +114,7 @@ class ProviderUserAssignmentDaoTest extends TestCase { $this->assertCount(1, $data); } - public function testDeleteByUser() { + public function testDeleteByUser(): void { $this->dao->persist('twofactor_fail', 'user1', 1); $this->dao->persist('twofactor_u2f', 'user1', 1); $this->dao->persist('twofactor_fail', 'user2', 0); @@ -160,7 +143,7 @@ class ProviderUserAssignmentDaoTest extends TestCase { $this->assertCount(2, $statesUser2); } - public function testDeleteAll() { + public function testDeleteAll(): void { $this->dao->persist('twofactor_fail', 'user1', 1); $this->dao->persist('twofactor_u2f', 'user1', 1); $this->dao->persist('twofactor_fail', 'user2', 0); diff --git a/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php b/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php index f8c4be09a6f..f1d38c10801 100644 --- a/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/EnforcementStateTest.php @@ -1,23 +1,8 @@ <?php + /** - * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ /** @@ -33,25 +18,25 @@ use OC\Authentication\TwoFactorAuth\EnforcementState; use Test\TestCase; class EnforcementStateTest extends TestCase { - public function testIsEnforced() { + public function testIsEnforced(): void { $state = new EnforcementState(true); $this->assertTrue($state->isEnforced()); } - public function testGetEnforcedGroups() { + public function testGetEnforcedGroups(): void { $state = new EnforcementState(true, ['twofactorers']); $this->assertEquals(['twofactorers'], $state->getEnforcedGroups()); } - public function testGetExcludedGroups() { + public function testGetExcludedGroups(): void { $state = new EnforcementState(true, [], ['yoloers']); $this->assertEquals(['yoloers'], $state->getExcludedGroups()); } - public function testJsonSerialize() { + public function testJsonSerialize(): void { $state = new EnforcementState(true, ['twofactorers'], ['yoloers']); $expected = [ 'enforced' => true, diff --git a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php index da11b11e537..a2bed8a3652 100644 --- a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php @@ -1,34 +1,22 @@ <?php /** - * @author Christoph Wurst <christoph@owncloud.com> - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ 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; use OCP\Activity\IEvent; use OCP\Activity\IManager; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin; use OCP\Authentication\TwoFactorAuth\IProvider; @@ -39,9 +27,8 @@ use OCP\ISession; use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; -use function reset; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; +use function reset; class ManagerTest extends TestCase { /** @var IUser|MockObject */ @@ -84,10 +71,7 @@ class ManagerTest extends TestCase { private $timeFactory; /** @var IEventDispatcher|MockObject */ - private $newDispatcher; - - /** @var EventDispatcherInterface|MockObject */ - private $eventDispatcher; + private $dispatcher; protected function setUp(): void { parent::setUp(); @@ -102,8 +86,7 @@ class ManagerTest extends TestCase { $this->logger = $this->createMock(LoggerInterface::class); $this->tokenProvider = $this->createMock(TokenProvider::class); $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->newDispatcher = $this->createMock(IEventDispatcher::class); - $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->manager = new Manager( $this->providerLoader, @@ -115,8 +98,7 @@ class ManagerTest extends TestCase { $this->logger, $this->tokenProvider, $this->timeFactory, - $this->newDispatcher, - $this->eventDispatcher + $this->dispatcher, ); $this->fakeProvider = $this->createMock(IProvider::class); @@ -155,7 +137,7 @@ class ManagerTest extends TestCase { ]); } - public function testIsTwoFactorAuthenticatedEnforced() { + public function testIsTwoFactorAuthenticatedEnforced(): void { $this->mandatoryTwoFactor->expects($this->once()) ->method('isEnforcedFor') ->with($this->user) @@ -166,7 +148,7 @@ class ManagerTest extends TestCase { $this->assertTrue($enabled); } - public function testIsTwoFactorAuthenticatedNoProviders() { + public function testIsTwoFactorAuthenticatedNoProviders(): void { $this->mandatoryTwoFactor->expects($this->once()) ->method('isEnforcedFor') ->with($this->user) @@ -181,7 +163,7 @@ class ManagerTest extends TestCase { $this->assertFalse($this->manager->isTwoFactorAuthenticated($this->user)); } - public function testIsTwoFactorAuthenticatedOnlyBackupCodes() { + public function testIsTwoFactorAuthenticatedOnlyBackupCodes(): void { $this->mandatoryTwoFactor->expects($this->once()) ->method('isEnforcedFor') ->with($this->user) @@ -204,7 +186,7 @@ class ManagerTest extends TestCase { $this->assertFalse($this->manager->isTwoFactorAuthenticated($this->user)); } - public function testIsTwoFactorAuthenticatedFailingProviders() { + public function testIsTwoFactorAuthenticatedFailingProviders(): void { $this->mandatoryTwoFactor->expects($this->once()) ->method('isEnforcedFor') ->with($this->user) @@ -223,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], @@ -236,10 +218,9 @@ class ManagerTest extends TestCase { * enabled providers. * * If any of these providers is active, 2FA is enabled - * - * @dataProvider providerStatesFixData */ - public function testIsTwoFactorAuthenticatedFixesProviderStates(bool $providerEnabled, bool $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('providerStatesFixData')] + public function testIsTwoFactorAuthenticatedFixesProviderStates(bool $providerEnabled, bool $expected): void { $this->providerRegistry->expects($this->once()) ->method('getProviderStates') ->willReturn([]); // Nothing registered yet @@ -271,7 +252,7 @@ class ManagerTest extends TestCase { $this->assertEquals($expected, $this->manager->isTwoFactorAuthenticated($this->user)); } - public function testGetProvider() { + public function testGetProvider(): void { $this->providerRegistry->expects($this->once()) ->method('getProviderStates') ->with($this->user) @@ -288,7 +269,7 @@ class ManagerTest extends TestCase { $this->assertSame($this->fakeProvider, $provider); } - public function testGetInvalidProvider() { + public function testGetInvalidProvider(): void { $this->providerRegistry->expects($this->once()) ->method('getProviderStates') ->with($this->user) @@ -303,7 +284,7 @@ class ManagerTest extends TestCase { $this->assertNull($provider); } - public function testGetLoginSetupProviders() { + public function testGetLoginSetupProviders(): void { $provider1 = $this->createMock(IProvider::class); $provider2 = $this->createMock(IActivatableAtLogin::class); $this->providerLoader->expects($this->once()) @@ -320,7 +301,7 @@ class ManagerTest extends TestCase { $this->assertSame($provider2, reset($providers)); } - public function testGetProviders() { + public function testGetProviders(): void { $this->providerRegistry->expects($this->once()) ->method('getProviderStates') ->with($this->user) @@ -342,7 +323,7 @@ class ManagerTest extends TestCase { $this->assertFalse($providerSet->isProviderMissing()); } - public function testGetProvidersOneMissing() { + public function testGetProvidersOneMissing(): void { $this->providerRegistry->expects($this->once()) ->method('getProviderStates') ->with($this->user) @@ -362,7 +343,7 @@ class ManagerTest extends TestCase { $this->assertTrue($providerSet->isProviderMissing()); } - public function testVerifyChallenge() { + public function testVerifyChallenge(): void { $this->prepareProviders(); $challenge = 'passme'; @@ -375,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'); @@ -417,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); @@ -432,7 +419,7 @@ class ManagerTest extends TestCase { $this->assertTrue($result); } - public function testVerifyChallengeInvalidProviderId() { + public function testVerifyChallengeInvalidProviderId(): void { $this->prepareProviders(); $challenge = 'passme'; @@ -445,7 +432,7 @@ class ManagerTest extends TestCase { $this->assertFalse($this->manager->verifyChallenge('dontexist', $this->user, $challenge)); } - public function testVerifyInvalidChallenge() { + public function testVerifyInvalidChallenge(): void { $this->prepareProviders(); $challenge = 'dontpassme'; @@ -491,20 +478,25 @@ class ManagerTest extends TestCase { $this->assertFalse($this->manager->verifyChallenge('email', $this->user, $challenge)); } - public function testNeedsSecondFactor() { + 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); @@ -530,10 +522,9 @@ class ManagerTest extends TestCase { $this->logger, $this->tokenProvider, $this->timeFactory, - $this->newDispatcher, - $this->eventDispatcher + $this->dispatcher, ]) - ->setMethods(['loadTwoFactorApp', 'isTwoFactorAuthenticated'])// Do not actually load the apps + ->onlyMethods(['isTwoFactorAuthenticated'])// Do not actually load the apps ->getMock(); $manager->method('isTwoFactorAuthenticated') @@ -543,7 +534,7 @@ class ManagerTest extends TestCase { $this->assertTrue($manager->needsSecondFactor($user)); } - public function testNeedsSecondFactorUserIsNull() { + public function testNeedsSecondFactorUserIsNull(): void { $user = null; $this->session->expects($this->never()) ->method('exists'); @@ -551,7 +542,7 @@ class ManagerTest extends TestCase { $this->assertFalse($this->manager->needsSecondFactor($user)); } - public function testNeedsSecondFactorWithNoProviderAvailableAnymore() { + public function testNeedsSecondFactorWithNoProviderAvailableAnymore(): void { $this->prepareNoProviders(); $user = null; @@ -566,20 +557,24 @@ class ManagerTest extends TestCase { $this->assertFalse($this->manager->needsSecondFactor($user)); } - public function testPrepareTwoFactorLogin() { + public function testPrepareTwoFactorLogin(): void { $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); @@ -596,20 +591,24 @@ class ManagerTest extends TestCase { $this->manager->prepareTwoFactorLogin($this->user, true); } - public function testPrepareTwoFactorLoginDontRemember() { + public function testPrepareTwoFactorLoginDontRemember(): void { $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); @@ -625,7 +624,7 @@ class ManagerTest extends TestCase { $this->manager->prepareTwoFactorLogin($this->user, false); } - public function testNeedsSecondFactorSessionAuth() { + public function testNeedsSecondFactorSessionAuth(): void { $user = $this->createMock(IUser::class); $user->method('getUID') ->willReturn('user'); @@ -636,18 +635,31 @@ class ManagerTest extends TestCase { return false; } elseif ($var === 'app_password') { return false; + } elseif ($var === 'app_api') { + return false; } return true; }); + $this->session->method('get') + ->willReturnCallback(function ($var) { + if ($var === Manager::SESSION_UID_KEY) { + return 'user'; + } elseif ($var === 'app_api') { + return true; + } + return null; + }); $this->session->expects($this->once()) ->method('get') - ->with(Manager::SESSION_UID_DONE) - ->willReturn('user'); + ->willReturnMap([ + [Manager::SESSION_UID_DONE, 'user'], + ['app_api', true] + ]); $this->assertFalse($this->manager->needsSecondFactor($user)); } - public function testNeedsSecondFactorSessionAuthFailDBPass() { + public function testNeedsSecondFactorSessionAuthFailDBPass(): void { $user = $this->createMock(IUser::class); $user->method('getUID') ->willReturn('user'); @@ -657,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); @@ -678,7 +690,7 @@ class ManagerTest extends TestCase { $this->assertFalse($this->manager->needsSecondFactor($user)); } - public function testNeedsSecondFactorInvalidToken() { + public function testNeedsSecondFactorInvalidToken(): void { $this->prepareNoProviders(); $user = $this->createMock(IUser::class); @@ -692,19 +704,92 @@ class ManagerTest extends TestCase { $this->tokenProvider->method('getToken') ->with('mysessionid') - ->willThrowException(new OC\Authentication\Exceptions\InvalidTokenException()); + ->willThrowException(new InvalidTokenException()); $this->config->method('getUserKeys')->willReturn([]); $this->assertFalse($this->manager->needsSecondFactor($user)); } - public function testNeedsSecondFactorAppPassword() { + public function testNeedsSecondFactorAppPassword(): void { $user = $this->createMock(IUser::class); $this->session->method('exists') - ->with('app_password') - ->willReturn(true); + ->willReturnMap([ + ['app_password', true], + ['app_api', true] + ]); $this->assertFalse($this->manager->needsSecondFactor($user)); } + + public function testClearTwoFactorPending() { + $this->config->method('getUserKeys') + ->with('theUserId', 'login_token_2fa') + ->willReturn([ + '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') + ->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') + ->willReturnCallback(function () use (&$invalidateCalls): void { + $expected = array_shift($invalidateCalls); + $this->assertEquals($expected, func_get_args()); + }); + + $this->manager->clearTwoFactorPending('theUserId'); + } + + public function testClearTwoFactorPendingTokenDoesNotExist() { + $this->config->method('getUserKeys') + ->with('theUserId', 'login_token_2fa') + ->willReturn([ + '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') + ->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') + ->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'); + } + }); + + $this->manager->clearTwoFactorPending('theUserId'); + } } diff --git a/tests/lib/Authentication/TwoFactorAuth/MandatoryTwoFactorTest.php b/tests/lib/Authentication/TwoFactorAuth/MandatoryTwoFactorTest.php index 0119c977696..d2ecd3c509c 100644 --- a/tests/lib/Authentication/TwoFactorAuth/MandatoryTwoFactorTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/MandatoryTwoFactorTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Tests\Authentication\TwoFactorAuth; @@ -53,7 +36,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->mandatoryTwoFactor = new MandatoryTwoFactor($this->config, $this->groupManager); } - public function testIsNotEnforced() { + public function testIsNotEnforced(): void { $this->config ->method('getSystemValue') ->willReturnMap([ @@ -67,7 +50,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->assertFalse($state->isEnforced()); } - public function testIsEnforced() { + public function testIsEnforced(): void { $this->config ->method('getSystemValue') ->willReturnMap([ @@ -81,7 +64,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->assertTrue($state->isEnforced()); } - public function testIsNotEnforcedForAnybody() { + public function testIsNotEnforcedForAnybody(): void { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('user123'); $this->config @@ -97,7 +80,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->assertFalse($isEnforced); } - public function testIsEnforcedForAGroupMember() { + public function testIsEnforcedForAGroupMember(): void { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('user123'); $this->config @@ -117,7 +100,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->assertTrue($isEnforced); } - public function testIsEnforcedForOtherGroups() { + public function testIsEnforcedForOtherGroups(): void { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('user123'); $this->config @@ -135,7 +118,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->assertFalse($isEnforced); } - public function testIsEnforcedButMemberOfExcludedGroup() { + public function testIsEnforcedButMemberOfExcludedGroup(): void { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('user123'); $this->config @@ -155,7 +138,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->assertFalse($isEnforced); } - public function testSetEnforced() { + public function testSetEnforced(): void { $this->config ->expects($this->exactly(3)) ->method('setSystemValue') @@ -168,7 +151,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->mandatoryTwoFactor->setState(new EnforcementState(true)); } - public function testSetEnforcedForGroups() { + public function testSetEnforcedForGroups(): void { $this->config ->expects($this->exactly(3)) ->method('setSystemValue') @@ -181,7 +164,7 @@ class MandatoryTwoFactorTest extends TestCase { $this->mandatoryTwoFactor->setState(new EnforcementState(true, ['twofactorers'], ['yoloers'])); } - public function testSetNotEnforced() { + public function testSetNotEnforced(): void { $this->config ->expects($this->exactly(3)) ->method('setSystemValue') diff --git a/tests/lib/Authentication/TwoFactorAuth/ProviderLoaderTest.php b/tests/lib/Authentication/TwoFactorAuth/ProviderLoaderTest.php index b71339dd0ed..6eb3b7dfb26 100644 --- a/tests/lib/Authentication/TwoFactorAuth/ProviderLoaderTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/ProviderLoaderTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace lib\Authentication\TwoFactorAuth; @@ -64,7 +47,7 @@ class ProviderLoaderTest extends TestCase { } - public function testFailHardIfProviderCanNotBeLoaded() { + public function testFailHardIfProviderCanNotBeLoaded(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Could not load two-factor auth provider \\OCA\\MyFaulty2faApp\\DoesNotExist'); @@ -86,7 +69,7 @@ class ProviderLoaderTest extends TestCase { $this->loader->getProviders($this->user); } - public function testGetProviders() { + public function testGetProviders(): void { $provider = $this->createMock(IProvider::class); $provider->method('getId')->willReturn('test'); \OC::$server->registerService('\\OCA\\TwoFactorTest\\Provider', function () use ($provider) { @@ -110,7 +93,7 @@ class ProviderLoaderTest extends TestCase { $this->assertSame($provider, $providers['test']); } - public function testGetProvidersBootstrap() { + public function testGetProvidersBootstrap(): void { $provider = $this->createMock(IProvider::class); $provider->method('getId')->willReturn('test'); diff --git a/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php index 65447e1d8d6..a1f2a6fa69a 100644 --- a/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/ProviderManagerTest.php @@ -3,29 +3,13 @@ declare(strict_types=1); /** - * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace lib\Authentication\TwoFactorAuth; +use OC\Authentication\Exceptions\InvalidProviderException; use OC\Authentication\TwoFactorAuth\ProviderLoader; use OC\Authentication\TwoFactorAuth\ProviderManager; use OCP\Authentication\TwoFactorAuth\IActivatableByAdmin; @@ -58,15 +42,15 @@ class ProviderManagerTest extends TestCase { ); } - - public function testTryEnableInvalidProvider() { - $this->expectException(\OC\Authentication\Exceptions\InvalidProviderException::class); + + public function testTryEnableInvalidProvider(): void { + $this->expectException(InvalidProviderException::class); $user = $this->createMock(IUser::class); $this->providerManager->tryEnableProviderFor('none', $user); } - public function testTryEnableUnsupportedProvider() { + public function testTryEnableUnsupportedProvider(): void { $user = $this->createMock(IUser::class); $provider = $this->createMock(IProvider::class); $this->providerLoader->expects($this->once()) @@ -83,7 +67,7 @@ class ProviderManagerTest extends TestCase { $this->assertFalse($res); } - public function testTryEnableProvider() { + public function testTryEnableProvider(): void { $user = $this->createMock(IUser::class); $provider = $this->createMock(IActivatableByAdmin::class); $this->providerLoader->expects($this->once()) @@ -104,15 +88,15 @@ class ProviderManagerTest extends TestCase { $this->assertTrue($res); } - - public function testTryDisableInvalidProvider() { - $this->expectException(\OC\Authentication\Exceptions\InvalidProviderException::class); + + public function testTryDisableInvalidProvider(): void { + $this->expectException(InvalidProviderException::class); $user = $this->createMock(IUser::class); $this->providerManager->tryDisableProviderFor('none', $user); } - public function testTryDisableUnsupportedProvider() { + public function testTryDisableUnsupportedProvider(): void { $user = $this->createMock(IUser::class); $provider = $this->createMock(IProvider::class); $this->providerLoader->expects($this->once()) @@ -129,7 +113,7 @@ class ProviderManagerTest extends TestCase { $this->assertFalse($res); } - public function testTryDisableProvider() { + public function testTryDisableProvider(): void { $user = $this->createMock(IUser::class); $provider = $this->createMock(IDeactivatableByAdmin::class); $this->providerLoader->expects($this->once()) diff --git a/tests/lib/Authentication/TwoFactorAuth/ProviderSetTest.php b/tests/lib/Authentication/TwoFactorAuth/ProviderSetTest.php index f6afbf245ee..568b83567f8 100644 --- a/tests/lib/Authentication/TwoFactorAuth/ProviderSetTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/ProviderSetTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\TwoFactorAuth; @@ -35,7 +18,7 @@ class ProviderSetTest extends TestCase { /** @var ProviderSet */ private $providerSet; - public function testIndexesProviders() { + public function testIndexesProviders(): void { $p1 = $this->createMock(IProvider::class); $p1->method('getId')->willReturn('p1'); $p2 = $this->createMock(IProvider::class); @@ -50,7 +33,7 @@ class ProviderSetTest extends TestCase { $this->assertEquals($expected, $set->getProviders()); } - public function testGet3rdPartyProviders() { + public function testGet3rdPartyProviders(): void { $p1 = $this->createMock(IProvider::class); $p1->method('getId')->willReturn('p1'); $p2 = $this->createMock(IProvider::class); @@ -67,7 +50,7 @@ class ProviderSetTest extends TestCase { $this->assertEquals($expected, $set->getPrimaryProviders()); } - public function testGetProvider() { + public function testGetProvider(): void { $p1 = $this->createMock(IProvider::class); $p1->method('getId')->willReturn('p1'); @@ -77,14 +60,14 @@ class ProviderSetTest extends TestCase { $this->assertEquals($p1, $provider); } - public function testGetProviderNotFound() { + public function testGetProviderNotFound(): void { $set = new ProviderSet([], false); $provider = $set->getProvider('p1'); $this->assertNull($provider); } - public function testIsProviderMissing() { + public function testIsProviderMissing(): void { $set = new ProviderSet([], true); $this->assertTrue($set->isProviderMissing()); diff --git a/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php b/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php index b6e0caff427..2018dc1a634 100644 --- a/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php +++ b/tests/lib/Authentication/TwoFactorAuth/RegistryTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\TwoFactorAuth; @@ -32,6 +15,9 @@ use OCP\Authentication\TwoFactorAuth\IProvider; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\Authentication\TwoFactorAuth\TwoFactorProviderDisabled; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderUserDeleted; use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; @@ -56,7 +42,7 @@ class RegistryTest extends TestCase { $this->registry = new Registry($this->dao, $this->dispatcher); } - public function testGetProviderStates() { + public function testGetProviderStates(): void { $user = $this->createMock(IUser::class); $user->expects($this->once())->method('getUID')->willReturn('user123'); $state = [ @@ -69,7 +55,7 @@ class RegistryTest extends TestCase { $this->assertEquals($state, $actual); } - public function testEnableProvider() { + public function testEnableProvider(): void { $user = $this->createMock(IUser::class); $provider = $this->createMock(IProvider::class); $user->expects($this->once())->method('getUID')->willReturn('user123'); @@ -85,11 +71,17 @@ class RegistryTest extends TestCase { return $e->getUser() === $user && $e->getProvider() === $provider; }) ); + $this->dispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new TwoFactorProviderForUserRegistered( + $user, + $provider, + )); $this->registry->enableProviderFor($provider, $user); } - public function testDisableProvider() { + public function testDisableProvider(): void { $user = $this->createMock(IUser::class); $provider = $this->createMock(IProvider::class); $user->expects($this->once())->method('getUID')->willReturn('user123'); @@ -106,11 +98,17 @@ class RegistryTest extends TestCase { return $e->getUser() === $user && $e->getProvider() === $provider; }) ); + $this->dispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new TwoFactorProviderForUserUnregistered( + $user, + $provider, + )); $this->registry->disableProviderFor($provider, $user); } - public function testDeleteUserData() { + public function testDeleteUserData(): void { $user = $this->createMock(IUser::class); $user->expects($this->once())->method('getUID')->willReturn('user123'); $this->dao->expects($this->once()) @@ -121,14 +119,22 @@ class RegistryTest extends TestCase { 'provider_id' => 'twofactor_u2f', ] ]); - $this->dispatcher->expects($this->once()) + + $calls = [ + [new TwoFactorProviderDisabled('twofactor_u2f')], + [new TwoFactorProviderUserDeleted($user, 'twofactor_u2f')], + ]; + $this->dispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->with(new TwoFactorProviderDisabled('twofactor_u2f')); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->registry->deleteUserData($user); } - public function testCleanUp() { + public function testCleanUp(): void { $this->dao->expects($this->once()) ->method('deleteAll') ->with('twofactor_u2f'); |