LostControllerTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 Tests\Core\Controller;
  22. use OC\Authentication\TwoFactorAuth\Manager;
  23. use OC\Core\Controller\LostController;
  24. use OC\Core\Events\BeforePasswordResetEvent;
  25. use OC\Core\Events\PasswordResetEvent;
  26. use OC\Mail\Message;
  27. use OC\Security\RateLimiting\Limiter;
  28. use OCP\AppFramework\Http\JSONResponse;
  29. use OCP\AppFramework\Http\TemplateResponse;
  30. use OCP\AppFramework\Services\IInitialState;
  31. use OCP\Defaults;
  32. use OCP\Encryption\IEncryptionModule;
  33. use OCP\Encryption\IManager;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\IConfig;
  36. use OCP\IL10N;
  37. use OCP\IRequest;
  38. use OCP\IURLGenerator;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. use OCP\Mail\IEMailTemplate;
  42. use OCP\Mail\IMailer;
  43. use OCP\Security\VerificationToken\InvalidTokenException;
  44. use OCP\Security\VerificationToken\IVerificationToken;
  45. use PHPUnit\Framework\MockObject\MockObject;
  46. use Psr\Log\LoggerInterface;
  47. use Test\TestCase;
  48. /**
  49. * Class LostControllerTest
  50. *
  51. * @package OC\Core\Controller
  52. */
  53. class LostControllerTest extends TestCase {
  54. private LostController $lostController;
  55. /** @var IUser */
  56. private $existingUser;
  57. /** @var IURLGenerator | MockObject */
  58. private $urlGenerator;
  59. /** @var IL10N */
  60. private $l10n;
  61. /** @var IUserManager | MockObject */
  62. private $userManager;
  63. /** @var Defaults */
  64. private $defaults;
  65. /** @var IConfig | MockObject */
  66. private $config;
  67. /** @var IMailer | MockObject */
  68. private $mailer;
  69. /** @var IManager|MockObject */
  70. private $encryptionManager;
  71. /** @var IRequest|MockObject */
  72. private $request;
  73. /** @var LoggerInterface|MockObject */
  74. private $logger;
  75. /** @var Manager|MockObject */
  76. private $twofactorManager;
  77. /** @var IInitialState|MockObject */
  78. private $initialState;
  79. /** @var IVerificationToken|MockObject */
  80. private $verificationToken;
  81. /** @var IEventDispatcher|MockObject */
  82. private $eventDispatcher;
  83. /** @var Limiter|MockObject */
  84. private $limiter;
  85. protected function setUp(): void {
  86. parent::setUp();
  87. $this->existingUser = $this->createMock(IUser::class);
  88. $this->existingUser->expects($this->any())
  89. ->method('getEMailAddress')
  90. ->willReturn('test@example.com');
  91. $this->existingUser->expects($this->any())
  92. ->method('getUID')
  93. ->willReturn('ExistingUser');
  94. $this->existingUser->expects($this->any())
  95. ->method('getDisplayName')
  96. ->willReturn('Existing User');
  97. $this->existingUser->expects($this->any())
  98. ->method('isEnabled')
  99. ->willReturn(true);
  100. $this->config = $this->createMock(IConfig::class);
  101. $this->config->expects($this->any())
  102. ->method('getSystemValue')
  103. ->willReturnMap([
  104. ['secret', null, 'SECRET'],
  105. ['secret', '', 'SECRET'],
  106. ['lost_password_link', '', ''],
  107. ]);
  108. $this->l10n = $this->createMock(IL10N::class);
  109. $this->l10n
  110. ->expects($this->any())
  111. ->method('t')
  112. ->willReturnCallback(function ($text, $parameters = []) {
  113. return vsprintf($text, $parameters);
  114. });
  115. $this->defaults = $this->createMock(Defaults::class);
  116. $this->userManager = $this->createMock(IUserManager::class);
  117. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  118. $this->mailer = $this->createMock(IMailer::class);
  119. $this->request = $this->createMock(IRequest::class);
  120. $this->encryptionManager = $this->createMock(IManager::class);
  121. $this->encryptionManager->expects($this->any())
  122. ->method('isEnabled')
  123. ->willReturn(true);
  124. $this->logger = $this->createMock(LoggerInterface::class);
  125. $this->twofactorManager = $this->createMock(Manager::class);
  126. $this->initialState = $this->createMock(IInitialState::class);
  127. $this->verificationToken = $this->createMock(IVerificationToken::class);
  128. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  129. $this->limiter = $this->createMock(Limiter::class);
  130. $this->lostController = new LostController(
  131. 'Core',
  132. $this->request,
  133. $this->urlGenerator,
  134. $this->userManager,
  135. $this->defaults,
  136. $this->l10n,
  137. $this->config,
  138. 'lostpassword-noreply@localhost',
  139. $this->encryptionManager,
  140. $this->mailer,
  141. $this->logger,
  142. $this->twofactorManager,
  143. $this->initialState,
  144. $this->verificationToken,
  145. $this->eventDispatcher,
  146. $this->limiter
  147. );
  148. }
  149. public function testResetFormTokenError() {
  150. $this->userManager->method('get')
  151. ->with('ValidTokenUser')
  152. ->willReturn($this->existingUser);
  153. $this->verificationToken->expects($this->once())
  154. ->method('check')
  155. ->with('12345:MySecretToken', $this->existingUser, 'lostpassword', 'test@example.com')
  156. ->willThrowException(new InvalidTokenException(InvalidTokenException::TOKEN_DECRYPTION_ERROR));
  157. $response = $this->lostController->resetform('12345:MySecretToken', 'ValidTokenUser');
  158. $expectedResponse = new TemplateResponse('core',
  159. 'error',
  160. [
  161. 'errors' => [
  162. ['error' => 'Could not reset password because the token is invalid'],
  163. ]
  164. ],
  165. 'guest');
  166. $expectedResponse->throttle();
  167. $this->assertEquals($expectedResponse, $response);
  168. }
  169. public function testResetFormValidToken() {
  170. $this->userManager->method('get')
  171. ->with('ValidTokenUser')
  172. ->willReturn($this->existingUser);
  173. $this->verificationToken->expects($this->once())
  174. ->method('check')
  175. ->with('MySecretToken', $this->existingUser, 'lostpassword', 'test@example.com');
  176. $this->urlGenerator
  177. ->expects($this->once())
  178. ->method('linkToRouteAbsolute')
  179. ->with('core.lost.setPassword', ['userId' => 'ValidTokenUser', 'token' => 'MySecretToken'])
  180. ->willReturn('https://example.tld/index.php/lostpassword/set/sometoken/someuser');
  181. $this->initialState
  182. ->expects($this->exactly(2))
  183. ->method('provideInitialState')
  184. ->withConsecutive(
  185. ['resetPasswordUser', 'ValidTokenUser'],
  186. ['resetPasswordTarget', 'https://example.tld/index.php/lostpassword/set/sometoken/someuser']
  187. );
  188. $response = $this->lostController->resetform('MySecretToken', 'ValidTokenUser');
  189. $expectedResponse = new TemplateResponse('core',
  190. 'login',
  191. [],
  192. 'guest');
  193. $this->assertEquals($expectedResponse, $response);
  194. }
  195. public function testEmailUnsuccessful() {
  196. $existingUser = 'ExistingUser';
  197. $nonExistingUser = 'NonExistingUser';
  198. $this->userManager
  199. ->expects($this->any())
  200. ->method('userExists')
  201. ->willReturnMap([
  202. [true, $existingUser],
  203. [false, $nonExistingUser]
  204. ]);
  205. $this->logger->expects($this->exactly(0))
  206. ->method('error');
  207. $this->logger->expects($this->exactly(2))
  208. ->method('warning');
  209. $this->userManager
  210. ->method('getByEmail')
  211. ->willReturn([]);
  212. // With a non existing user
  213. $response = $this->lostController->email($nonExistingUser);
  214. $expectedResponse = new JSONResponse([
  215. 'status' => 'success',
  216. ]);
  217. $expectedResponse->throttle();
  218. $this->assertEquals($expectedResponse, $response);
  219. // With no mail address
  220. $this->config
  221. ->expects($this->any())
  222. ->method('getUserValue')
  223. ->with($existingUser, 'settings', 'email')
  224. ->willReturn(null);
  225. $response = $this->lostController->email($existingUser);
  226. $expectedResponse = new JSONResponse([
  227. 'status' => 'success',
  228. ]);
  229. $expectedResponse->throttle();
  230. $this->assertEquals($expectedResponse, $response);
  231. }
  232. public function testEmailSuccessful() {
  233. $this->userManager
  234. ->expects($this->any())
  235. ->method('get')
  236. ->with('ExistingUser')
  237. ->willReturn($this->existingUser);
  238. $this->verificationToken->expects($this->once())
  239. ->method('create')
  240. ->willReturn('ThisIsMaybeANotSoSecretToken!');
  241. $this->urlGenerator
  242. ->expects($this->once())
  243. ->method('linkToRouteAbsolute')
  244. ->with('core.lost.resetform', ['userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'])
  245. ->willReturn('https://example.tld/index.php/lostpassword/');
  246. $message = $this->getMockBuilder('\OC\Mail\Message')
  247. ->disableOriginalConstructor()->getMock();
  248. $message
  249. ->expects($this->once())
  250. ->method('setTo')
  251. ->with(['test@example.com' => 'Existing User']);
  252. $message
  253. ->expects($this->once())
  254. ->method('setFrom')
  255. ->with(['lostpassword-noreply@localhost' => null]);
  256. $emailTemplate = $this->createMock(IEMailTemplate::class);
  257. $emailTemplate->expects($this->any())
  258. ->method('renderHtml')
  259. ->willReturn('HTML body');
  260. $emailTemplate->expects($this->any())
  261. ->method('renderText')
  262. ->willReturn('text body');
  263. $message
  264. ->expects($this->once())
  265. ->method('useTemplate')
  266. ->with($emailTemplate);
  267. $this->mailer
  268. ->expects($this->once())
  269. ->method('createEMailTemplate')
  270. ->willReturn($emailTemplate);
  271. $this->mailer
  272. ->expects($this->once())
  273. ->method('createMessage')
  274. ->willReturn($message);
  275. $this->mailer
  276. ->expects($this->once())
  277. ->method('send')
  278. ->with($message);
  279. $response = $this->lostController->email('ExistingUser');
  280. $expectedResponse = new JSONResponse(['status' => 'success']);
  281. $expectedResponse->throttle();
  282. $this->assertEquals($expectedResponse, $response);
  283. }
  284. public function testEmailWithMailSuccessful() {
  285. $this->userManager
  286. ->expects($this->any())
  287. ->method('get')
  288. ->with('test@example.com')
  289. ->willReturn(null);
  290. $this->userManager
  291. ->expects($this->any())
  292. ->method('getByEmail')
  293. ->with('test@example.com')
  294. ->willReturn([$this->existingUser]);
  295. $this->verificationToken->expects($this->once())
  296. ->method('create')
  297. ->willReturn('ThisIsMaybeANotSoSecretToken!');
  298. $this->urlGenerator
  299. ->expects($this->once())
  300. ->method('linkToRouteAbsolute')
  301. ->with('core.lost.resetform', ['userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'])
  302. ->willReturn('https://example.tld/index.php/lostpassword/');
  303. $message = $this->getMockBuilder('\OC\Mail\Message')
  304. ->disableOriginalConstructor()->getMock();
  305. $message
  306. ->expects($this->once())
  307. ->method('setTo')
  308. ->with(['test@example.com' => 'Existing User']);
  309. $message
  310. ->expects($this->once())
  311. ->method('setFrom')
  312. ->with(['lostpassword-noreply@localhost' => null]);
  313. $emailTemplate = $this->createMock(IEMailTemplate::class);
  314. $emailTemplate->expects($this->any())
  315. ->method('renderHtml')
  316. ->willReturn('HTML body');
  317. $emailTemplate->expects($this->any())
  318. ->method('renderText')
  319. ->willReturn('text body');
  320. $message
  321. ->expects($this->once())
  322. ->method('useTemplate')
  323. ->with($emailTemplate);
  324. $this->mailer
  325. ->expects($this->once())
  326. ->method('createEMailTemplate')
  327. ->willReturn($emailTemplate);
  328. $this->mailer
  329. ->expects($this->once())
  330. ->method('createMessage')
  331. ->willReturn($message);
  332. $this->mailer
  333. ->expects($this->once())
  334. ->method('send')
  335. ->with($message);
  336. $response = $this->lostController->email('test@example.com');
  337. $expectedResponse = new JSONResponse(['status' => 'success']);
  338. $expectedResponse->throttle();
  339. $this->assertEquals($expectedResponse, $response);
  340. }
  341. public function testEmailCantSendException() {
  342. $this->userManager
  343. ->expects($this->any())
  344. ->method('get')
  345. ->with('ExistingUser')
  346. ->willReturn($this->existingUser);
  347. $this->verificationToken->expects($this->once())
  348. ->method('create')
  349. ->willReturn('ThisIsMaybeANotSoSecretToken!');
  350. $this->urlGenerator
  351. ->expects($this->once())
  352. ->method('linkToRouteAbsolute')
  353. ->with('core.lost.resetform', ['userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'])
  354. ->willReturn('https://example.tld/index.php/lostpassword/');
  355. $message = $this->createMock(Message::class);
  356. $message
  357. ->expects($this->once())
  358. ->method('setTo')
  359. ->with(['test@example.com' => 'Existing User']);
  360. $message
  361. ->expects($this->once())
  362. ->method('setFrom')
  363. ->with(['lostpassword-noreply@localhost' => null]);
  364. $emailTemplate = $this->createMock(IEMailTemplate::class);
  365. $emailTemplate->expects($this->any())
  366. ->method('renderHtml')
  367. ->willReturn('HTML body');
  368. $emailTemplate->expects($this->any())
  369. ->method('renderText')
  370. ->willReturn('text body');
  371. $message
  372. ->expects($this->once())
  373. ->method('useTemplate')
  374. ->with($emailTemplate);
  375. $this->mailer
  376. ->expects($this->once())
  377. ->method('createEMailTemplate')
  378. ->willReturn($emailTemplate);
  379. $this->mailer
  380. ->expects($this->once())
  381. ->method('createMessage')
  382. ->willReturn($message);
  383. $this->mailer
  384. ->expects($this->once())
  385. ->method('send')
  386. ->with($message)
  387. ->will($this->throwException(new \Exception()));
  388. $this->logger->expects($this->exactly(1))
  389. ->method('error');
  390. $response = $this->lostController->email('ExistingUser');
  391. $expectedResponse = new JSONResponse(['status' => 'success']);
  392. $expectedResponse->throttle();
  393. $this->assertEquals($expectedResponse, $response);
  394. }
  395. public function testSetPasswordUnsuccessful() {
  396. $this->config->method('getUserValue')
  397. ->with('ValidTokenUser', 'core', 'lostpassword', null)
  398. ->willReturn('encryptedData');
  399. $this->existingUser->method('getLastLogin')
  400. ->willReturn(12344);
  401. $this->existingUser->expects($this->once())
  402. ->method('setPassword')
  403. ->with('NewPassword')
  404. ->willReturn(false);
  405. $this->userManager->method('get')
  406. ->with('ValidTokenUser')
  407. ->willReturn($this->existingUser);
  408. $beforePasswordResetEvent = new BeforePasswordResetEvent($this->existingUser, 'NewPassword');
  409. $this->eventDispatcher
  410. ->expects($this->once())
  411. ->method('dispatchTyped')
  412. ->with($beforePasswordResetEvent);
  413. $this->config->expects($this->never())
  414. ->method('deleteUserValue');
  415. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  416. $expectedResponse = ['status' => 'error', 'msg' => ''];
  417. $this->assertSame($expectedResponse, $response->getData());
  418. }
  419. public function testSetPasswordSuccessful() {
  420. $this->config->method('getUserValue')
  421. ->with('ValidTokenUser', 'core', 'lostpassword', null)
  422. ->willReturn('encryptedData');
  423. $this->existingUser->method('getLastLogin')
  424. ->willReturn(12344);
  425. $this->existingUser->expects($this->once())
  426. ->method('setPassword')
  427. ->with('NewPassword')
  428. ->willReturn(true);
  429. $this->userManager->method('get')
  430. ->with('ValidTokenUser')
  431. ->willReturn($this->existingUser);
  432. $beforePasswordResetEvent = new BeforePasswordResetEvent($this->existingUser, 'NewPassword');
  433. $passwordResetEvent = new PasswordResetEvent($this->existingUser, 'NewPassword');
  434. $this->eventDispatcher
  435. ->expects($this->exactly(2))
  436. ->method('dispatchTyped')
  437. ->withConsecutive([$beforePasswordResetEvent], [$passwordResetEvent]);
  438. $this->config->expects($this->once())
  439. ->method('deleteUserValue')
  440. ->with('ValidTokenUser', 'core', 'lostpassword');
  441. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  442. $expectedResponse = ['user' => 'ValidTokenUser', 'status' => 'success'];
  443. $this->assertSame($expectedResponse, $response->getData());
  444. }
  445. public function testSetPasswordExpiredToken() {
  446. $this->config->method('getUserValue')
  447. ->with('ValidTokenUser', 'core', 'lostpassword', null)
  448. ->willReturn('encryptedData');
  449. $this->userManager->method('get')
  450. ->with('ValidTokenUser')
  451. ->willReturn($this->existingUser);
  452. $this->verificationToken->expects($this->atLeastOnce())
  453. ->method('check')
  454. ->willThrowException(new InvalidTokenException(InvalidTokenException::TOKEN_EXPIRED));
  455. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  456. $expectedResponse = [
  457. 'status' => 'error',
  458. 'msg' => 'Could not reset password because the token is expired',
  459. ];
  460. $this->assertSame($expectedResponse, $response->getData());
  461. }
  462. public function testSetPasswordInvalidDataInDb() {
  463. $this->config->method('getUserValue')
  464. ->with('ValidTokenUser', 'core', 'lostpassword', null)
  465. ->willReturn('invalidEncryptedData');
  466. $this->userManager
  467. ->method('get')
  468. ->with('ValidTokenUser')
  469. ->willReturn($this->existingUser);
  470. $this->verificationToken->expects($this->atLeastOnce())
  471. ->method('check')
  472. ->willThrowException(new InvalidTokenException(InvalidTokenException::TOKEN_INVALID_FORMAT));
  473. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  474. $expectedResponse = [
  475. 'status' => 'error',
  476. 'msg' => 'Could not reset password because the token is invalid',
  477. ];
  478. $this->assertSame($expectedResponse, $response->getData());
  479. }
  480. public function testIsSetPasswordWithoutTokenFailing() {
  481. $this->config->method('getUserValue')
  482. ->with('ValidTokenUser', 'core', 'lostpassword', null)
  483. ->willReturn('aValidtoken');
  484. $this->userManager->method('get')
  485. ->with('ValidTokenUser')
  486. ->willReturn($this->existingUser);
  487. $this->verificationToken->expects($this->atLeastOnce())
  488. ->method('check')
  489. ->willThrowException(new InvalidTokenException(InvalidTokenException::TOKEN_MISMATCH));
  490. $response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true);
  491. $expectedResponse = [
  492. 'status' => 'error',
  493. 'msg' => 'Could not reset password because the token is invalid'
  494. ];
  495. $this->assertSame($expectedResponse, $response->getData());
  496. }
  497. public function testSetPasswordForDisabledUser() {
  498. $user = $this->createMock(IUser::class);
  499. $user->expects($this->any())
  500. ->method('isEnabled')
  501. ->willReturn(false);
  502. $user->expects($this->never())
  503. ->method('setPassword');
  504. $user->expects($this->any())
  505. ->method('getEMailAddress')
  506. ->willReturn('random@example.org');
  507. $this->config->method('getUserValue')
  508. ->with('ValidTokenUser', 'core', 'lostpassword', null)
  509. ->willReturn('encryptedData');
  510. $this->userManager->method('get')
  511. ->with('DisabledUser')
  512. ->willReturn($user);
  513. $this->verificationToken->expects($this->atLeastOnce())
  514. ->method('check')
  515. ->willThrowException(new InvalidTokenException(InvalidTokenException::USER_UNKNOWN));
  516. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'DisabledUser', 'NewPassword', true);
  517. $expectedResponse = [
  518. 'status' => 'error',
  519. 'msg' => 'Could not reset password because the token is invalid'
  520. ];
  521. $this->assertSame($expectedResponse, $response->getData());
  522. }
  523. public function testSendEmailNoEmail() {
  524. $user = $this->createMock(IUser::class);
  525. $user->expects($this->any())
  526. ->method('isEnabled')
  527. ->willReturn(true);
  528. $this->userManager->method('userExists')
  529. ->with('ExistingUser')
  530. ->willReturn(true);
  531. $this->userManager->method('get')
  532. ->with('ExistingUser')
  533. ->willReturn($user);
  534. $this->logger->expects($this->exactly(0))
  535. ->method('error');
  536. $this->logger->expects($this->once())
  537. ->method('warning');
  538. $response = $this->lostController->email('ExistingUser');
  539. $expectedResponse = new JSONResponse(['status' => 'success']);
  540. $expectedResponse->throttle();
  541. $this->assertEquals($expectedResponse, $response);
  542. }
  543. public function testSetPasswordEncryptionDontProceedPerUserKey() {
  544. /** @var IEncryptionModule|MockObject $encryptionModule */
  545. $encryptionModule = $this->createMock(IEncryptionModule::class);
  546. $encryptionModule->expects($this->once())->method('needDetailedAccessList')->willReturn(true);
  547. $this->encryptionManager->expects($this->once())->method('getEncryptionModules')
  548. ->willReturn([0 => ['callback' => function () use ($encryptionModule) {
  549. return $encryptionModule;
  550. }]]);
  551. $response = $this->lostController->setPassword('myToken', 'user', 'newpass', false);
  552. $expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true];
  553. $this->assertSame($expectedResponse, $response->getData());
  554. }
  555. public function testSetPasswordDontProceedMasterKey() {
  556. $encryptionModule = $this->createMock(IEncryptionModule::class);
  557. $encryptionModule->expects($this->once())->method('needDetailedAccessList')->willReturn(false);
  558. $this->encryptionManager->expects($this->once())->method('getEncryptionModules')
  559. ->willReturn([0 => ['callback' => function () use ($encryptionModule) {
  560. return $encryptionModule;
  561. }]]);
  562. $this->config->method('getUserValue')
  563. ->with('ValidTokenUser', 'core', 'lostpassword', null)
  564. ->willReturn('encryptedData');
  565. $this->existingUser->method('getLastLogin')
  566. ->willReturn(12344);
  567. $this->existingUser->expects($this->once())
  568. ->method('setPassword')
  569. ->with('NewPassword')
  570. ->willReturn(true);
  571. $this->userManager->method('get')
  572. ->with('ValidTokenUser')
  573. ->willReturn($this->existingUser);
  574. $this->config->expects($this->once())
  575. ->method('deleteUserValue')
  576. ->with('ValidTokenUser', 'core', 'lostpassword');
  577. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', false);
  578. $expectedResponse = ['user' => 'ValidTokenUser', 'status' => 'success'];
  579. $this->assertSame($expectedResponse, $response->getData());
  580. }
  581. public function testTwoUsersWithSameEmail() {
  582. $user1 = $this->createMock(IUser::class);
  583. $user1->expects($this->any())
  584. ->method('getEMailAddress')
  585. ->willReturn('test@example.com');
  586. $user1->expects($this->any())
  587. ->method('getUID')
  588. ->willReturn('User1');
  589. $user1->expects($this->any())
  590. ->method('isEnabled')
  591. ->willReturn(true);
  592. $user2 = $this->createMock(IUser::class);
  593. $user2->expects($this->any())
  594. ->method('getEMailAddress')
  595. ->willReturn('test@example.com');
  596. $user2->expects($this->any())
  597. ->method('getUID')
  598. ->willReturn('User2');
  599. $user2->expects($this->any())
  600. ->method('isEnabled')
  601. ->willReturn(true);
  602. $this->userManager
  603. ->method('get')
  604. ->willReturn(null);
  605. $this->userManager
  606. ->method('getByEmail')
  607. ->willReturn([$user1, $user2]);
  608. $this->logger->expects($this->exactly(0))
  609. ->method('error');
  610. $this->logger->expects($this->once())
  611. ->method('warning');
  612. // request password reset for test@example.com
  613. $response = $this->lostController->email('test@example.com');
  614. $expectedResponse = new JSONResponse([
  615. 'status' => 'success'
  616. ]);
  617. $expectedResponse->throttle();
  618. $this->assertEquals($expectedResponse, $response);
  619. }
  620. /**
  621. * @return array
  622. */
  623. public function dataTwoUserswithSameEmailOneDisabled(): array {
  624. return [
  625. ['user1' => true, 'user2' => false],
  626. ['user1' => false, 'user2' => true]
  627. ];
  628. }
  629. /**
  630. * @dataProvider dataTwoUserswithSameEmailOneDisabled
  631. * @param bool $userEnabled1
  632. * @param bool $userEnabled2
  633. */
  634. public function testTwoUsersWithSameEmailOneDisabled(bool $userEnabled1, bool $userEnabled2): void {
  635. $user1 = $this->createMock(IUser::class);
  636. $user1->method('getEMailAddress')
  637. ->willReturn('test@example.com');
  638. $user1->method('getUID')
  639. ->willReturn('User1');
  640. $user1->method('isEnabled')
  641. ->willReturn($userEnabled1);
  642. $user2 = $this->createMock(IUser::class);
  643. $user2->method('getEMailAddress')
  644. ->willReturn('test@example.com');
  645. $user2->method('getUID')
  646. ->willReturn('User2');
  647. $user2->method('isEnabled')
  648. ->willReturn($userEnabled2);
  649. $this->userManager
  650. ->method('get')
  651. ->willReturn(null);
  652. $this->userManager
  653. ->method('getByEmail')
  654. ->willReturn([$user1, $user2]);
  655. $result = self::invokePrivate($this->lostController, 'findUserByIdOrMail', ['test@example.com']);
  656. $this->assertInstanceOf(IUser::class, $result);
  657. }
  658. }