diff options
Diffstat (limited to 'apps/encryption/tests/Controller')
3 files changed, 521 insertions, 0 deletions
diff --git a/apps/encryption/tests/Controller/RecoveryControllerTest.php b/apps/encryption/tests/Controller/RecoveryControllerTest.php new file mode 100644 index 00000000000..63c99e1277d --- /dev/null +++ b/apps/encryption/tests/Controller/RecoveryControllerTest.php @@ -0,0 +1,180 @@ +<?php +/** + * @author Clark Tomlinson <fallen013@gmail.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/> + * + */ + + +namespace OCA\Encryption\Tests\Controller; + + +use OCA\Encryption\Controller\RecoveryController; +use OCP\AppFramework\Http; +use Test\TestCase; + +class RecoveryControllerTest extends TestCase { + /** @var RecoveryController */ + private $controller; + /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ + private $requestMock; + /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $configMock; + /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l10nMock; + /** @var \OCA\Encryption\Recovery|\PHPUnit_Framework_MockObject_MockObject */ + private $recoveryMock; + + public function adminRecoveryProvider() { + return [ + ['test', 'test', '1', 'Recovery key successfully enabled', Http::STATUS_OK], + ['', 'test', '1', 'Missing recovery key password', Http::STATUS_BAD_REQUEST], + ['test', '', '1', 'Please repeat the recovery key password', Http::STATUS_BAD_REQUEST], + ['test', 'soimething that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], + ['test', 'test', '0', 'Recovery key successfully disabled', Http::STATUS_OK], + ]; + } + + /** + * @dataProvider adminRecoveryProvider + * @param $recoveryPassword + * @param $passConfirm + * @param $enableRecovery + * @param $expectedMessage + * @param $expectedStatus + */ + public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecovery, $expectedMessage, $expectedStatus) { + + + $this->recoveryMock->expects($this->any()) + ->method('enableAdminRecovery') + ->willReturn(true); + + $this->recoveryMock->expects($this->any()) + ->method('disableAdminRecovery') + ->willReturn(true); + + $response = $this->controller->adminRecovery($recoveryPassword, + $passConfirm, + $enableRecovery); + + + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); + $this->assertEquals($expectedStatus, $response->getStatus()); + + + } + + public function changeRecoveryPasswordProvider() { + return [ + ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', Http::STATUS_BAD_REQUEST], + ['test', 'test', 'oldtest', 'Password successfully changed.', Http::STATUS_OK], + ['test', 'notmatch', 'oldtest', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], + ['', 'test', 'oldtest', 'Please provide a new recovery password', Http::STATUS_BAD_REQUEST], + ['test', 'test', '', 'Please provide the old recovery password', Http::STATUS_BAD_REQUEST] + ]; + } + + /** + * @dataProvider changeRecoveryPasswordProvider + * @param $password + * @param $confirmPassword + * @param $oldPassword + * @param $expectedMessage + * @param $expectedStatus + */ + public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus) { + $this->recoveryMock->expects($this->any()) + ->method('changeRecoveryKeyPassword') + ->with($password, $oldPassword) + ->will($this->returnValueMap([ + ['test', 'oldTestFail', false], + ['test', 'oldtest', true] + ])); + + $response = $this->controller->changeRecoveryPassword($password, + $oldPassword, + $confirmPassword); + + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); + $this->assertEquals($expectedStatus, $response->getStatus()); + + + } + + public function userSetRecoveryProvider() { + return [ + ['1', 'Recovery Key enabled', Http::STATUS_OK], + ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST] + ]; + } + + /** + * @dataProvider userSetRecoveryProvider + * @param $enableRecovery + * @param $expectedMessage + * @param $expectedStatus + */ + public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus) { + $this->recoveryMock->expects($this->any()) + ->method('setRecoveryForUser') + ->with($enableRecovery) + ->will($this->returnValueMap([ + ['1', true], + ['0', false] + ])); + + + $response = $this->controller->userSetRecovery($enableRecovery); + + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); + $this->assertEquals($expectedStatus, $response->getStatus()); + + } + + protected function setUp() { + parent::setUp(); + + $this->requestMock = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + + $this->configMock = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + + $this->l10nMock = $this->getMockBuilder('OCP\IL10N') + ->disableOriginalConstructor() + ->getMock(); + + // Make l10n work in our tests + $this->l10nMock->expects($this->any()) + ->method('t') + ->willReturnArgument(0); + + $this->recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery') + ->disableOriginalConstructor() + ->getMock(); + + $this->controller = new RecoveryController('encryption', + $this->requestMock, + $this->configMock, + $this->l10nMock, + $this->recoveryMock); + } + +} diff --git a/apps/encryption/tests/Controller/SettingsControllerTest.php b/apps/encryption/tests/Controller/SettingsControllerTest.php new file mode 100644 index 00000000000..8180cae113f --- /dev/null +++ b/apps/encryption/tests/Controller/SettingsControllerTest.php @@ -0,0 +1,251 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * @author Joas Schilling <nickvergessen@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/> + * + */ + +namespace OCA\Encryption\Tests\Controller; + +use OCA\Encryption\Controller\SettingsController; +use OCA\Encryption\Session; +use OCP\AppFramework\Http; +use Test\TestCase; + +class SettingsControllerTest extends TestCase { + + /** @var SettingsController */ + private $controller; + + /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ + private $requestMock; + + /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l10nMock; + + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManagerMock; + + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + private $userSessionMock; + + /** @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject */ + private $keyManagerMock; + + /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit_Framework_MockObject_MockObject */ + private $cryptMock; + + /** @var \OCA\Encryption\Session|\PHPUnit_Framework_MockObject_MockObject */ + private $sessionMock; + + /** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */ + private $ocSessionMock; + + /** @var \OCA\Encryption\Util|\PHPUnit_Framework_MockObject_MockObject */ + private $utilMock; + + protected function setUp() { + + parent::setUp(); + + $this->requestMock = $this->getMock('OCP\IRequest'); + + $this->l10nMock = $this->getMockBuilder('OCP\IL10N') + ->disableOriginalConstructor()->getMock(); + + $this->l10nMock->expects($this->any()) + ->method('t') + ->will($this->returnCallback(function($message) { + return $message; + })); + + $this->userManagerMock = $this->getMockBuilder('OCP\IUserManager') + ->disableOriginalConstructor()->getMock(); + + $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager') + ->disableOriginalConstructor()->getMock(); + + $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt') + ->disableOriginalConstructor()->getMock(); + + $this->userSessionMock = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->setMethods([ + 'isLoggedIn', + 'getUID', + 'login', + 'logout', + 'setUser', + 'getUser', + 'canChangePassword', + ]) + ->getMock(); + + $this->ocSessionMock = $this->getMockBuilder('OCP\ISession')->disableOriginalConstructor()->getMock(); + + $this->userSessionMock->expects($this->any()) + ->method('getUID') + ->willReturn('testUserUid'); + + $this->userSessionMock->expects($this->any()) + ->method($this->anything()) + ->will($this->returnSelf()); + + $this->sessionMock = $this->getMockBuilder('OCA\Encryption\Session') + ->disableOriginalConstructor()->getMock(); + + $this->utilMock = $this->getMockBuilder('OCA\Encryption\Util') + ->disableOriginalConstructor() + ->getMock(); + + $this->controller = new SettingsController( + 'encryption', + $this->requestMock, + $this->l10nMock, + $this->userManagerMock, + $this->userSessionMock, + $this->keyManagerMock, + $this->cryptMock, + $this->sessionMock, + $this->ocSessionMock, + $this->utilMock + ); + } + + /** + * test updatePrivateKeyPassword() if wrong new password was entered + */ + public function testUpdatePrivateKeyPasswordWrongNewPassword() { + + $oldPassword = 'old'; + $newPassword = 'new'; + + $this->userSessionMock->expects($this->once())->method('getUID')->willReturn('uid'); + + $this->userManagerMock + ->expects($this->exactly(2)) + ->method('checkPassword') + ->willReturn(false); + + $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword); + + $data = $result->getData(); + + $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus()); + $this->assertSame('The current log-in password was not correct, please try again.', + $data['message']); + } + + /** + * test updatePrivateKeyPassword() if wrong old password was entered + */ + public function testUpdatePrivateKeyPasswordWrongOldPassword() { + + $oldPassword = 'old'; + $newPassword = 'new'; + + $this->userManagerMock + ->expects($this->once()) + ->method('checkPassword') + ->willReturn(true); + + $this->cryptMock + ->expects($this->once()) + ->method('decryptPrivateKey') + ->willReturn(false); + + $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword); + + $data = $result->getData(); + + $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus()); + $this->assertSame('The old password was not correct, please try again.', + $data['message']); + } + + /** + * test updatePrivateKeyPassword() with the correct old and new password + */ + public function testUpdatePrivateKeyPassword() { + + $oldPassword = 'old'; + $newPassword = 'new'; + + $this->ocSessionMock->expects($this->once()) + ->method('get')->with('loginname')->willReturn('testUser'); + + $this->userManagerMock + ->expects($this->at(0)) + ->method('checkPassword') + ->with('testUserUid', 'new') + ->willReturn(false); + $this->userManagerMock + ->expects($this->at(1)) + ->method('checkPassword') + ->with('testUser', 'new') + ->willReturn(true); + + + + $this->cryptMock + ->expects($this->once()) + ->method('decryptPrivateKey') + ->willReturn('decryptedKey'); + + $this->cryptMock + ->expects($this->once()) + ->method('encryptPrivateKey') + ->willReturn('encryptedKey'); + + $this->cryptMock + ->expects($this->once()) + ->method('generateHeader') + ->willReturn('header.'); + + // methods which must be called after successful changing the key password + $this->keyManagerMock + ->expects($this->once()) + ->method('setPrivateKey') + ->with($this->equalTo('testUserUid'), $this->equalTo('header.encryptedKey')); + + $this->sessionMock + ->expects($this->once()) + ->method('setPrivateKey') + ->with($this->equalTo('decryptedKey')); + + $this->sessionMock + ->expects($this->once()) + ->method('setStatus') + ->with($this->equalTo(Session::INIT_SUCCESSFUL)); + + $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword); + + $data = $result->getData(); + + $this->assertSame(Http::STATUS_OK, $result->getStatus()); + $this->assertSame('Private key password successfully updated.', + $data['message']); + } + + function testSetEncryptHomeStorage() { + $value = true; + $this->utilMock->expects($this->once())->method('setEncryptHomeStorage')->with($value); + $this->controller->setEncryptHomeStorage($value); + } + +} diff --git a/apps/encryption/tests/Controller/StatusControllerTest.php b/apps/encryption/tests/Controller/StatusControllerTest.php new file mode 100644 index 00000000000..0410b36cd2a --- /dev/null +++ b/apps/encryption/tests/Controller/StatusControllerTest.php @@ -0,0 +1,90 @@ +<?php +/** + * @author Björn Schießle <schiessle@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/> + * + */ + + +namespace OCA\Encryption\Tests\Controller; + + +use OCA\Encryption\Controller\StatusController; +use OCA\Encryption\Session; +use Test\TestCase; + +class StatusControllerTest extends TestCase { + + /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ + private $requestMock; + + /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l10nMock; + + /** @var \OCA\Encryption\Session | \PHPUnit_Framework_MockObject_MockObject */ + protected $sessionMock; + + /** @var StatusController */ + protected $controller; + + protected function setUp() { + + parent::setUp(); + + $this->sessionMock = $this->getMockBuilder('OCA\Encryption\Session') + ->disableOriginalConstructor()->getMock(); + $this->requestMock = $this->getMock('OCP\IRequest'); + + $this->l10nMock = $this->getMockBuilder('OCP\IL10N') + ->disableOriginalConstructor()->getMock(); + $this->l10nMock->expects($this->any()) + ->method('t') + ->will($this->returnCallback(function($message) { + return $message; + })); + + $this->controller = new StatusController('encryptionTest', + $this->requestMock, + $this->l10nMock, + $this->sessionMock); + + } + + /** + * @dataProvider dataTestGetStatus + * + * @param string $status + * @param string $expectedStatus + */ + public function testGetStatus($status, $expectedStatus) { + $this->sessionMock->expects($this->once()) + ->method('getStatus')->willReturn($status); + $result = $this->controller->getStatus(); + $data = $result->getData(); + $this->assertSame($expectedStatus, $data['status']); + } + + public function dataTestGetStatus() { + return array( + array(Session::RUN_MIGRATION, 'interactionNeeded'), + array(Session::INIT_EXECUTED, 'interactionNeeded'), + array(Session::INIT_SUCCESSFUL, 'success'), + array(Session::NOT_INITIALIZED, 'interactionNeeded'), + array('unknown', 'error'), + ); + } +} |