You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserHooksTest.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Clark Tomlinson <fallen013@gmail.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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\Hooks;
  24. use OCA\Encryption\Crypto\Crypt;
  25. use OCA\Encryption\Hooks\UserHooks;
  26. use Test\TestCase;
  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 $userSetupMock;
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $userSessionMock;
  52. /**
  53. * @var \PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $cryptMock;
  56. /**
  57. * @var UserHooks
  58. */
  59. private $instance;
  60. private $params = ['uid' => 'testUser', 'password' => 'password'];
  61. public function testLogin() {
  62. $this->userSetupMock->expects($this->exactly(2))
  63. ->method('setupUser')
  64. ->willReturnOnConsecutiveCalls(true, false);
  65. $this->keyManagerMock->expects($this->once())
  66. ->method('init')
  67. ->with('testUser', 'password');
  68. $this->assertNull($this->instance->login($this->params));
  69. $this->assertFalse($this->instance->login($this->params));
  70. }
  71. public function testLogout() {
  72. $this->sessionMock->expects($this->once())
  73. ->method('clear');
  74. $this->assertNull($this->instance->logout());
  75. }
  76. public function testPostCreateUser() {
  77. $this->userSetupMock->expects($this->once())
  78. ->method('setupUser');
  79. $this->assertNull($this->instance->postCreateUser($this->params));
  80. }
  81. public function testPostDeleteUser() {
  82. $this->keyManagerMock->expects($this->once())
  83. ->method('deletePublicKey')
  84. ->with('testUser');
  85. $this->assertNull($this->instance->postDeleteUser($this->params));
  86. }
  87. public function testPreSetPassphrase() {
  88. $this->userSessionMock->expects($this->once())
  89. ->method('canChangePassword');
  90. $this->assertNull($this->instance->preSetPassphrase($this->params));
  91. }
  92. public function testSetPassphrase() {
  93. $this->sessionMock->expects($this->exactly(4))
  94. ->method('getPrivateKey')
  95. ->willReturnOnConsecutiveCalls(true, false);
  96. $this->cryptMock->expects($this->exactly(4))
  97. ->method('symmetricEncryptFileContent')
  98. ->willReturn(true);
  99. $this->cryptMock->expects($this->any())
  100. ->method('generateHeader')
  101. ->willReturn(Crypt::HEADER_START . ':Cipher:test:' . Crypt::HEADER_END);
  102. $this->keyManagerMock->expects($this->exactly(4))
  103. ->method('setPrivateKey')
  104. ->willReturnCallback(function ($user, $key) {
  105. $header = substr($key, 0, strlen(Crypt::HEADER_START));
  106. $this->assertSame(
  107. Crypt::HEADER_START,
  108. $header, 'every encrypted file should start with a header');
  109. });
  110. $this->assertNull($this->instance->setPassphrase($this->params));
  111. $this->params['recoveryPassword'] = 'password';
  112. $this->recoveryMock->expects($this->exactly(3))
  113. ->method('isRecoveryEnabledForUser')
  114. ->with('testUser')
  115. ->willReturnOnConsecutiveCalls(true, false);
  116. // Test first if statement
  117. $this->assertNull($this->instance->setPassphrase($this->params));
  118. // Test Second if conditional
  119. $this->keyManagerMock->expects($this->exactly(2))
  120. ->method('userHasKeys')
  121. ->with('testUser')
  122. ->willReturn(true);
  123. $this->assertNull($this->instance->setPassphrase($this->params));
  124. // Test third and final if condition
  125. $this->utilMock->expects($this->once())
  126. ->method('userHasFiles')
  127. ->with('testUser')
  128. ->willReturn(false);
  129. $this->cryptMock->expects($this->once())
  130. ->method('createKeyPair');
  131. $this->keyManagerMock->expects($this->once())
  132. ->method('setPrivateKey');
  133. $this->recoveryMock->expects($this->once())
  134. ->method('recoverUsersFiles')
  135. ->with('password', 'testUser');
  136. $this->assertNull($this->instance->setPassphrase($this->params));
  137. }
  138. public function testSetPasswordNoUser() {
  139. $this->sessionMock->expects($this->once())
  140. ->method('getPrivateKey')
  141. ->willReturn(true);
  142. $userSessionMock = $this->getMockBuilder('OCP\IUserSession')
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $userSessionMock->expects($this->any())->method('getUser')->will($this->returnValue(null));
  146. $this->recoveryMock->expects($this->once())
  147. ->method('isRecoveryEnabledForUser')
  148. ->with('testUser')
  149. ->willReturn(false);
  150. $userHooks = new UserHooks($this->keyManagerMock,
  151. $this->loggerMock,
  152. $this->userSetupMock,
  153. $userSessionMock,
  154. $this->utilMock,
  155. $this->sessionMock,
  156. $this->cryptMock,
  157. $this->recoveryMock
  158. );
  159. $this->assertNull($userHooks->setPassphrase($this->params));
  160. }
  161. public function testPostPasswordReset() {
  162. $this->keyManagerMock->expects($this->once())
  163. ->method('replaceUserKeys')
  164. ->with('testUser');
  165. $this->userSetupMock->expects($this->once())
  166. ->method('setupServerSide')
  167. ->with('testUser', 'password');
  168. $this->assertNull($this->instance->postPasswordReset($this->params));
  169. }
  170. protected function setUp() {
  171. parent::setUp();
  172. $this->loggerMock = $this->getMock('OCP\ILogger');
  173. $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. $this->userSetupMock = $this->getMockBuilder('OCA\Encryption\Users\Setup')
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $this->userSessionMock = $this->getMockBuilder('OCP\IUserSession')
  180. ->disableOriginalConstructor()
  181. ->setMethods([
  182. 'isLoggedIn',
  183. 'getUID',
  184. 'login',
  185. 'logout',
  186. 'setUser',
  187. 'getUser',
  188. 'canChangePassword'
  189. ])
  190. ->getMock();
  191. $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('testUser'));
  192. $this->userSessionMock->expects($this->any())
  193. ->method($this->anything())
  194. ->will($this->returnSelf());
  195. $utilMock = $this->getMockBuilder('OCA\Encryption\Util')
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $sessionMock = $this->getMockBuilder('OCA\Encryption\Session')
  199. ->disableOriginalConstructor()
  200. ->getMock();
  201. $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
  202. ->disableOriginalConstructor()
  203. ->getMock();
  204. $recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery')
  205. ->disableOriginalConstructor()
  206. ->getMock();
  207. $this->sessionMock = $sessionMock;
  208. $this->recoveryMock = $recoveryMock;
  209. $this->utilMock = $utilMock;
  210. $this->instance = new UserHooks($this->keyManagerMock,
  211. $this->loggerMock,
  212. $this->userSetupMock,
  213. $this->userSessionMock,
  214. $this->utilMock,
  215. $this->sessionMock,
  216. $this->cryptMock,
  217. $this->recoveryMock
  218. );
  219. }
  220. }