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 OC\SubAdmin;
use OCA\Provisioning_API\Controller\UsersController; use OCA\Provisioning_API\Controller\UsersController;
use OCA\Settings\Mailer\NewUserMailHelper; use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
@ -926,7 +928,6 @@ class UsersControllerTest extends TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->userSession $this->userSession
->expects($this->once())
->method('getUser') ->method('getUser')
->willReturn($loggedInUser); ->willReturn($loggedInUser);
$this->userManager $this->userManager
@ -996,16 +997,13 @@ class UsersControllerTest extends TestCase {
$group->expects($this->at(3)) $group->expects($this->at(3))
->method('getGID') ->method('getGID')
->willReturn('group3'); ->willReturn('group3');
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser) $this->mockAccount($targetUser, [
->willReturn( IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
[ IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'], IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
IAccountManager::PROPERTY_PHONE => ['value' => 'phone'], IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'], ]);
IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
$this->config $this->config
->expects($this->at(0)) ->expects($this->at(0))
->method('getUserValue') ->method('getUserValue')
@ -1165,16 +1163,13 @@ class UsersControllerTest extends TestCase {
$targetUser $targetUser
->method('getUID') ->method('getUID')
->willReturn('UID'); ->willReturn('UID');
$this->accountManager->expects($this->any())->method('getUser')
->with($targetUser) $this->mockAccount($targetUser, [
->willReturn( IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
[ IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'], IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
IAccountManager::PROPERTY_PHONE => ['value' => 'phone'], IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'], ]);
IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
$this->l10nFactory $this->l10nFactory
->expects($this->once()) ->expects($this->once())
@ -1217,14 +1212,13 @@ class UsersControllerTest extends TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$loggedInUser $loggedInUser
->expects($this->exactly(2)) ->expects($this->exactly(3))
->method('getUID') ->method('getUID')
->willReturn('subadmin'); ->willReturn('subadmin');
$targetUser = $this->getMockBuilder(IUser::class) $targetUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->userSession $this->userSession
->expects($this->once())
->method('getUser') ->method('getUser')
->willReturn($loggedInUser); ->willReturn($loggedInUser);
$this->userManager $this->userManager
@ -1336,16 +1330,12 @@ class UsersControllerTest extends TestCase {
->expects($this->once()) ->expects($this->once())
->method('getBackend') ->method('getBackend')
->willReturn($backend); ->willReturn($backend);
$this->accountManager->expects($this->any())->method('getUser') $this->mockAccount($targetUser, [
->with($targetUser) IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
->willReturn( IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
[ IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'], IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
IAccountManager::PROPERTY_PHONE => ['value' => 'phone'], ]);
IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
]
);
$this->l10nFactory $this->l10nFactory
->expects($this->once()) ->expects($this->once())
@ -1530,6 +1520,88 @@ class UsersControllerTest extends TestCase {
$this->api->editUser('UserToEdit', 'email', 'demo.org'); $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() { public function testEditUserRegularUserSelfEditChangePassword() {
$loggedInUser = $this->getMockBuilder(IUser::class) $loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
@ -3247,7 +3319,7 @@ class UsersControllerTest extends TestCase {
->setMethods(['getUserData']) ->setMethods(['getUserData'])
->getMock(); ->getMock();
$api->expects($this->once())->method('getUserData')->with('UID') $api->expects($this->once())->method('getUserData')->with('UID', true)
->willReturn( ->willReturn(
[ [
'id' => 'UID', 'id' => 'UID',
@ -3288,8 +3360,15 @@ class UsersControllerTest extends TestCase {
$this->api->getCurrentUser(); $this->api->getCurrentUser();
} }
public function testGetUser() { public function testGetUser() {
$loggedInUser = $this->createMock(IUser::class);
$loggedInUser
->method('getUID')
->willReturn('currentuser');
$this->userSession
->method('getUser')
->willReturn($loggedInUser);
/** @var UsersController | MockObject $api */ /** @var UsersController | MockObject $api */
$api = $this->getMockBuilder(UsersController::class) $api = $this->getMockBuilder(UsersController::class)
->setConstructorArgs([ ->setConstructorArgs([
@ -3325,11 +3404,16 @@ class UsersControllerTest extends TestCase {
'displayname' => 'Demo User' 'displayname' => 'Demo User'
]; ];
$api->expects($this->once())->method('getUserData') $api->expects($this->at(0))->method('getUserData')
->with('uid') ->with('uid', false)
->willReturn($expected);
$api->expects($this->at(1))->method('getUserData')
->with('currentuser', true)
->willReturn($expected); ->willReturn($expected);
$this->assertSame($expected, $api->getUser('uid')->getData()); $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); $expectedResp = new DataResponse($expected);
$this->assertEquals($expectedResp, $this->api->getEditableFields()); $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);
}
} }