]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move Groups over to OCSController
authorRoeland Jago Douma <roeland@famdouma.nl>
Thu, 11 Aug 2016 07:46:25 +0000 (09:46 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Sun, 14 Aug 2016 16:34:01 +0000 (18:34 +0200)
* Take advantage of the AppFramework
* Fix tests

apps/provisioning_api/appinfo/routes.php
apps/provisioning_api/lib/Controller/GroupsController.php [new file with mode: 0644]
apps/provisioning_api/lib/Groups.php [deleted file]
apps/provisioning_api/tests/Controller/GroupsControllerTest.php [new file with mode: 0644]
apps/provisioning_api/tests/GroupsTest.php [deleted file]

index 907e16ebf3c4830e5ed885bc6011cad2afe1a07e..dbc0321efb2f1cd8e584419224557ea18d406722 100644 (file)
  *
  */
 
-namespace OCA\Provisioning_API\AppInfo;
-
 use OCA\Provisioning_API\Apps;
-use OCA\Provisioning_API\Groups;
 use OCA\Provisioning_API\Users;
 use OCP\API;
 
+$app = new \OCA\Provisioning_API\AppInfo\Application();
+$app->registerRoutes($this, [
+       'ocs' => [
+               // Groups
+               ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'],
+               ['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'],
+               ['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'],
+               ['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'],
+               ['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET'],
+       ],
+]);
+
 // Users
 $users = new Users(
        \OC::$server->getUserManager(),
@@ -55,18 +64,6 @@ API::register('post', '/cloud/users/{userid}/subadmins', [$users, 'addSubAdmin']
 API::register('delete', '/cloud/users/{userid}/subadmins', [$users, 'removeSubAdmin'], 'provisioning_api', API::ADMIN_AUTH);
 API::register('get', '/cloud/users/{userid}/subadmins', [$users, 'getUserSubAdminGroups'], 'provisioning_api', API::ADMIN_AUTH);
 
-// Groups
-$groups = new Groups(
-       \OC::$server->getGroupManager(),
-       \OC::$server->getUserSession(),
-       \OC::$server->getRequest()
-);
-API::register('get', '/cloud/groups', [$groups, 'getGroups'], 'provisioning_api', API::SUBADMIN_AUTH);
-API::register('post', '/cloud/groups', [$groups, 'addGroup'], 'provisioning_api', API::SUBADMIN_AUTH);
-API::register('get', '/cloud/groups/{groupid}', [$groups, 'getGroup'], 'provisioning_api', API::SUBADMIN_AUTH);
-API::register('delete', '/cloud/groups/{groupid}', [$groups, 'deleteGroup'], 'provisioning_api', API::ADMIN_AUTH);
-API::register('get', '/cloud/groups/{groupid}/subadmins', [$groups, 'getSubAdminsOfGroup'], 'provisioning_api', API::ADMIN_AUTH);
-
 // Apps
 $apps = new Apps(
        \OC::$server->getAppManager(),
diff --git a/apps/provisioning_api/lib/Controller/GroupsController.php b/apps/provisioning_api/lib/Controller/GroupsController.php
new file mode 100644 (file)
index 0000000..d36d0de
--- /dev/null
@@ -0,0 +1,190 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ *
+ * @author Lukas Reschke <lukas@statuscode.ch>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ * @author Tom Needham <tom@owncloud.com>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Provisioning_API\Controller;
+
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\OCS\OCSException;
+use OCP\AppFramework\OCSController;
+use OCP\IGroup;
+use OCP\IGroupManager;
+use OCP\IRequest;
+use OCP\IUserSession;
+use OCP\IUser;
+
+
+class GroupsController extends OCSController {
+
+       /** @var IGroupManager */
+       private $groupManager;
+
+       /** @var IUserSession */
+       private $userSession;
+
+       /**
+        * @param string $appName
+        * @param IRequest $request
+        * @param IGroupManager $groupManager
+        * @param IUserSession $userSession
+        */
+       public function __construct(
+                       $appName,
+                       IRequest $request,
+                       IGroupManager $groupManager,
+                       IUserSession $userSession) {
+               parent::__construct($appName, $request);
+
+               $this->groupManager = $groupManager;
+               $this->userSession = $userSession;
+       }
+
+       /**
+        * returns a list of groups
+        *
+        * @NoAdminRequired
+        *
+        * @param string $search
+        * @param int $limit
+        * @param int $offset
+        * @return DataResponse
+        */
+       public function getGroups($search = '', $limit = null, $offset = null) {
+               if ($limit !== null) {
+                       $limit = (int)$limit;
+               }
+               if ($offset !== null) {
+                       $offset = (int)$offset;
+               }
+
+               $groups = $this->groupManager->search($search, $limit, $offset);
+               $groups = array_map(function($group) {
+                       /** @var IGroup $group */
+                       return $group->getGID();
+               }, $groups);
+
+               return new DataResponse(['groups' => $groups]);
+       }
+
+       /**
+        * returns an array of users in the group specified
+        *
+        * @NoAdminRequired
+        *
+        * @param string $groupId
+        * @return DataResponse
+        * @throws OCSException
+        */
+       public function getGroup($groupId) {
+               $user = $this->userSession->getUser();
+
+               // Check the group exists
+               if(!$this->groupManager->groupExists($groupId)) {
+                       throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND);
+               }
+
+               $isSubadminOfGroup = false;
+               $group = $this->groupManager->get($groupId);
+               if ($group !== null) {
+                       $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group);
+               }
+
+               // Check subadmin has access to this group
+               if($this->groupManager->isAdmin($user->getUID())
+                  || $isSubadminOfGroup) {
+                       $users = $this->groupManager->get($groupId)->getUsers();
+                       $users =  array_map(function($user) {
+                               /** @var IUser $user */
+                               return $user->getUID();
+                       }, $users);
+                       $users = array_values($users);
+                       return new DataResponse(['users' => $users]);
+               } else {
+                       throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);
+               }
+       }
+
+       /**
+        * creates a new group
+        *
+        * @NoAdminRequired
+        *
+        * @param string $groupid
+        * @return DataResponse
+        * @throws OCSException
+        */
+       public function addGroup($groupid) {
+               // Validate name
+               if(empty($groupid)){
+                       \OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR);
+                       throw new OCSException('Invalid group name', 101);
+               }
+               // Check if it exists
+               if($this->groupManager->groupExists($groupid)){
+                       throw new OCSException('', 102);
+               }
+               $this->groupManager->createGroup($groupid);
+               return new DataResponse();
+       }
+
+       /**
+        * @param string $groupId
+        * @return DataResponse
+        * @throws OCSException
+        */
+       public function deleteGroup($groupId) {
+               // Check it exists
+               if(!$this->groupManager->groupExists($groupId)){
+                       throw new OCSException('', 101);
+               } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){
+                       // Cannot delete admin group
+                       throw new OCSException('', 102);
+               }
+
+               return new DataResponse(null, 100);
+       }
+
+       /**
+        * @param string $groupId
+        * @return DataResponse
+        * @throws OCSException
+        */
+       public function getSubAdminsOfGroup($groupId) {
+               // Check group exists
+               $targetGroup = $this->groupManager->get($groupId);
+               if($targetGroup === null) {
+                       throw new OCSException('Group does not exist', 101);
+               }
+
+               $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
+               // New class returns IUser[] so convert back
+               $uids = [];
+               foreach ($subadmins as $user) {
+                       $uids[] = $user->getUID();
+               }
+
+               return new DataResponse($uids);
+       }
+
+}
diff --git a/apps/provisioning_api/lib/Groups.php b/apps/provisioning_api/lib/Groups.php
deleted file mode 100644 (file)
index 1830259..0000000
+++ /dev/null
@@ -1,182 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Tom Needham <tom@owncloud.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program.  If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-namespace OCA\Provisioning_API;
-
-use OCP\IGroup;
-use OCP\IUser;
-
-class Groups{
-
-       /** @var \OCP\IGroupManager */
-       private $groupManager;
-
-       /** @var \OCP\IUserSession */
-       private $userSession;
-
-       /** @var \OCP\IRequest */
-       private $request;
-
-       /**
-        * @param \OCP\IGroupManager $groupManager
-        * @param \OCP\IUserSession $userSession
-        * @param \OCP\IRequest $request
-        */
-       public function __construct(\OCP\IGroupManager $groupManager,
-                                                               \OCP\IUserSession $userSession,
-                                                               \OCP\IRequest $request) {
-               $this->groupManager = $groupManager;
-               $this->userSession = $userSession;
-               $this->request = $request;
-       }
-
-       /**
-        * returns a list of groups
-        *
-        * @param array $parameters
-        * @return \OC\OCS\Result
-        */
-       public function getGroups($parameters) {
-               $search = $this->request->getParam('search', '');
-               $limit = $this->request->getParam('limit');
-               $offset = $this->request->getParam('offset');
-
-               if ($limit !== null) {
-                       $limit = (int)$limit;
-               }
-               if ($offset !== null) {
-                       $offset = (int)$offset;
-               }
-
-               $groups = $this->groupManager->search($search, $limit, $offset);
-               $groups = array_map(function($group) {
-                       /** @var IGroup $group */
-                       return $group->getGID();
-               }, $groups);
-
-               return new \OC\OCS\Result(['groups' => $groups]);
-       }
-
-       /**
-        * returns an array of users in the group specified
-        *
-        * @param array $parameters
-        * @return \OC\OCS\Result
-        */
-       public function getGroup($parameters) {
-               // Check if user is logged in
-               $user = $this->userSession->getUser();
-               if ($user === null) {
-                       return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
-               }
-
-               $groupId = $parameters['groupid'];
-
-               // Check the group exists
-               if(!$this->groupManager->groupExists($groupId)) {
-                       return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found');
-               }
-
-               $isSubadminOfGroup = false;
-               $group = $this->groupManager->get($groupId);
-               if ($group !== null) {
-                       $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group);
-               }
-
-               // Check subadmin has access to this group
-               if($this->groupManager->isAdmin($user->getUID())
-                  || $isSubadminOfGroup) {
-                       $users = $this->groupManager->get($groupId)->getUsers();
-                       $users =  array_map(function($user) {
-                               /** @var IUser $user */
-                               return $user->getUID();
-                       }, $users);
-                       $users = array_values($users);
-                       return new \OC\OCS\Result(['users' => $users]);
-               } else {
-                       return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group');
-               }
-       }
-
-       /**
-        * creates a new group
-        *
-        * @param array $parameters
-        * @return \OC\OCS\Result
-        */
-       public function addGroup($parameters) {
-               // Validate name
-               $groupId = $this->request->getParam('groupid', '');
-               if(empty($groupId)){
-                       \OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR);
-                       return new \OC\OCS\Result(null, 101, 'Invalid group name');
-               }
-               // Check if it exists
-               if($this->groupManager->groupExists($groupId)){
-                       return new \OC\OCS\Result(null, 102);
-               }
-               $this->groupManager->createGroup($groupId);
-               return new \OC\OCS\Result(null, 100);
-       }
-
-       /**
-        * @param array $parameters
-        * @return \OC\OCS\Result
-        */
-       public function deleteGroup($parameters) {
-               // Check it exists
-               if(!$this->groupManager->groupExists($parameters['groupid'])){
-                       return new \OC\OCS\Result(null, 101);
-               } else if($parameters['groupid'] === 'admin' || !$this->groupManager->get($parameters['groupid'])->delete()){
-                       // Cannot delete admin group
-                       return new \OC\OCS\Result(null, 102);
-               } else {
-                       return new \OC\OCS\Result(null, 100);
-               }
-       }
-
-       /**
-        * @param array $parameters
-        * @return \OC\OCS\Result
-        */
-       public function getSubAdminsOfGroup($parameters) {
-               $group = $parameters['groupid'];
-               // Check group exists
-               $targetGroup = $this->groupManager->get($group);
-               if($targetGroup === null) {
-                       return new \OC\OCS\Result(null, 101, 'Group does not exist');
-               }
-
-               $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
-               // New class returns IUser[] so convert back
-               $uids = [];
-               foreach ($subadmins as $user) {
-                       $uids[] = $user->getUID();
-               }
-
-               return new \OC\OCS\Result($uids);
-       }
-
-}
diff --git a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php
new file mode 100644 (file)
index 0000000..25059e8
--- /dev/null
@@ -0,0 +1,378 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ * @author Lukas Reschke <lukas@statuscode.ch>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ * @author Tom Needham <tom@owncloud.com>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Provisioning_API\Tests\Controller;
+
+use OCA\Provisioning_API\Controller\GroupsController;
+use OCP\IGroupManager;
+use OCP\IUserSession;
+
+class GroupsControllerTest extends \Test\TestCase {
+       /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
+       protected $groupManager;
+       /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+       protected $userSession;
+       /** @var \OC\SubAdmin|\PHPUnit_Framework_MockObject_MockObject */
+       protected $subAdminManager;
+       /** @var GroupsController */
+       protected $api;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->subAdminManager = $this->getMockBuilder('OC\SubAdmin')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $this->groupManager = $this->getMockBuilder('OC\Group\Manager')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $this->groupManager
+                       ->method('getSubAdmin')
+                       ->willReturn($this->subAdminManager);
+
+               $this->userSession = $this->getMockBuilder('OCP\IUserSession')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $request = $this->getMockBuilder('OCP\IRequest')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $this->api = new GroupsController(
+                       'provisioning_api',
+                       $request,
+                       $this->groupManager,
+                       $this->userSession
+               );
+       }
+
+       /**
+        * @param string $gid
+        * @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject
+        */
+       private function createGroup($gid) {
+               $group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
+               $group
+                       ->method('getGID')
+                       ->willReturn($gid);
+               return $group;
+       }
+
+       /**
+        * @param string $uid
+        * @return \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject
+        */
+       private function createUser($uid) {
+               $user = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
+               $user
+                       ->method('getUID')
+                       ->willReturn($uid);
+               return $user;
+       }
+
+       private function asUser() {
+               $user = $this->createUser('user');
+               $this->userSession
+                       ->method('getUser')
+                       ->willReturn($user);
+       }
+
+       private function asAdmin() {
+               $user = $this->createUser('admin');
+               $this->userSession
+                       ->method('getUser')
+                       ->willReturn($user);
+
+               $this->groupManager
+                       ->method('isAdmin')
+                       ->with('admin')
+                       ->willReturn(true);
+       }
+
+       private function asSubAdminOfGroup($group) {
+               $user = $this->createUser('subAdmin');
+               $this->userSession
+                       ->method('getUser')
+                       ->willReturn($user);
+
+               $this->subAdminManager
+                       ->method('isSubAdminOfGroup')
+                       ->will($this->returnCallback(function($_user, $_group) use ($user, $group) {
+                               if ($_user === $user && $_group === $group) {
+                                       return true;
+                               }
+                               return false;
+                       }));
+       }
+
+       public function dataGetGroups() {
+               return [
+                       [null, null, null],
+                       ['foo', null, null],
+                       [null, 1, null],
+                       [null, null, 2],
+                       ['foo', 1, 2],
+               ];
+       }
+
+       /**
+        * @dataProvider dataGetGroups
+        *
+        * @param string|null $search
+        * @param int|null $limit
+        * @param int|null $offset
+        */
+       public function testGetGroups($search, $limit, $offset) {
+               $groups = [$this->createGroup('group1'), $this->createGroup('group2')];
+
+               $search = $search === null ? '' : $search;
+
+               $this->groupManager
+                       ->expects($this->once())
+                       ->method('search')
+                       ->with($search, $limit, $offset)
+                       ->willReturn($groups);
+
+               $result = $this->api->getGroups($search, $limit, $offset);
+               $this->assertEquals(['groups' => ['group1', 'group2']], $result->getData());
+       }
+
+       public function testGetGroupAsSubadmin() {
+               $group = $this->createGroup('group');
+               $this->asSubAdminOfGroup($group);
+
+               $this->groupManager
+                       ->method('get')
+                       ->with('group')
+                       ->willReturn($group);
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('group')
+                       ->willReturn(true);
+               $group
+                       ->method('getUsers')
+                       ->willReturn([
+                               $this->createUser('user1'),
+                               $this->createUser('user2')
+                       ]);
+
+               $result = $this->api->getGroup('group');
+
+               $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        * @expectedExceptionCode 997
+        */
+       public function testGetGroupAsIrrelevantSubadmin() {
+               $group = $this->createGroup('group');
+               $otherGroup = $this->createGroup('otherGroup');
+               $this->asSubAdminOfGroup($otherGroup);
+
+               $this->groupManager
+                       ->method('get')
+                       ->with('group')
+                       ->willReturn($group);
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('group')
+                       ->willReturn(true);
+
+               $this->api->getGroup('group');
+       }
+
+       public function testGetGroupAsAdmin() {
+               $group = $this->createGroup('group');
+               $this->asAdmin();
+
+               $this->groupManager
+                       ->method('get')
+                       ->with('group')
+                       ->willReturn($group);
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('group')
+                       ->willReturn(true);
+               $group
+                       ->method('getUsers')
+                       ->willReturn([
+                               $this->createUser('user1'),
+                               $this->createUser('user2')
+                       ]);
+
+               $result = $this->api->getGroup('group');
+
+               $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        * @expectedExceptionCode 998
+        * @expectedExceptionMessage The requested group could not be found
+        */
+       public function testGetGroupNonExisting() {
+               $this->asUser();
+
+               $this->api->getGroup($this->getUniqueID());
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        * @expectedExceptionCode 101
+        * @expectedExceptionMessage Group does not exist
+        */
+       public function testGetSubAdminsOfGroupsNotExists() {
+               $this->api->getSubAdminsOfGroup('NonExistingGroup');
+       }
+
+       public function testGetSubAdminsOfGroup() {
+               $group = $this->createGroup('GroupWithSubAdmins');
+               $this->groupManager
+                       ->method('get')
+                       ->with('GroupWithSubAdmins')
+                       ->willReturn($group);
+
+               $this->subAdminManager
+                       ->expects($this->once())
+                       ->method('getGroupsSubAdmins')
+                       ->with($group)
+                       ->willReturn([
+                               $this->createUser('SubAdmin1'),
+                               $this->createUser('SubAdmin2'),
+                       ]);
+
+               $result = $this->api->getSubAdminsOfGroup('GroupWithSubAdmins');
+               $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData());
+       }
+
+       public function testGetSubAdminsOfGroupEmptyList() {
+               $group = $this->createGroup('GroupWithOutSubAdmins');
+               $this->groupManager
+                       ->method('get')
+                       ->with('GroupWithOutSubAdmins')
+                       ->willReturn($group);
+
+               $this->subAdminManager
+                       ->expects($this->once())
+                       ->method('getGroupsSubAdmins')
+                       ->with($group)
+                       ->willReturn([
+                       ]);
+
+               $result = $this->api->getSubAdminsOfGroup('GroupWithOutSubAdmins');
+               $this->assertEquals([], $result->getData());
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        * @expectedExceptionCode 101
+        * @expectedExceptionMessage Invalid group name
+        */
+       public function testAddGroupEmptyGroup() {
+               $this->api->addGroup('');
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        * @expectedExceptionCode 102
+        */
+       public function testAddGroupExistingGroup() {
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('ExistingGroup')
+                       ->willReturn(true);
+
+               $this->api->addGroup('ExistingGroup');
+       }
+
+       public function testAddGroup() {
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('NewGroup')
+                       ->willReturn(false);
+
+               $this->groupManager
+                       ->expects($this->once())
+                       ->method('createGroup')
+                       ->with('NewGroup');
+
+               $this->api->addGroup('NewGroup');
+       }
+
+       public function testAddGroupWithSpecialChar() {
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('Iñtërnâtiônàlizætiøn')
+                       ->willReturn(false);
+
+               $this->groupManager
+                       ->expects($this->once())
+                       ->method('createGroup')
+                       ->with('Iñtërnâtiônàlizætiøn');
+
+               $this->api->addGroup('Iñtërnâtiônàlizætiøn');
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        * @expectedExceptionCode 101
+        */
+       public function testDeleteGroupNonExisting() {
+               $this->api->deleteGroup('NonExistingGroup');
+       }
+
+       /**
+        * @expectedException \OCP\AppFramework\OCS\OCSException
+        * @expectedExceptionCode 102
+        */
+       public function testDeleteAdminGroup() {
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('admin')
+                       ->willReturn('true');
+
+               $this->api->deleteGroup('admin');
+       }
+
+       public function testDeleteGroup() {
+               $this->groupManager
+                       ->method('groupExists')
+                       ->with('ExistingGroup')
+                       ->willReturn('true');
+
+               $group = $this->createGroup('ExistingGroup');
+               $this->groupManager
+                       ->method('get')
+                       ->with('ExistingGroup')
+                       ->willReturn($group);
+               $group
+                       ->expects($this->once())
+                       ->method('delete')
+                       ->willReturn(true);
+
+               $this->api->deleteGroup('ExistingGroup');
+       }
+}
diff --git a/apps/provisioning_api/tests/GroupsTest.php b/apps/provisioning_api/tests/GroupsTest.php
deleted file mode 100644 (file)
index 2fa19c4..0000000
+++ /dev/null
@@ -1,459 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Tom Needham <tom@owncloud.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program.  If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-namespace OCA\Provisioning_API\Tests;
-
-use OCA\Provisioning_API\Groups;
-use OCP\API;
-use OCP\IGroupManager;
-use OCP\IUserSession;
-use OCP\IRequest;
-
-class GroupsTest extends \Test\TestCase {
-       /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
-       protected $groupManager;
-       /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
-       protected $userSession;
-       /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
-       protected $request;
-       /** @var \OC\SubAdmin|\PHPUnit_Framework_MockObject_MockObject */
-       protected $subAdminManager;
-       /** @var Groups */
-       protected $api;
-
-       protected function setUp() {
-               parent::setUp();
-
-               $this->subAdminManager = $this->getMockBuilder('OC\SubAdmin')
-                       ->disableOriginalConstructor()
-                       ->getMock();
-
-               $this->groupManager = $this->getMockBuilder('OC\Group\Manager')
-                       ->disableOriginalConstructor()
-                       ->getMock();
-               $this->groupManager
-                       ->method('getSubAdmin')
-                       ->willReturn($this->subAdminManager);
-
-               $this->userSession = $this->getMockBuilder('OCP\IUserSession')
-                       ->disableOriginalConstructor()
-                       ->getMock();
-               $this->request = $this->getMockBuilder('OCP\IRequest')
-                       ->disableOriginalConstructor()
-                       ->getMock();
-               $this->api = new Groups(
-                       $this->groupManager,
-                       $this->userSession,
-                       $this->request
-               );
-       }
-
-       /**
-        * @param string $gid
-        * @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject
-        */
-       private function createGroup($gid) {
-               $group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
-               $group
-                       ->method('getGID')
-                       ->willReturn($gid);
-               return $group;
-       }
-
-       /**
-        * @param string $uid
-        * @return \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject
-        */
-       private function createUser($uid) {
-               $user = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
-               $user
-                       ->method('getUID')
-                       ->willReturn($uid);
-               return $user;
-       }
-
-       private function asUser() {
-               $user = $this->createUser('user');
-               $this->userSession
-                       ->method('getUser')
-                       ->willReturn($user);
-       }
-
-       private function asAdmin() {
-               $user = $this->createUser('admin');
-               $this->userSession
-                       ->method('getUser')
-                       ->willReturn($user);
-
-               $this->groupManager
-                       ->method('isAdmin')
-                       ->with('admin')
-                       ->willReturn(true);
-       }
-
-       private function asSubAdminOfGroup($group) {
-               $user = $this->createUser('subAdmin');
-               $this->userSession
-                       ->method('getUser')
-                       ->willReturn($user);
-
-               $this->subAdminManager
-                       ->method('isSubAdminOfGroup')
-                       ->will($this->returnCallback(function($_user, $_group) use ($user, $group) {
-                               if ($_user === $user && $_group === $group) {
-                                       return true;
-                               }
-                               return false;
-                       }));
-       }
-
-       public function dataGetGroups() {
-               return [
-                       [null, null, null],
-                       ['foo', null, null],
-                       [null, 1, null],
-                       [null, null, 2],
-                       ['foo', 1, 2],
-               ];
-       }
-
-       /**
-        * @dataProvider dataGetGroups
-        *
-        * @param string|null $search
-        * @param int|null $limit
-        * @param int|null $offset
-        */
-       public function testGetGroups($search, $limit, $offset) {
-               $this->request
-                       ->expects($this->exactly(3))
-                       ->method('getParam')
-                       ->will($this->returnValueMap([
-                               ['search', '', $search],
-                               ['limit', null, $limit],
-                               ['offset', null, $offset],
-                       ]));
-
-               $groups = [$this->createGroup('group1'), $this->createGroup('group2')];
-
-               $search = $search === null ? '' : $search;
-
-               $this->groupManager
-                       ->expects($this->once())
-                       ->method('search')
-                       ->with($search, $limit, $offset)
-                       ->willReturn($groups);
-
-               $result = $this->api->getGroups([]);
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-               $this->assertEquals(['group1', 'group2'], $result->getData()['groups']);
-       }
-
-       public function testGetGroupAsUser() {
-               $result = $this->api->getGroup([]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(API::RESPOND_UNAUTHORISED, $result->getStatusCode());
-
-       }
-
-       public function testGetGroupAsSubadmin() {
-               $group = $this->createGroup('group');
-               $this->asSubAdminOfGroup($group);
-
-               $this->groupManager
-                       ->method('get')
-                       ->with('group')
-                       ->willReturn($group);
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('group')
-                       ->willReturn(true);
-               $group
-                       ->method('getUsers')
-                       ->willReturn([
-                               $this->createUser('user1'),
-                               $this->createUser('user2')
-                       ]);
-
-               $result = $this->api->getGroup([
-                       'groupid' => 'group',
-               ]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-               $this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key');
-               $this->assertArrayHasKey('users', $result->getData());
-               $this->assertEquals(['user1', 'user2'], $result->getData()['users']);
-       }
-
-       public function testGetGroupAsIrrelevantSubadmin() {
-               $group = $this->createGroup('group');
-               $otherGroup = $this->createGroup('otherGroup');
-               $this->asSubAdminOfGroup($otherGroup);
-
-               $this->groupManager
-                       ->method('get')
-                       ->with('group')
-                       ->willReturn($group);
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('group')
-                       ->willReturn(true);
-
-               $result = $this->api->getGroup([
-                       'groupid' => 'group',
-               ]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(API::RESPOND_UNAUTHORISED, $result->getStatusCode());
-       }
-
-       public function testGetGroupAsAdmin() {
-               $group = $this->createGroup('group');
-               $this->asAdmin();
-
-               $this->groupManager
-                       ->method('get')
-                       ->with('group')
-                       ->willReturn($group);
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('group')
-                       ->willReturn(true);
-               $group
-                       ->method('getUsers')
-                       ->willReturn([
-                               $this->createUser('user1'),
-                               $this->createUser('user2')
-                       ]);
-
-               $result = $this->api->getGroup([
-                       'groupid' => 'group',
-               ]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-               $this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key');
-               $this->assertArrayHasKey('users', $result->getData());
-               $this->assertEquals(['user1', 'user2'], $result->getData()['users']);
-       }
-
-       public function testGetGroupNonExisting() {
-               $this->asUser();
-
-               $result = $this->api->getGroup([
-                       'groupid' => $this->getUniqueID()
-               ]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(API::RESPOND_NOT_FOUND, $result->getStatusCode());
-               $this->assertEquals('The requested group could not be found', $result->getMeta()['message']);
-       }
-
-       public function testGetSubAdminsOfGroupsNotExists() {
-               $result = $this->api->getSubAdminsOfGroup([
-                       'groupid' => 'NonExistingGroup',
-               ]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(101, $result->getStatusCode());
-               $this->assertEquals('Group does not exist', $result->getMeta()['message']);
-       }
-
-       public function testGetSubAdminsOfGroup() {
-               $group = $this->createGroup('GroupWithSubAdmins');
-               $this->groupManager
-                       ->method('get')
-                       ->with('GroupWithSubAdmins')
-                       ->willReturn($group);
-
-               $this->subAdminManager
-                       ->expects($this->once())
-                       ->method('getGroupsSubAdmins')
-                       ->with($group)
-                       ->willReturn([
-                               $this->createUser('SubAdmin1'),
-                               $this->createUser('SubAdmin2'),
-                       ]);
-
-               $result = $this->api->getSubAdminsOfGroup([
-                       'groupid' => 'GroupWithSubAdmins',
-               ]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-               $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData());
-       }
-
-       public function testGetSubAdminsOfGroupEmptyList() {
-               $group = $this->createGroup('GroupWithOutSubAdmins');
-               $this->groupManager
-                       ->method('get')
-                       ->with('GroupWithOutSubAdmins')
-                       ->willReturn($group);
-
-               $this->subAdminManager
-                       ->expects($this->once())
-                       ->method('getGroupsSubAdmins')
-                       ->with($group)
-                       ->willReturn([
-                       ]);
-
-               $result = $this->api->getSubAdminsOfGroup([
-                       'groupid' => 'GroupWithOutSubAdmins',
-               ]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-               $this->assertEquals([], $result->getData());
-       }
-
-       public function testAddGroupEmptyGroup() {
-               $this->request
-                       ->method('getParam')
-                       ->with('groupid')
-                       ->willReturn('');
-
-               $result = $this->api->addGroup([]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(101, $result->getStatusCode());
-               $this->assertEquals('Invalid group name', $result->getMeta()['message']);
-       }
-
-       public function testAddGroupExistingGroup() {
-               $this->request
-                       ->method('getParam')
-                       ->with('groupid')
-                       ->willReturn('ExistingGroup');
-
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('ExistingGroup')
-                       ->willReturn(true);
-
-               $result = $this->api->addGroup([]);
-
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(102, $result->getStatusCode());
-       }
-
-       public function testAddGroup() {
-               $this->request
-                       ->method('getParam')
-                       ->with('groupid')
-                       ->willReturn('NewGroup');
-
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('NewGroup')
-                       ->willReturn(false);
-
-               $this->groupManager
-                       ->expects($this->once())
-                       ->method('createGroup')
-                       ->with('NewGroup');
-
-               $result = $this->api->addGroup([]);
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-       }
-
-       public function testAddGroupWithSpecialChar() {
-               $this->request
-                       ->method('getParam')
-                       ->with('groupid')
-                       ->willReturn('Iñtërnâtiônàlizætiøn');
-
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('Iñtërnâtiônàlizætiøn')
-                       ->willReturn(false);
-
-               $this->groupManager
-                       ->expects($this->once())
-                       ->method('createGroup')
-                       ->with('Iñtërnâtiônàlizætiøn');
-
-               $result = $this->api->addGroup([]);
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-       }
-
-       public function testDeleteGroupNonExisting() {
-               $result = $this->api->deleteGroup([
-                       'groupid' => 'NonExistingGroup'
-               ]);
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(101, $result->getStatusCode());
-       }
-
-       public function testDeleteAdminGroup() {
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('admin')
-                       ->willReturn('true');
-
-               $result = $this->api->deleteGroup([
-                       'groupid' => 'admin'
-               ]);
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertFalse($result->succeeded());
-               $this->assertEquals(102, $result->getStatusCode());
-       }
-
-       public function testDeleteGroup() {
-               $this->groupManager
-                       ->method('groupExists')
-                       ->with('ExistingGroup')
-                       ->willReturn('true');
-
-               $group = $this->createGroup('ExistingGroup');
-               $this->groupManager
-                       ->method('get')
-                       ->with('ExistingGroup')
-                       ->willReturn($group);
-               $group
-                       ->expects($this->once())
-                       ->method('delete')
-                       ->willReturn(true);
-
-               $result = $this->api->deleteGroup([
-                       'groupid' => 'ExistingGroup',
-               ]);
-               $this->assertInstanceOf('\OC\OCS\Result', $result);
-               $this->assertTrue($result->succeeded());
-       }
-}