1
0

SettingsControllerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests\Controller;
  8. use OCA\Encryption\Controller\SettingsController;
  9. use OCA\Encryption\Crypto\Crypt;
  10. use OCA\Encryption\KeyManager;
  11. use OCA\Encryption\Session;
  12. use OCA\Encryption\Util;
  13. use OCP\AppFramework\Http;
  14. use OCP\IL10N;
  15. use OCP\IRequest;
  16. use OCP\ISession;
  17. use OCP\IUser;
  18. use OCP\IUserManager;
  19. use OCP\IUserSession;
  20. use PHPUnit\Framework\MockObject\MockObject;
  21. use Test\TestCase;
  22. class SettingsControllerTest extends TestCase {
  23. /** @var SettingsController */
  24. private $controller;
  25. /** @var \OCP\IRequest|\PHPUnit\Framework\MockObject\MockObject */
  26. private $requestMock;
  27. /** @var \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject */
  28. private $l10nMock;
  29. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  30. private $userManagerMock;
  31. /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  32. private $userSessionMock;
  33. /** @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject */
  34. private $keyManagerMock;
  35. /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject */
  36. private $cryptMock;
  37. /** @var \OCA\Encryption\Session|\PHPUnit\Framework\MockObject\MockObject */
  38. private $sessionMock;
  39. /** @var MockObject|IUser */
  40. private $user;
  41. /** @var \OCP\ISession|\PHPUnit\Framework\MockObject\MockObject */
  42. private $ocSessionMock;
  43. /** @var \OCA\Encryption\Util|\PHPUnit\Framework\MockObject\MockObject */
  44. private $utilMock;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->requestMock = $this->createMock(IRequest::class);
  48. $this->l10nMock = $this->getMockBuilder(IL10N::class)
  49. ->disableOriginalConstructor()->getMock();
  50. $this->l10nMock->expects($this->any())
  51. ->method('t')
  52. ->willReturnCallback(function ($message) {
  53. return $message;
  54. });
  55. $this->userManagerMock = $this->getMockBuilder(IUserManager::class)
  56. ->disableOriginalConstructor()->getMock();
  57. $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
  58. ->disableOriginalConstructor()->getMock();
  59. $this->cryptMock = $this->getMockBuilder(Crypt::class)
  60. ->disableOriginalConstructor()->getMock();
  61. $this->ocSessionMock = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
  62. $this->user = $this->createMock(IUser::class);
  63. $this->user->expects($this->any())
  64. ->method('getUID')
  65. ->willReturn('testUserUid');
  66. $this->userSessionMock = $this->createMock(IUserSession::class);
  67. $this->userSessionMock->expects($this->any())
  68. ->method('getUser')
  69. ->willReturn($this->user);
  70. $this->sessionMock = $this->getMockBuilder(Session::class)
  71. ->disableOriginalConstructor()->getMock();
  72. $this->utilMock = $this->getMockBuilder(Util::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->controller = new SettingsController(
  76. 'encryption',
  77. $this->requestMock,
  78. $this->l10nMock,
  79. $this->userManagerMock,
  80. $this->userSessionMock,
  81. $this->keyManagerMock,
  82. $this->cryptMock,
  83. $this->sessionMock,
  84. $this->ocSessionMock,
  85. $this->utilMock
  86. );
  87. }
  88. /**
  89. * test updatePrivateKeyPassword() if wrong new password was entered
  90. */
  91. public function testUpdatePrivateKeyPasswordWrongNewPassword(): void {
  92. $oldPassword = 'old';
  93. $newPassword = 'new';
  94. $this->user->expects($this->any())
  95. ->method('getUID')
  96. ->willReturn('uid');
  97. $this->userManagerMock
  98. ->expects($this->exactly(2))
  99. ->method('checkPassword')
  100. ->willReturn(false);
  101. $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
  102. $data = $result->getData();
  103. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  104. $this->assertSame('The current log-in password was not correct, please try again.',
  105. $data['message']);
  106. }
  107. /**
  108. * test updatePrivateKeyPassword() if wrong old password was entered
  109. */
  110. public function testUpdatePrivateKeyPasswordWrongOldPassword(): void {
  111. $oldPassword = 'old';
  112. $newPassword = 'new';
  113. $this->userManagerMock
  114. ->expects($this->once())
  115. ->method('checkPassword')
  116. ->willReturn(true);
  117. $this->cryptMock
  118. ->expects($this->once())
  119. ->method('decryptPrivateKey')
  120. ->willReturn(false);
  121. $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
  122. $data = $result->getData();
  123. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  124. $this->assertSame('The old password was not correct, please try again.',
  125. $data['message']);
  126. }
  127. /**
  128. * test updatePrivateKeyPassword() with the correct old and new password
  129. */
  130. public function testUpdatePrivateKeyPassword(): void {
  131. $oldPassword = 'old';
  132. $newPassword = 'new';
  133. $this->ocSessionMock->expects($this->once())
  134. ->method('get')->with('loginname')->willReturn('testUser');
  135. $this->userManagerMock
  136. ->expects($this->exactly(2))
  137. ->method('checkPassword')
  138. ->withConsecutive(
  139. ['testUserUid', 'new'],
  140. ['testUser', 'new'],
  141. )
  142. ->willReturnOnConsecutiveCalls(
  143. false,
  144. true,
  145. );
  146. $this->cryptMock
  147. ->expects($this->once())
  148. ->method('decryptPrivateKey')
  149. ->willReturn('decryptedKey');
  150. $this->cryptMock
  151. ->expects($this->once())
  152. ->method('encryptPrivateKey')
  153. ->willReturn('encryptedKey');
  154. $this->cryptMock
  155. ->expects($this->once())
  156. ->method('generateHeader')
  157. ->willReturn('header.');
  158. // methods which must be called after successful changing the key password
  159. $this->keyManagerMock
  160. ->expects($this->once())
  161. ->method('setPrivateKey')
  162. ->with($this->equalTo('testUserUid'), $this->equalTo('header.encryptedKey'));
  163. $this->sessionMock
  164. ->expects($this->once())
  165. ->method('setPrivateKey')
  166. ->with($this->equalTo('decryptedKey'));
  167. $this->sessionMock
  168. ->expects($this->once())
  169. ->method('setStatus')
  170. ->with($this->equalTo(Session::INIT_SUCCESSFUL));
  171. $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
  172. $data = $result->getData();
  173. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  174. $this->assertSame('Private key password successfully updated.',
  175. $data['message']);
  176. }
  177. public function testSetEncryptHomeStorage(): void {
  178. $value = true;
  179. $this->utilMock->expects($this->once())->method('setEncryptHomeStorage')->with($value);
  180. $this->controller->setEncryptHomeStorage($value);
  181. }
  182. }