UserHooksTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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\Hooks;
  8. use OCA\Encryption\Crypto\Crypt;
  9. use OCA\Encryption\Hooks\UserHooks;
  10. use OCA\Encryption\KeyManager;
  11. use OCA\Encryption\Recovery;
  12. use OCA\Encryption\Session;
  13. use OCA\Encryption\Users\Setup;
  14. use OCA\Encryption\Util;
  15. use OCP\IUser;
  16. use OCP\IUserManager;
  17. use OCP\IUserSession;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Psr\Log\LoggerInterface;
  20. use Test\TestCase;
  21. /**
  22. * Class UserHooksTest
  23. *
  24. * @group DB
  25. * @package OCA\Encryption\Tests\Hooks
  26. */
  27. class UserHooksTest extends TestCase {
  28. /**
  29. * @var \PHPUnit\Framework\MockObject\MockObject
  30. */
  31. private $utilMock;
  32. /**
  33. * @var \PHPUnit\Framework\MockObject\MockObject
  34. */
  35. private $recoveryMock;
  36. /**
  37. * @var \PHPUnit\Framework\MockObject\MockObject
  38. */
  39. private $sessionMock;
  40. /**
  41. * @var \PHPUnit\Framework\MockObject\MockObject
  42. */
  43. private $keyManagerMock;
  44. /**
  45. * @var \PHPUnit\Framework\MockObject\MockObject
  46. */
  47. private $userManagerMock;
  48. /**
  49. * @var \PHPUnit\Framework\MockObject\MockObject
  50. */
  51. private $userSetupMock;
  52. /**
  53. * @var \PHPUnit\Framework\MockObject\MockObject
  54. */
  55. private $userSessionMock;
  56. /**
  57. * @var MockObject|IUser
  58. */
  59. private $user;
  60. /**
  61. * @var \PHPUnit\Framework\MockObject\MockObject
  62. */
  63. private $cryptMock;
  64. /**
  65. * @var \PHPUnit\Framework\MockObject\MockObject
  66. */
  67. private $loggerMock;
  68. /**
  69. * @var UserHooks
  70. */
  71. private $instance;
  72. private $params = ['uid' => 'testUser', 'password' => 'password'];
  73. public function testLogin() {
  74. $this->userSetupMock->expects($this->once())
  75. ->method('setupUser')
  76. ->willReturnOnConsecutiveCalls(true, false);
  77. $this->keyManagerMock->expects($this->once())
  78. ->method('init')
  79. ->with('testUser', 'password');
  80. $this->assertNull($this->instance->login($this->params));
  81. }
  82. public function testLogout() {
  83. $this->sessionMock->expects($this->once())
  84. ->method('clear');
  85. $this->instance->logout();
  86. $this->addToAssertionCount(1);
  87. }
  88. public function testPostCreateUser() {
  89. $this->userSetupMock->expects($this->once())
  90. ->method('setupUser');
  91. $this->instance->postCreateUser($this->params);
  92. $this->addToAssertionCount(1);
  93. }
  94. public function testPostDeleteUser() {
  95. $this->keyManagerMock->expects($this->once())
  96. ->method('deletePublicKey')
  97. ->with('testUser');
  98. $this->instance->postDeleteUser($this->params);
  99. $this->addToAssertionCount(1);
  100. }
  101. public function testPrePasswordReset() {
  102. $params = ['uid' => 'user1'];
  103. $expected = ['user1' => true];
  104. $this->instance->prePasswordReset($params);
  105. $passwordResetUsers = $this->invokePrivate($this->instance, 'passwordResetUsers');
  106. $this->assertSame($expected, $passwordResetUsers);
  107. }
  108. public function testPostPasswordReset() {
  109. $params = ['uid' => 'user1', 'password' => 'password'];
  110. $this->invokePrivate($this->instance, 'passwordResetUsers', [['user1' => true]]);
  111. $this->keyManagerMock->expects($this->once())->method('backupUserKeys')
  112. ->with('passwordReset', 'user1');
  113. $this->keyManagerMock->expects($this->once())->method('deleteUserKeys')
  114. ->with('user1');
  115. $this->userSetupMock->expects($this->once())->method('setupUser')
  116. ->with('user1', 'password');
  117. $this->instance->postPasswordReset($params);
  118. $passwordResetUsers = $this->invokePrivate($this->instance, 'passwordResetUsers');
  119. $this->assertEmpty($passwordResetUsers);
  120. }
  121. /**
  122. * @dataProvider dataTestPreSetPassphrase
  123. */
  124. public function testPreSetPassphrase($canChange) {
  125. /** @var UserHooks | \PHPUnit\Framework\MockObject\MockObject $instance */
  126. $instance = $this->getMockBuilder(UserHooks::class)
  127. ->setConstructorArgs(
  128. [
  129. $this->keyManagerMock,
  130. $this->userManagerMock,
  131. $this->loggerMock,
  132. $this->userSetupMock,
  133. $this->userSessionMock,
  134. $this->utilMock,
  135. $this->sessionMock,
  136. $this->cryptMock,
  137. $this->recoveryMock
  138. ]
  139. )
  140. ->setMethods(['setPassphrase'])
  141. ->getMock();
  142. $userMock = $this->createMock(IUser::class);
  143. $this->userManagerMock->expects($this->once())
  144. ->method('get')
  145. ->with($this->params['uid'])
  146. ->willReturn($userMock);
  147. $userMock->expects($this->once())
  148. ->method('canChangePassword')
  149. ->willReturn($canChange);
  150. if ($canChange) {
  151. // in this case the password will be changed in the post hook
  152. $instance->expects($this->never())->method('setPassphrase');
  153. } else {
  154. // if user can't change the password we update the encryption
  155. // key password already in the pre hook
  156. $instance->expects($this->once())
  157. ->method('setPassphrase')
  158. ->with($this->params);
  159. }
  160. $instance->preSetPassphrase($this->params);
  161. }
  162. public function dataTestPreSetPassphrase() {
  163. return [
  164. [true],
  165. [false]
  166. ];
  167. }
  168. public function XtestSetPassphrase() {
  169. $this->sessionMock->expects($this->once())
  170. ->method('getPrivateKey')
  171. ->willReturn(true);
  172. $this->cryptMock->expects($this->exactly(4))
  173. ->method('encryptPrivateKey')
  174. ->willReturn(true);
  175. $this->cryptMock->expects($this->any())
  176. ->method('generateHeader')
  177. ->willReturn(Crypt::HEADER_START . ':Cipher:test:' . Crypt::HEADER_END);
  178. $this->keyManagerMock->expects($this->exactly(4))
  179. ->method('setPrivateKey')
  180. ->willReturnCallback(function ($user, $key) {
  181. $header = substr($key, 0, strlen(Crypt::HEADER_START));
  182. $this->assertSame(
  183. Crypt::HEADER_START,
  184. $header, 'every encrypted file should start with a header');
  185. });
  186. $this->assertNull($this->instance->setPassphrase($this->params));
  187. $this->params['recoveryPassword'] = 'password';
  188. $this->recoveryMock->expects($this->exactly(3))
  189. ->method('isRecoveryEnabledForUser')
  190. ->with('testUser1')
  191. ->willReturnOnConsecutiveCalls(true, false);
  192. $this->instance = $this->getMockBuilder(UserHooks::class)
  193. ->setConstructorArgs(
  194. [
  195. $this->keyManagerMock,
  196. $this->userManagerMock,
  197. $this->loggerMock,
  198. $this->userSetupMock,
  199. $this->userSessionMock,
  200. $this->utilMock,
  201. $this->sessionMock,
  202. $this->cryptMock,
  203. $this->recoveryMock
  204. ]
  205. )->setMethods(['initMountPoints'])->getMock();
  206. $this->instance->expects($this->exactly(3))->method('initMountPoints');
  207. $this->params['uid'] = 'testUser1';
  208. // Test first if statement
  209. $this->assertNull($this->instance->setPassphrase($this->params));
  210. // Test Second if conditional
  211. $this->keyManagerMock->expects($this->exactly(2))
  212. ->method('userHasKeys')
  213. ->with('testUser1')
  214. ->willReturn(true);
  215. $this->assertNull($this->instance->setPassphrase($this->params));
  216. // Test third and final if condition
  217. $this->utilMock->expects($this->once())
  218. ->method('userHasFiles')
  219. ->with('testUser1')
  220. ->willReturn(false);
  221. $this->cryptMock->expects($this->once())
  222. ->method('createKeyPair');
  223. $this->keyManagerMock->expects($this->once())
  224. ->method('setPrivateKey');
  225. $this->recoveryMock->expects($this->once())
  226. ->method('recoverUsersFiles')
  227. ->with('password', 'testUser1');
  228. $this->assertNull($this->instance->setPassphrase($this->params));
  229. }
  230. public function testSetPassphraseResetUserMode() {
  231. $params = ['uid' => 'user1', 'password' => 'password'];
  232. $this->invokePrivate($this->instance, 'passwordResetUsers', [[$params['uid'] => true]]);
  233. $this->sessionMock->expects($this->never())->method('getPrivateKey');
  234. $this->keyManagerMock->expects($this->never())->method('setPrivateKey');
  235. $this->assertTrue($this->instance->setPassphrase($params));
  236. $this->invokePrivate($this->instance, 'passwordResetUsers', [[]]);
  237. }
  238. public function XtestSetPasswordNoUser() {
  239. $userSessionMock = $this->getMockBuilder(IUserSession::class)
  240. ->disableOriginalConstructor()
  241. ->getMock();
  242. $userSessionMock->expects($this->any())->method('getUser')->willReturn(null);
  243. $this->recoveryMock->expects($this->once())
  244. ->method('isRecoveryEnabledForUser')
  245. ->with('testUser')
  246. ->willReturn(false);
  247. $userHooks = $this->getMockBuilder(UserHooks::class)
  248. ->setConstructorArgs(
  249. [
  250. $this->keyManagerMock,
  251. $this->userManagerMock,
  252. $this->loggerMock,
  253. $this->userSetupMock,
  254. $userSessionMock,
  255. $this->utilMock,
  256. $this->sessionMock,
  257. $this->cryptMock,
  258. $this->recoveryMock
  259. ]
  260. )->setMethods(['initMountPoints'])->getMock();
  261. /** @var \OCA\Encryption\Hooks\UserHooks $userHooks */
  262. $this->assertNull($userHooks->setPassphrase($this->params));
  263. }
  264. protected function setUp(): void {
  265. parent::setUp();
  266. $this->loggerMock = $this->createMock(LoggerInterface::class);
  267. $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
  268. ->disableOriginalConstructor()
  269. ->getMock();
  270. $this->userManagerMock = $this->getMockBuilder(IUserManager::class)
  271. ->disableOriginalConstructor()
  272. ->getMock();
  273. $this->userSetupMock = $this->getMockBuilder(Setup::class)
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $this->user = $this->createMock(IUser::class);
  277. $this->user->expects($this->any())
  278. ->method('getUID')
  279. ->willReturn('testUser');
  280. $this->userSessionMock = $this->createMock(IUserSession::class);
  281. $this->userSessionMock->expects($this->any())
  282. ->method('getUser')
  283. ->willReturn($this->user);
  284. $utilMock = $this->getMockBuilder(Util::class)
  285. ->disableOriginalConstructor()
  286. ->getMock();
  287. $sessionMock = $this->getMockBuilder(Session::class)
  288. ->disableOriginalConstructor()
  289. ->getMock();
  290. $this->cryptMock = $this->getMockBuilder(Crypt::class)
  291. ->disableOriginalConstructor()
  292. ->getMock();
  293. $recoveryMock = $this->getMockBuilder(Recovery::class)
  294. ->disableOriginalConstructor()
  295. ->getMock();
  296. $this->sessionMock = $sessionMock;
  297. $this->recoveryMock = $recoveryMock;
  298. $this->utilMock = $utilMock;
  299. $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  300. $this->instance = $this->getMockBuilder(UserHooks::class)
  301. ->setConstructorArgs(
  302. [
  303. $this->keyManagerMock,
  304. $this->userManagerMock,
  305. $this->loggerMock,
  306. $this->userSetupMock,
  307. $this->userSessionMock,
  308. $this->utilMock,
  309. $this->sessionMock,
  310. $this->cryptMock,
  311. $this->recoveryMock
  312. ]
  313. )->setMethods(['setupFS'])->getMock();
  314. }
  315. }