2016-08-15 22:07:57 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-08-15 22:07:57 +03:00
|
|
|
namespace Tests\Core\Controller;
|
|
|
|
|
2016-10-06 11:24:02 +03:00
|
|
|
use OC\HintException;
|
2016-08-15 22:07:57 +03:00
|
|
|
use OC\User\Session;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Settings\Controller\ChangePasswordController;
|
2016-08-15 22:07:57 +03:00
|
|
|
use OCP\App\IAppManager;
|
2017-04-18 18:55:51 +03:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2016-08-15 22:07:57 +03:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IL10N;
|
2017-04-18 18:55:51 +03:00
|
|
|
use OCP\IRequest;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IUser;
|
2016-08-15 22:07:57 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
|
|
|
|
class ChangePasswordControllerTest extends \Test\TestCase {
|
|
|
|
/** @var string */
|
|
|
|
private $userId = 'currentUser';
|
2017-04-18 18:55:51 +03:00
|
|
|
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-15 22:07:57 +03:00
|
|
|
private $userManager;
|
2017-04-18 18:55:51 +03:00
|
|
|
/** @var Session|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-15 22:07:57 +03:00
|
|
|
private $userSession;
|
2017-04-18 18:55:51 +03:00
|
|
|
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-15 22:07:57 +03:00
|
|
|
private $groupManager;
|
2017-04-18 18:55:51 +03:00
|
|
|
/** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-15 22:07:57 +03:00
|
|
|
private $appManager;
|
2017-04-18 18:55:51 +03:00
|
|
|
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-15 22:07:57 +03:00
|
|
|
private $l;
|
|
|
|
/** @var ChangePasswordController */
|
|
|
|
private $controller;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-08-15 22:07:57 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-07-19 19:28:06 +03:00
|
|
|
$this->userManager = $this->createMock(\OC\User\Manager::class);
|
2017-04-18 18:55:51 +03:00
|
|
|
$this->userSession = $this->createMock(Session::class);
|
2017-07-19 19:28:06 +03:00
|
|
|
$this->groupManager = $this->createMock(\OC\Group\Manager::class);
|
2017-04-18 18:55:51 +03:00
|
|
|
$this->appManager = $this->createMock(IAppManager::class);
|
|
|
|
$this->l = $this->createMock(IL10N::class);
|
2016-08-15 22:07:57 +03:00
|
|
|
$this->l->method('t')->will($this->returnArgument(0));
|
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */
|
|
|
|
$request = $this->createMock(IRequest::class);
|
2016-08-15 22:07:57 +03:00
|
|
|
|
|
|
|
$this->controller = new ChangePasswordController(
|
|
|
|
'core',
|
|
|
|
$request,
|
|
|
|
$this->userId,
|
|
|
|
$this->userManager,
|
|
|
|
$this->userSession,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->appManager,
|
|
|
|
$this->l
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePersonalPasswordWrongPassword() {
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->with($this->userId, 'old')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$expects = new JSONResponse([
|
2016-08-15 22:07:57 +03:00
|
|
|
'status' => 'error',
|
|
|
|
'data' => [
|
|
|
|
'message' => 'Wrong password',
|
|
|
|
],
|
2017-04-18 18:55:51 +03:00
|
|
|
]);
|
|
|
|
$expects->throttle();
|
2016-08-15 22:07:57 +03:00
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-08-15 22:07:57 +03:00
|
|
|
}
|
|
|
|
|
2016-10-06 11:24:02 +03:00
|
|
|
public function testChangePersonalPasswordCommonPassword() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-10-06 11:24:02 +03:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->with($this->userId, 'old')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('new')
|
|
|
|
->will($this->throwException(new HintException('Common password')));
|
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$expects = new JSONResponse([
|
2016-10-06 11:24:02 +03:00
|
|
|
'status' => 'error',
|
|
|
|
'data' => [
|
|
|
|
'message' => 'Common password',
|
|
|
|
],
|
2017-04-18 18:55:51 +03:00
|
|
|
]);
|
2016-10-06 11:24:02 +03:00
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-10-06 11:24:02 +03:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:07:57 +03:00
|
|
|
public function testChangePersonalPasswordNoNewPassword() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-08-15 22:07:57 +03:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->with($this->userId, 'old')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$expects = [
|
|
|
|
'status' => 'error',
|
|
|
|
];
|
|
|
|
|
|
|
|
$res = $this->controller->changePersonalPassword('old');
|
|
|
|
|
|
|
|
$this->assertEquals($expects, $res->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePersonalPasswordCantSetPassword() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-08-15 22:07:57 +03:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->with($this->userId, 'old')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('new')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$expects = new JSONResponse([
|
2016-08-15 22:07:57 +03:00
|
|
|
'status' => 'error',
|
2017-04-18 18:55:51 +03:00
|
|
|
]);
|
2016-08-15 22:07:57 +03:00
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-08-15 22:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePersonalPassword() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-08-15 22:07:57 +03:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->with($this->userId, 'old')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('new')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('updateSessionTokenPassword')
|
|
|
|
->with('new');
|
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$expects = new JSONResponse([
|
2016-08-15 22:07:57 +03:00
|
|
|
'status' => 'success',
|
|
|
|
'data' => [
|
|
|
|
'message' => 'Saved',
|
|
|
|
],
|
2017-04-18 18:55:51 +03:00
|
|
|
]);
|
2016-08-15 22:07:57 +03:00
|
|
|
|
2017-04-18 18:55:51 +03:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-08-15 22:07:57 +03:00
|
|
|
}
|
|
|
|
}
|