2018-04-24 12:25:52 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright 2014-2015 Lukas Reschke lukas@owncloud.com
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @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/>.
|
|
|
|
*
|
2018-04-24 12:25:52 +03:00
|
|
|
*/
|
|
|
|
|
2019-09-17 17:33:27 +03:00
|
|
|
namespace OCA\Settings\Tests\Controller;
|
2018-04-24 12:25:52 +03:00
|
|
|
|
|
|
|
use OC\Accounts\AccountManager;
|
2018-12-20 13:11:04 +03:00
|
|
|
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
|
2018-04-24 12:25:52 +03:00
|
|
|
use OC\Group\Manager;
|
2019-09-17 17:33:27 +03:00
|
|
|
use OCA\Settings\Controller\UsersController;
|
2020-12-01 16:33:22 +03:00
|
|
|
use OCP\Accounts\IAccountManager;
|
2018-04-24 12:25:52 +03:00
|
|
|
use OCP\App\IAppManager;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\BackgroundJob\IJobList;
|
|
|
|
use OCP\Encryption\IEncryptionModule;
|
|
|
|
use OCP\Encryption\IManager;
|
2020-09-07 18:51:07 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2018-04-24 12:25:52 +03:00
|
|
|
use OCP\IAvatarManager;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
2018-04-24 17:20:01 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2018-04-24 12:25:52 +03:00
|
|
|
use OCP\Mail\IMailer;
|
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package Tests\Settings\Controller
|
|
|
|
*/
|
|
|
|
class UsersControllerTest extends \Test\TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $groupManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $userManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $userSession;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $config;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $logger;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $mailer;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 17:20:01 +03:00
|
|
|
private $l10nFactory;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $appManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $avatarManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $l;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var AccountManager | \PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $accountManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISecureRandom | \PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $secureRandom;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\Settings\Mailer\NewUserMailHelper|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $newUserMailHelper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IJobList | \PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $jobList;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\Security\IdentityProof\Manager |\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $securityManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $encryptionManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 12:25:52 +03:00
|
|
|
private $encryptionModule;
|
2020-09-07 18:51:07 +03:00
|
|
|
/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
|
|
|
|
private $dispatcher;
|
2018-04-24 12:25:52 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2018-04-24 12:25:52 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
2018-04-24 17:20:01 +03:00
|
|
|
$this->groupManager = $this->createMock(Manager::class);
|
2018-04-24 12:25:52 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2018-04-24 17:20:01 +03:00
|
|
|
$this->l = $this->createMock(IL10N::class);
|
2018-04-24 12:25:52 +03:00
|
|
|
$this->mailer = $this->createMock(IMailer::class);
|
2018-04-24 17:20:01 +03:00
|
|
|
$this->l10nFactory = $this->createMock(IFactory::class);
|
2018-04-24 12:25:52 +03:00
|
|
|
$this->appManager = $this->createMock(IAppManager::class);
|
|
|
|
$this->accountManager = $this->createMock(AccountManager::class);
|
|
|
|
$this->securityManager = $this->getMockBuilder(\OC\Security\IdentityProof\Manager::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$this->jobList = $this->createMock(IJobList::class);
|
|
|
|
$this->encryptionManager = $this->createMock(IManager::class);
|
2020-09-07 18:51:07 +03:00
|
|
|
$this->dispatcher = $this->createMock(IEventDispatcher::class);
|
2018-12-20 13:11:04 +03:00
|
|
|
|
2018-04-24 12:25:52 +03:00
|
|
|
$this->l->method('t')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($text, $parameters = []) {
|
2018-04-24 12:25:52 +03:00
|
|
|
return vsprintf($text, $parameters);
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2018-04-24 12:25:52 +03:00
|
|
|
|
|
|
|
$this->encryptionModule = $this->createMock(IEncryptionModule::class);
|
|
|
|
$this->encryptionManager->expects($this->any())->method('getEncryptionModules')
|
2020-04-10 15:19:56 +03:00
|
|
|
->willReturn(['encryptionModule' => ['callback' => function () {
|
|
|
|
return $this->encryptionModule;
|
|
|
|
}]]);
|
2018-04-24 12:25:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $isAdmin
|
2020-08-11 22:32:18 +03:00
|
|
|
* @return UsersController | \PHPUnit\Framework\MockObject\MockObject
|
2018-04-24 12:25:52 +03:00
|
|
|
*/
|
|
|
|
protected function getController($isAdmin = false, $mockedMethods = []) {
|
|
|
|
if (empty($mockedMethods)) {
|
|
|
|
return new UsersController(
|
|
|
|
'settings',
|
|
|
|
$this->createMock(IRequest::class),
|
|
|
|
$this->userManager,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->userSession,
|
|
|
|
$this->config,
|
|
|
|
$isAdmin,
|
|
|
|
$this->l,
|
|
|
|
$this->mailer,
|
2018-04-24 17:20:01 +03:00
|
|
|
$this->l10nFactory,
|
2018-04-24 12:25:52 +03:00
|
|
|
$this->appManager,
|
|
|
|
$this->accountManager,
|
|
|
|
$this->securityManager,
|
|
|
|
$this->jobList,
|
2020-09-07 18:51:07 +03:00
|
|
|
$this->encryptionManager,
|
|
|
|
$this->dispatcher
|
2018-04-24 12:25:52 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->getMockBuilder(UsersController::class)
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
'settings',
|
|
|
|
$this->createMock(IRequest::class),
|
|
|
|
$this->userManager,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->userSession,
|
|
|
|
$this->config,
|
|
|
|
$isAdmin,
|
|
|
|
$this->l,
|
|
|
|
$this->mailer,
|
2018-04-24 17:20:01 +03:00
|
|
|
$this->l10nFactory,
|
2018-04-24 12:25:52 +03:00
|
|
|
$this->appManager,
|
|
|
|
$this->accountManager,
|
|
|
|
$this->securityManager,
|
|
|
|
$this->jobList,
|
2020-09-07 18:51:07 +03:00
|
|
|
$this->encryptionManager,
|
|
|
|
$this->dispatcher
|
2018-04-24 12:25:52 +03:00
|
|
|
]
|
|
|
|
)->setMethods($mockedMethods)->getMock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestSetUserSettings
|
|
|
|
*
|
|
|
|
* @param string $email
|
|
|
|
* @param bool $validEmail
|
|
|
|
* @param $expectedStatus
|
|
|
|
*/
|
|
|
|
public function testSetUserSettings($email, $validEmail, $expectedStatus) {
|
|
|
|
$controller = $this->getController(false, ['saveUserSettings']);
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
|
|
|
$this->userSession->method('getUser')->willReturn($user);
|
|
|
|
|
|
|
|
if (!empty($email) && $validEmail) {
|
|
|
|
$this->mailer->expects($this->once())->method('validateMailAddress')
|
|
|
|
->willReturn($validEmail);
|
|
|
|
}
|
|
|
|
|
|
|
|
$saveData = (!empty($email) && $validEmail) || empty($email);
|
|
|
|
|
|
|
|
if ($saveData) {
|
|
|
|
$this->accountManager->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->with($user)
|
|
|
|
->willReturn([
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME =>
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'value' => 'Display name',
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
'verified' => AccountManager::NOT_VERIFIED,
|
|
|
|
],
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_ADDRESS =>
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
'verified' => AccountManager::NOT_VERIFIED,
|
|
|
|
],
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_WEBSITE =>
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
'verified' => AccountManager::NOT_VERIFIED,
|
|
|
|
],
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL =>
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
'verified' => AccountManager::NOT_VERIFIED,
|
|
|
|
],
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_AVATAR =>
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY
|
|
|
|
],
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_PHONE =>
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
'verified' => AccountManager::NOT_VERIFIED,
|
|
|
|
],
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_TWITTER =>
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
'verified' => AccountManager::NOT_VERIFIED,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$controller->expects($this->once())->method('saveUserSettings');
|
|
|
|
} else {
|
|
|
|
$controller->expects($this->never())->method('saveUserSettings');
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $controller->setUserSettings(
|
|
|
|
AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
'displayName',
|
|
|
|
AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
'47658468',
|
|
|
|
AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
$email,
|
|
|
|
AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
'nextcloud.com',
|
|
|
|
AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
'street and city',
|
|
|
|
AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
'@nextclouders',
|
|
|
|
AccountManager::VISIBILITY_CONTACTS_ONLY
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame($expectedStatus, $result->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestSetUserSettings() {
|
|
|
|
return [
|
|
|
|
['', true, Http::STATUS_OK],
|
|
|
|
['', false, Http::STATUS_OK],
|
|
|
|
['example.com', false, Http::STATUS_UNPROCESSABLE_ENTITY],
|
|
|
|
['john@example.com', true, Http::STATUS_OK],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestSaveUserSettings
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param string $oldEmailAddress
|
|
|
|
* @param string $oldDisplayName
|
|
|
|
*/
|
|
|
|
public function testSaveUserSettings($data,
|
|
|
|
$oldEmailAddress,
|
|
|
|
$oldDisplayName
|
|
|
|
) {
|
|
|
|
$controller = $this->getController();
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
|
|
|
$user->method('getDisplayName')->willReturn($oldDisplayName);
|
|
|
|
$user->method('getEMailAddress')->willReturn($oldEmailAddress);
|
|
|
|
$user->method('canChangeDisplayName')->willReturn(true);
|
|
|
|
|
2020-12-01 16:33:22 +03:00
|
|
|
if ($data[IAccountManager::PROPERTY_EMAIL]['value'] === $oldEmailAddress ||
|
|
|
|
($oldEmailAddress === null && $data[IAccountManager::PROPERTY_EMAIL]['value'] === '')) {
|
2018-04-24 12:25:52 +03:00
|
|
|
$user->expects($this->never())->method('setEMailAddress');
|
|
|
|
} else {
|
|
|
|
$user->expects($this->once())->method('setEMailAddress')
|
2020-12-01 16:33:22 +03:00
|
|
|
->with($data[IAccountManager::PROPERTY_EMAIL]['value'])
|
2018-04-24 12:25:52 +03:00
|
|
|
->willReturn(true);
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:33:22 +03:00
|
|
|
if ($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'] === $oldDisplayName ||
|
|
|
|
($oldDisplayName === null && $data[IAccountManager::PROPERTY_DISPLAYNAME]['value'] === '')) {
|
2018-04-24 12:25:52 +03:00
|
|
|
$user->expects($this->never())->method('setDisplayName');
|
|
|
|
} else {
|
|
|
|
$user->expects($this->once())->method('setDisplayName')
|
2020-12-01 16:33:22 +03:00
|
|
|
->with($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'])
|
2018-04-24 12:25:52 +03:00
|
|
|
->willReturn(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->accountManager->expects($this->once())->method('updateUser')
|
|
|
|
->with($user, $data);
|
|
|
|
|
|
|
|
$this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestSaveUserSettings() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'john@example.com',
|
|
|
|
'john doe'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'johnNew@example.com',
|
|
|
|
'john New doe'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'johnNew@example.com',
|
|
|
|
'john doe'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'john@example.com',
|
|
|
|
'john New doe'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => ''],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
null,
|
|
|
|
'john New doe'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'john@example.com',
|
|
|
|
null
|
|
|
|
],
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestSaveUserSettingsException
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param string $oldEmailAddress
|
|
|
|
* @param string $oldDisplayName
|
|
|
|
* @param bool $setDisplayNameResult
|
|
|
|
* @param bool $canChangeEmail
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSaveUserSettingsException($data,
|
|
|
|
$oldEmailAddress,
|
|
|
|
$oldDisplayName,
|
|
|
|
$setDisplayNameResult,
|
|
|
|
$canChangeEmail
|
|
|
|
) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OC\ForbiddenException::class);
|
|
|
|
|
2018-04-24 12:25:52 +03:00
|
|
|
$controller = $this->getController();
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
|
|
|
$user->method('getDisplayName')->willReturn($oldDisplayName);
|
|
|
|
$user->method('getEMailAddress')->willReturn($oldEmailAddress);
|
|
|
|
|
2020-12-01 16:33:22 +03:00
|
|
|
if ($data[IAccountManager::PROPERTY_EMAIL]['value'] !== $oldEmailAddress) {
|
2018-04-24 12:25:52 +03:00
|
|
|
$user->method('canChangeDisplayName')
|
|
|
|
->willReturn($canChangeEmail);
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:33:22 +03:00
|
|
|
if ($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'] !== $oldDisplayName) {
|
2018-04-24 12:25:52 +03:00
|
|
|
$user->method('setDisplayName')
|
2020-12-01 16:33:22 +03:00
|
|
|
->with($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'])
|
2018-04-24 12:25:52 +03:00
|
|
|
->willReturn($setDisplayNameResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function dataTestSaveUserSettingsException() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'johnNew@example.com',
|
|
|
|
'john New doe',
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'johnNew@example.com',
|
|
|
|
'john New doe',
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
|
2018-04-24 12:25:52 +03:00
|
|
|
],
|
|
|
|
'johnNew@example.com',
|
|
|
|
'john New doe',
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
],
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $account
|
|
|
|
* @param string $type
|
|
|
|
* @param array $dataBefore
|
|
|
|
* @param array $expectedData
|
|
|
|
*
|
|
|
|
* @dataProvider dataTestGetVerificationCode
|
|
|
|
*/
|
|
|
|
public function testGetVerificationCode($account, $type, $dataBefore, $expectedData, $onlyVerificationCode) {
|
|
|
|
$message = 'Use my Federated Cloud ID to share with me: user@nextcloud.com';
|
|
|
|
$signature = 'theSignature';
|
|
|
|
|
|
|
|
$code = $message . ' ' . $signature;
|
2020-12-01 16:33:22 +03:00
|
|
|
if ($type === IAccountManager::PROPERTY_TWITTER) {
|
2018-04-24 12:25:52 +03:00
|
|
|
$code = $message . ' ' . md5($signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
$controller = $this->getController(false, ['signMessage', 'getCurrentTime']);
|
|
|
|
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userSession->expects($this->once())->method('getUser')->willReturn($user);
|
|
|
|
$this->accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($dataBefore);
|
|
|
|
$user->expects($this->any())->method('getCloudId')->willReturn('user@nextcloud.com');
|
|
|
|
$user->expects($this->any())->method('getUID')->willReturn('uid');
|
|
|
|
$controller->expects($this->once())->method('signMessage')->with($user, $message)->willReturn($signature);
|
|
|
|
$controller->expects($this->any())->method('getCurrentTime')->willReturn(1234567);
|
|
|
|
|
|
|
|
if ($onlyVerificationCode === false) {
|
|
|
|
$this->accountManager->expects($this->once())->method('updateUser')->with($user, $expectedData);
|
|
|
|
$this->jobList->expects($this->once())->method('add')
|
2019-09-17 17:33:27 +03:00
|
|
|
->with('OCA\Settings\BackgroundJobs\VerifyUserData',
|
2018-04-24 12:25:52 +03:00
|
|
|
[
|
|
|
|
'verificationCode' => $code,
|
|
|
|
'data' => $dataBefore[$type]['value'],
|
|
|
|
'type' => $type,
|
|
|
|
'uid' => 'uid',
|
|
|
|
'try' => 0,
|
|
|
|
'lastRun' => 1234567
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $controller->getVerificationCode($account, $onlyVerificationCode);
|
|
|
|
|
|
|
|
$data = $result->getData();
|
|
|
|
$this->assertSame(Http::STATUS_OK, $result->getStatus());
|
|
|
|
$this->assertSame($code, $data['code']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestGetVerificationCode() {
|
|
|
|
$accountDataBefore = [
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => AccountManager::NOT_VERIFIED],
|
|
|
|
IAccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => AccountManager::NOT_VERIFIED, 'signature' => 'theSignature'],
|
2018-04-24 12:25:52 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$accountDataAfterWebsite = [
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => AccountManager::VERIFICATION_IN_PROGRESS, 'signature' => 'theSignature'],
|
|
|
|
IAccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => AccountManager::NOT_VERIFIED, 'signature' => 'theSignature'],
|
2018-04-24 12:25:52 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$accountDataAfterTwitter = [
|
2020-12-01 16:33:22 +03:00
|
|
|
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => AccountManager::NOT_VERIFIED],
|
|
|
|
IAccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => AccountManager::VERIFICATION_IN_PROGRESS, 'signature' => 'theSignature'],
|
2018-04-24 12:25:52 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
return [
|
2020-12-01 16:33:22 +03:00
|
|
|
['verify-twitter', IAccountManager::PROPERTY_TWITTER, $accountDataBefore, $accountDataAfterTwitter, false],
|
|
|
|
['verify-website', IAccountManager::PROPERTY_WEBSITE, $accountDataBefore, $accountDataAfterWebsite, false],
|
|
|
|
['verify-twitter', IAccountManager::PROPERTY_TWITTER, $accountDataBefore, $accountDataAfterTwitter, true],
|
|
|
|
['verify-website', IAccountManager::PROPERTY_WEBSITE, $accountDataBefore, $accountDataAfterWebsite, true],
|
2018-04-24 12:25:52 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test get verification code in case no valid user was given
|
|
|
|
*/
|
|
|
|
public function testGetVerificationCodeInvalidUser() {
|
|
|
|
$controller = $this->getController();
|
|
|
|
$this->userSession->expects($this->once())->method('getUser')->willReturn(null);
|
|
|
|
$result = $controller->getVerificationCode('account', false);
|
|
|
|
|
|
|
|
$this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:11:04 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestCanAdminChangeUserPasswords
|
|
|
|
*
|
|
|
|
* @param bool $encryptionEnabled
|
|
|
|
* @param bool $encryptionModuleLoaded
|
|
|
|
* @param bool $masterKeyEnabled
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
public function testCanAdminChangeUserPasswords($encryptionEnabled,
|
|
|
|
$encryptionModuleLoaded,
|
|
|
|
$masterKeyEnabled,
|
|
|
|
$expected) {
|
|
|
|
$controller = $this->getController();
|
|
|
|
|
|
|
|
$this->encryptionManager->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn($encryptionEnabled);
|
|
|
|
$this->encryptionManager->expects($this->any())
|
|
|
|
->method('getEncryptionModule')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function () use ($encryptionModuleLoaded) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($encryptionModuleLoaded) {
|
|
|
|
return $this->encryptionModule;
|
|
|
|
} else {
|
|
|
|
throw new ModuleDoesNotExistsException();
|
|
|
|
}
|
2018-12-20 13:11:04 +03:00
|
|
|
});
|
|
|
|
$this->encryptionModule->expects($this->any())
|
|
|
|
->method('needDetailedAccessList')
|
|
|
|
->willReturn(!$masterKeyEnabled);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($controller, 'canAdminChangeUserPasswords', []);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestCanAdminChangeUserPasswords() {
|
|
|
|
return [
|
|
|
|
// encryptionEnabled, encryptionModuleLoaded, masterKeyEnabled, expectedResult
|
|
|
|
[true, true, true, true],
|
|
|
|
[false, true, true, true],
|
|
|
|
[true, false, true, false],
|
|
|
|
[false, false, true, true],
|
|
|
|
[true, true, false, false],
|
|
|
|
[false, true, false, false],
|
|
|
|
[true, false, false, false],
|
|
|
|
[false, false, false, true],
|
|
|
|
];
|
|
|
|
}
|
2018-04-24 12:25:52 +03:00
|
|
|
}
|