SettingsControllerTest.php 7.0 KB

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