diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-05-03 16:09:19 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-05-07 19:04:36 +0300 |
commit | 170582d4f5eef1cc53c043d67c5d9d07d097155c (patch) | |
tree | 640d7a0c0fbfdf1d9a567c7fa5ddbb3cdb4f5316 /tests/lib/Authentication | |
parent | 5893f218c247d4c0829fb74161e663f3903e15ad (diff) |
Add a login chain to reduce the complexity of LoginController::tryLogin
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests/lib/Authentication')
13 files changed, 1254 insertions, 0 deletions
diff --git a/tests/lib/Authentication/Login/ALoginCommandTest.php b/tests/lib/Authentication/Login/ALoginCommandTest.php new file mode 100644 index 00000000000..ec3b324b2ce --- /dev/null +++ b/tests/lib/Authentication/Login/ALoginCommandTest.php @@ -0,0 +1,122 @@ +<?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 lib\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 { + + /** @var IRequest|MockObject */ + protected $request; + + /** @var string */ + protected $username = 'user123'; + + /** @var string */ + protected $password = '123456'; + + /** @var string */ + protected $redirectUrl = '/apps/contacts'; + + /** @var string */ + protected $timezone = 'Europe/Vienna'; + + protected $timeZoneOffset = '2'; + + /** @var IUser|MockObject */ + protected $user; + + /** @var ALoginCommand */ + protected $cmd; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->user = $this->createMock(IUser::class); + } + + protected function getBasicLoginData(): LoginData { + return new LoginData( + $this->request, + $this->username, + $this->password + ); + } + + protected function getInvalidLoginData(): LoginData { + return new LoginData( + $this->request, + $this->username, + $this->password + ); + } + + protected function getFailedLoginData(): LoginData { + $data = new LoginData( + $this->request, + $this->username, + $this->password + ); + $data->setUser(false); + return $data; + } + + protected function getLoggedInLoginData(): LoginData { + $basic = $this->getBasicLoginData(); + $basic->setUser($this->user); + return $basic; + } + + protected function getLoggedInLoginDataWithRedirectUrl(): LoginData { + $data = new LoginData( + $this->request, + $this->username, + $this->password, + $this->redirectUrl + ); + $data->setUser($this->user); + return $data; + } + + protected function getLoggedInLoginDataWithTimezone(): LoginData { + $data = new LoginData( + $this->request, + $this->username, + $this->password, + null, + $this->timezone, + $this->timeZoneOffset + ); + $data->setUser($this->user); + return $data; + } + +} diff --git a/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php b/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php new file mode 100644 index 00000000000..05078cde930 --- /dev/null +++ b/tests/lib/Authentication/Login/ClearLostPasswordTokensCommandTest.php @@ -0,0 +1,67 @@ +<?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 lib\Authentication\Login\ALoginCommandTest; +use OC\Authentication\Login\ClearLostPasswordTokensCommand; +use OC\Authentication\Login\LoginData; +use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; + +class ClearLostPasswordTokensCommandTest extends ALoginCommandTest { + + /** @var IConfig|MockObject */ + private $config; + + protected function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + + $this->cmd = new ClearLostPasswordTokensCommand( + $this->config + ); + } + + public function testProcess() { + $data = $this->getLoggedInLoginData(); + $this->user->expects($this->once()) + ->method('getUID') + ->willReturn($this->username); + $this->config->expects($this->once()) + ->method('deleteUserValue') + ->with( + $this->username, + 'core', + 'lostpassword' + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + +} diff --git a/tests/lib/Authentication/Login/CompleteLoginCommandTest.php b/tests/lib/Authentication/Login/CompleteLoginCommandTest.php new file mode 100644 index 00000000000..8912dcab09c --- /dev/null +++ b/tests/lib/Authentication/Login/CompleteLoginCommandTest.php @@ -0,0 +1,67 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\CompleteLoginCommand; +use OC\User\Session; +use PHPUnit\Framework\MockObject\MockObject; + +class CompleteLoginCommandTest extends ALoginCommandTest { + + /** @var Session|MockObject */ + private $session; + + protected function setUp() { + parent::setUp(); + + $this->session = $this->createMock(Session::class); + + $this->cmd = new CompleteLoginCommand( + $this->session + ); + } + + public function testProcess() { + $data = $this->getLoggedInLoginData(); + $this->session->expects($this->once()) + ->method('completeLogin') + ->with( + $this->user, + $this->equalTo( + [ + 'loginName' => $this->username, + 'password' => $this->password, + ] + ) + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + +}
\ No newline at end of file diff --git a/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php b/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php new file mode 100644 index 00000000000..136906f42b0 --- /dev/null +++ b/tests/lib/Authentication/Login/CreateSessionTokenCommandTest.php @@ -0,0 +1,121 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\CreateSessionTokenCommand; +use OC\Authentication\Token\IToken; +use OC\User\Session; +use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; + +class CreateSessionTokenCommandTest extends ALoginCommandTest { + + /** @var IConfig|MockObject */ + private $config; + + /** @var Session|MockObject */ + private $userSession; + + protected function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->userSession = $this->createMock(Session::class); + + $this->cmd = new CreateSessionTokenCommand( + $this->config, + $this->userSession + ); + } + + public function testProcess() { + $data = $this->getLoggedInLoginData(); + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with( + 'remember_login_cookie_lifetime', + 60 * 60 * 24 * 15 + ) + ->willReturn(100); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn($this->username); + $this->userSession->expects($this->once()) + ->method('createSessionToken') + ->with( + $this->request, + $this->username, + $this->username, + $this->password, + IToken::REMEMBER + ); + $this->userSession->expects($this->once()) + ->method('updateTokens') + ->with( + $this->username, + $this->username + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + public function testProcessDoNotRemember() { + $data = $this->getLoggedInLoginData(); + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with( + 'remember_login_cookie_lifetime', + 60 * 60 * 24 * 15 + ) + ->willReturn(0); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn($this->username); + $this->userSession->expects($this->once()) + ->method('createSessionToken') + ->with( + $this->request, + $this->username, + $this->username, + $this->password, + IToken::DO_NOT_REMEMBER + ); + $this->userSession->expects($this->once()) + ->method('updateTokens') + ->with( + $this->username, + $this->username + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + $this->assertFalse($data->isRememberLogin()); + } + +} diff --git a/tests/lib/Authentication/Login/EmailLoginCommandTest.php b/tests/lib/Authentication/Login/EmailLoginCommandTest.php new file mode 100644 index 00000000000..c72048d8877 --- /dev/null +++ b/tests/lib/Authentication/Login/EmailLoginCommandTest.php @@ -0,0 +1,165 @@ +<?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 lib\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() { + 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->once()) + ->method('getByEmail') + ->with($this->username) + ->willReturn([]); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + public function testProcessDuplicateEmailLogin() { + $data = $this->getFailedLoginData(); + $this->userManager->expects($this->once()) + ->method('getByEmail') + ->with($this->username) + ->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 new file mode 100644 index 00000000000..33926aeea0e --- /dev/null +++ b/tests/lib/Authentication/Login/FinishRememberedLoginCommandTest.php @@ -0,0 +1,69 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\FinishRememberedLoginCommand; +use OC\User\Session; +use PHPUnit\Framework\MockObject\MockObject; + +class FinishRememberedLoginCommandTest extends ALoginCommandTest { + + /** @var Session|MockObject */ + private $userSession; + + protected function setUp() { + parent::setUp(); + + $this->userSession = $this->createMock(Session::class); + + $this->cmd = new FinishRememberedLoginCommand( + $this->userSession + ); + } + + public function testProcessNotRememberedLogin() { + $data = $this->getLoggedInLoginData(); + $data->setRememberLogin(false); + $this->userSession->expects($this->never()) + ->method('createRememberMeToken'); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + public function testProcess() { + $data = $this->getLoggedInLoginData(); + $this->userSession->expects($this->once()) + ->method('createRememberMeToken') + ->with($this->user); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + +} diff --git a/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php b/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php new file mode 100644 index 00000000000..16938307338 --- /dev/null +++ b/tests/lib/Authentication/Login/LoggedInCheckCommandTest.php @@ -0,0 +1,67 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\LoggedInCheckCommand; +use OC\Core\Controller\LoginController; +use OCP\ILogger; +use PHPUnit\Framework\MockObject\MockObject; + +class LoggedInCheckCommandTest extends ALoginCommandTest { + + /** @var ILogger|MockObject */ + private $logger; + + protected function setUp() { + parent::setUp(); + + $this->logger = $this->createMock(ILogger::class); + + $this->cmd = new LoggedInCheckCommand( + $this->logger + ); + } + + public function testProcessSuccessfulLogin() { + $data = $this->getLoggedInLoginData(); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + public function testProcessFailedLogin() { + $data = $this->getFailedLoginData(); + $this->logger->expects($this->once()) + ->method('warning'); + + $result = $this->cmd->process($data); + + $this->assertFalse($result->isSuccess()); + $this->assertSame(LoginController::LOGIN_MSG_INVALIDPASSWORD, $result->getErrorMessage()); + } + +} diff --git a/tests/lib/Authentication/Login/PreLoginHookCommandTest.php b/tests/lib/Authentication/Login/PreLoginHookCommandTest.php new file mode 100644 index 00000000000..3329f3ad82f --- /dev/null +++ b/tests/lib/Authentication/Login/PreLoginHookCommandTest.php @@ -0,0 +1,66 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\PreLoginHookCommand; +use OC\User\Manager; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; + +class PreLoginHookCommandTest extends ALoginCommandTest { + + /** @var IUserManager|MockObject */ + private $userManager; + + protected function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(Manager::class); + + $this->cmd = new PreLoginHookCommand( + $this->userManager + ); + } + + public function testProcess() { + $data = $this->getBasicLoginData(); + $this->userManager->expects($this->once()) + ->method('emit') + ->with( + '\OC\User', + 'preLogin', + [ + $this->username, + $this->password, + ] + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + +} diff --git a/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php new file mode 100644 index 00000000000..a2ce656a14b --- /dev/null +++ b/tests/lib/Authentication/Login/SetUserTimezoneCommandTest.php @@ -0,0 +1,90 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\SetUserTimezoneCommand; +use OCP\IConfig; +use OCP\ISession; +use PHPUnit\Framework\MockObject\MockObject; + +class SetUserTimezoneCommandTest extends ALoginCommandTest { + + /** @var IConfig|MockObject */ + private $config; + + /** @var ISession|MockObject */ + private $session; + + protected function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->session = $this->createMock(ISession::class); + + $this->cmd = new SetUserTimezoneCommand( + $this->config, + $this->session + ); + } + + public function testProcessNoTimezoneSet() { + $data = $this->getLoggedInLoginData(); + $this->config->expects($this->never()) + ->method('setUserValue'); + $this->session->expects($this->never()) + ->method('set'); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + public function testProcess() { + $data = $this->getLoggedInLoginDataWithTimezone(); + $this->user->expects($this->once()) + ->method('getUID') + ->willReturn($this->username); + $this->config->expects($this->once()) + ->method('setUserValue') + ->with( + $this->username, + 'core', + 'timezone', + $this->timezone + ); + $this->session->expects($this->once()) + ->method('set') + ->with( + 'timezone', + $this->timeZoneOffset + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + +} diff --git a/tests/lib/Authentication/Login/TwoFactorCommandTest.php b/tests/lib/Authentication/Login/TwoFactorCommandTest.php new file mode 100644 index 00000000000..a5c1c8e352b --- /dev/null +++ b/tests/lib/Authentication/Login/TwoFactorCommandTest.php @@ -0,0 +1,179 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\TwoFactorCommand; +use OC\Authentication\TwoFactorAuth\Manager; +use OC\Authentication\TwoFactorAuth\ProviderSet; +use OCP\Authentication\TwoFactorAuth\IProvider as ITwoFactorAuthProvider; +use OCP\IURLGenerator; +use PHPUnit\Framework\MockObject\MockObject; + +class TwoFactorCommandTest extends ALoginCommandTest { + + /** @var Manager|MockObject */ + private $twoFactorManager; + + /** @var IURLGenerator|MockObject */ + private $urlGenerator; + + protected function setUp() { + parent::setUp(); + + $this->twoFactorManager = $this->createMock(Manager::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + + $this->cmd = new TwoFactorCommand( + $this->twoFactorManager, + $this->urlGenerator + ); + } + + public function testNotTwoFactorAuthenticated() { + $data = $this->getLoggedInLoginData(); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->willReturn(false); + $this->twoFactorManager->expects($this->never()) + ->method('prepareTwoFactorLogin'); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + public function testProcessOneActiveProvider() { + $data = $this->getLoggedInLoginData(); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->willReturn(true); + $this->twoFactorManager->expects($this->once()) + ->method('prepareTwoFactorLogin') + ->with( + $this->user, + $data->isRememberLogin() + ); + $provider = $this->createMock(ITwoFactorAuthProvider::class); + $this->twoFactorManager->expects($this->once()) + ->method('getProviderSet') + ->willReturn(new ProviderSet([ + $provider, + ], false)); + $provider->expects($this->once()) + ->method('getId') + ->willReturn('test'); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with( + 'core.TwoFactorChallenge.showChallenge', + [ + 'challengeProviderId' => 'test' + ] + ) + ->willReturn('two/factor/url'); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + $this->assertEquals('two/factor/url', $result->getRedirectUrl()); + } + + public function testProcessTwoActiveProviders() { + $data = $this->getLoggedInLoginData(); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->willReturn(true); + $this->twoFactorManager->expects($this->once()) + ->method('prepareTwoFactorLogin') + ->with( + $this->user, + $data->isRememberLogin() + ); + $provider1 = $this->createMock(ITwoFactorAuthProvider::class); + $provider2 = $this->createMock(ITwoFactorAuthProvider::class); + $provider1->expects($this->once()) + ->method('getId') + ->willReturn('test1'); + $provider2->expects($this->once()) + ->method('getId') + ->willReturn('test2'); + $this->twoFactorManager->expects($this->once()) + ->method('getProviderSet') + ->willReturn(new ProviderSet([ + $provider1, + $provider2, + ], false)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with( + 'core.TwoFactorChallenge.selectChallenge' + ) + ->willReturn('two/factor/url'); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + $this->assertEquals('two/factor/url', $result->getRedirectUrl()); + } + + public function testProcessWithRedirectUrl() { + $data = $this->getLoggedInLoginDataWithRedirectUrl(); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->willReturn(true); + $this->twoFactorManager->expects($this->once()) + ->method('prepareTwoFactorLogin') + ->with( + $this->user, + $data->isRememberLogin() + ); + $provider = $this->createMock(ITwoFactorAuthProvider::class); + $this->twoFactorManager->expects($this->once()) + ->method('getProviderSet') + ->willReturn(new ProviderSet([ + $provider, + ], false)); + $provider->expects($this->once()) + ->method('getId') + ->willReturn('test'); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with( + 'core.TwoFactorChallenge.showChallenge', + [ + 'challengeProviderId' => 'test', + 'redirect_url' => $this->redirectUrl, + ] + ) + ->willReturn('two/factor/url'); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + $this->assertEquals('two/factor/url', $result->getRedirectUrl()); + } + +} diff --git a/tests/lib/Authentication/Login/UidLoginCommandTest.php b/tests/lib/Authentication/Login/UidLoginCommandTest.php new file mode 100644 index 00000000000..0a7889c227c --- /dev/null +++ b/tests/lib/Authentication/Login/UidLoginCommandTest.php @@ -0,0 +1,80 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\UidCheckCommand; +use OC\Authentication\Login\UidLoginCommand; +use OC\User\Manager; +use PHPUnit\Framework\MockObject\MockObject; + +class UidLoginCommandTest extends ALoginCommandTest { + + /** @var Manager|MockObject */ + private $userManager; + + protected function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(Manager::class); + + $this->cmd = new UidLoginCommand( + $this->userManager + ); + } + + public function testProcessFailingLogin() { + $data = $this->getBasicLoginData(); + $this->userManager->expects($this->once()) + ->method('checkPasswordNoLogging') + ->with( + $this->username, + $this->password + ) + ->willReturn(false); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + $this->assertFalse($data->getUser()); + } + + public function testProcess() { + $data = $this->getBasicLoginData(); + $this->userManager->expects($this->once()) + ->method('checkPasswordNoLogging') + ->with( + $this->username, + $this->password + ) + ->willReturn($this->user); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + $this->assertEquals($this->user, $data->getUser()); + } + +} diff --git a/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php b/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php new file mode 100644 index 00000000000..5f5bd3032a8 --- /dev/null +++ b/tests/lib/Authentication/Login/UpdateLastPasswordConfirmCommandTest.php @@ -0,0 +1,64 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\UpdateLastPasswordConfirmCommand; +use OCP\ISession; +use PHPUnit\Framework\MockObject\MockObject; + +class UpdateLastPasswordConfirmCommandTest extends ALoginCommandTest { + + /** @var ISession|MockObject */ + private $session; + + protected function setUp() { + parent::setUp(); + + $this->session = $this->createMock(ISession::class); + + $this->cmd = new UpdateLastPasswordConfirmCommand( + $this->session + ); + } + + public function testProcess() { + $data = $this->getLoggedInLoginData(); + $this->user->expects($this->once()) + ->method('getLastLogin') + ->willReturn(1234); + $this->session->expects($this->once()) + ->method('set') + ->with( + 'last-password-confirm', + 1234 + ); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + +} diff --git a/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php b/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php new file mode 100644 index 00000000000..35d8ac63de6 --- /dev/null +++ b/tests/lib/Authentication/Login/UserDisabledCheckCommandTest.php @@ -0,0 +1,97 @@ +<?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 lib\Authentication\Login; + +use OC\Authentication\Login\UserDisabledCheckCommand; +use OC\Core\Controller\LoginController; +use OCP\ILogger; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; + +class UserDisabledCheckCommandTest extends ALoginCommandTest { + + /** @var IUserManager|MockObject */ + private $userManager; + + /** @var ILogger|MockObject */ + private $logger; + + protected function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->logger = $this->createMock(ILogger::class); + + $this->cmd = new UserDisabledCheckCommand( + $this->userManager, + $this->logger + ); + } + + public function testProcessNonExistingUser() { + $data = $this->getBasicLoginData(); + $this->userManager->expects($this->once()) + ->method('get') + ->with($this->username) + ->willReturn(null); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + + public function testProcessDisabledUser() { + $data = $this->getBasicLoginData(); + $this->userManager->expects($this->once()) + ->method('get') + ->with($this->username) + ->willReturn($this->user); + $this->user->expects($this->once()) + ->method('isEnabled') + ->willReturn(false); + + $result = $this->cmd->process($data); + + $this->assertFalse($result->isSuccess()); + $this->assertSame(LoginController::LOGIN_MSG_USERDISABLED, $result->getErrorMessage()); + } + + public function testProcess() { + $data = $this->getBasicLoginData(); + $this->userManager->expects($this->once()) + ->method('get') + ->with($this->username) + ->willReturn($this->user); + $this->user->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $result = $this->cmd->process($data); + + $this->assertTrue($result->isSuccess()); + } + +} |