]> source.dussan.org Git - nextcloud-server.git/commitdiff
Enhance UsersControllerTest of provisioning API with scopes
authorVincent Petry <vincent@nextcloud.com>
Wed, 24 Mar 2021 15:17:28 +0000 (16:17 +0100)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Mon, 29 Mar 2021 07:03:35 +0000 (07:03 +0000)
Signed-off-by: Vincent Petry <vincent@nextcloud.com>
apps/provisioning_api/tests/Controller/UsersControllerTest.php

index 8f4e8395f272ea73351e35045b7eb17cc3b66489..ca60304465409bd656027e9d5ca565e2b3fb69f0 100644 (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(
-                               [
-                                       IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
-                                       IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
-                                       IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
-                                       IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
-                               ]
-                       );
+
+               $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(
-                               [
-                                       IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
-                                       IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
-                                       IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
-                                       IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
-                               ]
-                       );
+
+               $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(
-                               [
-                                       IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
-                                       IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
-                                       IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
-                                       IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
-                               ]
-                       );
+               $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);
+       }
 }