Enhance UsersControllerTest of provisioning API with scopes

Signed-off-by: Vincent Petry <vincent@nextcloud.com>
This commit is contained in:
Vincent Petry 2021-03-24 16:17:28 +01:00 committed by backportbot[bot]
parent ad402ffc96
commit 2fd62b4f0d
1 changed files with 140 additions and 37 deletions

View File

@ -48,7 +48,9 @@ use OC\KnownUser\KnownUserService;
use OC\SubAdmin;
use OCA\Provisioning_API\Controller\UsersController;
use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
@ -926,7 +928,6 @@ class UsersControllerTest extends TestCase {
->disableOriginalConstructor()
->getMock();
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->userManager
@ -996,16 +997,13 @@ class UsersControllerTest extends TestCase {
$group->expects($this->at(3))
->method('getGID')
->willReturn('group3');
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser)
->willReturn(
[
$this->mockAccount($targetUser, [
IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
]);
$this->config
->expects($this->at(0))
->method('getUserValue')
@ -1165,16 +1163,13 @@ class UsersControllerTest extends TestCase {
$targetUser
->method('getUID')
->willReturn('UID');
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser)
->willReturn(
[
$this->mockAccount($targetUser, [
IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
]);
$this->l10nFactory
->expects($this->once())
@ -1217,14 +1212,13 @@ class UsersControllerTest extends TestCase {
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->exactly(2))
->expects($this->exactly(3))
->method('getUID')
->willReturn('subadmin');
$targetUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->userManager
@ -1336,16 +1330,12 @@ class UsersControllerTest extends TestCase {
->expects($this->once())
->method('getBackend')
->willReturn($backend);
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser)
->willReturn(
[
$this->mockAccount($targetUser, [
IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
]);
$this->l10nFactory
->expects($this->once())
@ -1530,6 +1520,88 @@ class UsersControllerTest extends TestCase {
$this->api->editUser('UserToEdit', 'email', 'demo.org');
}
public function selfEditChangePropertyProvider() {
return [
[IAccountManager::PROPERTY_TWITTER, '@oldtwitter', '@newtwitter'],
[IAccountManager::PROPERTY_PHONE, '1234', '12345'],
[IAccountManager::PROPERTY_ADDRESS, 'Something street 2', 'Another street 3'],
[IAccountManager::PROPERTY_WEBSITE, 'https://examplesite1', 'https://examplesite2'],
];
}
/**
* @dataProvider selfEditChangePropertyProvider
*/
public function testEditUserRegularUserSelfEditChangeProperty($propertyName, $oldValue, $newValue) {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->any())
->method('getUID')
->willReturn('UID');
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->userManager
->expects($this->once())
->method('get')
->with('UserToEdit')
->willReturn($loggedInUser);
$this->accountManager->expects($this->once())
->method('getUser')
->with($loggedInUser)
->willReturn([$propertyName => ['value' => $oldValue, 'scope' => IAccountManager::SCOPE_LOCAL]]);
$this->accountManager->expects($this->once())
->method('updateUser')
->with($loggedInUser, [$propertyName => ['value' => $newValue, 'scope' => IAccountManager::SCOPE_LOCAL]], true);
$this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName, $newValue)->getData());
}
public function selfEditChangePropertyScopeProvider() {
return [
[IAccountManager::PROPERTY_TWITTER, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
[IAccountManager::PROPERTY_PHONE, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
[IAccountManager::PROPERTY_ADDRESS, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
[IAccountManager::PROPERTY_WEBSITE, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
];
}
/**
* @dataProvider selfEditChangePropertyProvider
*/
public function testEditUserRegularUserSelfEditChangePropertyScope($propertyName, $oldScope, $newScope) {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->any())
->method('getUID')
->willReturn('UID');
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->userManager
->expects($this->once())
->method('get')
->with('UserToEdit')
->willReturn($loggedInUser);
$this->accountManager->expects($this->once())
->method('getUser')
->with($loggedInUser)
->willReturn([$propertyName => ['value' => 'somevalue', 'scope' => $oldScope]]);
$this->accountManager->expects($this->once())
->method('updateUser')
->with($loggedInUser, [$propertyName => ['value' => 'somevalue', 'scope' => $newScope]], true);
$this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName . 'Scope', $newScope)->getData());
}
public function testEditUserRegularUserSelfEditChangePassword() {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
@ -3247,7 +3319,7 @@ class UsersControllerTest extends TestCase {
->setMethods(['getUserData'])
->getMock();
$api->expects($this->once())->method('getUserData')->with('UID')
$api->expects($this->once())->method('getUserData')->with('UID', true)
->willReturn(
[
'id' => 'UID',
@ -3288,8 +3360,15 @@ class UsersControllerTest extends TestCase {
$this->api->getCurrentUser();
}
public function testGetUser() {
$loggedInUser = $this->createMock(IUser::class);
$loggedInUser
->method('getUID')
->willReturn('currentuser');
$this->userSession
->method('getUser')
->willReturn($loggedInUser);
/** @var UsersController | MockObject $api */
$api = $this->getMockBuilder(UsersController::class)
->setConstructorArgs([
@ -3325,11 +3404,16 @@ class UsersControllerTest extends TestCase {
'displayname' => 'Demo User'
];
$api->expects($this->once())->method('getUserData')
->with('uid')
$api->expects($this->at(0))->method('getUserData')
->with('uid', false)
->willReturn($expected);
$api->expects($this->at(1))->method('getUserData')
->with('currentuser', true)
->willReturn($expected);
$this->assertSame($expected, $api->getUser('uid')->getData());
$this->assertSame($expected, $api->getUser('currentuser')->getData());
}
@ -3663,4 +3747,23 @@ class UsersControllerTest extends TestCase {
$expectedResp = new DataResponse($expected);
$this->assertEquals($expectedResp, $this->api->getEditableFields());
}
private function mockAccount($targetUser, $accountProperties) {
$mockedProperties = [];
foreach ($accountProperties as $propertyName => $data) {
$mockedProperty = $this->createMock(IAccountProperty::class);
$mockedProperty->method('getValue')->willReturn($data['value'] ?? '');
$mockedProperty->method('getScope')->willReturn($data['scope'] ?? '');
$mockedProperties[] = [$propertyName, $mockedProperty];
}
$account = $this->createMock(IAccount::class);
$account->method('getProperty')
->will($this->returnValueMap($mockedProperties));
$this->accountManager->expects($this->any())->method('getAccount')
->with($targetUser)
->willReturn($account);
}
}