]> source.dussan.org Git - nextcloud-server.git/commitdiff
unify endpoints form core and the the provisioning api 3232/head
authorBjoern Schiessle <bjoern@schiessle.org>
Tue, 24 Jan 2017 14:45:55 +0000 (15:45 +0100)
committerBjoern Schiessle <bjoern@schiessle.org>
Wed, 25 Jan 2017 10:20:35 +0000 (11:20 +0100)
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
apps/provisioning_api/appinfo/routes.php
apps/provisioning_api/lib/Controller/UsersController.php
apps/provisioning_api/tests/Controller/UsersControllerTest.php
core/Controller/OCSController.php
core/routes.php
tests/Core/Controller/OCSControllerTest.php

index 04a34fba90399cd805b1a953c0d913a8ab2a2302..baa4e475be87245313183f68686642c4fe309c2a 100644 (file)
@@ -45,6 +45,7 @@ return [
                ['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'],
                ['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'],
                ['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'],
+               ['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
                ['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'],
                ['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'],
                ['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'],
index e659b49443b728c6b3f7175e964cb2b838103de6..45839cf4f8d26aff079093d22e27b8c00186d4fc 100644 (file)
@@ -218,6 +218,7 @@ class UsersController extends OCSController {
                $userAccount = $this->accountManager->getUser($targetUserObject);
 
                // Find the data
+               $data['id'] = $targetUserObject->getUID();
                $data['quota'] = $this->fillStorageInfo($userId);
                $data['email'] = $targetUserObject->getEMailAddress();
                $data['displayname'] = $targetUserObject->getDisplayName();
@@ -229,6 +230,30 @@ class UsersController extends OCSController {
                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);
+       }
+
        /**
         * @NoAdminRequired
         * @NoSubAdminRequired
index 1565407b8395ef05df908743abeb4f689019806c..52f7f391c973a9d4f1a4ad1b27b53494fb955979 100644 (file)
@@ -33,6 +33,7 @@ use OC\Accounts\AccountManager;
 use OCA\Provisioning_API\Controller\UsersController;
 use OCP\AppFramework\Http\DataResponse;
 use OCP\IGroup;
+use OCP\IRequest;
 use OCP\IUser;
 use OCP\IUserManager;
 use OCP\IConfig;
@@ -57,6 +58,8 @@ class UsersControllerTest extends OriginalTest {
        protected $api;
        /** @var  AccountManager | PHPUnit_Framework_MockObject_MockObject */
        protected $accountManager;
+       /** @var  IRequest | PHPUnit_Framework_MockObject_MockObject */
+       protected $request;
 
        protected function tearDown() {
                parent::tearDown();
@@ -80,7 +83,7 @@ class UsersControllerTest extends OriginalTest {
                $this->logger = $this->getMockBuilder('OCP\ILogger')
                        ->disableOriginalConstructor()
                        ->getMock();
-               $request = $this->getMockBuilder('OCP\IRequest')
+               $this->request = $this->getMockBuilder('OCP\IRequest')
                        ->disableOriginalConstructor()
                        ->getMock();
                $this->accountManager = $this->getMockBuilder(AccountManager::class)
@@ -89,7 +92,7 @@ class UsersControllerTest extends OriginalTest {
                $this->api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
                        ->setConstructorArgs([
                                'provisioning_api',
-                               $request,
+                               $this->request,
                                $this->userManager,
                                $this->config,
                                $this->groupManager,
@@ -683,8 +686,13 @@ class UsersControllerTest extends OriginalTest {
                        ->expects($this->once())
                        ->method('getDisplayName')
                        ->will($this->returnValue('Demo User'));
+               $targetUser
+                       ->expects($this->once())
+                       ->method('getUID')
+                       ->will($this->returnValue('UID'));
 
                $expected = [
+                       'id' => 'UID',
                        'enabled' => 'true',
                        'quota' => ['DummyValue'],
                        'email' => 'demo@owncloud.org',
@@ -752,6 +760,10 @@ class UsersControllerTest extends OriginalTest {
                        ->expects($this->once())
                        ->method('getDisplayName')
                        ->will($this->returnValue('Demo User'));
+               $targetUser
+                       ->expects($this->once())
+                       ->method('getUID')
+                       ->will($this->returnValue('UID'));
                $this->accountManager->expects($this->any())->method('getUser')
                        ->with($targetUser)
                        ->willReturn(
@@ -764,6 +776,7 @@ class UsersControllerTest extends OriginalTest {
                        );
 
                $expected = [
+                       'id' => 'UID',
                        'enabled' => 'true',
                        'quota' => ['DummyValue'],
                        'email' => 'demo@owncloud.org',
@@ -872,6 +885,10 @@ class UsersControllerTest extends OriginalTest {
                        ->expects($this->once())
                        ->method('getEMailAddress')
                        ->will($this->returnValue('subadmin@owncloud.org'));
+               $targetUser
+                       ->expects($this->once())
+                       ->method('getUID')
+                       ->will($this->returnValue('UID'));
                $this->accountManager->expects($this->any())->method('getUser')
                        ->with($targetUser)
                        ->willReturn(
@@ -884,6 +901,7 @@ class UsersControllerTest extends OriginalTest {
                        );
 
                $expected = [
+                       'id' => 'UID',
                        'quota' => ['DummyValue'],
                        'email' => 'subadmin@owncloud.org',
                        'displayname' => 'Subadmin User',
@@ -2534,4 +2552,70 @@ class UsersControllerTest extends OriginalTest {
 
                $this->assertEquals([], $this->api->disableUser('RequestedUser')->getData());
        }
+
+       public function testGetCurrentUserLoggedIn() {
+
+               $user = $this->getMock(IUser::class);
+               $user->expects($this->once())->method('getUID')->willReturn('UID');
+
+               $this->userSession->expects($this->once())->method('getUser')
+                       ->willReturn($user);
+
+               /** @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(['getUser'])
+                       ->getMock();
+
+               $api->expects($this->once())->method('getUser')->with('UID')
+                       ->willReturn(
+                               [
+                                       'id' => 'UID',
+                                       'enabled' => 'true',
+                                       'quota' => ['DummyValue'],
+                                       'email' => 'demo@owncloud.org',
+                                       'displayname' => 'Demo User',
+                                       'phone' => 'phone',
+                                       'address' => 'address',
+                                       'webpage' => 'website',
+                                       'twitter' => 'twitter'
+                               ]
+                       );
+
+               $expected = [
+                       'id' => 'UID',
+                       'enabled' => 'true',
+                       'quota' => ['DummyValue'],
+                       'email' => 'demo@owncloud.org',
+                       'phone' => 'phone',
+                       'address' => 'address',
+                       'webpage' => 'website',
+                       'twitter' => 'twitter',
+                       'display-name' => 'Demo User'
+               ];
+
+               $this->assertSame($expected, $api->getCurrentUser());
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        */
+       public function testGetCurrentUserNotLoggedIn() {
+
+               $this->userSession->expects($this->once())->method('getUser')
+                       ->willReturn(null);
+
+               $this->api->getCurrentUser();
+       }
+
+
 }
index dc9775f2603257b81a6a650d1140c08ba4922fa4..1deb5e958bdfb7e619a3179f25d136d9fb341ddf 100644 (file)
@@ -105,20 +105,6 @@ class OCSController extends \OCP\AppFramework\OCSController {
                return new DataResponse($result);
        }
 
-       /**
-        * @NoAdminRequired
-        * @return DataResponse
-        */
-       public function getCurrentUser() {
-               $userObject = $this->userSession->getUser();
-               $data  = [
-                       'id' => $userObject->getUID(),
-                       'display-name' => $userObject->getDisplayName(),
-                       'email' => $userObject->getEMailAddress(),
-               ];
-               return new DataResponse($data);
-       }
-
        /**
         * @PublicPage
         *
index 6f1892d19acdfcb143c3a38bba58bcd6a434f4cd..5d61d58e03795508f7a78ccac728f92905c268d6 100644 (file)
@@ -59,7 +59,6 @@ $application->registerRoutes($this, [
        ],
        'ocs' => [
                ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
-               ['root' => '/cloud', 'name' => 'OCS#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
                ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'],
                ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'],
                ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'],
index 6c47521786f74c0694c7ab4a8847d8ca63408af8..7241df9317cd544c202a5b968913312a56c71c9e 100644 (file)
@@ -116,24 +116,6 @@ class OCSControllerTest extends TestCase {
                $this->assertEquals($expected, $this->controller->getCapabilities());
        }
 
-       public function testGetCurrentUser() {
-               $user = $this->createMock(IUser::class);
-               $user->method('getUID')->willReturn('uid');
-               $user->method('getDisplayName')->willReturn('displayName');
-               $user->method('getEMailAddress')->willReturn('e@mail.com');
-
-
-               $this->userSession->method('getUser')
-                       ->willReturn($user);
-
-               $expected = new DataResponse([
-                       'id' => 'uid',
-                       'display-name' => 'displayName',
-                       'email' => 'e@mail.com',
-               ]);
-               $this->assertEquals($expected, $this->controller->getCurrentUser());
-       }
-
        public function testPersonCheckValid() {
                $this->request->method('getRemoteAddress')
                        ->willReturn('1.2.3.4');