0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-05-22 22:10:09 +00:00
nextcloud_server/tests/Core/Controller/ChangePasswordControllerTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

193 lines
5.1 KiB
PHP
Raw Normal View History

2016-08-15 21:07:57 +02:00
<?php
/**
* 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
*/
2016-08-15 21:07:57 +02:00
namespace Tests\Core\Controller;
use OC\User\Session;
use OCA\Settings\Controller\ChangePasswordController;
2016-08-15 21:07:57 +02:00
use OCP\App\IAppManager;
use OCP\AppFramework\Http\JSONResponse;
use OCP\HintException;
2016-08-15 21:07:57 +02:00
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
2016-08-15 21:07:57 +02:00
use OCP\IUserManager;
class ChangePasswordControllerTest extends \Test\TestCase {
/** @var string */
private $userId = 'currentUser';
/** @var string */
private $loginName = 'ua1337';
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
2016-08-15 21:07:57 +02:00
private $userManager;
/** @var Session|\PHPUnit\Framework\MockObject\MockObject */
2016-08-15 21:07:57 +02:00
private $userSession;
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
2016-08-15 21:07:57 +02:00
private $groupManager;
/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
2016-08-15 21:07:57 +02:00
private $appManager;
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
2016-08-15 21:07:57 +02:00
private $l;
/** @var ChangePasswordController */
private $controller;
protected function setUp(): void {
2016-08-15 21:07:57 +02:00
parent::setUp();
$this->userManager = $this->createMock(\OC\User\Manager::class);
$this->userSession = $this->createMock(Session::class);
$this->groupManager = $this->createMock(\OC\Group\Manager::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->l = $this->createMock(IL10N::class);
$this->l->method('t')->willReturnArgument(0);
2016-08-15 21:07:57 +02:00
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
$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 {
$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')
->with($this->loginName, 'old')
2016-08-15 21:07:57 +02:00
->willReturn(false);
$expects = new JSONResponse([
2016-08-15 21:07:57 +02:00
'status' => 'error',
'data' => [
'message' => 'Wrong password',
],
]);
$expects->throttle();
2016-08-15 21:07:57 +02:00
$actual = $this->controller->changePersonalPassword('old', 'new');
$this->assertEquals($expects, $actual);
2016-08-15 21:07:57 +02:00
}
public function testChangePersonalPasswordCommonPassword(): void {
$this->userSession->expects($this->once())
->method('getLoginName')
->willReturn($this->loginName);
$user = $this->getMockBuilder(IUser::class)->getMock();
$this->userManager->expects($this->once())
->method('checkPassword')
->with($this->loginName, 'old')
->willReturn($user);
$user->expects($this->once())
->method('setPassword')
->with('new')
->will($this->throwException(new HintException('Common password')));
$expects = new JSONResponse([
'status' => 'error',
'data' => [
'message' => 'Common password',
],
]);
$actual = $this->controller->changePersonalPassword('old', 'new');
$this->assertEquals($expects, $actual);
}
2016-08-15 21:07:57 +02:00
public function testChangePersonalPasswordNoNewPassword(): void {
$this->userSession->expects($this->once())
->method('getLoginName')
->willReturn($this->loginName);
$user = $this->getMockBuilder(IUser::class)->getMock();
2016-08-15 21:07:57 +02:00
$this->userManager->expects($this->once())
->method('checkPassword')
->with($this->loginName, 'old')
2016-08-15 21:07:57 +02:00
->willReturn($user);
$expects = [
'status' => 'error',
'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 {
$this->userSession->expects($this->once())
->method('getLoginName')
->willReturn($this->loginName);
$user = $this->getMockBuilder(IUser::class)->getMock();
2016-08-15 21:07:57 +02:00
$this->userManager->expects($this->once())
->method('checkPassword')
->with($this->loginName, 'old')
2016-08-15 21:07:57 +02:00
->willReturn($user);
$user->expects($this->once())
->method('setPassword')
->with('new')
->willReturn(false);
$expects = new JSONResponse([
2016-08-15 21:07:57 +02:00
'status' => 'error',
'data' => [
'message' => 'Unable to change personal password',
],
]);
2016-08-15 21:07:57 +02:00
$actual = $this->controller->changePersonalPassword('old', 'new');
$this->assertEquals($expects, $actual);
2016-08-15 21:07:57 +02:00
}
public function testChangePersonalPassword(): void {
$this->userSession->expects($this->once())
->method('getLoginName')
->willReturn($this->loginName);
$user = $this->getMockBuilder(IUser::class)->getMock();
2016-08-15 21:07:57 +02:00
$this->userManager->expects($this->once())
->method('checkPassword')
->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');
$expects = new JSONResponse([
2016-08-15 21:07:57 +02:00
'status' => 'success',
'data' => [
'message' => 'Saved',
],
]);
2016-08-15 21:07:57 +02:00
$actual = $this->controller->changePersonalPassword('old', 'new');
$this->assertEquals($expects, $actual);
2016-08-15 21:07:57 +02:00
}
}