summaryrefslogtreecommitdiffstats
path: root/apps/encryption/tests
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-04-20 20:48:12 +0200
committerThomas Müller <thomas.mueller@tmit.eu>2015-04-20 20:48:12 +0200
commitb78e76a1cb68ffb374044851e9f96a0c7f463862 (patch)
tree06ec35e0efbc695e097d277da130d0051561cae4 /apps/encryption/tests
parenta13088818a73ecdf6f7d6600c011e0fa49054b51 (diff)
parent04674c06cca5884e6269461b2ae9a6e64a00953d (diff)
downloadnextcloud-server-b78e76a1cb68ffb374044851e9f96a0c7f463862.tar.gz
nextcloud-server-b78e76a1cb68ffb374044851e9f96a0c7f463862.zip
Merge pull request #15677 from owncloud/enc_reset_private_key_password
[encryption] let user update the private key password
Diffstat (limited to 'apps/encryption/tests')
-rw-r--r--apps/encryption/tests/controller/SettingsControllerTest.php222
1 files changed, 222 insertions, 0 deletions
diff --git a/apps/encryption/tests/controller/SettingsControllerTest.php b/apps/encryption/tests/controller/SettingsControllerTest.php
new file mode 100644
index 00000000000..478bf8213b5
--- /dev/null
+++ b/apps/encryption/tests/controller/SettingsControllerTest.php
@@ -0,0 +1,222 @@
+<?php
+/**
+ * @author Björn Schießle <schiessle@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, 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 \PHPUnit_Framework_MockObject_MockObject */
+ private $requestMock;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $l10nMock;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $userManagerMock;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $userSessionMock;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $keyManagerMock;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $cryptMock;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $sessionMock;
+
+ 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->userSessionMock->expects($this->any())
+ ->method('getUID')
+ ->willReturn('testUser');
+
+ $this->userSessionMock->expects($this->any())
+ ->method($this->anything())
+ ->will($this->returnSelf());
+
+ $this->sessionMock = $this->getMockBuilder('OCA\Encryption\Session')
+ ->disableOriginalConstructor()->getMock();
+
+ $this->controller = new SettingsController(
+ 'encryption',
+ $this->requestMock,
+ $this->l10nMock,
+ $this->userManagerMock,
+ $this->userSessionMock,
+ $this->keyManagerMock,
+ $this->cryptMock,
+ $this->sessionMock
+ );
+ }
+
+ /**
+ * test updatePrivateKeyPassword() if wrong new password was entered
+ */
+ public function testUpdatePrivateKeyPasswordWrongNewPassword() {
+
+ $oldPassword = 'old';
+ $newPassword = 'new';
+
+ $this->userManagerMock
+ ->expects($this->once())
+ ->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->userSessionMock
+ ->expects($this->once())
+ ->method('getUID')
+ ->willReturn('testUser');
+
+ $this->userManagerMock
+ ->expects($this->once())
+ ->method('checkPassword')
+ ->willReturn(true);
+
+ $this->cryptMock
+ ->expects($this->once())
+ ->method('decryptPrivateKey')
+ ->willReturn('decryptedKey');
+
+ $this->cryptMock
+ ->expects($this->once())
+ ->method('symmetricEncryptFileContent')
+ ->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('testUser'), $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']);
+ }
+
+}