]> source.dussan.org Git - nextcloud-server.git/commitdiff
make sure that 'getCurrentUser' gets an array in order to manipulate the data to... 3272/head
authorBjoern Schiessle <bjoern@schiessle.org>
Thu, 26 Jan 2017 10:31:08 +0000 (11:31 +0100)
committerBjoern Schiessle <bjoern@schiessle.org>
Thu, 26 Jan 2017 10:31:08 +0000 (11:31 +0100)
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
apps/provisioning_api/lib/Controller/UsersController.php
apps/provisioning_api/tests/Controller/UsersControllerTest.php

index 45839cf4f8d26aff079093d22e27b8c00186d4fc..1e8a767b33a497114ee2497b80a4ed399b8be8ca 100644 (file)
@@ -194,6 +194,42 @@ class UsersController extends OCSController {
         * @throws OCSException
         */
        public function getUser($userId) {
+               $data = $this->getUserData($userId);
+               return new DataResponse($data);
+       }
+
+       /**
+        * @NoAdminRequired
+        * @NoSubAdminRequired
+        *
+        * gets user info from the currently logged in user
+        *
+        * @return DataResponse
+        * @throws OCSException
+        */
+       public function getCurrentUser() {
+               $user = $this->userSession->getUser();
+               if ($user) {
+                       $data =  $this->getUserData($user->getUID());
+                       // rename "displayname" to "display-name" only for this call to keep
+                       // the API stable.
+                       $data['display-name'] = $data['displayname'];
+                       unset($data['displayname']);
+                       return new DataResponse($data);
+
+               }
+
+               throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
+       }
+
+       /**
+        * creates a array with all user data
+        *
+        * @param $userId
+        * @return array
+        * @throws OCSException
+        */
+       protected function getUserData($userId) {
                $currentLoggedInUser = $this->userSession->getUser();
 
                $data = [];
@@ -227,31 +263,7 @@ class UsersController extends OCSController {
                $data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
                $data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
 
-               return new DataResponse($data);
-       }
-
-       /**
-        * @NoAdminRequired
-        * @NoSubAdminRequired
-        *
-        * gets user info from the currently logged in user
-        *
-        * @return DataResponse
-        * @throws OCSException
-        */
-       public function getCurrentUser() {
-               $user = $this->userSession->getUser();
-               if ($user) {
-                       $result =  $this->getUser($user->getUID());
-                       // rename "displayname" to "display-name" only for this call to keep
-                       // the API stable.
-                       $result['display-name'] = $result['displayname'];
-                       unset($result['displayname']);
-                       return $result;
-
-               }
-
-               throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
+               return $data;
        }
 
        /**
index 52f7f391c973a9d4f1a4ad1b27b53494fb955979..a3e5bf6fde6a077ab537c3eb12f83e64e15076b5 100644 (file)
@@ -634,7 +634,7 @@ class UsersControllerTest extends OriginalTest {
                $this->api->getUser('UserToGet');
        }
 
-       public function testGetUserAsAdmin() {
+       public function testGetUserDataAsAdmin() {
                $loggedInUser = $this->getMockBuilder('OCP\IUser')
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -702,10 +702,10 @@ class UsersControllerTest extends OriginalTest {
                        'webpage' => 'website',
                        'twitter' => 'twitter'
                ];
-               $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
+               $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UserToGet']));
        }
 
-       public function testGetUserAsSubAdminAndUserIsAccessible() {
+       public function testGetUserDataAsSubAdminAndUserIsAccessible() {
                $loggedInUser = $this->getMockBuilder('OCP\IUser')
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -786,7 +786,7 @@ class UsersControllerTest extends OriginalTest {
                        'webpage' => 'website',
                        'twitter' => 'twitter'
                ];
-               $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
+               $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UserToGet']));
        }
 
 
@@ -794,7 +794,7 @@ class UsersControllerTest extends OriginalTest {
         * @expectedException \OCP\AppFramework\OCS\OCSException
         * @expectedExceptionCode 997
         */
-       public function testGetUserAsSubAdminAndUserIsNotAccessible() {
+       public function testGetUserDataAsSubAdminAndUserIsNotAccessible() {
                $loggedInUser = $this->getMockBuilder('OCP\IUser')
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -832,10 +832,10 @@ class UsersControllerTest extends OriginalTest {
                        ->method('getSubAdmin')
                        ->will($this->returnValue($subAdminManager));
 
-               $this->api->getUser('UserToGet');
+               $this->invokePrivate($this->api, 'getUserData', ['UserToGet']);
        }
 
-       public function testGetUserAsSubAdminSelfLookup() {
+       public function testGetUserDataAsSubAdminSelfLookup() {
                $loggedInUser = $this->getMockBuilder('OCP\IUser')
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -910,7 +910,7 @@ class UsersControllerTest extends OriginalTest {
                        'webpage' => 'website',
                        'twitter' => 'twitter'
                ];
-               $this->assertEquals($expected, $this->api->getUser('subadmin')->getData());
+               $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['subadmin']));
        }
 
        public function testEditUserRegularUserSelfEditChangeDisplayName() {
@@ -2573,10 +2573,10 @@ class UsersControllerTest extends OriginalTest {
                                $this->accountManager,
                                $this->logger,
                        ])
-                       ->setMethods(['getUser'])
+                       ->setMethods(['getUserData'])
                        ->getMock();
 
-               $api->expects($this->once())->method('getUser')->with('UID')
+               $api->expects($this->once())->method('getUserData')->with('UID')
                        ->willReturn(
                                [
                                        'id' => 'UID',
@@ -2603,7 +2603,7 @@ class UsersControllerTest extends OriginalTest {
                        'display-name' => 'Demo User'
                ];
 
-               $this->assertSame($expected, $api->getCurrentUser());
+               $this->assertSame($expected, $api->getCurrentUser()->getData());
        }
 
        /**
@@ -2618,4 +2618,39 @@ class UsersControllerTest extends OriginalTest {
        }
 
 
+       public function testGetUser() {
+               /** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
+               $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
+                       ->setConstructorArgs([
+                               'provisioning_api',
+                               $this->request,
+                               $this->userManager,
+                               $this->config,
+                               $this->groupManager,
+                               $this->userSession,
+                               $this->accountManager,
+                               $this->logger,
+                       ])
+                       ->setMethods(['getUserData'])
+                       ->getMock();
+
+               $expected = [
+                       'id' => 'UID',
+                       'enabled' => 'true',
+                       'quota' => ['DummyValue'],
+                       'email' => 'demo@owncloud.org',
+                       'phone' => 'phone',
+                       'address' => 'address',
+                       'webpage' => 'website',
+                       'twitter' => 'twitter',
+                       'displayname' => 'Demo User'
+               ];
+
+               $api->expects($this->once())->method('getUserData')
+                       ->with('uid')
+                       ->willReturn($expected);
+
+               $this->assertSame($expected, $api->getUser('uid')->getData());
+       }
+
 }