Commit 5ec64c4f authored by Thomas Müller's avatar Thomas Müller
Browse files

Merge pull request #20292 from owncloud/provisioning_api_group_unittest

Moved grouptest over to pure unit tests
parents 854f9dbe 61425b76
...@@ -51,7 +51,8 @@ API::register('get', '/cloud/users/{userid}/subadmins', [$users, 'getUserSubAdmi ...@@ -51,7 +51,8 @@ API::register('get', '/cloud/users/{userid}/subadmins', [$users, 'getUserSubAdmi
// Groups // Groups
$groups = new \OCA\Provisioning_API\Groups( $groups = new \OCA\Provisioning_API\Groups(
\OC::$server->getGroupManager(), \OC::$server->getGroupManager(),
\OC::$server->getUserSession() \OC::$server->getUserSession(),
\OC::$server->getRequest()
); );
API::register('get', '/cloud/groups', [$groups, 'getGroups'], 'provisioning_api', API::SUBADMIN_AUTH); 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('post', '/cloud/groups', [$groups, 'addGroup'], 'provisioning_api', API::SUBADMIN_AUTH);
......
...@@ -37,14 +37,20 @@ class Groups{ ...@@ -37,14 +37,20 @@ class Groups{
/** @var \OCP\IUserSession */ /** @var \OCP\IUserSession */
private $userSession; private $userSession;
/** @var \OCP\IRequest */
private $request;
/** /**
* @param \OCP\IGroupManager $groupManager * @param \OCP\IGroupManager $groupManager
* @param \OCP\IUserSession $userSession * @param \OCP\IUserSession $userSession
* @param \OCP\IRequest $request
*/ */
public function __construct(\OCP\IGroupManager $groupManager, public function __construct(\OCP\IGroupManager $groupManager,
\OCP\IUserSession $userSession) { \OCP\IUserSession $userSession,
\OCP\IRequest $request) {
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->request = $request;
} }
/** /**
...@@ -54,9 +60,16 @@ class Groups{ ...@@ -54,9 +60,16 @@ class Groups{
* @return OC_OCS_Result * @return OC_OCS_Result
*/ */
public function getGroups($parameters) { public function getGroups($parameters) {
$search = !empty($_GET['search']) ? $_GET['search'] : ''; $search = $this->request->getParam('search', '');
$limit = !empty($_GET['limit']) ? $_GET['limit'] : null; $limit = $this->request->getParam('limit');
$offset = !empty($_GET['offset']) ? $_GET['offset'] : null; $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 = $this->groupManager->search($search, $limit, $offset);
$groups = array_map(function($group) { $groups = array_map(function($group) {
...@@ -80,21 +93,23 @@ class Groups{ ...@@ -80,21 +93,23 @@ class Groups{
return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); 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($parameters['groupid'])) { if(!$this->groupManager->groupExists($groupId)) {
return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found'); return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found');
} }
$isSubadminOfGroup = false; $isSubadminOfGroup = false;
$targetGroupObject =$this->groupManager->get($parameters['groupid']); $group = $this->groupManager->get($groupId);
if($targetGroupObject !== null) { if ($group !== null) {
$isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $targetGroupObject); $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group);
} }
// Check subadmin has access to this group // Check subadmin has access to this group
if($this->groupManager->isAdmin($user->getUID()) if($this->groupManager->isAdmin($user->getUID())
|| $isSubadminOfGroup) { || $isSubadminOfGroup) {
$users = $this->groupManager->get($parameters['groupid'])->getUsers(); $users = $this->groupManager->get($groupId)->getUsers();
$users = array_map(function($user) { $users = array_map(function($user) {
/** @var IUser $user */ /** @var IUser $user */
return $user->getUID(); return $user->getUID();
...@@ -114,7 +129,7 @@ class Groups{ ...@@ -114,7 +129,7 @@ class Groups{
*/ */
public function addGroup($parameters) { public function addGroup($parameters) {
// Validate name // Validate name
$groupId = isset($_POST['groupid']) ? $_POST['groupid'] : ''; $groupId = $this->request->getParam('groupid', '');
if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $groupId ) || empty($groupId)){ if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $groupId ) || empty($groupId)){
\OCP\Util::writeLog('provisioning_api', 'Attempt made to create group using invalid characters.', \OCP\Util::ERROR); \OCP\Util::writeLog('provisioning_api', 'Attempt made to create group using invalid characters.', \OCP\Util::ERROR);
return new OC_OCS_Result(null, 101, 'Invalid group name'); return new OC_OCS_Result(null, 101, 'Invalid group name');
......
...@@ -25,79 +25,131 @@ ...@@ -25,79 +25,131 @@
namespace OCA\Provisioning_API\Tests; namespace OCA\Provisioning_API\Tests;
use OCP\IUserManager;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\IRequest;
class GroupsTest extends TestCase { class GroupsTest extends \Test\TestCase {
/** @var IUserManager */
protected $userManager;
/** @var IGroupManager */ /** @var IGroupManager */
protected $groupManager; protected $groupManager;
/** @var IUserSession */ /** @var IUserSession */
protected $userSession; protected $userSession;
/** @var IRequest */
protected $request;
/** @var \OC\SubAdmin */
protected $subAdminManager;
/** @var \OCA\Provisioning_API\Groups */ /** @var \OCA\Provisioning_API\Groups */
protected $api; protected $api;
protected function setup() { protected function setup() {
parent::setup(); $this->subAdminManager = $this->getMockBuilder('OC\SubAdmin')->disableOriginalConstructor()->getMock();
$this->userManager = \OC::$server->getUserManager(); $this->groupManager = $this->getMockBuilder('OC\Group\Manager')->disableOriginalConstructor()->getMock();
$this->groupManager = \OC::$server->getGroupManager(); $this->groupManager
$this->userSession = \OC::$server->getUserSession(); ->method('getSubAdmin')
->willReturn($this->subAdminManager);
$this->userSession = $this->getMock('OCP\IUserSession');
$this->request = $this->getMock('OCP\IRequest');
$this->api = new \OCA\Provisioning_API\Groups( $this->api = new \OCA\Provisioning_API\Groups(
$this->groupManager, $this->groupManager,
$this->userSession $this->userSession,
$this->request
); );
} }
public function testGetGroups() { private function createGroup($gid) {
$groups = []; $group = $this->getMock('OCP\IGroup');
$id = $this->getUniqueID(); $group
->method('getGID')
->willReturn($gid);
return $group;
}
for ($i=0; $i < 10; $i++) { private function createUser($uid) {
$groups[] = $this->groupManager->createGroup($id . '_' . $i); $user = $this->getMock('OCP\IUser');
} $user
->method('getUID')
->willReturn($uid);
return $user;
}
$_GET = []; private function asUser() {
$result = $this->api->getGroups([]); $user = $this->createUser('user');
$this->assertInstanceOf('OC_OCS_Result', $result); $this->userSession
$this->assertTrue($result->succeeded()); ->method('getUser')
$this->assertCount(count($this->groupManager->search('')), $result->getData()['groups']); ->willReturn($user);
$this->assertContains('admin', $result->getData()['groups']); }
foreach ($groups as $group) {
$this->assertContains($group->getGID(), $result->getData()['groups']); private function asAdmin() {
} $user = $this->createUser('admin');
$this->userSession
$_GET = [ ->method('getUser')
'search' => $id, ->willReturn($user);
'limit' => 5,
'offset' => 2 $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
*/
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([]); $result = $this->api->getGroups([]);
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded()); $this->assertTrue($result->succeeded());
$this->assertCount(5, $result->getData()['groups']); $this->assertEquals(['group1', 'group2'], $result->getData()['groups']);
foreach (array_splice($groups, 2, 5) as $group) {
$this->assertContains($group->getGID(), $result->getData()['groups']);
}
foreach ($groups as $group) {
$group->delete();
}
} }
public function testGetGroupAsUser() { public function testGetGroupAsUser() {
$result = $this->api->getGroup([]);
$users = $this->generateUsers(2);
$this->userSession->setUser($users[0]);
$group = $this->groupManager->createGroup($this->getUniqueID());
$group->addUser($users[1]);
$result = $this->api->getGroup(array(
'groupid' => $group->getGID(),
));
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertFalse($result->succeeded()); $this->assertFalse($result->succeeded());
...@@ -106,80 +158,91 @@ class GroupsTest extends TestCase { ...@@ -106,80 +158,91 @@ class GroupsTest extends TestCase {
} }
public function testGetGroupAsSubadmin() { public function testGetGroupAsSubadmin() {
$group = $this->createGroup('group');
$users = $this->generateUsers(2); $this->asSubAdminOfGroup($group);
$this->userSession->setUser($users[0]);
$this->groupManager
$group = $this->groupManager->createGroup($this->getUniqueID()); ->method('get')
$group->addUser($users[0]); ->with('group')
$group->addUser($users[1]); ->willReturn($group);
$this->groupManager
$this->groupManager->getSubAdmin()->createSubAdmin($users[0], $group); ->method('groupExists')
->with('group')
->willReturn(true);
$group
->method('getUsers')
->willReturn([
$this->createUser('user1'),
$this->createUser('user2')
]);
$result = $this->api->getGroup([ $result = $this->api->getGroup([
'groupid' => $group->getGID(), 'groupid' => 'group',
]); ]);
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded()); $this->assertTrue($result->succeeded());
$this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key'); $this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key');
$this->assertArrayHasKey('users', $result->getData()); $this->assertArrayHasKey('users', $result->getData());
$resultData = $result->getData(); $this->assertEquals(['user1', 'user2'], $result->getData()['users']);
$resultData = $resultData['users'];
$users = array_map(function($user) {
return $user->getUID();
}, $users);
sort($users);
sort($resultData);
$this->assertEquals($users, $resultData);
} }
public function testGetGroupAsIrrelevantSubadmin() { public function testGetGroupAsIrrelevantSubadmin() {
$group = $this->createGroup('group');
$users = $this->generateUsers(2); $otherGroup = $this->createGroup('otherGroup');
$this->userSession->setUser($users[0]); $this->asSubAdminOfGroup($otherGroup);
$group1 = $this->groupManager->createGroup($this->getUniqueID()); $this->groupManager
$group2 = $this->groupManager->createGroup($this->getUniqueID()); ->method('get')
$group1->addUser($users[1]); ->with('group')
$group2->addUser($users[0]); ->willReturn($group);
$this->groupManager
$this->groupManager->getSubAdmin()->createSubAdmin($users[0], $group2); ->method('groupExists')
->with('group')
->willReturn(true);
$result = $this->api->getGroup([ $result = $this->api->getGroup([
'groupid' => $group1->getGID(), 'groupid' => 'group',
]); ]);
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertFalse($result->succeeded()); $this->assertFalse($result->succeeded());
$this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode()); $this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode());
} }
public function testGetGroupAsAdmin() { public function testGetGroupAsAdmin() {
$group = $this->createGroup('group');
$users = $this->generateUsers(2); $this->asAdmin();
$this->userSession->setUser($users[0]);
$this->groupManager
$group = $this->groupManager->createGroup($this->getUniqueID()); ->method('get')
->with('group')
$group->addUser($users[1]); ->willReturn($group);
$this->groupManager->get('admin')->addUser($users[0]); $this->groupManager
->method('groupExists')
->with('group')
->willReturn(true);
$group
->method('getUsers')
->willReturn([
$this->createUser('user1'),
$this->createUser('user2')
]);
$result = $this->api->getGroup([ $result = $this->api->getGroup([
'groupid' => $group->getGID(), 'groupid' => 'group',
]); ]);
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded()); $this->assertTrue($result->succeeded());
$this->assertEquals(['users' => [$users[1]->getUID()]], $result->getData()); $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() { public function testGetGroupNonExisting() {
$this->asUser();
$result = $this->api->getGroup([ $result = $this->api->getGroup([
'groupid' => $this->getUniqueId() 'groupid' => $this->getUniqueId()
]); ]);
...@@ -190,35 +253,72 @@ class GroupsTest extends TestCase { ...@@ -190,35 +253,72 @@ class GroupsTest extends TestCase {
$this->assertEquals('The requested group could not be found', $result->getMeta()['message']); $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() { public function testGetSubAdminsOfGroup() {
$user1 = $this->generateUsers(); $group = $this->createGroup('GroupWithSubAdmins');
$user2 = $this->generateUsers(); $this->groupManager
$this->userSession->setUser($user1); ->method('get')
$this->groupManager->get('admin')->addUser($user1); ->with('GroupWithSubAdmins')
$group1 = $this->groupManager->createGroup($this->getUniqueID()); ->willReturn($group);
$this->groupManager->getSubAdmin()->createSubAdmin($user2, $group1);
$this->subAdminManager
->expects($this->once())
->method('getGroupsSubAdmins')
->with($group)
->willReturn([
$this->createUser('SubAdmin1'),
$this->createUser('SubAdmin2'),
]);
$result = $this->api->getSubAdminsOfGroup([ $result = $this->api->getSubAdminsOfGroup([
'groupid' => $group1->getGID(), 'groupid' => 'GroupWithSubAdmins',
]); ]);
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertTrue($result->succeeded()); $this->assertTrue($result->succeeded());
$data = $result->getData(); $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData());
$this->assertEquals($user2->getUID(), reset($data)); }
$group1->delete();
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([
]);
$user1 = $this->generateUsers();
$this->userSession->setUser($user1);
$this->groupManager->get('admin')->addUser($user1);
$result = $this->api->getSubAdminsOfGroup([ $result = $this->api->getSubAdminsOfGroup([
'groupid' => $this->getUniqueID(), 'groupid' => 'GroupWithOutSubAdmins',
]); ]);
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
$this->assertFalse($result->succeeded()); $this->assertFalse($result->succeeded());
$this->assertEquals(101, $result->getStatusCode()); $this->assertEquals(102, $result->getStatusCode());
$this->assertEquals('Unknown error occured', $result->getMeta()['message']);
} }
public function testAddGroupEmptyGroup() { public function testAddGroupEmptyGroup() {
$_POST = []; $this->request
->method('getParam')
->with('groupid')
->willReturn('');
$result = $this->api->addGroup([]); $result = $this->api->addGroup([]);
$this->assertInstanceOf('OC_OCS_Result', $result); $this->assertInstanceOf('OC_OCS_Result', $result);
...@@ -228,40 +328,47 @@ class GroupsTest extends TestCase { ...@@ -228,40 +328,47 @@ class GroupsTest extends TestCase {
} }
public function testAddGroupExistingGroup() { public function testAddGroupExistingGroup() {
$group = $this->groupManager->createGroup($this->getUniqueID()); $this->request
->method('getParam')
->with('groupid')
->willReturn('ExistingGroup');
$this->groupManager
->method('groupExists')
->with('ExistingGroup')
->willReturn(true