SettingsControllerTest.php 6.9 KB

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