Move Groups over to OCSController

* Take advantage of the AppFramework
* Fix tests
This commit is contained in:
Roeland Jago Douma 2016-08-11 09:46:25 +02:00
parent 0fdeefe47c
commit 432e7c93c6
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
3 changed files with 138 additions and 214 deletions

View File

@ -26,13 +26,22 @@
* *
*/ */
namespace OCA\Provisioning_API\AppInfo;
use OCA\Provisioning_API\Apps; use OCA\Provisioning_API\Apps;
use OCA\Provisioning_API\Groups;
use OCA\Provisioning_API\Users; use OCA\Provisioning_API\Users;
use OCP\API; 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
$users = new Users( $users = new Users(
\OC::$server->getUserManager(), \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('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); 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
$apps = new Apps( $apps = new Apps(
\OC::$server->getAppManager(), \OC::$server->getAppManager(),

View File

@ -23,46 +23,54 @@
* *
*/ */
namespace OCA\Provisioning_API; namespace OCA\Provisioning_API\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCSController;
use OCP\IGroup; use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\IUser; use OCP\IUser;
class Groups{
/** @var \OCP\IGroupManager */ class GroupsController extends OCSController {
/** @var IGroupManager */
private $groupManager; private $groupManager;
/** @var \OCP\IUserSession */ /** @var IUserSession */
private $userSession; private $userSession;
/** @var \OCP\IRequest */
private $request;
/** /**
* @param \OCP\IGroupManager $groupManager * @param string $appName
* @param \OCP\IUserSession $userSession * @param IRequest $request
* @param \OCP\IRequest $request * @param IGroupManager $groupManager
* @param IUserSession $userSession
*/ */
public function __construct(\OCP\IGroupManager $groupManager, public function __construct(
\OCP\IUserSession $userSession, $appName,
\OCP\IRequest $request) { IRequest $request,
IGroupManager $groupManager,
IUserSession $userSession) {
parent::__construct($appName, $request);
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->request = $request;
} }
/** /**
* returns a list of groups * returns a list of groups
* *
* @param array $parameters * @NoAdminRequired
* @return \OC\OCS\Result *
* @param string $search
* @param int $limit
* @param int $offset
* @return DataResponse
*/ */
public function getGroups($parameters) { public function getGroups($search = '', $limit = null, $offset = null) {
$search = $this->request->getParam('search', '');
$limit = $this->request->getParam('limit');
$offset = $this->request->getParam('offset');
if ($limit !== null) { if ($limit !== null) {
$limit = (int)$limit; $limit = (int)$limit;
} }
@ -76,27 +84,24 @@ class Groups{
return $group->getGID(); return $group->getGID();
}, $groups); }, $groups);
return new \OC\OCS\Result(['groups' => $groups]); return new DataResponse(['groups' => $groups]);
} }
/** /**
* returns an array of users in the group specified * returns an array of users in the group specified
* *
* @param array $parameters * @NoAdminRequired
* @return \OC\OCS\Result *
* @param string $groupId
* @return DataResponse
* @throws OCSException
*/ */
public function getGroup($parameters) { public function getGroup($groupId) {
// Check if user is logged in
$user = $this->userSession->getUser(); $user = $this->userSession->getUser();
if ($user === null) {
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
}
$groupId = $parameters['groupid'];
// Check the group exists // Check the group exists
if(!$this->groupManager->groupExists($groupId)) { if(!$this->groupManager->groupExists($groupId)) {
return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found'); throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND);
} }
$isSubadminOfGroup = false; $isSubadminOfGroup = false;
@ -114,59 +119,62 @@ class Groups{
return $user->getUID(); return $user->getUID();
}, $users); }, $users);
$users = array_values($users); $users = array_values($users);
return new \OC\OCS\Result(['users' => $users]); return new DataResponse(['users' => $users]);
} else { } else {
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group'); throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);
} }
} }
/** /**
* creates a new group * creates a new group
* *
* @param array $parameters * @NoAdminRequired
* @return \OC\OCS\Result *
* @param string $groupid
* @return DataResponse
* @throws OCSException
*/ */
public function addGroup($parameters) { public function addGroup($groupid) {
// Validate name // Validate name
$groupId = $this->request->getParam('groupid', ''); if(empty($groupid)){
if(empty($groupId)){
\OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR); \OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR);
return new \OC\OCS\Result(null, 101, 'Invalid group name'); throw new OCSException('Invalid group name', 101);
} }
// Check if it exists // Check if it exists
if($this->groupManager->groupExists($groupId)){ if($this->groupManager->groupExists($groupid)){
return new \OC\OCS\Result(null, 102); throw new OCSException('', 102);
} }
$this->groupManager->createGroup($groupId); $this->groupManager->createGroup($groupid);
return new \OC\OCS\Result(null, 100); return new DataResponse();
} }
/** /**
* @param array $parameters * @param string $groupId
* @return \OC\OCS\Result * @return DataResponse
* @throws OCSException
*/ */
public function deleteGroup($parameters) { public function deleteGroup($groupId) {
// Check it exists // Check it exists
if(!$this->groupManager->groupExists($parameters['groupid'])){ if(!$this->groupManager->groupExists($groupId)){
return new \OC\OCS\Result(null, 101); throw new OCSException('', 101);
} else if($parameters['groupid'] === 'admin' || !$this->groupManager->get($parameters['groupid'])->delete()){ } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){
// Cannot delete admin group // Cannot delete admin group
return new \OC\OCS\Result(null, 102); throw new OCSException('', 102);
} else {
return new \OC\OCS\Result(null, 100);
} }
return new DataResponse(null, 100);
} }
/** /**
* @param array $parameters * @param string $groupId
* @return \OC\OCS\Result * @return DataResponse
* @throws OCSException
*/ */
public function getSubAdminsOfGroup($parameters) { public function getSubAdminsOfGroup($groupId) {
$group = $parameters['groupid'];
// Check group exists // Check group exists
$targetGroup = $this->groupManager->get($group); $targetGroup = $this->groupManager->get($groupId);
if($targetGroup === null) { if($targetGroup === null) {
return new \OC\OCS\Result(null, 101, 'Group does not exist'); throw new OCSException('Group does not exist', 101);
} }
$subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
@ -176,7 +184,7 @@ class Groups{
$uids[] = $user->getUID(); $uids[] = $user->getUID();
} }
return new \OC\OCS\Result($uids); return new DataResponse($uids);
} }
} }

View File

@ -24,24 +24,20 @@
* *
*/ */
namespace OCA\Provisioning_API\Tests; namespace OCA\Provisioning_API\Tests\Controller;
use OCA\Provisioning_API\Groups; use OCA\Provisioning_API\Controller\GroupsController;
use OCP\API;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\IRequest;
class GroupsTest extends \Test\TestCase { class GroupsControllerTest extends \Test\TestCase {
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
protected $groupManager; protected $groupManager;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession; protected $userSession;
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
protected $request;
/** @var \OC\SubAdmin|\PHPUnit_Framework_MockObject_MockObject */ /** @var \OC\SubAdmin|\PHPUnit_Framework_MockObject_MockObject */
protected $subAdminManager; protected $subAdminManager;
/** @var Groups */ /** @var GroupsController */
protected $api; protected $api;
protected function setUp() { protected function setUp() {
@ -61,13 +57,14 @@ class GroupsTest extends \Test\TestCase {
$this->userSession = $this->getMockBuilder('OCP\IUserSession') $this->userSession = $this->getMockBuilder('OCP\IUserSession')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->request = $this->getMockBuilder('OCP\IRequest') $request = $this->getMockBuilder('OCP\IRequest')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->api = new Groups( $this->api = new GroupsController(
'provisioning_api',
$request,
$this->groupManager, $this->groupManager,
$this->userSession, $this->userSession
$this->request
); );
} }
@ -148,15 +145,6 @@ class GroupsTest extends \Test\TestCase {
* @param int|null $offset * @param int|null $offset
*/ */
public function testGetGroups($search, $limit, $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')]; $groups = [$this->createGroup('group1'), $this->createGroup('group2')];
$search = $search === null ? '' : $search; $search = $search === null ? '' : $search;
@ -167,19 +155,8 @@ class GroupsTest extends \Test\TestCase {
->with($search, $limit, $offset) ->with($search, $limit, $offset)
->willReturn($groups); ->willReturn($groups);
$result = $this->api->getGroups([]); $result = $this->api->getGroups($search, $limit, $offset);
$this->assertInstanceOf('\OC\OCS\Result', $result); $this->assertEquals(['groups' => ['group1', 'group2']], $result->getData());
$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() { public function testGetGroupAsSubadmin() {
@ -201,17 +178,15 @@ class GroupsTest extends \Test\TestCase {
$this->createUser('user2') $this->createUser('user2')
]); ]);
$result = $this->api->getGroup([ $result = $this->api->getGroup('group');
'groupid' => 'group',
]);
$this->assertInstanceOf('\OC\OCS\Result', $result); $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
$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']);
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 997
*/
public function testGetGroupAsIrrelevantSubadmin() { public function testGetGroupAsIrrelevantSubadmin() {
$group = $this->createGroup('group'); $group = $this->createGroup('group');
$otherGroup = $this->createGroup('otherGroup'); $otherGroup = $this->createGroup('otherGroup');
@ -226,13 +201,7 @@ class GroupsTest extends \Test\TestCase {
->with('group') ->with('group')
->willReturn(true); ->willReturn(true);
$result = $this->api->getGroup([ $this->api->getGroup('group');
'groupid' => 'group',
]);
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertFalse($result->succeeded());
$this->assertEquals(API::RESPOND_UNAUTHORISED, $result->getStatusCode());
} }
public function testGetGroupAsAdmin() { public function testGetGroupAsAdmin() {
@ -254,39 +223,29 @@ class GroupsTest extends \Test\TestCase {
$this->createUser('user2') $this->createUser('user2')
]); ]);
$result = $this->api->getGroup([ $result = $this->api->getGroup('group');
'groupid' => 'group',
]);
$this->assertInstanceOf('\OC\OCS\Result', $result); $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
$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']);
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 998
* @expectedExceptionMessage The requested group could not be found
*/
public function testGetGroupNonExisting() { public function testGetGroupNonExisting() {
$this->asUser(); $this->asUser();
$result = $this->api->getGroup([ $this->api->getGroup($this->getUniqueID());
'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']);
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 101
* @expectedExceptionMessage Group does not exist
*/
public function testGetSubAdminsOfGroupsNotExists() { public function testGetSubAdminsOfGroupsNotExists() {
$result = $this->api->getSubAdminsOfGroup([ $this->api->getSubAdminsOfGroup('NonExistingGroup');
'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() { public function testGetSubAdminsOfGroup() {
@ -305,12 +264,7 @@ class GroupsTest extends \Test\TestCase {
$this->createUser('SubAdmin2'), $this->createUser('SubAdmin2'),
]); ]);
$result = $this->api->getSubAdminsOfGroup([ $result = $this->api->getSubAdminsOfGroup('GroupWithSubAdmins');
'groupid' => 'GroupWithSubAdmins',
]);
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertTrue($result->succeeded());
$this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData()); $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData());
} }
@ -328,53 +282,33 @@ class GroupsTest extends \Test\TestCase {
->willReturn([ ->willReturn([
]); ]);
$result = $this->api->getSubAdminsOfGroup([ $result = $this->api->getSubAdminsOfGroup('GroupWithOutSubAdmins');
'groupid' => 'GroupWithOutSubAdmins',
]);
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertTrue($result->succeeded());
$this->assertEquals([], $result->getData()); $this->assertEquals([], $result->getData());
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 101
* @expectedExceptionMessage Invalid group name
*/
public function testAddGroupEmptyGroup() { public function testAddGroupEmptyGroup() {
$this->request $this->api->addGroup('');
->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']);
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 102
*/
public function testAddGroupExistingGroup() { public function testAddGroupExistingGroup() {
$this->request
->method('getParam')
->with('groupid')
->willReturn('ExistingGroup');
$this->groupManager $this->groupManager
->method('groupExists') ->method('groupExists')
->with('ExistingGroup') ->with('ExistingGroup')
->willReturn(true); ->willReturn(true);
$result = $this->api->addGroup([]); $this->api->addGroup('ExistingGroup');
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertFalse($result->succeeded());
$this->assertEquals(102, $result->getStatusCode());
} }
public function testAddGroup() { public function testAddGroup() {
$this->request
->method('getParam')
->with('groupid')
->willReturn('NewGroup');
$this->groupManager $this->groupManager
->method('groupExists') ->method('groupExists')
->with('NewGroup') ->with('NewGroup')
@ -385,17 +319,10 @@ class GroupsTest extends \Test\TestCase {
->method('createGroup') ->method('createGroup')
->with('NewGroup'); ->with('NewGroup');
$result = $this->api->addGroup([]); $this->api->addGroup('NewGroup');
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertTrue($result->succeeded());
} }
public function testAddGroupWithSpecialChar() { public function testAddGroupWithSpecialChar() {
$this->request
->method('getParam')
->with('groupid')
->willReturn('Iñtërnâtiônàlizætiøn');
$this->groupManager $this->groupManager
->method('groupExists') ->method('groupExists')
->with('Iñtërnâtiônàlizætiøn') ->with('Iñtërnâtiônàlizætiøn')
@ -406,32 +333,28 @@ class GroupsTest extends \Test\TestCase {
->method('createGroup') ->method('createGroup')
->with('Iñtërnâtiônàlizætiøn'); ->with('Iñtërnâtiônàlizætiøn');
$result = $this->api->addGroup([]); $this->api->addGroup('Iñtërnâtiônàlizætiøn');
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertTrue($result->succeeded());
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 101
*/
public function testDeleteGroupNonExisting() { public function testDeleteGroupNonExisting() {
$result = $this->api->deleteGroup([ $this->api->deleteGroup('NonExistingGroup');
'groupid' => 'NonExistingGroup'
]);
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertFalse($result->succeeded());
$this->assertEquals(101, $result->getStatusCode());
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 102
*/
public function testDeleteAdminGroup() { public function testDeleteAdminGroup() {
$this->groupManager $this->groupManager
->method('groupExists') ->method('groupExists')
->with('admin') ->with('admin')
->willReturn('true'); ->willReturn('true');
$result = $this->api->deleteGroup([ $this->api->deleteGroup('admin');
'groupid' => 'admin'
]);
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertFalse($result->succeeded());
$this->assertEquals(102, $result->getStatusCode());
} }
public function testDeleteGroup() { public function testDeleteGroup() {
@ -450,10 +373,6 @@ class GroupsTest extends \Test\TestCase {
->method('delete') ->method('delete')
->willReturn(true); ->willReturn(true);
$result = $this->api->deleteGroup([ $this->api->deleteGroup('ExistingGroup');
'groupid' => 'ExistingGroup',
]);
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertTrue($result->succeeded());
} }
} }