GroupPrincipalTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\DAV;
  8. use OC\Group\Group;
  9. use OCA\DAV\DAV\GroupPrincipalBackend;
  10. use OCP\IConfig;
  11. use OCP\IGroup;
  12. use OCP\IGroupManager;
  13. use OCP\IUser;
  14. use OCP\IUserSession;
  15. use OCP\Share\IManager;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Sabre\DAV\PropPatch;
  18. class GroupPrincipalTest extends \Test\TestCase {
  19. /** @var IConfig|MockObject */
  20. private $config;
  21. /** @var IGroupManager | MockObject */
  22. private $groupManager;
  23. /** @var IUserSession | MockObject */
  24. private $userSession;
  25. /** @var IManager | MockObject */
  26. private $shareManager;
  27. /** @var GroupPrincipalBackend */
  28. private $connector;
  29. protected function setUp(): void {
  30. $this->groupManager = $this->createMock(IGroupManager::class);
  31. $this->userSession = $this->createMock(IUserSession::class);
  32. $this->shareManager = $this->createMock(IManager::class);
  33. $this->config = $this->createMock(IConfig::class);
  34. $this->connector = new GroupPrincipalBackend(
  35. $this->groupManager,
  36. $this->userSession,
  37. $this->shareManager,
  38. $this->config
  39. );
  40. parent::setUp();
  41. }
  42. public function testGetPrincipalsByPrefixWithoutPrefix(): void {
  43. $response = $this->connector->getPrincipalsByPrefix('');
  44. $this->assertSame([], $response);
  45. }
  46. public function testGetPrincipalsByPrefixWithUsers(): void {
  47. $group1 = $this->mockGroup('foo');
  48. $group2 = $this->mockGroup('bar');
  49. $this->groupManager
  50. ->expects($this->once())
  51. ->method('search')
  52. ->with('')
  53. ->willReturn([$group1, $group2]);
  54. $expectedResponse = [
  55. 0 => [
  56. 'uri' => 'principals/groups/foo',
  57. '{DAV:}displayname' => 'Group foo',
  58. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  59. ],
  60. 1 => [
  61. 'uri' => 'principals/groups/bar',
  62. '{DAV:}displayname' => 'Group bar',
  63. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  64. ]
  65. ];
  66. $response = $this->connector->getPrincipalsByPrefix('principals/groups');
  67. $this->assertSame($expectedResponse, $response);
  68. }
  69. public function testGetPrincipalsByPrefixEmpty(): void {
  70. $this->groupManager
  71. ->expects($this->once())
  72. ->method('search')
  73. ->with('')
  74. ->willReturn([]);
  75. $response = $this->connector->getPrincipalsByPrefix('principals/groups');
  76. $this->assertSame([], $response);
  77. }
  78. public function testGetPrincipalsByPathWithoutMail(): void {
  79. $group1 = $this->mockGroup('foo');
  80. $this->groupManager
  81. ->expects($this->once())
  82. ->method('get')
  83. ->with('foo')
  84. ->willReturn($group1);
  85. $expectedResponse = [
  86. 'uri' => 'principals/groups/foo',
  87. '{DAV:}displayname' => 'Group foo',
  88. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  89. ];
  90. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  91. $this->assertSame($expectedResponse, $response);
  92. }
  93. public function testGetPrincipalsByPathWithMail(): void {
  94. $fooUser = $this->mockGroup('foo');
  95. $this->groupManager
  96. ->expects($this->once())
  97. ->method('get')
  98. ->with('foo')
  99. ->willReturn($fooUser);
  100. $expectedResponse = [
  101. 'uri' => 'principals/groups/foo',
  102. '{DAV:}displayname' => 'Group foo',
  103. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  104. ];
  105. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  106. $this->assertSame($expectedResponse, $response);
  107. }
  108. public function testGetPrincipalsByPathEmpty(): void {
  109. $this->groupManager
  110. ->expects($this->once())
  111. ->method('get')
  112. ->with('foo')
  113. ->willReturn(null);
  114. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  115. $this->assertSame(null, $response);
  116. }
  117. public function testGetPrincipalsByPathGroupWithSlash(): void {
  118. $group1 = $this->mockGroup('foo/bar');
  119. $this->groupManager
  120. ->expects($this->once())
  121. ->method('get')
  122. ->with('foo/bar')
  123. ->willReturn($group1);
  124. $expectedResponse = [
  125. 'uri' => 'principals/groups/foo%2Fbar',
  126. '{DAV:}displayname' => 'Group foo/bar',
  127. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  128. ];
  129. $response = $this->connector->getPrincipalByPath('principals/groups/foo/bar');
  130. $this->assertSame($expectedResponse, $response);
  131. }
  132. public function testGetPrincipalsByPathGroupWithHash(): void {
  133. $group1 = $this->mockGroup('foo#bar');
  134. $this->groupManager
  135. ->expects($this->once())
  136. ->method('get')
  137. ->with('foo#bar')
  138. ->willReturn($group1);
  139. $expectedResponse = [
  140. 'uri' => 'principals/groups/foo%23bar',
  141. '{DAV:}displayname' => 'Group foo#bar',
  142. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  143. ];
  144. $response = $this->connector->getPrincipalByPath('principals/groups/foo#bar');
  145. $this->assertSame($expectedResponse, $response);
  146. }
  147. public function testGetGroupMemberSet(): void {
  148. $response = $this->connector->getGroupMemberSet('principals/groups/foo');
  149. $this->assertSame([], $response);
  150. }
  151. public function testGetGroupMembership(): void {
  152. $response = $this->connector->getGroupMembership('principals/groups/foo');
  153. $this->assertSame([], $response);
  154. }
  155. public function testSetGroupMembership(): void {
  156. $this->expectException(\Sabre\DAV\Exception::class);
  157. $this->expectExceptionMessage('Setting members of the group is not supported yet');
  158. $this->connector->setGroupMemberSet('principals/groups/foo', ['foo']);
  159. }
  160. public function testUpdatePrincipal(): void {
  161. $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch([])));
  162. }
  163. public function testSearchPrincipalsWithEmptySearchProperties(): void {
  164. $this->assertSame([], $this->connector->searchPrincipals('principals/groups', []));
  165. }
  166. public function testSearchPrincipalsWithWrongPrefixPath(): void {
  167. $this->assertSame([], $this->connector->searchPrincipals('principals/users',
  168. ['{DAV:}displayname' => 'Foo']));
  169. }
  170. /**
  171. * @dataProvider searchPrincipalsDataProvider
  172. * @param bool $sharingEnabled
  173. * @param bool $groupSharingEnabled
  174. * @param bool $groupsOnly
  175. * @param string $test
  176. * @param array $result
  177. */
  178. public function testSearchPrincipals(bool $sharingEnabled, bool $groupSharingEnabled, bool $groupsOnly, string $test, array $result): void {
  179. $this->shareManager->expects($this->once())
  180. ->method('shareAPIEnabled')
  181. ->willReturn($sharingEnabled);
  182. $this->shareManager->expects($sharingEnabled ? $this->once() : $this->never())
  183. ->method('allowGroupSharing')
  184. ->willReturn($groupSharingEnabled);
  185. if ($sharingEnabled && $groupSharingEnabled) {
  186. $this->shareManager->expects($this->once())
  187. ->method('shareWithGroupMembersOnly')
  188. ->willReturn($groupsOnly);
  189. if ($groupsOnly) {
  190. $user = $this->createMock(IUser::class);
  191. $this->userSession->expects($this->once())
  192. ->method('getUser')
  193. ->willReturn($user);
  194. $this->groupManager->expects($this->once())
  195. ->method('getUserGroupIds')
  196. ->with($user)
  197. ->willReturn(['group1', 'group2', 'group5']);
  198. }
  199. } else {
  200. $this->shareManager->expects($this->never())
  201. ->method('shareWithGroupMembersOnly');
  202. $this->groupManager->expects($this->never())
  203. ->method($this->anything());
  204. }
  205. $group1 = $this->createMock(IGroup::class);
  206. $group1->method('getGID')->willReturn('group1');
  207. $group2 = $this->createMock(IGroup::class);
  208. $group2->method('getGID')->willReturn('group2');
  209. $group3 = $this->createMock(IGroup::class);
  210. $group3->method('getGID')->willReturn('group3');
  211. $group4 = $this->createMock(IGroup::class);
  212. $group4->method('getGID')->willReturn('group4');
  213. $group5 = $this->createMock(IGroup::class);
  214. $group5->method('getGID')->willReturn('group5');
  215. if ($sharingEnabled && $groupSharingEnabled) {
  216. $this->groupManager->expects($this->once())
  217. ->method('search')
  218. ->with('Foo')
  219. ->willReturn([$group1, $group2, $group3, $group4, $group5]);
  220. } else {
  221. $this->groupManager->expects($this->never())
  222. ->method('search');
  223. }
  224. $this->assertSame($result, $this->connector->searchPrincipals('principals/groups',
  225. ['{DAV:}displayname' => 'Foo'], $test));
  226. }
  227. public function searchPrincipalsDataProvider() {
  228. return [
  229. [true, true, false, 'allof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']],
  230. [true, true, false, 'anyof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']],
  231. [true, true, true, 'allof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group5']],
  232. [true, true, true, 'anyof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group5']],
  233. [true, false, false, 'allof', []],
  234. [false, true, false, 'anyof', []],
  235. [false, false, false, 'allof', []],
  236. [false, false, false, 'anyof', []],
  237. ];
  238. }
  239. /**
  240. * @dataProvider findByUriDataProvider
  241. * @param bool $sharingEnabled
  242. * @param bool $groupSharingEnabled
  243. * @param bool $groupsOnly
  244. * @param string $findUri
  245. * @param string|null $result
  246. */
  247. public function testFindByUri(bool $sharingEnabled, bool $groupSharingEnabled, bool $groupsOnly, string $findUri, ?string $result): void {
  248. $this->shareManager->expects($this->once())
  249. ->method('shareAPIEnabled')
  250. ->willReturn($sharingEnabled);
  251. $this->shareManager->expects($sharingEnabled ? $this->once() : $this->never())
  252. ->method('allowGroupSharing')
  253. ->willReturn($groupSharingEnabled);
  254. if ($sharingEnabled && $groupSharingEnabled) {
  255. $this->shareManager->expects($this->once())
  256. ->method('shareWithGroupMembersOnly')
  257. ->willReturn($groupsOnly);
  258. if ($groupsOnly) {
  259. $user = $this->createMock(IUser::class);
  260. $this->userSession->expects($this->once())
  261. ->method('getUser')
  262. ->willReturn($user);
  263. $this->groupManager->expects($this->once())
  264. ->method('getUserGroupIds')
  265. ->with($user)
  266. ->willReturn(['group1', 'group2', 'group5']);
  267. }
  268. } else {
  269. $this->shareManager->expects($this->never())
  270. ->method('shareWithGroupMembersOnly');
  271. $this->groupManager->expects($this->never())
  272. ->method($this->anything());
  273. }
  274. $this->assertEquals($result, $this->connector->findByUri($findUri, 'principals/groups'));
  275. }
  276. public function findByUriDataProvider() {
  277. return [
  278. [false, false, false, 'principal:principals/groups/group1', null],
  279. [false, false, false, 'principal:principals/groups/group3', null],
  280. [false, true, false, 'principal:principals/groups/group1', null],
  281. [false, true, false, 'principal:principals/groups/group3', null],
  282. [false, false, true, 'principal:principals/groups/group1', null],
  283. [false, false, true, 'principal:principals/groups/group3', null],
  284. [true, false, true, 'principal:principals/groups/group1', null],
  285. [true, false, true, 'principal:principals/groups/group3', null],
  286. [true, true, true, 'principal:principals/groups/group1', 'principals/groups/group1'],
  287. [true, true, true, 'principal:principals/groups/group3', null],
  288. [true, true, false, 'principal:principals/groups/group1', 'principals/groups/group1'],
  289. [true, true, false, 'principal:principals/groups/group3', 'principals/groups/group3'],
  290. ];
  291. }
  292. /**
  293. * @return Group|MockObject
  294. */
  295. private function mockGroup($gid) {
  296. $fooGroup = $this->createMock(Group::class);
  297. $fooGroup
  298. ->expects($this->exactly(1))
  299. ->method('getGID')
  300. ->willReturn($gid);
  301. $fooGroup
  302. ->expects($this->exactly(1))
  303. ->method('getDisplayName')
  304. ->willReturn('Group '.$gid);
  305. return $fooGroup;
  306. }
  307. }