2016-01-08 14:11:02 +03:00
|
|
|
<?php
|
2016-01-12 17:02:16 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2018-10-17 17:06:58 +03:00
|
|
|
* @copyright Copyright (c) 2018, Georg Ehrke
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-01-12 17:02:16 +03:00
|
|
|
*
|
|
|
|
*/
|
2016-05-25 17:04:15 +03:00
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\unit\DAV;
|
2016-01-08 14:11:02 +03:00
|
|
|
|
2017-01-09 23:20:55 +03:00
|
|
|
use OC\Group\Group;
|
2016-01-08 14:11:02 +03:00
|
|
|
use OCA\DAV\DAV\GroupPrincipalBackend;
|
2018-10-17 17:06:58 +03:00
|
|
|
use OCP\IGroup;
|
2016-01-08 14:11:02 +03:00
|
|
|
use OCP\IGroupManager;
|
2018-10-17 17:06:58 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\Share\IManager;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Sabre\DAV\PropPatch;
|
2016-01-08 14:11:02 +03:00
|
|
|
|
|
|
|
class GroupPrincipalTest extends \Test\TestCase {
|
|
|
|
|
2018-10-17 17:06:58 +03:00
|
|
|
/** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */
|
2016-01-08 14:11:02 +03:00
|
|
|
private $groupManager;
|
|
|
|
|
2018-10-17 17:06:58 +03:00
|
|
|
/** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
/** @var IManager | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $shareManager;
|
|
|
|
|
2016-01-08 14:11:02 +03:00
|
|
|
/** @var GroupPrincipalBackend */
|
|
|
|
private $connector;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-01-09 23:20:55 +03:00
|
|
|
$this->groupManager = $this->createMock(IGroupManager::class);
|
2018-10-17 17:06:58 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->shareManager = $this->createMock(IManager::class);
|
2016-01-08 14:11:02 +03:00
|
|
|
|
2018-10-17 17:06:58 +03:00
|
|
|
$this->connector = new GroupPrincipalBackend(
|
|
|
|
$this->groupManager,
|
|
|
|
$this->userSession,
|
2018-11-10 11:51:09 +03:00
|
|
|
$this->shareManager);
|
2016-01-08 14:11:02 +03:00
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrincipalsByPrefixWithoutPrefix() {
|
|
|
|
$response = $this->connector->getPrincipalsByPrefix('');
|
|
|
|
$this->assertSame([], $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrincipalsByPrefixWithUsers() {
|
|
|
|
$group1 = $this->mockGroup('foo');
|
|
|
|
$group2 = $this->mockGroup('bar');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('search')
|
|
|
|
->with('')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([$group1, $group2]);
|
2016-01-08 14:11:02 +03:00
|
|
|
|
|
|
|
$expectedResponse = [
|
|
|
|
0 => [
|
|
|
|
'uri' => 'principals/groups/foo',
|
2018-11-10 11:51:09 +03:00
|
|
|
'{DAV:}displayname' => 'Group foo',
|
2018-10-17 17:06:58 +03:00
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
|
2016-01-08 14:11:02 +03:00
|
|
|
],
|
|
|
|
1 => [
|
|
|
|
'uri' => 'principals/groups/bar',
|
2018-11-10 11:51:09 +03:00
|
|
|
'{DAV:}displayname' => 'Group bar',
|
2018-10-17 17:06:58 +03:00
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
|
2016-01-08 14:11:02 +03:00
|
|
|
]
|
|
|
|
];
|
|
|
|
$response = $this->connector->getPrincipalsByPrefix('principals/groups');
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrincipalsByPrefixEmpty() {
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('search')
|
|
|
|
->with('')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([]);
|
2016-01-08 14:11:02 +03:00
|
|
|
|
|
|
|
$response = $this->connector->getPrincipalsByPrefix('principals/groups');
|
|
|
|
$this->assertSame([], $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrincipalsByPathWithoutMail() {
|
|
|
|
$group1 = $this->mockGroup('foo');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('foo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($group1);
|
2016-01-08 14:11:02 +03:00
|
|
|
|
|
|
|
$expectedResponse = [
|
|
|
|
'uri' => 'principals/groups/foo',
|
2018-11-10 11:51:09 +03:00
|
|
|
'{DAV:}displayname' => 'Group foo',
|
2018-10-17 17:06:58 +03:00
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
|
2016-01-08 14:11:02 +03:00
|
|
|
];
|
|
|
|
$response = $this->connector->getPrincipalByPath('principals/groups/foo');
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrincipalsByPathWithMail() {
|
|
|
|
$fooUser = $this->mockGroup('foo');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('foo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($fooUser);
|
2016-01-08 14:11:02 +03:00
|
|
|
|
|
|
|
$expectedResponse = [
|
|
|
|
'uri' => 'principals/groups/foo',
|
2018-11-10 11:51:09 +03:00
|
|
|
'{DAV:}displayname' => 'Group foo',
|
2018-10-17 17:06:58 +03:00
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
|
2016-01-08 14:11:02 +03:00
|
|
|
];
|
|
|
|
$response = $this->connector->getPrincipalByPath('principals/groups/foo');
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrincipalsByPathEmpty() {
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('foo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(null);
|
2016-01-08 14:11:02 +03:00
|
|
|
|
|
|
|
$response = $this->connector->getPrincipalByPath('principals/groups/foo');
|
|
|
|
$this->assertSame(null, $response);
|
|
|
|
}
|
|
|
|
|
2017-01-09 23:20:55 +03:00
|
|
|
public function testGetPrincipalsByPathGroupWithSlash() {
|
|
|
|
$group1 = $this->mockGroup('foo/bar');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('foo/bar')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($group1);
|
2017-01-09 23:20:55 +03:00
|
|
|
|
|
|
|
$expectedResponse = [
|
|
|
|
'uri' => 'principals/groups/foo%2Fbar',
|
2018-11-10 11:51:09 +03:00
|
|
|
'{DAV:}displayname' => 'Group foo/bar',
|
2018-10-17 17:06:58 +03:00
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
|
2017-01-09 23:20:55 +03:00
|
|
|
];
|
|
|
|
$response = $this->connector->getPrincipalByPath('principals/groups/foo/bar');
|
|
|
|
$this->assertSame($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2016-01-08 14:11:02 +03:00
|
|
|
public function testGetGroupMemberSet() {
|
|
|
|
$response = $this->connector->getGroupMemberSet('principals/groups/foo');
|
|
|
|
$this->assertSame([], $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetGroupMembership() {
|
|
|
|
$response = $this->connector->getGroupMembership('principals/groups/foo');
|
|
|
|
$this->assertSame([], $response);
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2016-01-08 14:11:02 +03:00
|
|
|
public function testSetGroupMembership() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception::class);
|
|
|
|
$this->expectExceptionMessage('Setting members of the group is not supported yet');
|
|
|
|
|
2016-01-08 14:11:02 +03:00
|
|
|
$this->connector->setGroupMemberSet('principals/groups/foo', ['foo']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatePrincipal() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch([])));
|
2016-01-08 14:11:02 +03:00
|
|
|
}
|
|
|
|
|
2018-10-17 17:06:58 +03:00
|
|
|
public function testSearchPrincipalsWithEmptySearchProperties() {
|
2016-01-08 14:11:02 +03:00
|
|
|
$this->assertSame([], $this->connector->searchPrincipals('principals/groups', []));
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:06:58 +03:00
|
|
|
public function testSearchPrincipalsWithWrongPrefixPath() {
|
|
|
|
$this->assertSame([], $this->connector->searchPrincipals('principals/users',
|
|
|
|
['{DAV:}displayname' => 'Foo']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider searchPrincipalsDataProvider
|
|
|
|
*/
|
|
|
|
public function testSearchPrincipals($sharingEnabled, $groupsOnly, $test, $result) {
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('shareAPIEnabled')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($sharingEnabled);
|
2018-10-17 17:06:58 +03:00
|
|
|
|
|
|
|
if ($sharingEnabled) {
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('shareWithGroupMembersOnly')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($groupsOnly);
|
2018-10-17 17:06:58 +03:00
|
|
|
|
|
|
|
if ($groupsOnly) {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2018-10-17 17:06:58 +03:00
|
|
|
|
|
|
|
$this->groupManager->expects($this->once())
|
|
|
|
->method('getUserGroupIds')
|
|
|
|
->with($user)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['group1', 'group2', 'group5']);
|
2018-10-17 17:06:58 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->shareManager->expects($this->never())
|
|
|
|
->method('shareWithGroupMembersOnly');
|
|
|
|
$this->groupManager->expects($this->never())
|
|
|
|
->method($this->anything());
|
|
|
|
}
|
|
|
|
|
|
|
|
$group1 = $this->createMock(IGroup::class);
|
2020-03-26 00:21:27 +03:00
|
|
|
$group1->method('getGID')->willReturn('group1');
|
2018-10-17 17:06:58 +03:00
|
|
|
$group2 = $this->createMock(IGroup::class);
|
2020-03-26 00:21:27 +03:00
|
|
|
$group2->method('getGID')->willReturn('group2');
|
2018-10-17 17:06:58 +03:00
|
|
|
$group3 = $this->createMock(IGroup::class);
|
2020-03-26 00:21:27 +03:00
|
|
|
$group3->method('getGID')->willReturn('group3');
|
2018-10-17 17:06:58 +03:00
|
|
|
$group4 = $this->createMock(IGroup::class);
|
2020-03-26 00:21:27 +03:00
|
|
|
$group4->method('getGID')->willReturn('group4');
|
2018-10-17 17:06:58 +03:00
|
|
|
$group5 = $this->createMock(IGroup::class);
|
2020-03-26 00:21:27 +03:00
|
|
|
$group5->method('getGID')->willReturn('group5');
|
2018-10-17 17:06:58 +03:00
|
|
|
|
|
|
|
if ($sharingEnabled) {
|
|
|
|
$this->groupManager->expects($this->once())
|
|
|
|
->method('search')
|
|
|
|
->with('Foo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([$group1, $group2, $group3, $group4, $group5]);
|
2018-10-17 17:06:58 +03:00
|
|
|
} else {
|
|
|
|
$this->groupManager->expects($this->never())
|
|
|
|
->method('search');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame($result, $this->connector->searchPrincipals('principals/groups',
|
|
|
|
['{DAV:}displayname' => 'Foo'], $test));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function searchPrincipalsDataProvider() {
|
|
|
|
return [
|
|
|
|
[true, false, 'allof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']],
|
|
|
|
[true, false, 'anyof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']],
|
|
|
|
[true, true, 'allof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group5']],
|
|
|
|
[true, true, 'anyof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group5']],
|
|
|
|
[false, false, 'allof', []],
|
|
|
|
[false, false, 'anyof', []],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider findByUriDataProvider
|
|
|
|
*/
|
|
|
|
public function testFindByUri($sharingEnabled, $groupsOnly, $findUri, $result) {
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('shareAPIEnabled')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($sharingEnabled);
|
2018-10-17 17:06:58 +03:00
|
|
|
|
|
|
|
if ($sharingEnabled) {
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
->method('shareWithGroupMembersOnly')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($groupsOnly);
|
2018-10-17 17:06:58 +03:00
|
|
|
|
|
|
|
if ($groupsOnly) {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2018-10-17 17:06:58 +03:00
|
|
|
|
|
|
|
$this->groupManager->expects($this->at(0))
|
|
|
|
->method('getUserGroupIds')
|
|
|
|
->with($user)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['group1', 'group2', 'group5']);
|
2018-10-17 17:06:58 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->shareManager->expects($this->never())
|
|
|
|
->method('shareWithGroupMembersOnly');
|
|
|
|
$this->groupManager->expects($this->never())
|
|
|
|
->method($this->anything());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($result, $this->connector->findByUri($findUri, 'principals/groups'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findByUriDataProvider() {
|
|
|
|
return [
|
|
|
|
[false, false, 'principal:principals/groups/group1', null],
|
|
|
|
[false, false, 'principal:principals/groups/group3', null],
|
|
|
|
[false, true, 'principal:principals/groups/group1', null],
|
|
|
|
[false, true, 'principal:principals/groups/group3', null],
|
|
|
|
[true, true, 'principal:principals/groups/group1', 'principals/groups/group1'],
|
|
|
|
[true, true, 'principal:principals/groups/group3', null],
|
|
|
|
[true, false, 'principal:principals/groups/group1', 'principals/groups/group1'],
|
|
|
|
[true, false, 'principal:principals/groups/group3', 'principals/groups/group3'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-08 14:11:02 +03:00
|
|
|
/**
|
2017-01-09 23:20:55 +03:00
|
|
|
* @return Group|\PHPUnit_Framework_MockObject_MockObject
|
2016-01-08 14:11:02 +03:00
|
|
|
*/
|
|
|
|
private function mockGroup($gid) {
|
2017-01-09 23:20:55 +03:00
|
|
|
$fooGroup = $this->createMock(Group::class);
|
|
|
|
$fooGroup
|
2016-01-08 14:11:02 +03:00
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('getGID')
|
2018-11-10 11:51:09 +03:00
|
|
|
->willReturn($gid);
|
|
|
|
$fooGroup
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('getDisplayName')
|
|
|
|
->willReturn('Group '.$gid);
|
2017-01-09 23:20:55 +03:00
|
|
|
return $fooGroup;
|
2016-01-08 14:11:02 +03:00
|
|
|
}
|
|
|
|
}
|