You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroupPrincipalTest.php 11KB

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