2014-10-20 21:05:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-08-22 21:42:45 +03:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-10-20 21:05:48 +04:00
|
|
|
*/
|
|
|
|
|
2016-05-19 11:43:49 +03:00
|
|
|
namespace Tests\Core\Controller;
|
2015-12-01 14:05:40 +03:00
|
|
|
|
2019-01-28 18:12:06 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\Manager;
|
2016-05-19 11:43:49 +03:00
|
|
|
use OC\Core\Controller\LostController;
|
2016-11-03 21:08:56 +03:00
|
|
|
use OC\Mail\Message;
|
2017-04-14 14:42:40 +03:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2014-10-20 21:05:48 +04:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
2018-08-10 17:40:09 +03:00
|
|
|
use OCP\Encryption\IEncryptionModule;
|
2016-09-29 17:38:29 +03:00
|
|
|
use OCP\Encryption\IManager;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\IConfig;
|
2019-07-25 18:04:33 +03:00
|
|
|
use OCP\IInitialStateService;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\IL10N;
|
2019-01-15 17:03:37 +03:00
|
|
|
use OCP\ILogger;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2017-04-12 01:24:58 +03:00
|
|
|
use OCP\Mail\IEMailTemplate;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\Mail\IMailer;
|
2016-08-28 15:22:29 +03:00
|
|
|
use OCP\Security\ICrypto;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2014-10-20 21:05:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class LostControllerTest
|
|
|
|
*
|
2016-01-20 12:42:19 +03:00
|
|
|
* @package OC\Core\Controller
|
2014-10-20 21:05:48 +04:00
|
|
|
*/
|
2016-10-06 15:11:04 +03:00
|
|
|
class LostControllerTest extends \Test\TestCase {
|
2014-10-20 21:05:48 +04:00
|
|
|
|
|
|
|
/** @var LostController */
|
|
|
|
private $lostController;
|
2015-12-01 14:05:40 +03:00
|
|
|
/** @var IUser */
|
|
|
|
private $existingUser;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IURLGenerator | \PHPUnit\Framework\MockObject\MockObject */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $urlGenerator;
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $userManager;
|
2017-04-07 23:42:43 +03:00
|
|
|
/** @var Defaults */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $defaults;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $config;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IMailer | \PHPUnit\Framework\MockObject\MockObject */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $mailer;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISecureRandom | \PHPUnit\Framework\MockObject\MockObject */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $secureRandom;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-09-29 17:38:29 +03:00
|
|
|
private $encryptionManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ITimeFactory | \PHPUnit\Framework\MockObject\MockObject */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $timeFactory;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
2015-12-01 14:05:40 +03:00
|
|
|
private $request;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */
|
2016-08-28 15:22:29 +03:00
|
|
|
private $crypto;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2019-01-15 17:03:37 +03:00
|
|
|
private $logger;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
|
2019-01-28 18:12:06 +03:00
|
|
|
private $twofactorManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IInitialStateService|\PHPUnit\Framework\MockObject\MockObject */
|
2019-07-25 18:04:33 +03:00
|
|
|
private $initialStateService;
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-10-06 15:11:04 +03:00
|
|
|
parent::setUp();
|
2015-12-01 14:05:40 +03:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->existingUser = $this->createMock(IUser::class);
|
2017-03-28 21:39:36 +03:00
|
|
|
$this->existingUser->expects($this->any())
|
|
|
|
->method('getEMailAddress')
|
2015-12-01 14:05:40 +03:00
|
|
|
->willReturn('test@example.com');
|
2017-03-28 21:39:36 +03:00
|
|
|
$this->existingUser->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('ExistingUser');
|
2017-08-18 14:03:40 +03:00
|
|
|
$this->existingUser->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
2015-12-01 14:05:40 +03:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2017-05-11 17:46:43 +03:00
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->willReturnMap([
|
|
|
|
['secret', null, 'SECRET'],
|
|
|
|
['secret', '', 'SECRET'],
|
|
|
|
['lost_password_link', '', ''],
|
|
|
|
]);
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->l10n
|
2015-02-01 19:46:48 +03:00
|
|
|
->expects($this->any())
|
|
|
|
->method('t')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($text, $parameters = []) {
|
2015-02-01 19:46:48 +03:00
|
|
|
return vsprintf($text, $parameters);
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2017-04-07 23:42:43 +03:00
|
|
|
$this->defaults = $this->getMockBuilder('\OCP\Defaults')
|
2014-10-20 21:05:48 +04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userManager = $this->getMockBuilder(IUserManager::class)
|
2014-10-20 21:05:48 +04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)
|
2014-10-20 21:05:48 +04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
|
2014-10-20 21:05:48 +04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')
|
2015-02-12 18:03:51 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->timeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory')
|
2014-10-20 21:05:48 +04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->request = $this->getMockBuilder(IRequest::class)
|
2015-08-22 21:42:45 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-09-29 17:38:29 +03:00
|
|
|
$this->encryptionManager = $this->getMockBuilder(IManager::class)
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->encryptionManager->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
2016-08-28 15:22:29 +03:00
|
|
|
$this->crypto = $this->createMock(ICrypto::class);
|
2019-01-15 17:03:37 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2019-01-28 18:12:06 +03:00
|
|
|
$this->twofactorManager = $this->createMock(Manager::class);
|
2019-07-25 18:04:33 +03:00
|
|
|
$this->initialStateService = $this->createMock(IInitialStateService::class);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->lostController = new LostController(
|
|
|
|
'Core',
|
|
|
|
$this->request,
|
|
|
|
$this->urlGenerator,
|
|
|
|
$this->userManager,
|
|
|
|
$this->defaults,
|
|
|
|
$this->l10n,
|
|
|
|
$this->config,
|
|
|
|
$this->secureRandom,
|
|
|
|
'lostpassword-noreply@localhost',
|
2016-09-29 17:38:29 +03:00
|
|
|
$this->encryptionManager,
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->mailer,
|
2016-08-28 15:22:29 +03:00
|
|
|
$this->timeFactory,
|
2019-01-15 17:03:37 +03:00
|
|
|
$this->crypto,
|
2019-01-28 18:12:06 +03:00
|
|
|
$this->logger,
|
2019-07-25 18:04:33 +03:00
|
|
|
$this->twofactorManager,
|
|
|
|
$this->initialStateService
|
2015-12-01 14:05:40 +03:00
|
|
|
);
|
2014-10-20 21:05:48 +04:00
|
|
|
}
|
|
|
|
|
2016-08-28 15:22:29 +03:00
|
|
|
public function testResetFormWithNotExistingUser() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->userManager->method('get')
|
2016-08-28 15:22:29 +03:00
|
|
|
->with('NotExistingUser')
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'error',
|
|
|
|
[
|
|
|
|
'errors' => [
|
|
|
|
['error' => 'Couldn\'t reset password because the token is invalid'],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->assertEquals($expectedResponse, $this->lostController->resetform('MySecretToken', 'NotExistingUser'));
|
2016-05-19 23:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testResetFormInvalidTokenMatch() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn('encryptedToken');
|
|
|
|
$this->existingUser->method('getLastLogin')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12344);
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->userManager->method('get')
|
2016-05-19 23:40:53 +03:00
|
|
|
->with('ValidTokenUser')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn($this->existingUser);
|
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedToken'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
|
|
|
|
|
|
|
$response = $this->lostController->resetform('12345:MySecretToken', 'ValidTokenUser');
|
2016-05-19 23:40:53 +03:00
|
|
|
$expectedResponse = new TemplateResponse('core',
|
|
|
|
'error',
|
|
|
|
[
|
|
|
|
'errors' => [
|
|
|
|
['error' => 'Couldn\'t reset password because the token is invalid'],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'guest');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testResetFormExpiredToken() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->userManager->method('get')
|
2016-05-19 23:40:53 +03:00
|
|
|
->with('ValidTokenUser')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn($this->existingUser);
|
2016-05-19 23:40:53 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('encryptedToken');
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedToken'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(999999);
|
|
|
|
|
|
|
|
$response = $this->lostController->resetform('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser');
|
2016-05-19 23:40:53 +03:00
|
|
|
$expectedResponse = new TemplateResponse('core',
|
|
|
|
'error',
|
|
|
|
[
|
|
|
|
'errors' => [
|
|
|
|
['error' => 'Couldn\'t reset password because the token is expired'],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'guest');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2016-05-19 23:40:53 +03:00
|
|
|
public function testResetFormValidToken() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->existingUser->method('getLastLogin')
|
|
|
|
->willReturn(12344);
|
|
|
|
$this->userManager->method('get')
|
2016-05-19 23:40:53 +03:00
|
|
|
->with('ValidTokenUser')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn($this->existingUser);
|
2016-05-19 23:40:53 +03:00
|
|
|
$this->timeFactory
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getTime')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn(12348);
|
|
|
|
|
|
|
|
$this->config->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn('encryptedToken');
|
|
|
|
|
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedToken'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->urlGenerator
|
2014-10-20 21:05:48 +04:00
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
2020-03-26 11:30:18 +03:00
|
|
|
->with('core.lost.setPassword', ['userId' => 'ValidTokenUser', 'token' => 'TheOnlyAndOnlyOneTokenToResetThePassword'])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('https://example.tld/index.php/lostpassword/');
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2019-07-25 18:04:33 +03:00
|
|
|
$this->initialStateService->expects($this->at(0))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with('core', 'resetPasswordUser', 'ValidTokenUser');
|
|
|
|
$this->initialStateService->expects($this->at(1))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with('core', 'resetPasswordTarget', 'https://example.tld/index.php/lostpassword/');
|
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$response = $this->lostController->resetform('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser');
|
2016-01-20 12:42:19 +03:00
|
|
|
$expectedResponse = new TemplateResponse('core',
|
2019-07-25 18:04:33 +03:00
|
|
|
'login',
|
|
|
|
[],
|
2014-10-20 21:05:48 +04:00
|
|
|
'guest');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2017-04-14 14:42:40 +03:00
|
|
|
public function testEmailUnsuccessful() {
|
2014-10-20 21:05:48 +04:00
|
|
|
$existingUser = 'ExistingUser';
|
|
|
|
$nonExistingUser = 'NonExistingUser';
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->userManager
|
2014-10-20 21:05:48 +04:00
|
|
|
->expects($this->any())
|
|
|
|
->method('userExists')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturnMap([
|
|
|
|
[true, $existingUser],
|
|
|
|
[false, $nonExistingUser]
|
|
|
|
]);
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2019-07-26 16:21:41 +03:00
|
|
|
$this->logger->expects($this->exactly(0))
|
2019-01-15 17:03:37 +03:00
|
|
|
->method('logException');
|
2019-07-26 16:21:41 +03:00
|
|
|
$this->logger->expects($this->exactly(2))
|
|
|
|
->method('warning');
|
2019-01-15 17:03:37 +03:00
|
|
|
|
2017-08-09 16:46:51 +03:00
|
|
|
$this->userManager
|
|
|
|
->method('getByEmail')
|
|
|
|
->willReturn([]);
|
|
|
|
|
2014-10-20 21:05:48 +04:00
|
|
|
// With a non existing user
|
|
|
|
$response = $this->lostController->email($nonExistingUser);
|
2017-04-14 14:42:40 +03:00
|
|
|
$expectedResponse = new JSONResponse([
|
2019-01-15 17:03:37 +03:00
|
|
|
'status' => 'success',
|
2017-04-14 14:42:40 +03:00
|
|
|
]);
|
|
|
|
$expectedResponse->throttle();
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2014-10-20 21:05:48 +04:00
|
|
|
|
|
|
|
// With no mail address
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->config
|
2014-10-20 21:05:48 +04:00
|
|
|
->expects($this->any())
|
|
|
|
->method('getUserValue')
|
|
|
|
->with($existingUser, 'settings', 'email')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(null);
|
2014-10-20 21:05:48 +04:00
|
|
|
$response = $this->lostController->email($existingUser);
|
2017-04-14 14:42:40 +03:00
|
|
|
$expectedResponse = new JSONResponse([
|
2019-01-15 17:03:37 +03:00
|
|
|
'status' => 'success',
|
2017-04-14 14:42:40 +03:00
|
|
|
]);
|
|
|
|
$expectedResponse->throttle();
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2014-10-20 21:05:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmailSuccessful() {
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->secureRandom
|
2014-10-20 21:05:48 +04:00
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
|
|
|
->with('21')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('ThisIsMaybeANotSoSecretToken!');
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->userManager
|
|
|
|
->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with('ExistingUser')
|
|
|
|
->willReturn($this->existingUser);
|
|
|
|
$this->timeFactory
|
2015-08-22 21:42:45 +03:00
|
|
|
->expects($this->once())
|
|
|
|
->method('getTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12348);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->config
|
2014-10-20 21:05:48 +04:00
|
|
|
->expects($this->once())
|
|
|
|
->method('setUserValue')
|
2016-11-03 21:08:56 +03:00
|
|
|
->with('ExistingUser', 'core', 'lostpassword', 'encryptedToken');
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->urlGenerator
|
2014-10-20 21:05:48 +04:00
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
2020-03-26 11:30:18 +03:00
|
|
|
->with('core.lost.resetform', ['userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('https://example.tld/index.php/lostpassword/');
|
2015-02-12 18:03:51 +03:00
|
|
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$message
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('setTo')
|
|
|
|
->with(['test@example.com' => 'ExistingUser']);
|
|
|
|
$message
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('setFrom')
|
|
|
|
->with(['lostpassword-noreply@localhost' => null]);
|
2017-09-15 12:01:21 +03:00
|
|
|
|
2017-04-12 01:24:58 +03:00
|
|
|
$emailTemplate = $this->createMock(IEMailTemplate::class);
|
|
|
|
$emailTemplate->expects($this->any())
|
2017-04-19 01:09:25 +03:00
|
|
|
->method('renderHtml')
|
2017-04-12 01:24:58 +03:00
|
|
|
->willReturn('HTML body');
|
|
|
|
$emailTemplate->expects($this->any())
|
|
|
|
->method('renderText')
|
|
|
|
->willReturn('text body');
|
2017-09-15 12:01:21 +03:00
|
|
|
|
|
|
|
$message
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('useTemplate')
|
|
|
|
->with($emailTemplate);
|
|
|
|
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->mailer
|
2015-02-12 18:03:51 +03:00
|
|
|
->expects($this->at(0))
|
2017-04-12 01:24:58 +03:00
|
|
|
->method('createEMailTemplate')
|
|
|
|
->willReturn($emailTemplate);
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->at(1))
|
2015-02-12 18:03:51 +03:00
|
|
|
->method('createMessage')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($message);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->mailer
|
2017-04-12 01:24:58 +03:00
|
|
|
->expects($this->at(2))
|
2015-02-12 18:03:51 +03:00
|
|
|
->method('send')
|
|
|
|
->with($message);
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->crypto->method('encrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('12348:ThisIsMaybeANotSoSecretToken!'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('encryptedToken');
|
|
|
|
|
2014-10-21 20:31:41 +04:00
|
|
|
$response = $this->lostController->email('ExistingUser');
|
2017-04-14 14:42:40 +03:00
|
|
|
$expectedResponse = new JSONResponse(['status' => 'success']);
|
|
|
|
$expectedResponse->throttle();
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2014-10-20 21:05:48 +04:00
|
|
|
}
|
|
|
|
|
2017-03-28 21:39:36 +03:00
|
|
|
public function testEmailWithMailSuccessful() {
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->secureRandom
|
2015-02-12 18:03:51 +03:00
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
|
|
|
->with('21')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('ThisIsMaybeANotSoSecretToken!');
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->userManager
|
2017-03-28 21:39:36 +03:00
|
|
|
->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with('test@example.com')
|
|
|
|
->willReturn(null);
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getByEmail')
|
|
|
|
->with('test@example.com')
|
|
|
|
->willReturn([$this->existingUser]);
|
|
|
|
$this->timeFactory
|
2015-02-12 18:03:51 +03:00
|
|
|
->expects($this->once())
|
2017-03-28 21:39:36 +03:00
|
|
|
->method('getTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12348);
|
2017-03-28 21:39:36 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setUserValue')
|
|
|
|
->with('ExistingUser', 'core', 'lostpassword', 'encryptedToken');
|
|
|
|
$this->urlGenerator
|
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
2020-03-26 11:30:18 +03:00
|
|
|
->with('core.lost.resetform', ['userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('https://example.tld/index.php/lostpassword/');
|
2017-03-28 21:39:36 +03:00
|
|
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$message
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('setTo')
|
|
|
|
->with(['test@example.com' => 'ExistingUser']);
|
|
|
|
$message
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('setFrom')
|
|
|
|
->with(['lostpassword-noreply@localhost' => null]);
|
2017-09-15 12:01:21 +03:00
|
|
|
|
2017-04-12 01:24:58 +03:00
|
|
|
$emailTemplate = $this->createMock(IEMailTemplate::class);
|
|
|
|
$emailTemplate->expects($this->any())
|
2017-04-19 01:09:25 +03:00
|
|
|
->method('renderHtml')
|
2017-04-12 01:24:58 +03:00
|
|
|
->willReturn('HTML body');
|
|
|
|
$emailTemplate->expects($this->any())
|
|
|
|
->method('renderText')
|
|
|
|
->willReturn('text body');
|
2017-09-15 12:01:21 +03:00
|
|
|
|
|
|
|
$message
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('useTemplate')
|
|
|
|
->with($emailTemplate);
|
|
|
|
|
2017-03-28 21:39:36 +03:00
|
|
|
$this->mailer
|
|
|
|
->expects($this->at(0))
|
2017-04-12 01:24:58 +03:00
|
|
|
->method('createEMailTemplate')
|
|
|
|
->willReturn($emailTemplate);
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->at(1))
|
2017-03-28 21:39:36 +03:00
|
|
|
->method('createMessage')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($message);
|
2017-03-28 21:39:36 +03:00
|
|
|
$this->mailer
|
2017-04-12 01:24:58 +03:00
|
|
|
->expects($this->at(2))
|
2017-03-28 21:39:36 +03:00
|
|
|
->method('send')
|
|
|
|
->with($message);
|
|
|
|
|
|
|
|
$this->crypto->method('encrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('12348:ThisIsMaybeANotSoSecretToken!'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('encryptedToken');
|
|
|
|
|
|
|
|
$response = $this->lostController->email('test@example.com');
|
2017-04-14 14:42:40 +03:00
|
|
|
$expectedResponse = new JSONResponse(['status' => 'success']);
|
|
|
|
$expectedResponse->throttle();
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2017-03-28 21:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmailCantSendException() {
|
|
|
|
$this->secureRandom
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
|
|
|
->with('21')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('ThisIsMaybeANotSoSecretToken!');
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->userManager
|
|
|
|
->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with('ExistingUser')
|
|
|
|
->willReturn($this->existingUser);
|
|
|
|
$this->config
|
2015-02-12 18:03:51 +03:00
|
|
|
->expects($this->once())
|
|
|
|
->method('setUserValue')
|
2016-11-03 21:08:56 +03:00
|
|
|
->with('ExistingUser', 'core', 'lostpassword', 'encryptedToken');
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->timeFactory
|
2015-08-22 21:42:45 +03:00
|
|
|
->expects($this->once())
|
|
|
|
->method('getTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12348);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->urlGenerator
|
2015-02-12 18:03:51 +03:00
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
2020-03-26 11:30:18 +03:00
|
|
|
->with('core.lost.resetform', ['userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('https://example.tld/index.php/lostpassword/');
|
2016-11-03 21:08:56 +03:00
|
|
|
$message = $this->createMock(Message::class);
|
2015-02-12 18:03:51 +03:00
|
|
|
$message
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('setTo')
|
|
|
|
->with(['test@example.com' => 'ExistingUser']);
|
|
|
|
$message
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('setFrom')
|
|
|
|
->with(['lostpassword-noreply@localhost' => null]);
|
2017-09-15 12:01:21 +03:00
|
|
|
|
2017-04-12 01:24:58 +03:00
|
|
|
$emailTemplate = $this->createMock(IEMailTemplate::class);
|
|
|
|
$emailTemplate->expects($this->any())
|
2017-04-19 01:09:25 +03:00
|
|
|
->method('renderHtml')
|
2017-04-12 01:24:58 +03:00
|
|
|
->willReturn('HTML body');
|
|
|
|
$emailTemplate->expects($this->any())
|
|
|
|
->method('renderText')
|
|
|
|
->willReturn('text body');
|
2017-09-15 12:01:21 +03:00
|
|
|
|
|
|
|
$message
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('useTemplate')
|
|
|
|
->with($emailTemplate);
|
|
|
|
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->mailer
|
2015-02-12 18:03:51 +03:00
|
|
|
->expects($this->at(0))
|
2017-04-12 01:24:58 +03:00
|
|
|
->method('createEMailTemplate')
|
|
|
|
->willReturn($emailTemplate);
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->at(1))
|
2015-02-12 18:03:51 +03:00
|
|
|
->method('createMessage')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($message);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->mailer
|
2017-04-12 01:24:58 +03:00
|
|
|
->expects($this->at(2))
|
2015-02-12 18:03:51 +03:00
|
|
|
->method('send')
|
|
|
|
->with($message)
|
|
|
|
->will($this->throwException(new \Exception()));
|
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->crypto->method('encrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('12348:ThisIsMaybeANotSoSecretToken!'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('encryptedToken');
|
|
|
|
|
2019-01-15 17:03:37 +03:00
|
|
|
$this->logger->expects($this->exactly(1))
|
|
|
|
->method('logException');
|
|
|
|
|
2015-02-12 18:03:51 +03:00
|
|
|
$response = $this->lostController->email('ExistingUser');
|
2019-01-15 17:03:37 +03:00
|
|
|
$expectedResponse = new JSONResponse(['status' => 'success']);
|
2017-04-14 14:42:40 +03:00
|
|
|
$expectedResponse->throttle();
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2015-02-12 18:03:51 +03:00
|
|
|
}
|
|
|
|
|
2014-10-20 21:05:48 +04:00
|
|
|
public function testSetPasswordUnsuccessful() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config->method('getUserValue')
|
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
|
|
|
->willReturn('encryptedData');
|
|
|
|
$this->existingUser->method('getLastLogin')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12344);
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->existingUser->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('NewPassword')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userManager->method('get')
|
|
|
|
->with('ValidTokenUser')
|
|
|
|
->willReturn($this->existingUser);
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
|
|
|
$this->timeFactory->method('getTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12348);
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedData'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
|
2020-03-26 11:30:18 +03:00
|
|
|
$expectedResponse = ['status' => 'error', 'msg' => ''];
|
2014-10-20 21:05:48 +04:00
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPasswordSuccessful() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn('encryptedData');
|
|
|
|
$this->existingUser->method('getLastLogin')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12344);
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->existingUser->expects($this->once())
|
2014-10-20 21:05:48 +04:00
|
|
|
->method('setPassword')
|
|
|
|
->with('NewPassword')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn(true);
|
|
|
|
$this->userManager->method('get')
|
2014-10-20 21:05:48 +04:00
|
|
|
->with('ValidTokenUser')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn($this->existingUser);
|
|
|
|
$this->config->expects($this->once())
|
2014-10-20 21:05:48 +04:00
|
|
|
->method('deleteUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword');
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->timeFactory->method('getTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12348);
|
2014-10-20 21:05:48 +04:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedData'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
|
|
|
|
2014-10-20 21:05:48 +04:00
|
|
|
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
|
2020-03-26 11:30:18 +03:00
|
|
|
$expectedResponse = ['user' => 'ValidTokenUser', 'status' => 'success'];
|
2014-10-20 21:05:48 +04:00
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
2015-02-01 19:34:03 +03:00
|
|
|
|
2015-08-22 21:42:45 +03:00
|
|
|
public function testSetPasswordExpiredToken() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn('encryptedData');
|
|
|
|
$this->userManager->method('get')
|
2015-08-22 21:42:45 +03:00
|
|
|
->with('ValidTokenUser')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn($this->existingUser);
|
|
|
|
$this->timeFactory->method('getTime')
|
2018-08-31 10:26:09 +03:00
|
|
|
->willReturn(617146);
|
2016-11-03 21:08:56 +03:00
|
|
|
|
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedData'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
2015-08-22 21:42:45 +03:00
|
|
|
|
|
|
|
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
|
|
|
|
$expectedResponse = [
|
|
|
|
'status' => 'error',
|
|
|
|
'msg' => 'Couldn\'t reset password because the token is expired',
|
|
|
|
];
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPasswordInvalidDataInDb() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn('invalidEncryptedData');
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->userManager
|
2015-08-22 21:42:45 +03:00
|
|
|
->method('get')
|
|
|
|
->with('ValidTokenUser')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn($this->existingUser);
|
|
|
|
|
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('invalidEncryptedData'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('TheOnlyAndOnlyOneTokenToResetThePassword');
|
2015-08-22 21:42:45 +03:00
|
|
|
|
|
|
|
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
|
|
|
|
$expectedResponse = [
|
|
|
|
'status' => 'error',
|
|
|
|
'msg' => 'Couldn\'t reset password because the token is invalid',
|
|
|
|
];
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPasswordExpiredTokenDueToLogin() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn('encryptedData');
|
|
|
|
$this->existingUser->method('getLastLogin')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12346);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->userManager
|
2015-08-22 21:42:45 +03:00
|
|
|
->method('get')
|
|
|
|
->with('ValidTokenUser')
|
2016-11-03 21:08:56 +03:00
|
|
|
->willReturn($this->existingUser);
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->timeFactory
|
2015-08-22 21:42:45 +03:00
|
|
|
->method('getTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12345);
|
2015-08-22 21:42:45 +03:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedData'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
|
|
|
|
2015-08-22 21:42:45 +03:00
|
|
|
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
|
|
|
|
$expectedResponse = [
|
|
|
|
'status' => 'error',
|
|
|
|
'msg' => 'Couldn\'t reset password because the token is expired',
|
|
|
|
];
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:34:03 +03:00
|
|
|
public function testIsSetPasswordWithoutTokenFailing() {
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->config->method('getUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
2018-01-13 15:58:06 +03:00
|
|
|
->willReturn('aValidtoken');
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->userManager->method('get')
|
|
|
|
->with('ValidTokenUser')
|
|
|
|
->willReturn($this->existingUser);
|
|
|
|
|
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
2018-01-13 15:58:06 +03:00
|
|
|
$this->equalTo('aValidtoken'),
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willThrowException(new \Exception());
|
2015-02-01 19:34:03 +03:00
|
|
|
|
|
|
|
$response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true);
|
2015-02-01 19:46:48 +03:00
|
|
|
$expectedResponse = [
|
|
|
|
'status' => 'error',
|
|
|
|
'msg' => 'Couldn\'t reset password because the token is invalid'
|
2020-04-09 10:22:29 +03:00
|
|
|
];
|
2015-02-01 19:34:03 +03:00
|
|
|
$this->assertSame($expectedResponse, $response);
|
2019-08-19 00:27:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsSetPasswordTokenNullFailing() {
|
|
|
|
$this->config->method('getUserValue')
|
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
|
|
|
->willReturn(null);
|
|
|
|
$this->userManager->method('get')
|
|
|
|
->with('ValidTokenUser')
|
|
|
|
->willReturn($this->existingUser);
|
|
|
|
|
|
|
|
$response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true);
|
|
|
|
$expectedResponse = [
|
|
|
|
'status' => 'error',
|
|
|
|
'msg' => 'Couldn\'t reset password because the token is invalid'
|
|
|
|
];
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
2015-02-01 19:34:03 +03:00
|
|
|
}
|
|
|
|
|
2017-08-18 14:03:40 +03:00
|
|
|
public function testSetPasswordForDisabledUser() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(false);
|
|
|
|
$user->expects($this->never())
|
|
|
|
->method('setPassword');
|
|
|
|
|
|
|
|
$this->config->method('getUserValue')
|
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
|
|
|
->willReturn('encryptedData');
|
|
|
|
$this->userManager->method('get')
|
|
|
|
->with('DisabledUser')
|
2019-08-19 00:18:39 +03:00
|
|
|
->willReturn($user);
|
2017-08-18 14:03:40 +03:00
|
|
|
|
|
|
|
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'DisabledUser', 'NewPassword', true);
|
|
|
|
$expectedResponse = [
|
|
|
|
'status' => 'error',
|
|
|
|
'msg' => 'Couldn\'t reset password because the token is invalid'
|
2020-04-09 10:22:29 +03:00
|
|
|
];
|
2017-08-18 14:03:40 +03:00
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
public function testSendEmailNoEmail() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
2017-08-18 14:03:40 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
2016-11-03 21:08:56 +03:00
|
|
|
$this->userManager->method('userExists')
|
|
|
|
->with('ExistingUser')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->userManager->method('get')
|
|
|
|
->with('ExistingUser')
|
|
|
|
->willReturn($user);
|
|
|
|
|
2019-07-26 16:21:41 +03:00
|
|
|
$this->logger->expects($this->exactly(0))
|
2019-01-15 17:03:37 +03:00
|
|
|
->method('logException');
|
2019-07-26 16:21:41 +03:00
|
|
|
$this->logger->expects($this->once())
|
|
|
|
->method('warning');
|
2019-01-15 17:03:37 +03:00
|
|
|
|
2016-11-03 21:08:56 +03:00
|
|
|
$response = $this->lostController->email('ExistingUser');
|
2019-01-15 17:03:37 +03:00
|
|
|
$expectedResponse = new JSONResponse(['status' => 'success']);
|
2017-04-14 14:42:40 +03:00
|
|
|
$expectedResponse->throttle();
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2016-11-03 21:08:56 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:40:09 +03:00
|
|
|
public function testSetPasswordEncryptionDontProceedPerUserKey() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IEncryptionModule|\PHPUnit\Framework\MockObject\MockObject $encryptionModule */
|
2018-08-10 17:40:09 +03:00
|
|
|
$encryptionModule = $this->createMock(IEncryptionModule::class);
|
|
|
|
$encryptionModule->expects($this->once())->method('needDetailedAccessList')->willReturn(true);
|
|
|
|
$this->encryptionManager->expects($this->once())->method('getEncryptionModules')
|
2020-04-10 15:19:56 +03:00
|
|
|
->willReturn([0 => ['callback' => function () use ($encryptionModule) {
|
|
|
|
return $encryptionModule;
|
|
|
|
}]]);
|
2016-11-03 21:08:56 +03:00
|
|
|
$response = $this->lostController->setPassword('myToken', 'user', 'newpass', false);
|
|
|
|
$expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true];
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:40:09 +03:00
|
|
|
public function testSetPasswordDontProceedMasterKey() {
|
|
|
|
$encryptionModule = $this->createMock(IEncryptionModule::class);
|
|
|
|
$encryptionModule->expects($this->once())->method('needDetailedAccessList')->willReturn(false);
|
|
|
|
$this->encryptionManager->expects($this->once())->method('getEncryptionModules')
|
2020-04-10 15:19:56 +03:00
|
|
|
->willReturn([0 => ['callback' => function () use ($encryptionModule) {
|
|
|
|
return $encryptionModule;
|
|
|
|
}]]);
|
2018-08-10 17:40:09 +03:00
|
|
|
$this->config->method('getUserValue')
|
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword', null)
|
|
|
|
->willReturn('encryptedData');
|
|
|
|
$this->existingUser->method('getLastLogin')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12344);
|
2018-08-10 17:40:09 +03:00
|
|
|
$this->existingUser->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('NewPassword')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->userManager->method('get')
|
|
|
|
->with('ValidTokenUser')
|
|
|
|
->willReturn($this->existingUser);
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
|
|
|
->with('ValidTokenUser', 'core', 'lostpassword');
|
|
|
|
$this->timeFactory->method('getTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12348);
|
2018-08-10 17:40:09 +03:00
|
|
|
|
|
|
|
$this->crypto->method('decrypt')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('encryptedData'),
|
|
|
|
$this->equalTo('test@example.comSECRET')
|
|
|
|
)->willReturn('12345:TheOnlyAndOnlyOneTokenToResetThePassword');
|
|
|
|
|
|
|
|
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', false);
|
2020-03-26 11:30:18 +03:00
|
|
|
$expectedResponse = ['user' => 'ValidTokenUser', 'status' => 'success'];
|
2018-08-10 17:40:09 +03:00
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2018-08-18 17:51:59 +03:00
|
|
|
public function testTwoUsersWithSameEmail() {
|
|
|
|
$user1 = $this->createMock(IUser::class);
|
|
|
|
$user1->expects($this->any())
|
|
|
|
->method('getEMailAddress')
|
|
|
|
->willReturn('test@example.com');
|
|
|
|
$user1->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('User1');
|
|
|
|
$user1->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$user2 = $this->createMock(IUser::class);
|
|
|
|
$user2->expects($this->any())
|
|
|
|
->method('getEMailAddress')
|
|
|
|
->willReturn('test@example.com');
|
|
|
|
$user2->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('User2');
|
|
|
|
$user2->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->userManager
|
|
|
|
->method('get')
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
$this->userManager
|
|
|
|
->method('getByEmail')
|
|
|
|
->willReturn([$user1, $user2]);
|
|
|
|
|
2019-07-26 16:21:41 +03:00
|
|
|
$this->logger->expects($this->exactly(0))
|
2019-01-15 17:03:37 +03:00
|
|
|
->method('logException');
|
2019-07-26 16:21:41 +03:00
|
|
|
$this->logger->expects($this->once())
|
|
|
|
->method('warning');
|
2019-01-15 17:03:37 +03:00
|
|
|
|
2018-08-18 17:51:59 +03:00
|
|
|
// request password reset for test@example.com
|
|
|
|
$response = $this->lostController->email('test@example.com');
|
|
|
|
|
|
|
|
$expectedResponse = new JSONResponse([
|
2019-01-15 17:03:37 +03:00
|
|
|
'status' => 'success'
|
2018-08-18 17:51:59 +03:00
|
|
|
]);
|
|
|
|
$expectedResponse->throttle();
|
|
|
|
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2019-07-09 12:58:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataTwoUserswithSameEmailOneDisabled(): array {
|
|
|
|
return [
|
|
|
|
['user1' => true, 'user2' => false],
|
|
|
|
['user1' => false, 'user2' => true]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTwoUserswithSameEmailOneDisabled
|
|
|
|
* @param bool $userEnabled1
|
|
|
|
* @param bool $userEnabled2
|
|
|
|
*/
|
|
|
|
public function testTwoUsersWithSameEmailOneDisabled(bool $userEnabled1, bool $userEnabled2): void {
|
2018-08-18 17:51:59 +03:00
|
|
|
$user1 = $this->createMock(IUser::class);
|
2019-07-09 12:58:14 +03:00
|
|
|
$user1->method('getEMailAddress')
|
2018-08-18 17:51:59 +03:00
|
|
|
->willReturn('test@example.com');
|
2019-07-09 12:58:14 +03:00
|
|
|
$user1->method('getUID')
|
2018-08-18 17:51:59 +03:00
|
|
|
->willReturn('User1');
|
2019-07-09 12:58:14 +03:00
|
|
|
$user1->method('isEnabled')
|
|
|
|
->willReturn($userEnabled1);
|
2018-08-18 17:51:59 +03:00
|
|
|
|
|
|
|
$user2 = $this->createMock(IUser::class);
|
2019-07-09 12:58:14 +03:00
|
|
|
$user2->method('getEMailAddress')
|
2018-08-18 17:51:59 +03:00
|
|
|
->willReturn('test@example.com');
|
2019-07-09 12:58:14 +03:00
|
|
|
$user2->method('getUID')
|
2018-08-18 17:51:59 +03:00
|
|
|
->willReturn('User2');
|
2019-07-09 12:58:14 +03:00
|
|
|
$user2->method('isEnabled')
|
|
|
|
->willReturn($userEnabled2);
|
2018-08-18 17:51:59 +03:00
|
|
|
|
|
|
|
$this->userManager
|
|
|
|
->method('get')
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
$this->userManager
|
|
|
|
->method('getByEmail')
|
|
|
|
->willReturn([$user1, $user2]);
|
|
|
|
|
2019-07-09 12:58:14 +03:00
|
|
|
$result = self::invokePrivate($this->lostController, 'findUserByIdOrMail', ['test@example.com']);
|
|
|
|
$this->assertInstanceOf(IUser::class, $result);
|
2018-08-18 17:51:59 +03:00
|
|
|
}
|
2014-10-20 21:05:48 +04:00
|
|
|
}
|