You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
7.1 KiB

10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Encryption\Tests\Controller;
  26. use OCA\Encryption\Controller\SettingsController;
  27. use OCA\Encryption\Crypto\Crypt;
  28. use OCA\Encryption\KeyManager;
  29. use OCA\Encryption\Session;
  30. use OCA\Encryption\Util;
  31. use OCP\AppFramework\Http;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\IUserManager;
  36. use OCP\IUserSession;
  37. use Test\TestCase;
  38. class SettingsControllerTest extends TestCase {
  39. /** @var SettingsController */
  40. private $controller;
  41. /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
  42. private $requestMock;
  43. /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
  44. private $l10nMock;
  45. /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  46. private $userManagerMock;
  47. /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  48. private $userSessionMock;
  49. /** @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject */
  50. private $keyManagerMock;
  51. /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit_Framework_MockObject_MockObject */
  52. private $cryptMock;
  53. /** @var \OCA\Encryption\Session|\PHPUnit_Framework_MockObject_MockObject */
  54. private $sessionMock;
  55. /** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */
  56. private $ocSessionMock;
  57. /** @var \OCA\Encryption\Util|\PHPUnit_Framework_MockObject_MockObject */
  58. private $utilMock;
  59. protected function setUp() {
  60. parent::setUp();
  61. $this->requestMock = $this->createMock(IRequest::class);
  62. $this->l10nMock = $this->getMockBuilder(IL10N::class)
  63. ->disableOriginalConstructor()->getMock();
  64. $this->l10nMock->expects($this->any())
  65. ->method('t')
  66. ->will($this->returnCallback(function($message) {
  67. return $message;
  68. }));
  69. $this->userManagerMock = $this->getMockBuilder(IUserManager::class)
  70. ->disableOriginalConstructor()->getMock();
  71. $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
  72. ->disableOriginalConstructor()->getMock();
  73. $this->cryptMock = $this->getMockBuilder(Crypt::class)
  74. ->disableOriginalConstructor()->getMock();
  75. $this->userSessionMock = $this->getMockBuilder(IUserSession::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods([
  78. 'isLoggedIn',
  79. 'getUID',
  80. 'login',
  81. 'logout',
  82. 'setUser',
  83. 'getUser',
  84. 'canChangePassword',
  85. ])
  86. ->getMock();
  87. $this->ocSessionMock = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
  88. $this->userSessionMock->expects($this->any())
  89. ->method('getUID')
  90. ->willReturn('testUserUid');
  91. $this->userSessionMock->expects($this->any())
  92. ->method($this->anything())
  93. ->will($this->returnSelf());
  94. $this->sessionMock = $this->getMockBuilder(Session::class)
  95. ->disableOriginalConstructor()->getMock();
  96. $this->utilMock = $this->getMockBuilder(Util::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $this->controller = new SettingsController(
  100. 'encryption',
  101. $this->requestMock,
  102. $this->l10nMock,
  103. $this->userManagerMock,
  104. $this->userSessionMock,
  105. $this->keyManagerMock,
  106. $this->cryptMock,
  107. $this->sessionMock,
  108. $this->ocSessionMock,
  109. $this->utilMock
  110. );
  111. }
  112. /**
  113. * test updatePrivateKeyPassword() if wrong new password was entered
  114. */
  115. public function testUpdatePrivateKeyPasswordWrongNewPassword() {
  116. $oldPassword = 'old';
  117. $newPassword = 'new';
  118. $this->userSessionMock->expects($this->once())->method('getUID')->willReturn('uid');
  119. $this->userManagerMock
  120. ->expects($this->exactly(2))
  121. ->method('checkPassword')
  122. ->willReturn(false);
  123. $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
  124. $data = $result->getData();
  125. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  126. $this->assertSame('The current log-in password was not correct, please try again.',
  127. $data['message']);
  128. }
  129. /**
  130. * test updatePrivateKeyPassword() if wrong old password was entered
  131. */
  132. public function testUpdatePrivateKeyPasswordWrongOldPassword() {
  133. $oldPassword = 'old';
  134. $newPassword = 'new';
  135. $this->userManagerMock
  136. ->expects($this->once())
  137. ->method('checkPassword')
  138. ->willReturn(true);
  139. $this->cryptMock
  140. ->expects($this->once())
  141. ->method('decryptPrivateKey')
  142. ->willReturn(false);
  143. $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
  144. $data = $result->getData();
  145. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  146. $this->assertSame('The old password was not correct, please try again.',
  147. $data['message']);
  148. }
  149. /**
  150. * test updatePrivateKeyPassword() with the correct old and new password
  151. */
  152. public function testUpdatePrivateKeyPassword() {
  153. $oldPassword = 'old';
  154. $newPassword = 'new';
  155. $this->ocSessionMock->expects($this->once())
  156. ->method('get')->with('loginname')->willReturn('testUser');
  157. $this->userManagerMock
  158. ->expects($this->at(0))
  159. ->method('checkPassword')
  160. ->with('testUserUid', 'new')
  161. ->willReturn(false);
  162. $this->userManagerMock
  163. ->expects($this->at(1))
  164. ->method('checkPassword')
  165. ->with('testUser', 'new')
  166. ->willReturn(true);
  167. $this->cryptMock
  168. ->expects($this->once())
  169. ->method('decryptPrivateKey')
  170. ->willReturn('decryptedKey');
  171. $this->cryptMock
  172. ->expects($this->once())
  173. ->method('encryptPrivateKey')
  174. ->willReturn('encryptedKey');
  175. $this->cryptMock
  176. ->expects($this->once())
  177. ->method('generateHeader')
  178. ->willReturn('header.');
  179. // methods which must be called after successful changing the key password
  180. $this->keyManagerMock
  181. ->expects($this->once())
  182. ->method('setPrivateKey')
  183. ->with($this->equalTo('testUserUid'), $this->equalTo('header.encryptedKey'));
  184. $this->sessionMock
  185. ->expects($this->once())
  186. ->method('setPrivateKey')
  187. ->with($this->equalTo('decryptedKey'));
  188. $this->sessionMock
  189. ->expects($this->once())
  190. ->method('setStatus')
  191. ->with($this->equalTo(Session::INIT_SUCCESSFUL));
  192. $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
  193. $data = $result->getData();
  194. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  195. $this->assertSame('Private key password successfully updated.',
  196. $data['message']);
  197. }
  198. function testSetEncryptHomeStorage() {
  199. $value = true;
  200. $this->utilMock->expects($this->once())->method('setEncryptHomeStorage')->with($value);
  201. $this->controller->setEncryptHomeStorage($value);
  202. }
  203. }