lostcontrollertest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Core\Controller;
  22. use OCP\AppFramework\Http\TemplateResponse;
  23. use OCP\AppFramework\Utility\ITimeFactory;
  24. use OCP\IConfig;
  25. use OCP\IL10N;
  26. use OCP\IRequest;
  27. use OCP\IURLGenerator;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use OCP\Mail\IMailer;
  31. use OCP\Security\ISecureRandom;
  32. use PHPUnit_Framework_MockObject_MockObject;
  33. /**
  34. * Class LostControllerTest
  35. *
  36. * @package OC\Core\Controller
  37. */
  38. class LostControllerTest extends \PHPUnit_Framework_TestCase {
  39. /** @var LostController */
  40. private $lostController;
  41. /** @var IUser */
  42. private $existingUser;
  43. /** @var IURLGenerator | PHPUnit_Framework_MockObject_MockObject */
  44. private $urlGenerator;
  45. /** @var IL10N */
  46. private $l10n;
  47. /** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */
  48. private $userManager;
  49. /** @var \OC_Defaults */
  50. private $defaults;
  51. /** @var IConfig | PHPUnit_Framework_MockObject_MockObject */
  52. private $config;
  53. /** @var IMailer | PHPUnit_Framework_MockObject_MockObject */
  54. private $mailer;
  55. /** @var ISecureRandom | PHPUnit_Framework_MockObject_MockObject */
  56. private $secureRandom;
  57. /** @var ITimeFactory | PHPUnit_Framework_MockObject_MockObject */
  58. private $timeFactory;
  59. /** @var IRequest */
  60. private $request;
  61. protected function setUp() {
  62. $this->existingUser = $this->getMockBuilder('OCP\IUser')
  63. ->disableOriginalConstructor()->getMock();
  64. $this->existingUser
  65. ->expects($this->any())
  66. ->method('getEMailAddress')
  67. ->willReturn('test@example.com');
  68. $this->config = $this->getMockBuilder('\OCP\IConfig')
  69. ->disableOriginalConstructor()->getMock();
  70. $this->l10n = $this->getMockBuilder('\OCP\IL10N')
  71. ->disableOriginalConstructor()->getMock();
  72. $this->l10n
  73. ->expects($this->any())
  74. ->method('t')
  75. ->will($this->returnCallback(function($text, $parameters = array()) {
  76. return vsprintf($text, $parameters);
  77. }));
  78. $this->defaults = $this->getMockBuilder('\OC_Defaults')
  79. ->disableOriginalConstructor()->getMock();
  80. $this->userManager = $this->getMockBuilder('\OCP\IUserManager')
  81. ->disableOriginalConstructor()->getMock();
  82. $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
  83. ->disableOriginalConstructor()->getMock();
  84. $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
  85. ->disableOriginalConstructor()->getMock();
  86. $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')
  87. ->disableOriginalConstructor()->getMock();
  88. $this->timeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory')
  89. ->disableOriginalConstructor()->getMock();
  90. $this->request = $this->getMockBuilder('OCP\IRequest')
  91. ->disableOriginalConstructor()->getMock();
  92. $this->lostController = new LostController(
  93. 'Core',
  94. $this->request,
  95. $this->urlGenerator,
  96. $this->userManager,
  97. $this->defaults,
  98. $this->l10n,
  99. $this->config,
  100. $this->secureRandom,
  101. 'lostpassword-noreply@localhost',
  102. true,
  103. $this->mailer,
  104. $this->timeFactory
  105. );
  106. }
  107. public function testResetFormUnsuccessful() {
  108. $userId = 'admin';
  109. $token = 'MySecretToken';
  110. $this->urlGenerator
  111. ->expects($this->once())
  112. ->method('linkToRouteAbsolute')
  113. ->with('core.lost.setPassword', array('userId' => 'admin', 'token' => 'MySecretToken'))
  114. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  115. $response = $this->lostController->resetform($token, $userId);
  116. $expectedResponse = new TemplateResponse('core',
  117. 'lostpassword/resetpassword',
  118. array(
  119. 'link' => 'https://ownCloud.com/index.php/lostpassword/',
  120. ),
  121. 'guest');
  122. $this->assertEquals($expectedResponse, $response);
  123. }
  124. public function testEmailUnsucessful() {
  125. $existingUser = 'ExistingUser';
  126. $nonExistingUser = 'NonExistingUser';
  127. $this->userManager
  128. ->expects($this->any())
  129. ->method('userExists')
  130. ->will($this->returnValueMap(array(
  131. array(true, $existingUser),
  132. array(false, $nonExistingUser)
  133. )));
  134. // With a non existing user
  135. $response = $this->lostController->email($nonExistingUser);
  136. $expectedResponse = [
  137. 'status' => 'error',
  138. 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
  139. ];
  140. $this->assertSame($expectedResponse, $response);
  141. // With no mail address
  142. $this->config
  143. ->expects($this->any())
  144. ->method('getUserValue')
  145. ->with($existingUser, 'settings', 'email')
  146. ->will($this->returnValue(null));
  147. $response = $this->lostController->email($existingUser);
  148. $expectedResponse = [
  149. 'status' => 'error',
  150. 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
  151. ];
  152. $this->assertSame($expectedResponse, $response);
  153. }
  154. public function testEmailSuccessful() {
  155. $this->secureRandom
  156. ->expects($this->once())
  157. ->method('generate')
  158. ->with('21')
  159. ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!'));
  160. $this->userManager
  161. ->expects($this->once())
  162. ->method('userExists')
  163. ->with('ExistingUser')
  164. ->will($this->returnValue(true));
  165. $this->userManager
  166. ->expects($this->any())
  167. ->method('get')
  168. ->with('ExistingUser')
  169. ->willReturn($this->existingUser);
  170. $this->timeFactory
  171. ->expects($this->once())
  172. ->method('getTime')
  173. ->will($this->returnValue(12348));
  174. $this->config
  175. ->expects($this->once())
  176. ->method('setUserValue')
  177. ->with('ExistingUser', 'owncloud', 'lostpassword', '12348:ThisIsMaybeANotSoSecretToken!');
  178. $this->urlGenerator
  179. ->expects($this->once())
  180. ->method('linkToRouteAbsolute')
  181. ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'))
  182. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  183. $message = $this->getMockBuilder('\OC\Mail\Message')
  184. ->disableOriginalConstructor()->getMock();
  185. $message
  186. ->expects($this->at(0))
  187. ->method('setTo')
  188. ->with(['test@example.com' => 'ExistingUser']);
  189. $message
  190. ->expects($this->at(1))
  191. ->method('setSubject')
  192. ->with(' password reset');
  193. $message
  194. ->expects($this->at(2))
  195. ->method('setPlainBody')
  196. ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/');
  197. $message
  198. ->expects($this->at(3))
  199. ->method('setFrom')
  200. ->with(['lostpassword-noreply@localhost' => null]);
  201. $this->mailer
  202. ->expects($this->at(0))
  203. ->method('createMessage')
  204. ->will($this->returnValue($message));
  205. $this->mailer
  206. ->expects($this->at(1))
  207. ->method('send')
  208. ->with($message);
  209. $response = $this->lostController->email('ExistingUser');
  210. $expectedResponse = array('status' => 'success');
  211. $this->assertSame($expectedResponse, $response);
  212. }
  213. public function testEmailCantSendException() {
  214. $this->secureRandom
  215. ->expects($this->once())
  216. ->method('generate')
  217. ->with('21')
  218. ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!'));
  219. $this->userManager
  220. ->expects($this->once())
  221. ->method('userExists')
  222. ->with('ExistingUser')
  223. ->will($this->returnValue(true));
  224. $this->userManager
  225. ->expects($this->any())
  226. ->method('get')
  227. ->with('ExistingUser')
  228. ->willReturn($this->existingUser);
  229. $this->config
  230. ->expects($this->once())
  231. ->method('setUserValue')
  232. ->with('ExistingUser', 'owncloud', 'lostpassword', '12348:ThisIsMaybeANotSoSecretToken!');
  233. $this->timeFactory
  234. ->expects($this->once())
  235. ->method('getTime')
  236. ->will($this->returnValue(12348));
  237. $this->urlGenerator
  238. ->expects($this->once())
  239. ->method('linkToRouteAbsolute')
  240. ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'))
  241. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  242. $message = $this->getMockBuilder('\OC\Mail\Message')
  243. ->disableOriginalConstructor()->getMock();
  244. $message
  245. ->expects($this->at(0))
  246. ->method('setTo')
  247. ->with(['test@example.com' => 'ExistingUser']);
  248. $message
  249. ->expects($this->at(1))
  250. ->method('setSubject')
  251. ->with(' password reset');
  252. $message
  253. ->expects($this->at(2))
  254. ->method('setPlainBody')
  255. ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/');
  256. $message
  257. ->expects($this->at(3))
  258. ->method('setFrom')
  259. ->with(['lostpassword-noreply@localhost' => null]);
  260. $this->mailer
  261. ->expects($this->at(0))
  262. ->method('createMessage')
  263. ->will($this->returnValue($message));
  264. $this->mailer
  265. ->expects($this->at(1))
  266. ->method('send')
  267. ->with($message)
  268. ->will($this->throwException(new \Exception()));
  269. $response = $this->lostController->email('ExistingUser');
  270. $expectedResponse = ['status' => 'error', 'msg' => 'Couldn\'t send reset email. Please contact your administrator.'];
  271. $this->assertSame($expectedResponse, $response);
  272. }
  273. public function testSetPasswordUnsuccessful() {
  274. $this->config
  275. ->expects($this->once())
  276. ->method('getUserValue')
  277. ->with('InvalidTokenUser', 'owncloud', 'lostpassword', null)
  278. ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
  279. // With an invalid token
  280. $userName = 'InvalidTokenUser';
  281. $response = $this->lostController->setPassword('wrongToken', $userName, 'NewPassword', true);
  282. $expectedResponse = [
  283. 'status' => 'error',
  284. 'msg' => 'Couldn\'t reset password because the token is invalid'
  285. ];
  286. $this->assertSame($expectedResponse, $response);
  287. // With a valid token and no proceed
  288. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword!', $userName, 'NewPassword', false);
  289. $expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true];
  290. $this->assertSame($expectedResponse, $response);
  291. }
  292. public function testSetPasswordSuccessful() {
  293. $this->config
  294. ->expects($this->once())
  295. ->method('getUserValue')
  296. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  297. ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword'));
  298. $user = $this->getMockBuilder('\OCP\IUser')
  299. ->disableOriginalConstructor()->getMock();
  300. $user
  301. ->expects($this->once())
  302. ->method('getLastLogin')
  303. ->will($this->returnValue(12344));
  304. $user->expects($this->once())
  305. ->method('setPassword')
  306. ->with('NewPassword')
  307. ->will($this->returnValue(true));
  308. $this->userManager
  309. ->expects($this->once())
  310. ->method('get')
  311. ->with('ValidTokenUser')
  312. ->will($this->returnValue($user));
  313. $this->config
  314. ->expects($this->once())
  315. ->method('deleteUserValue')
  316. ->with('ValidTokenUser', 'owncloud', 'lostpassword');
  317. $this->timeFactory
  318. ->expects($this->once())
  319. ->method('getTime')
  320. ->will($this->returnValue(12348));
  321. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  322. $expectedResponse = array('status' => 'success');
  323. $this->assertSame($expectedResponse, $response);
  324. }
  325. public function testSetPasswordExpiredToken() {
  326. $this->config
  327. ->expects($this->once())
  328. ->method('getUserValue')
  329. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  330. ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword'));
  331. $user = $this->getMockBuilder('\OCP\IUser')
  332. ->disableOriginalConstructor()->getMock();
  333. $this->userManager
  334. ->expects($this->once())
  335. ->method('get')
  336. ->with('ValidTokenUser')
  337. ->will($this->returnValue($user));
  338. $this->timeFactory
  339. ->expects($this->once())
  340. ->method('getTime')
  341. ->will($this->returnValue(55546));
  342. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  343. $expectedResponse = [
  344. 'status' => 'error',
  345. 'msg' => 'Couldn\'t reset password because the token is expired',
  346. ];
  347. $this->assertSame($expectedResponse, $response);
  348. }
  349. public function testSetPasswordInvalidDataInDb() {
  350. $this->config
  351. ->expects($this->once())
  352. ->method('getUserValue')
  353. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  354. ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
  355. $user = $this->getMockBuilder('\OCP\IUser')
  356. ->disableOriginalConstructor()->getMock();
  357. $this->userManager
  358. ->expects($this->once())
  359. ->method('get')
  360. ->with('ValidTokenUser')
  361. ->will($this->returnValue($user));
  362. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  363. $expectedResponse = [
  364. 'status' => 'error',
  365. 'msg' => 'Couldn\'t reset password because the token is invalid',
  366. ];
  367. $this->assertSame($expectedResponse, $response);
  368. }
  369. public function testSetPasswordExpiredTokenDueToLogin() {
  370. $this->config
  371. ->expects($this->once())
  372. ->method('getUserValue')
  373. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  374. ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword'));
  375. $user = $this->getMockBuilder('\OCP\IUser')
  376. ->disableOriginalConstructor()->getMock();
  377. $user
  378. ->expects($this->once())
  379. ->method('getLastLogin')
  380. ->will($this->returnValue(12346));
  381. $this->userManager
  382. ->expects($this->once())
  383. ->method('get')
  384. ->with('ValidTokenUser')
  385. ->will($this->returnValue($user));
  386. $this->timeFactory
  387. ->expects($this->once())
  388. ->method('getTime')
  389. ->will($this->returnValue(12345));
  390. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  391. $expectedResponse = [
  392. 'status' => 'error',
  393. 'msg' => 'Couldn\'t reset password because the token is expired',
  394. ];
  395. $this->assertSame($expectedResponse, $response);
  396. }
  397. public function testIsSetPasswordWithoutTokenFailing() {
  398. $this->config
  399. ->expects($this->once())
  400. ->method('getUserValue')
  401. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  402. ->will($this->returnValue(null));
  403. $response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true);
  404. $expectedResponse = [
  405. 'status' => 'error',
  406. 'msg' => 'Couldn\'t reset password because the token is invalid'
  407. ];
  408. $this->assertSame($expectedResponse, $response);
  409. }
  410. }