2014-12-22 16:54:50 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-10-27 16:09:45 +03:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-26 15:54:55 +03:00
|
|
|
* @author Roeland Jago Douma <rullzer@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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.
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-12-22 16:54:50 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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/>
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
namespace OCA\Provisioning_API\Tests;
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-08-11 12:47:01 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
class GroupsTest extends TestCase {
|
2015-08-11 12:47:01 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
/** @var IGroupManager */
|
|
|
|
protected $groupManager;
|
|
|
|
/** @var IUserSession */
|
|
|
|
protected $userSession;
|
2015-10-27 16:09:45 +03:00
|
|
|
/** @var \OCA\Provisioning_API\Groups */
|
|
|
|
protected $api;
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2015-07-25 15:32:49 +03:00
|
|
|
protected function setup() {
|
|
|
|
parent::setup();
|
|
|
|
|
|
|
|
$this->userManager = \OC::$server->getUserManager();
|
|
|
|
$this->groupManager = \OC::$server->getGroupManager();
|
2015-07-25 16:01:31 +03:00
|
|
|
$this->userSession = \OC::$server->getUserSession();
|
|
|
|
$this->api = new \OCA\Provisioning_API\Groups(
|
|
|
|
$this->groupManager,
|
|
|
|
$this->userSession
|
|
|
|
);
|
2015-07-25 15:32:49 +03:00
|
|
|
}
|
|
|
|
|
2015-08-11 12:47:01 +03:00
|
|
|
public function testGetGroups() {
|
|
|
|
$groups = [];
|
|
|
|
$id = $this->getUniqueID();
|
|
|
|
|
|
|
|
for ($i=0; $i < 10; $i++) {
|
|
|
|
$groups[] = $this->groupManager->createGroup($id . '_' . $i);
|
|
|
|
}
|
|
|
|
|
|
|
|
$_GET = [];
|
|
|
|
$result = $this->api->getGroups([]);
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertTrue($result->succeeded());
|
2015-08-11 16:22:59 +03:00
|
|
|
$this->assertCount(count($this->groupManager->search('')), $result->getData()['groups']);
|
2015-08-11 12:47:01 +03:00
|
|
|
$this->assertContains('admin', $result->getData()['groups']);
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$this->assertContains($group->getGID(), $result->getData()['groups']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$_GET = [
|
|
|
|
'search' => $id,
|
|
|
|
'limit' => 5,
|
|
|
|
'offset' => 2
|
|
|
|
];
|
|
|
|
$result = $this->api->getGroups([]);
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertTrue($result->succeeded());
|
|
|
|
$this->assertCount(5, $result->getData()['groups']);
|
|
|
|
foreach (array_splice($groups, 2, 5) as $group) {
|
|
|
|
$this->assertContains($group->getGID(), $result->getData()['groups']);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$group->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetGroupAsUser() {
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$users = $this->generateUsers(2);
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->userSession->setUser($users[0]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 15:32:49 +03:00
|
|
|
$group = $this->groupManager->createGroup($this->getUniqueID());
|
2015-07-25 23:37:14 +03:00
|
|
|
$group->addUser($users[1]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 15:32:49 +03:00
|
|
|
$result = $this->api->getGroup(array(
|
|
|
|
'groupid' => $group->getGID(),
|
2014-12-22 16:54:50 +03:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertFalse($result->succeeded());
|
2015-04-18 11:30:02 +03:00
|
|
|
$this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode());
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetGroupAsSubadmin() {
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$users = $this->generateUsers(2);
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->userSession->setUser($users[0]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 15:32:49 +03:00
|
|
|
$group = $this->groupManager->createGroup($this->getUniqueID());
|
2015-07-25 23:37:14 +03:00
|
|
|
$group->addUser($users[0]);
|
|
|
|
$group->addUser($users[1]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupManager->getSubAdmin()->createSubAdmin($users[0], $group);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 23:37:14 +03:00
|
|
|
$result = $this->api->getGroup([
|
2015-07-25 15:32:49 +03:00
|
|
|
'groupid' => $group->getGID(),
|
2015-07-25 23:37:14 +03:00
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertTrue($result->succeeded());
|
2015-02-25 18:21:18 +03:00
|
|
|
$this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key');
|
|
|
|
$this->assertArrayHasKey('users', $result->getData());
|
|
|
|
$resultData = $result->getData();
|
|
|
|
$resultData = $resultData['users'];
|
|
|
|
|
2015-07-25 23:37:14 +03:00
|
|
|
$users = array_map(function($user) {
|
|
|
|
return $user->getUID();
|
|
|
|
}, $users);
|
|
|
|
|
2015-02-25 18:21:18 +03:00
|
|
|
sort($users);
|
|
|
|
sort($resultData);
|
|
|
|
$this->assertEquals($users, $resultData);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetGroupAsIrrelevantSubadmin() {
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$users = $this->generateUsers(2);
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->userSession->setUser($users[0]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 15:32:49 +03:00
|
|
|
$group1 = $this->groupManager->createGroup($this->getUniqueID());
|
|
|
|
$group2 = $this->groupManager->createGroup($this->getUniqueID());
|
2015-07-25 23:37:14 +03:00
|
|
|
$group1->addUser($users[1]);
|
|
|
|
$group2->addUser($users[0]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupManager->getSubAdmin()->createSubAdmin($users[0], $group2);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 23:37:14 +03:00
|
|
|
$result = $this->api->getGroup([
|
2015-07-25 15:32:49 +03:00
|
|
|
'groupid' => $group1->getGID(),
|
2015-07-25 23:37:14 +03:00
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertFalse($result->succeeded());
|
2015-04-18 11:30:02 +03:00
|
|
|
$this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode());
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetGroupAsAdmin() {
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$users = $this->generateUsers(2);
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->userSession->setUser($users[0]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 15:32:49 +03:00
|
|
|
$group = $this->groupManager->createGroup($this->getUniqueID());
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 23:37:14 +03:00
|
|
|
$group->addUser($users[1]);
|
|
|
|
$this->groupManager->get('admin')->addUser($users[0]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2015-07-25 23:37:14 +03:00
|
|
|
$result = $this->api->getGroup([
|
2015-07-25 15:32:49 +03:00
|
|
|
'groupid' => $group->getGID(),
|
2015-07-25 23:37:14 +03:00
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertTrue($result->succeeded());
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->assertEquals(['users' => [$users[1]->getUID()]], $result->getData());
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-11 12:47:01 +03:00
|
|
|
public function testGetGroupNonExisting() {
|
|
|
|
$result = $this->api->getGroup([
|
|
|
|
'groupid' => $this->getUniqueId()
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertFalse($result->succeeded());
|
|
|
|
$this->assertEquals(\OCP\API::RESPOND_NOT_FOUND, $result->getStatusCode());
|
|
|
|
$this->assertEquals('The requested group could not be found', $result->getMeta()['message']);
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetSubAdminsOfGroup() {
|
2014-12-22 16:54:50 +03:00
|
|
|
$user1 = $this->generateUsers();
|
|
|
|
$user2 = $this->generateUsers();
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->userSession->setUser($user1);
|
|
|
|
$this->groupManager->get('admin')->addUser($user1);
|
2015-07-25 15:32:49 +03:00
|
|
|
$group1 = $this->groupManager->createGroup($this->getUniqueID());
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupManager->getSubAdmin()->createSubAdmin($user2, $group1);
|
2015-07-25 23:37:14 +03:00
|
|
|
$result = $this->api->getSubAdminsOfGroup([
|
2015-07-25 15:32:49 +03:00
|
|
|
'groupid' => $group1->getGID(),
|
2015-07-25 23:37:14 +03:00
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertTrue($result->succeeded());
|
|
|
|
$data = $result->getData();
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->assertEquals($user2->getUID(), reset($data));
|
2015-07-25 15:32:49 +03:00
|
|
|
$group1->delete();
|
2014-12-22 16:54:50 +03:00
|
|
|
|
|
|
|
$user1 = $this->generateUsers();
|
2015-07-25 23:37:14 +03:00
|
|
|
$this->userSession->setUser($user1);
|
|
|
|
$this->groupManager->get('admin')->addUser($user1);
|
|
|
|
$result = $this->api->getSubAdminsOfGroup([
|
2015-01-07 12:34:42 +03:00
|
|
|
'groupid' => $this->getUniqueID(),
|
2015-07-25 23:37:14 +03:00
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertFalse($result->succeeded());
|
|
|
|
$this->assertEquals(101, $result->getStatusCode());
|
|
|
|
}
|
2015-08-11 12:47:01 +03:00
|
|
|
|
|
|
|
public function testAddGroupEmptyGroup() {
|
|
|
|
$_POST = [];
|
|
|
|
$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() {
|
|
|
|
$group = $this->groupManager->createGroup($this->getUniqueID());
|
|
|
|
|
|
|
|
$_POST = [
|
|
|
|
'groupid' => $group->getGID()
|
|
|
|
];
|
|
|
|
$result = $this->api->addGroup([]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertFalse($result->succeeded());
|
|
|
|
$this->assertEquals(102, $result->getStatusCode());
|
|
|
|
|
|
|
|
$group->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddGroup() {
|
|
|
|
$group = $this->getUniqueId();
|
|
|
|
|
|
|
|
$_POST = [
|
|
|
|
'groupid' => $group
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->api->addGroup([]);
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertTrue($result->succeeded());
|
|
|
|
$this->assertTrue($this->groupManager->groupExists($group));
|
|
|
|
|
|
|
|
$this->groupManager->get($group)->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteGroupNonExisting() {
|
|
|
|
$group = $this->getUniqueId();
|
|
|
|
|
|
|
|
$result = $this->api->deleteGroup([
|
|
|
|
'groupid' => $group
|
|
|
|
]);
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertFalse($result->succeeded());
|
|
|
|
$this->assertEquals(101, $result->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteAdminGroup() {
|
|
|
|
$result = $this->api->deleteGroup([
|
|
|
|
'groupid' => 'admin'
|
|
|
|
]);
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertFalse($result->succeeded());
|
|
|
|
$this->assertEquals(102, $result->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteGroup() {
|
|
|
|
$group = $this->groupManager->createGroup($this->getUniqueId());
|
|
|
|
|
|
|
|
$result = $this->api->deleteGroup([
|
|
|
|
'groupid' => $group->getGID()
|
|
|
|
]);
|
|
|
|
$this->assertInstanceOf('OC_OCS_Result', $result);
|
|
|
|
$this->assertTrue($result->succeeded());
|
|
|
|
$this->assertFalse($this->groupManager->groupExists($group->getGID()));
|
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|