1
0

UserHooksTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Encryption\Tests\Hooks;
  25. use OCA\Encryption\Crypto\Crypt;
  26. use OCA\Encryption\Hooks\UserHooks;
  27. use OCP\ILogger;
  28. use OCP\IUser;
  29. use Test\TestCase;
  30. /**
  31. * Class UserHooksTest
  32. *
  33. * @group DB
  34. * @package OCA\Encryption\Tests\Hooks
  35. */
  36. class UserHooksTest extends TestCase {
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $utilMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $recoveryMock;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $sessionMock;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $keyManagerMock;
  53. /**
  54. * @var \PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $userManagerMock;
  57. /**
  58. * @var \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $userSetupMock;
  61. /**
  62. * @var \PHPUnit_Framework_MockObject_MockObject
  63. */
  64. private $userSessionMock;
  65. /**
  66. * @var \PHPUnit_Framework_MockObject_MockObject
  67. */
  68. private $cryptMock;
  69. /**
  70. * @var \PHPUnit_Framework_MockObject_MockObject
  71. */
  72. private $loggerMock;
  73. /**
  74. * @var UserHooks
  75. */
  76. private $instance;
  77. private $params = ['uid' => 'testUser', 'password' => 'password'];
  78. public function testLogin() {
  79. $this->userSetupMock->expects($this->once())
  80. ->method('setupUser')
  81. ->willReturnOnConsecutiveCalls(true, false);
  82. $this->keyManagerMock->expects($this->once())
  83. ->method('init')
  84. ->with('testUser', 'password');
  85. $this->assertNull($this->instance->login($this->params));
  86. }
  87. public function testLogout() {
  88. $this->sessionMock->expects($this->once())
  89. ->method('clear');
  90. $this->instance->logout();
  91. $this->assertTrue(true);
  92. }
  93. public function testPostCreateUser() {
  94. $this->userSetupMock->expects($this->once())
  95. ->method('setupUser');
  96. $this->instance->postCreateUser($this->params);
  97. $this->assertTrue(true);
  98. }
  99. public function testPostDeleteUser() {
  100. $this->keyManagerMock->expects($this->once())
  101. ->method('deletePublicKey')
  102. ->with('testUser');
  103. $this->instance->postDeleteUser($this->params);
  104. $this->assertTrue(true);
  105. }
  106. public function testPrePasswordReset() {
  107. $params = ['uid' => 'user1'];
  108. $expected = ['user1' => true];
  109. $this->instance->prePasswordReset($params);
  110. $passwordResetUsers = $this->invokePrivate($this->instance, 'passwordResetUsers');
  111. $this->assertSame($expected, $passwordResetUsers);
  112. }
  113. public function testPostPasswordReset() {
  114. $params = ['uid' => 'user1', 'password' => 'password'];
  115. $this->invokePrivate($this->instance, 'passwordResetUsers', [['user1' => true]]);
  116. $this->keyManagerMock->expects($this->once())->method('backupUserKeys')
  117. ->with('passwordReset', 'user1');
  118. $this->keyManagerMock->expects($this->once())->method('deleteUserKeys')
  119. ->with('user1');
  120. $this->userSetupMock->expects($this->once())->method('setupUser')
  121. ->with('user1', 'password');
  122. $this->instance->postPasswordReset($params);
  123. $passwordResetUsers = $this->invokePrivate($this->instance, 'passwordResetUsers');
  124. $this->assertEmpty($passwordResetUsers);
  125. }
  126. /**
  127. * @dataProvider dataTestPreSetPassphrase
  128. */
  129. public function testPreSetPassphrase($canChange) {
  130. /** @var UserHooks | \PHPUnit_Framework_MockObject_MockObject $instance */
  131. $instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
  132. ->setConstructorArgs(
  133. [
  134. $this->keyManagerMock,
  135. $this->userManagerMock,
  136. $this->loggerMock,
  137. $this->userSetupMock,
  138. $this->userSessionMock,
  139. $this->utilMock,
  140. $this->sessionMock,
  141. $this->cryptMock,
  142. $this->recoveryMock
  143. ]
  144. )
  145. ->setMethods(['setPassphrase'])
  146. ->getMock();
  147. $userMock = $this->createMock(IUser::class);
  148. $this->userManagerMock->expects($this->once())
  149. ->method('get')
  150. ->with($this->params['uid'])
  151. ->willReturn($userMock);
  152. $userMock->expects($this->once())
  153. ->method('canChangePassword')
  154. ->willReturn($canChange);
  155. if ($canChange) {
  156. // in this case the password will be changed in the post hook
  157. $instance->expects($this->never())->method('setPassphrase');
  158. } else {
  159. // if user can't change the password we update the encryption
  160. // key password already in the pre hook
  161. $instance->expects($this->once())
  162. ->method('setPassphrase')
  163. ->with($this->params);
  164. }
  165. $instance->preSetPassphrase($this->params);
  166. }
  167. public function dataTestPreSetPassphrase() {
  168. return [
  169. [true],
  170. [false]
  171. ];
  172. }
  173. public function testSetPassphrase() {
  174. $this->sessionMock->expects($this->exactly(4))
  175. ->method('getPrivateKey')
  176. ->willReturnOnConsecutiveCalls(true, false);
  177. $this->cryptMock->expects($this->exactly(4))
  178. ->method('encryptPrivateKey')
  179. ->willReturn(true);
  180. $this->cryptMock->expects($this->any())
  181. ->method('generateHeader')
  182. ->willReturn(Crypt::HEADER_START . ':Cipher:test:' . Crypt::HEADER_END);
  183. $this->keyManagerMock->expects($this->exactly(4))
  184. ->method('setPrivateKey')
  185. ->willReturnCallback(function ($user, $key) {
  186. $header = substr($key, 0, strlen(Crypt::HEADER_START));
  187. $this->assertSame(
  188. Crypt::HEADER_START,
  189. $header, 'every encrypted file should start with a header');
  190. });
  191. $this->assertNull($this->instance->setPassphrase($this->params));
  192. $this->params['recoveryPassword'] = 'password';
  193. $this->recoveryMock->expects($this->exactly(3))
  194. ->method('isRecoveryEnabledForUser')
  195. ->with('testUser')
  196. ->willReturnOnConsecutiveCalls(true, false);
  197. $this->instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
  198. ->setConstructorArgs(
  199. [
  200. $this->keyManagerMock,
  201. $this->userManagerMock,
  202. $this->loggerMock,
  203. $this->userSetupMock,
  204. $this->userSessionMock,
  205. $this->utilMock,
  206. $this->sessionMock,
  207. $this->cryptMock,
  208. $this->recoveryMock
  209. ]
  210. )->setMethods(['initMountPoints'])->getMock();
  211. $this->instance->expects($this->exactly(3))->method('initMountPoints');
  212. // Test first if statement
  213. $this->assertNull($this->instance->setPassphrase($this->params));
  214. // Test Second if conditional
  215. $this->keyManagerMock->expects($this->exactly(2))
  216. ->method('userHasKeys')
  217. ->with('testUser')
  218. ->willReturn(true);
  219. $this->assertNull($this->instance->setPassphrase($this->params));
  220. // Test third and final if condition
  221. $this->utilMock->expects($this->once())
  222. ->method('userHasFiles')
  223. ->with('testUser')
  224. ->willReturn(false);
  225. $this->cryptMock->expects($this->once())
  226. ->method('createKeyPair');
  227. $this->keyManagerMock->expects($this->once())
  228. ->method('setPrivateKey');
  229. $this->recoveryMock->expects($this->once())
  230. ->method('recoverUsersFiles')
  231. ->with('password', 'testUser');
  232. $this->assertNull($this->instance->setPassphrase($this->params));
  233. }
  234. public function testSetPassphraseResetUserMode() {
  235. $params = ['uid' => 'user1', 'password' => 'password'];
  236. $this->invokePrivate($this->instance, 'passwordResetUsers', [[$params['uid'] => true]]);
  237. $this->sessionMock->expects($this->never())->method('getPrivateKey');
  238. $this->keyManagerMock->expects($this->never())->method('setPrivateKey');
  239. $this->assertTrue($this->instance->setPassphrase($params));
  240. $this->invokePrivate($this->instance, 'passwordResetUsers', [[]]);
  241. }
  242. public function testSetPasswordNoUser() {
  243. $this->sessionMock->expects($this->once())
  244. ->method('getPrivateKey')
  245. ->willReturn(true);
  246. $userSessionMock = $this->getMockBuilder('OCP\IUserSession')
  247. ->disableOriginalConstructor()
  248. ->getMock();
  249. $userSessionMock->expects($this->any())->method('getUser')->will($this->returnValue(null));
  250. $this->recoveryMock->expects($this->once())
  251. ->method('isRecoveryEnabledForUser')
  252. ->with('testUser')
  253. ->willReturn(false);
  254. $userHooks = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
  255. ->setConstructorArgs(
  256. [
  257. $this->keyManagerMock,
  258. $this->userManagerMock,
  259. $this->loggerMock,
  260. $this->userSetupMock,
  261. $userSessionMock,
  262. $this->utilMock,
  263. $this->sessionMock,
  264. $this->cryptMock,
  265. $this->recoveryMock
  266. ]
  267. )->setMethods(['initMountPoints'])->getMock();
  268. /** @var \OCA\Encryption\Hooks\UserHooks $userHooks */
  269. $this->assertNull($userHooks->setPassphrase($this->params));
  270. }
  271. protected function setUp() {
  272. parent::setUp();
  273. $this->loggerMock = $this->createMock(ILogger::class);
  274. $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
  275. ->disableOriginalConstructor()
  276. ->getMock();
  277. $this->userManagerMock = $this->getMockBuilder('OCP\IUserManager')
  278. ->disableOriginalConstructor()
  279. ->getMock();
  280. $this->userSetupMock = $this->getMockBuilder('OCA\Encryption\Users\Setup')
  281. ->disableOriginalConstructor()
  282. ->getMock();
  283. $this->userSessionMock = $this->getMockBuilder('OCP\IUserSession')
  284. ->disableOriginalConstructor()
  285. ->setMethods([
  286. 'isLoggedIn',
  287. 'getUID',
  288. 'login',
  289. 'logout',
  290. 'setUser',
  291. 'getUser',
  292. 'canChangePassword'
  293. ])
  294. ->getMock();
  295. $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('testUser'));
  296. $this->userSessionMock->expects($this->any())
  297. ->method($this->anything())
  298. ->will($this->returnSelf());
  299. $utilMock = $this->getMockBuilder('OCA\Encryption\Util')
  300. ->disableOriginalConstructor()
  301. ->getMock();
  302. $sessionMock = $this->getMockBuilder('OCA\Encryption\Session')
  303. ->disableOriginalConstructor()
  304. ->getMock();
  305. $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
  306. ->disableOriginalConstructor()
  307. ->getMock();
  308. $recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery')
  309. ->disableOriginalConstructor()
  310. ->getMock();
  311. $this->sessionMock = $sessionMock;
  312. $this->recoveryMock = $recoveryMock;
  313. $this->utilMock = $utilMock;
  314. $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  315. $this->instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
  316. ->setConstructorArgs(
  317. [
  318. $this->keyManagerMock,
  319. $this->userManagerMock,
  320. $this->loggerMock,
  321. $this->userSetupMock,
  322. $this->userSessionMock,
  323. $this->utilMock,
  324. $this->sessionMock,
  325. $this->cryptMock,
  326. $this->recoveryMock
  327. ]
  328. )->setMethods(['setupFS'])->getMock();
  329. }
  330. }