2016-08-15 21:07:57 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2024-05-10 15:09:14 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-08-15 21:07:57 +02:00
|
|
|
*/
|
2019-11-22 20:52:10 +01:00
|
|
|
|
2016-08-15 21:07:57 +02:00
|
|
|
namespace Tests\Core\Controller;
|
|
|
|
|
|
|
|
use OC\User\Session;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCA\Settings\Controller\ChangePasswordController;
|
2016-08-15 21:07:57 +02:00
|
|
|
use OCP\App\IAppManager;
|
2017-04-18 17:55:51 +02:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2021-06-29 19:20:33 -04:00
|
|
|
use OCP\HintException;
|
2016-08-15 21:07:57 +02:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IL10N;
|
2017-04-18 17:55:51 +02:00
|
|
|
use OCP\IRequest;
|
2017-10-24 15:26:53 +02:00
|
|
|
use OCP\IUser;
|
2016-08-15 21:07:57 +02:00
|
|
|
use OCP\IUserManager;
|
|
|
|
|
|
|
|
class ChangePasswordControllerTest extends \Test\TestCase {
|
|
|
|
/** @var string */
|
|
|
|
private $userId = 'currentUser';
|
2020-05-25 23:00:00 +02:00
|
|
|
/** @var string */
|
|
|
|
private $loginName = 'ua1337';
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-08-15 21:07:57 +02:00
|
|
|
private $userManager;
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var Session|\PHPUnit\Framework\MockObject\MockObject */
|
2016-08-15 21:07:57 +02:00
|
|
|
private $userSession;
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-08-15 21:07:57 +02:00
|
|
|
private $groupManager;
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-08-15 21:07:57 +02:00
|
|
|
private $appManager;
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2016-08-15 21:07:57 +02:00
|
|
|
private $l;
|
|
|
|
/** @var ChangePasswordController */
|
|
|
|
private $controller;
|
|
|
|
|
2019-11-27 15:27:18 +01:00
|
|
|
protected function setUp(): void {
|
2016-08-15 21:07:57 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-07-19 18:28:06 +02:00
|
|
|
$this->userManager = $this->createMock(\OC\User\Manager::class);
|
2017-04-18 17:55:51 +02:00
|
|
|
$this->userSession = $this->createMock(Session::class);
|
2017-07-19 18:28:06 +02:00
|
|
|
$this->groupManager = $this->createMock(\OC\Group\Manager::class);
|
2017-04-18 17:55:51 +02:00
|
|
|
$this->appManager = $this->createMock(IAppManager::class);
|
|
|
|
$this->l = $this->createMock(IL10N::class);
|
2020-03-25 22:21:27 +01:00
|
|
|
$this->l->method('t')->willReturnArgument(0);
|
2016-08-15 21:07:57 +02:00
|
|
|
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
|
2017-04-18 17:55:51 +02:00
|
|
|
$request = $this->createMock(IRequest::class);
|
2016-08-15 21:07:57 +02:00
|
|
|
|
|
|
|
$this->controller = new ChangePasswordController(
|
|
|
|
'core',
|
|
|
|
$request,
|
|
|
|
$this->userId,
|
|
|
|
$this->userManager,
|
|
|
|
$this->userSession,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->appManager,
|
|
|
|
$this->l
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePersonalPasswordWrongPassword(): void {
|
2020-05-25 23:00:00 +02:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn($this->loginName);
|
|
|
|
|
2016-08-15 21:07:57 +02:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
2020-05-25 23:00:00 +02:00
|
|
|
->with($this->loginName, 'old')
|
2016-08-15 21:07:57 +02:00
|
|
|
->willReturn(false);
|
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$expects = new JSONResponse([
|
2016-08-15 21:07:57 +02:00
|
|
|
'status' => 'error',
|
|
|
|
'data' => [
|
|
|
|
'message' => 'Wrong password',
|
|
|
|
],
|
2017-04-18 17:55:51 +02:00
|
|
|
]);
|
|
|
|
$expects->throttle();
|
2016-08-15 21:07:57 +02:00
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-08-15 21:07:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-06 10:24:02 +02:00
|
|
|
public function testChangePersonalPasswordCommonPassword(): void {
|
2020-05-25 23:00:00 +02:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn($this->loginName);
|
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-10-06 10:24:02 +02:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
2020-05-25 23:00:00 +02:00
|
|
|
->with($this->loginName, 'old')
|
2016-10-06 10:24:02 +02:00
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('new')
|
|
|
|
->will($this->throwException(new HintException('Common password')));
|
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$expects = new JSONResponse([
|
2016-10-06 10:24:02 +02:00
|
|
|
'status' => 'error',
|
|
|
|
'data' => [
|
|
|
|
'message' => 'Common password',
|
|
|
|
],
|
2017-04-18 17:55:51 +02:00
|
|
|
]);
|
2016-10-06 10:24:02 +02:00
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-10-06 10:24:02 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 21:07:57 +02:00
|
|
|
public function testChangePersonalPasswordNoNewPassword(): void {
|
2020-05-25 23:00:00 +02:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn($this->loginName);
|
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-08-15 21:07:57 +02:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
2020-05-25 23:00:00 +02:00
|
|
|
->with($this->loginName, 'old')
|
2016-08-15 21:07:57 +02:00
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$expects = [
|
|
|
|
'status' => 'error',
|
2022-05-05 11:21:20 +02:00
|
|
|
'data' => [
|
|
|
|
'message' => 'Unable to change personal password',
|
|
|
|
],
|
2016-08-15 21:07:57 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$res = $this->controller->changePersonalPassword('old');
|
|
|
|
|
|
|
|
$this->assertEquals($expects, $res->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePersonalPasswordCantSetPassword(): void {
|
2020-05-25 23:00:00 +02:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn($this->loginName);
|
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-08-15 21:07:57 +02:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
2020-05-25 23:00:00 +02:00
|
|
|
->with($this->loginName, 'old')
|
2016-08-15 21:07:57 +02:00
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('new')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$expects = new JSONResponse([
|
2016-08-15 21:07:57 +02:00
|
|
|
'status' => 'error',
|
2022-05-05 11:21:20 +02:00
|
|
|
'data' => [
|
|
|
|
'message' => 'Unable to change personal password',
|
|
|
|
],
|
2017-04-18 17:55:51 +02:00
|
|
|
]);
|
2016-08-15 21:07:57 +02:00
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-08-15 21:07:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePersonalPassword(): void {
|
2020-05-25 23:00:00 +02:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn($this->loginName);
|
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-08-15 21:07:57 +02:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
2020-05-25 23:00:00 +02:00
|
|
|
->with($this->loginName, 'old')
|
2016-08-15 21:07:57 +02:00
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('setPassword')
|
|
|
|
->with('new')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('updateSessionTokenPassword')
|
|
|
|
->with('new');
|
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$expects = new JSONResponse([
|
2016-08-15 21:07:57 +02:00
|
|
|
'status' => 'success',
|
|
|
|
'data' => [
|
|
|
|
'message' => 'Saved',
|
|
|
|
],
|
2017-04-18 17:55:51 +02:00
|
|
|
]);
|
2016-08-15 21:07:57 +02:00
|
|
|
|
2017-04-18 17:55:51 +02:00
|
|
|
$actual = $this->controller->changePersonalPassword('old', 'new');
|
|
|
|
$this->assertEquals($expects, $actual);
|
2016-08-15 21:07:57 +02:00
|
|
|
}
|
|
|
|
}
|