GroupPrincipalTest.php 11 KB

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