GroupPrincipalTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2018, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\Tests\unit\DAV;
  30. use OC\Group\Group;
  31. use OCA\DAV\DAV\GroupPrincipalBackend;
  32. use OCP\IGroup;
  33. use OCP\IGroupManager;
  34. use OCP\IUser;
  35. use OCP\IUserSession;
  36. use OCP\Share\IManager;
  37. use Sabre\DAV\PropPatch;
  38. class GroupPrincipalTest extends \Test\TestCase {
  39. /** @var IGroupManager | \PHPUnit\Framework\MockObject\MockObject */
  40. private $groupManager;
  41. /** @var IUserSession | \PHPUnit\Framework\MockObject\MockObject */
  42. private $userSession;
  43. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  44. private $shareManager;
  45. /** @var GroupPrincipalBackend */
  46. private $connector;
  47. protected function setUp(): void {
  48. $this->groupManager = $this->createMock(IGroupManager::class);
  49. $this->userSession = $this->createMock(IUserSession::class);
  50. $this->shareManager = $this->createMock(IManager::class);
  51. $this->connector = new GroupPrincipalBackend(
  52. $this->groupManager,
  53. $this->userSession,
  54. $this->shareManager);
  55. parent::setUp();
  56. }
  57. public function testGetPrincipalsByPrefixWithoutPrefix() {
  58. $response = $this->connector->getPrincipalsByPrefix('');
  59. $this->assertSame([], $response);
  60. }
  61. public function testGetPrincipalsByPrefixWithUsers() {
  62. $group1 = $this->mockGroup('foo');
  63. $group2 = $this->mockGroup('bar');
  64. $this->groupManager
  65. ->expects($this->once())
  66. ->method('search')
  67. ->with('')
  68. ->willReturn([$group1, $group2]);
  69. $expectedResponse = [
  70. 0 => [
  71. 'uri' => 'principals/groups/foo',
  72. '{DAV:}displayname' => 'Group foo',
  73. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  74. ],
  75. 1 => [
  76. 'uri' => 'principals/groups/bar',
  77. '{DAV:}displayname' => 'Group bar',
  78. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  79. ]
  80. ];
  81. $response = $this->connector->getPrincipalsByPrefix('principals/groups');
  82. $this->assertSame($expectedResponse, $response);
  83. }
  84. public function testGetPrincipalsByPrefixEmpty() {
  85. $this->groupManager
  86. ->expects($this->once())
  87. ->method('search')
  88. ->with('')
  89. ->willReturn([]);
  90. $response = $this->connector->getPrincipalsByPrefix('principals/groups');
  91. $this->assertSame([], $response);
  92. }
  93. public function testGetPrincipalsByPathWithoutMail() {
  94. $group1 = $this->mockGroup('foo');
  95. $this->groupManager
  96. ->expects($this->once())
  97. ->method('get')
  98. ->with('foo')
  99. ->willReturn($group1);
  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 testGetPrincipalsByPathWithMail() {
  109. $fooUser = $this->mockGroup('foo');
  110. $this->groupManager
  111. ->expects($this->once())
  112. ->method('get')
  113. ->with('foo')
  114. ->willReturn($fooUser);
  115. $expectedResponse = [
  116. 'uri' => 'principals/groups/foo',
  117. '{DAV:}displayname' => 'Group foo',
  118. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  119. ];
  120. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  121. $this->assertSame($expectedResponse, $response);
  122. }
  123. public function testGetPrincipalsByPathEmpty() {
  124. $this->groupManager
  125. ->expects($this->once())
  126. ->method('get')
  127. ->with('foo')
  128. ->willReturn(null);
  129. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  130. $this->assertSame(null, $response);
  131. }
  132. public function testGetPrincipalsByPathGroupWithSlash() {
  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%2Fbar',
  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() {
  148. $response = $this->connector->getGroupMemberSet('principals/groups/foo');
  149. $this->assertSame([], $response);
  150. }
  151. public function testGetGroupMembership() {
  152. $response = $this->connector->getGroupMembership('principals/groups/foo');
  153. $this->assertSame([], $response);
  154. }
  155. public function testSetGroupMembership() {
  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() {
  161. $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch([])));
  162. }
  163. public function testSearchPrincipalsWithEmptySearchProperties() {
  164. $this->assertSame([], $this->connector->searchPrincipals('principals/groups', []));
  165. }
  166. public function testSearchPrincipalsWithWrongPrefixPath() {
  167. $this->assertSame([], $this->connector->searchPrincipals('principals/users',
  168. ['{DAV:}displayname' => 'Foo']));
  169. }
  170. /**
  171. * @dataProvider searchPrincipalsDataProvider
  172. */
  173. public function testSearchPrincipals($sharingEnabled, $groupsOnly, $test, $result) {
  174. $this->shareManager->expects($this->once())
  175. ->method('shareAPIEnabled')
  176. ->willReturn($sharingEnabled);
  177. if ($sharingEnabled) {
  178. $this->shareManager->expects($this->once())
  179. ->method('shareWithGroupMembersOnly')
  180. ->willReturn($groupsOnly);
  181. if ($groupsOnly) {
  182. $user = $this->createMock(IUser::class);
  183. $this->userSession->expects($this->once())
  184. ->method('getUser')
  185. ->willReturn($user);
  186. $this->groupManager->expects($this->once())
  187. ->method('getUserGroupIds')
  188. ->with($user)
  189. ->willReturn(['group1', 'group2', 'group5']);
  190. }
  191. } else {
  192. $this->shareManager->expects($this->never())
  193. ->method('shareWithGroupMembersOnly');
  194. $this->groupManager->expects($this->never())
  195. ->method($this->anything());
  196. }
  197. $group1 = $this->createMock(IGroup::class);
  198. $group1->method('getGID')->willReturn('group1');
  199. $group2 = $this->createMock(IGroup::class);
  200. $group2->method('getGID')->willReturn('group2');
  201. $group3 = $this->createMock(IGroup::class);
  202. $group3->method('getGID')->willReturn('group3');
  203. $group4 = $this->createMock(IGroup::class);
  204. $group4->method('getGID')->willReturn('group4');
  205. $group5 = $this->createMock(IGroup::class);
  206. $group5->method('getGID')->willReturn('group5');
  207. if ($sharingEnabled) {
  208. $this->groupManager->expects($this->once())
  209. ->method('search')
  210. ->with('Foo')
  211. ->willReturn([$group1, $group2, $group3, $group4, $group5]);
  212. } else {
  213. $this->groupManager->expects($this->never())
  214. ->method('search');
  215. }
  216. $this->assertSame($result, $this->connector->searchPrincipals('principals/groups',
  217. ['{DAV:}displayname' => 'Foo'], $test));
  218. }
  219. public function searchPrincipalsDataProvider() {
  220. return [
  221. [true, false, 'allof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']],
  222. [true, false, 'anyof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']],
  223. [true, true, 'allof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group5']],
  224. [true, true, 'anyof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group5']],
  225. [false, false, 'allof', []],
  226. [false, false, 'anyof', []],
  227. ];
  228. }
  229. /**
  230. * @dataProvider findByUriDataProvider
  231. */
  232. public function testFindByUri($sharingEnabled, $groupsOnly, $findUri, $result) {
  233. $this->shareManager->expects($this->once())
  234. ->method('shareAPIEnabled')
  235. ->willReturn($sharingEnabled);
  236. if ($sharingEnabled) {
  237. $this->shareManager->expects($this->once())
  238. ->method('shareWithGroupMembersOnly')
  239. ->willReturn($groupsOnly);
  240. if ($groupsOnly) {
  241. $user = $this->createMock(IUser::class);
  242. $this->userSession->expects($this->once())
  243. ->method('getUser')
  244. ->willReturn($user);
  245. $this->groupManager->expects($this->at(0))
  246. ->method('getUserGroupIds')
  247. ->with($user)
  248. ->willReturn(['group1', 'group2', 'group5']);
  249. }
  250. } else {
  251. $this->shareManager->expects($this->never())
  252. ->method('shareWithGroupMembersOnly');
  253. $this->groupManager->expects($this->never())
  254. ->method($this->anything());
  255. }
  256. $this->assertEquals($result, $this->connector->findByUri($findUri, 'principals/groups'));
  257. }
  258. public function findByUriDataProvider() {
  259. return [
  260. [false, false, 'principal:principals/groups/group1', null],
  261. [false, false, 'principal:principals/groups/group3', null],
  262. [false, true, 'principal:principals/groups/group1', null],
  263. [false, true, 'principal:principals/groups/group3', null],
  264. [true, true, 'principal:principals/groups/group1', 'principals/groups/group1'],
  265. [true, true, 'principal:principals/groups/group3', null],
  266. [true, false, 'principal:principals/groups/group1', 'principals/groups/group1'],
  267. [true, false, 'principal:principals/groups/group3', 'principals/groups/group3'],
  268. ];
  269. }
  270. /**
  271. * @return Group|\PHPUnit\Framework\MockObject\MockObject
  272. */
  273. private function mockGroup($gid) {
  274. $fooGroup = $this->createMock(Group::class);
  275. $fooGroup
  276. ->expects($this->exactly(1))
  277. ->method('getGID')
  278. ->willReturn($gid);
  279. $fooGroup
  280. ->expects($this->exactly(1))
  281. ->method('getDisplayName')
  282. ->willReturn('Group '.$gid);
  283. return $fooGroup;
  284. }
  285. }