2014-12-22 16:54:50 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
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
|
|
|
* @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
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
namespace OCA\Provisioning_API\Tests\Controller;
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
use OCA\Provisioning_API\Controller\GroupsController;
|
2015-08-11 12:47:01 +03:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
class GroupsControllerTest extends \Test\TestCase {
|
2015-12-08 11:11:50 +03:00
|
|
|
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
|
2015-08-11 12:47:01 +03:00
|
|
|
protected $groupManager;
|
2015-12-08 11:11:50 +03:00
|
|
|
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
2015-08-11 12:47:01 +03:00
|
|
|
protected $userSession;
|
2015-12-08 11:11:50 +03:00
|
|
|
/** @var \OC\SubAdmin|\PHPUnit_Framework_MockObject_MockObject */
|
2015-11-06 16:54:31 +03:00
|
|
|
protected $subAdminManager;
|
2017-07-24 08:44:09 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/** @var GroupsController */
|
2015-10-27 16:09:45 +03:00
|
|
|
protected $api;
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2016-05-11 20:38:49 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2015-07-25 15:32:49 +03:00
|
|
|
|
2016-05-11 20:38:49 +03:00
|
|
|
$this->subAdminManager = $this->getMockBuilder('OC\SubAdmin')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->groupManager = $this->getMockBuilder('OC\Group\Manager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-11-06 16:54:31 +03:00
|
|
|
$this->groupManager
|
|
|
|
->method('getSubAdmin')
|
|
|
|
->willReturn($this->subAdminManager);
|
|
|
|
|
2016-07-12 09:38:56 +03:00
|
|
|
$this->userSession = $this->getMockBuilder('OCP\IUserSession')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-11 10:46:25 +03:00
|
|
|
$request = $this->getMockBuilder('OCP\IRequest')
|
2016-07-12 09:38:56 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-07-24 08:44:09 +03:00
|
|
|
|
|
|
|
$logger = $this->createMock(ILogger::class);
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api = new GroupsController(
|
|
|
|
'provisioning_api',
|
|
|
|
$request,
|
2015-07-25 16:01:31 +03:00
|
|
|
$this->groupManager,
|
2017-07-24 08:44:09 +03:00
|
|
|
$this->userSession,
|
|
|
|
$logger
|
2015-07-25 16:01:31 +03:00
|
|
|
);
|
2015-07-25 15:32:49 +03:00
|
|
|
}
|
|
|
|
|
2015-12-07 23:08:20 +03:00
|
|
|
/**
|
|
|
|
* @param string $gid
|
2015-12-08 11:11:50 +03:00
|
|
|
* @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject
|
2015-12-07 23:08:20 +03:00
|
|
|
*/
|
2015-11-06 16:54:31 +03:00
|
|
|
private function createGroup($gid) {
|
2016-07-12 09:38:56 +03:00
|
|
|
$group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
|
2015-11-06 16:54:31 +03:00
|
|
|
$group
|
|
|
|
->method('getGID')
|
|
|
|
->willReturn($gid);
|
|
|
|
return $group;
|
|
|
|
}
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2015-12-07 23:08:20 +03:00
|
|
|
/**
|
|
|
|
* @param string $uid
|
2015-12-08 11:11:50 +03:00
|
|
|
* @return \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject
|
2015-12-07 23:08:20 +03:00
|
|
|
*/
|
2015-11-06 16:54:31 +03:00
|
|
|
private function createUser($uid) {
|
2016-07-12 09:38:56 +03:00
|
|
|
$user = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
|
2015-11-06 16:54:31 +03:00
|
|
|
$user
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($uid);
|
|
|
|
return $user;
|
|
|
|
}
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2015-11-06 16:54:31 +03:00
|
|
|
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],
|
2015-08-11 12:47:01 +03:00
|
|
|
];
|
2015-11-06 16:54:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetGroups
|
2016-05-11 20:38:49 +03:00
|
|
|
*
|
|
|
|
* @param string|null $search
|
|
|
|
* @param int|null $limit
|
|
|
|
* @param int|null $offset
|
2015-11-06 16:54:31 +03:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$result = $this->api->getGroups($search, $limit, $offset);
|
|
|
|
$this->assertEquals(['groups' => ['group1', 'group2']], $result->getData());
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetGroupAsSubadmin() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$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')
|
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$result = $this->api->getGroup('group');
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\AppFramework\OCS\OCSException
|
|
|
|
* @expectedExceptionCode 997
|
|
|
|
*/
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetGroupAsIrrelevantSubadmin() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$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);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->getGroup('group');
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetGroupAsAdmin() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$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')
|
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$result = $this->api->getGroup('group');
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\AppFramework\OCS\OCSException
|
|
|
|
* @expectedExceptionCode 998
|
|
|
|
* @expectedExceptionMessage The requested group could not be found
|
|
|
|
*/
|
2015-08-11 12:47:01 +03:00
|
|
|
public function testGetGroupNonExisting() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$this->asUser();
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->getGroup($this->getUniqueID());
|
2015-08-11 12:47:01 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\AppFramework\OCS\OCSException
|
|
|
|
* @expectedExceptionCode 101
|
|
|
|
* @expectedExceptionMessage Group does not exist
|
|
|
|
*/
|
2015-11-06 16:54:31 +03:00
|
|
|
public function testGetSubAdminsOfGroupsNotExists() {
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->getSubAdminsOfGroup('NonExistingGroup');
|
2015-11-06 16:54:31 +03:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:34:42 +03:00
|
|
|
public function testGetSubAdminsOfGroup() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$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'),
|
|
|
|
]);
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$result = $this->api->getSubAdminsOfGroup('GroupWithSubAdmins');
|
2015-11-06 16:54:31 +03:00
|
|
|
$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([
|
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$result = $this->api->getSubAdminsOfGroup('GroupWithOutSubAdmins');
|
2015-11-17 00:24:53 +03:00
|
|
|
$this->assertEquals([], $result->getData());
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\AppFramework\OCS\OCSException
|
|
|
|
* @expectedExceptionCode 101
|
|
|
|
* @expectedExceptionMessage Invalid group name
|
|
|
|
*/
|
2015-08-11 12:47:01 +03:00
|
|
|
public function testAddGroupEmptyGroup() {
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->addGroup('');
|
2015-08-11 12:47:01 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\AppFramework\OCS\OCSException
|
|
|
|
* @expectedExceptionCode 102
|
|
|
|
*/
|
2015-08-11 12:47:01 +03:00
|
|
|
public function testAddGroupExistingGroup() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$this->groupManager
|
|
|
|
->method('groupExists')
|
|
|
|
->with('ExistingGroup')
|
|
|
|
->willReturn(true);
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->addGroup('ExistingGroup');
|
2015-08-11 12:47:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddGroup() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$this->groupManager
|
|
|
|
->method('groupExists')
|
|
|
|
->with('NewGroup')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('createGroup')
|
|
|
|
->with('NewGroup');
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->addGroup('NewGroup');
|
2015-08-11 12:47:01 +03:00
|
|
|
}
|
|
|
|
|
2015-12-04 16:21:24 +03:00
|
|
|
public function testAddGroupWithSpecialChar() {
|
|
|
|
$this->groupManager
|
|
|
|
->method('groupExists')
|
2015-12-23 10:31:20 +03:00
|
|
|
->with('Iñtërnâtiônàlizætiøn')
|
2015-12-04 16:21:24 +03:00
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('createGroup')
|
|
|
|
->with('Iñtërnâtiônàlizætiøn');
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->addGroup('Iñtërnâtiônàlizætiøn');
|
2015-12-04 16:21:24 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\AppFramework\OCS\OCSException
|
|
|
|
* @expectedExceptionCode 101
|
|
|
|
*/
|
2015-08-11 12:47:01 +03:00
|
|
|
public function testDeleteGroupNonExisting() {
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->deleteGroup('NonExistingGroup');
|
2015-08-11 12:47:01 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\AppFramework\OCS\OCSException
|
|
|
|
* @expectedExceptionCode 102
|
|
|
|
*/
|
2015-08-11 12:47:01 +03:00
|
|
|
public function testDeleteAdminGroup() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$this->groupManager
|
|
|
|
->method('groupExists')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn('true');
|
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->deleteGroup('admin');
|
2015-08-11 12:47:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteGroup() {
|
2015-11-06 16:54:31 +03:00
|
|
|
$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);
|
2015-08-11 12:47:01 +03:00
|
|
|
|
2016-08-11 10:46:25 +03:00
|
|
|
$this->api->deleteGroup('ExistingGroup');
|
2015-08-11 12:47:01 +03:00
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|