GroupPrincipalTest.php 10 KB

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