GroupsControllerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Provisioning_API\Tests\Controller;
  8. use OC\Group\Manager;
  9. use OC\User\NoUserException;
  10. use OCA\Provisioning_API\Controller\GroupsController;
  11. use OCP\Accounts\IAccountManager;
  12. use OCP\AppFramework\OCS\OCSException;
  13. use OCP\Group\ISubAdmin;
  14. use OCP\IConfig;
  15. use OCP\IGroup;
  16. use OCP\IRequest;
  17. use OCP\IUser;
  18. use OCP\IUserManager;
  19. use OCP\IUserSession;
  20. use OCP\L10N\IFactory;
  21. use OCP\UserInterface;
  22. use Psr\Log\LoggerInterface;
  23. class GroupsControllerTest extends \Test\TestCase {
  24. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  25. protected $request;
  26. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  27. protected $userManager;
  28. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  29. protected $config;
  30. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  31. protected $groupManager;
  32. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  33. protected $userSession;
  34. /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
  35. protected $accountManager;
  36. /** @var ISubAdmin|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $subAdminManager;
  38. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $l10nFactory;
  40. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $logger;
  42. /** @var GroupsController|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $api;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->request = $this->createMock(IRequest::class);
  47. $this->userManager = $this->createMock(IUserManager::class);
  48. $this->config = $this->createMock(IConfig::class);
  49. $this->groupManager = $this->createMock(Manager::class);
  50. $this->userSession = $this->createMock(IUserSession::class);
  51. $this->accountManager = $this->createMock(IAccountManager::class);
  52. $this->subAdminManager = $this->createMock(ISubAdmin::class);
  53. $this->l10nFactory = $this->createMock(IFactory::class);
  54. $this->logger = $this->createMock(LoggerInterface::class);
  55. $this->groupManager
  56. ->method('getSubAdmin')
  57. ->willReturn($this->subAdminManager);
  58. $this->api = $this->getMockBuilder(GroupsController::class)
  59. ->setConstructorArgs([
  60. 'provisioning_api',
  61. $this->request,
  62. $this->userManager,
  63. $this->config,
  64. $this->groupManager,
  65. $this->userSession,
  66. $this->accountManager,
  67. $this->subAdminManager,
  68. $this->l10nFactory,
  69. $this->logger
  70. ])
  71. ->setMethods(['fillStorageInfo'])
  72. ->getMock();
  73. }
  74. /**
  75. * @param string $gid
  76. * @return IGroup|\PHPUnit\Framework\MockObject\MockObject
  77. */
  78. private function createGroup($gid) {
  79. $group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  80. $group
  81. ->method('getGID')
  82. ->willReturn($gid);
  83. $group
  84. ->method('getDisplayName')
  85. ->willReturn($gid . '-name');
  86. $group
  87. ->method('count')
  88. ->willReturn(123);
  89. $group
  90. ->method('countDisabled')
  91. ->willReturn(11);
  92. $group
  93. ->method('canAddUser')
  94. ->willReturn(true);
  95. $group
  96. ->method('canRemoveUser')
  97. ->willReturn(true);
  98. return $group;
  99. }
  100. /**
  101. * @param string $uid
  102. * @return IUser|\PHPUnit\Framework\MockObject\MockObject
  103. */
  104. private function createUser($uid) {
  105. $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  106. $user
  107. ->method('getUID')
  108. ->willReturn($uid);
  109. $backendMock = $this->createMock(UserInterface::class);
  110. $user
  111. ->method('getBackend')
  112. ->willReturn($backendMock);
  113. return $user;
  114. }
  115. private function asUser() {
  116. $user = $this->createUser('user');
  117. $this->userSession
  118. ->method('getUser')
  119. ->willReturn($user);
  120. }
  121. private function asAdmin() {
  122. $user = $this->createUser('admin');
  123. $this->userSession
  124. ->method('getUser')
  125. ->willReturn($user);
  126. $this->groupManager
  127. ->method('isAdmin')
  128. ->with('admin')
  129. ->willReturn(true);
  130. }
  131. private function asSubAdminOfGroup($group) {
  132. $user = $this->createUser('subAdmin');
  133. $this->userSession
  134. ->method('getUser')
  135. ->willReturn($user);
  136. $this->subAdminManager
  137. ->method('isSubAdminOfGroup')
  138. ->willReturnCallback(function ($_user, $_group) use ($user, $group) {
  139. if ($_user === $user && $_group === $group) {
  140. return true;
  141. }
  142. return false;
  143. });
  144. }
  145. public function dataGetGroups() {
  146. return [
  147. [null, 0, 0],
  148. ['foo', 0, 0],
  149. [null, 1, 0],
  150. [null, 0, 2],
  151. ['foo', 1, 2],
  152. ];
  153. }
  154. /**
  155. * @dataProvider dataGetGroups
  156. *
  157. * @param string|null $search
  158. * @param int|null $limit
  159. * @param int|null $offset
  160. */
  161. public function testGetGroups($search, $limit, $offset): void {
  162. $groups = [$this->createGroup('group1'), $this->createGroup('group2')];
  163. $search = $search === null ? '' : $search;
  164. $this->groupManager
  165. ->expects($this->once())
  166. ->method('search')
  167. ->with($search, $limit, $offset)
  168. ->willReturn($groups);
  169. $result = $this->api->getGroups($search, $limit, $offset);
  170. $this->assertEquals(['groups' => ['group1', 'group2']], $result->getData());
  171. }
  172. /**
  173. * @dataProvider dataGetGroups
  174. *
  175. * @param string|null $search
  176. * @param int|null $limit
  177. * @param int|null $offset
  178. */
  179. public function testGetGroupsDetails($search, $limit, $offset): void {
  180. $groups = [$this->createGroup('group1'), $this->createGroup('group2')];
  181. $search = $search === null ? '' : $search;
  182. $this->groupManager
  183. ->expects($this->once())
  184. ->method('search')
  185. ->with($search, $limit, $offset)
  186. ->willReturn($groups);
  187. $result = $this->api->getGroupsDetails($search, $limit, $offset);
  188. $this->assertEquals(['groups' => [
  189. [
  190. 'id' => 'group1',
  191. 'displayname' => 'group1-name',
  192. 'usercount' => 123,
  193. 'disabled' => 11,
  194. 'canAdd' => true,
  195. 'canRemove' => true
  196. ],
  197. [
  198. 'id' => 'group2',
  199. 'displayname' => 'group2-name',
  200. 'usercount' => 123,
  201. 'disabled' => 11,
  202. 'canAdd' => true,
  203. 'canRemove' => true
  204. ]
  205. ]], $result->getData());
  206. }
  207. public function testGetGroupAsSubadmin(): void {
  208. $group = $this->createGroup('group');
  209. $this->asSubAdminOfGroup($group);
  210. $this->groupManager
  211. ->method('get')
  212. ->with('group')
  213. ->willReturn($group);
  214. $this->groupManager
  215. ->method('groupExists')
  216. ->with('group')
  217. ->willReturn(true);
  218. $group
  219. ->method('getUsers')
  220. ->willReturn([
  221. $this->createUser('user1'),
  222. $this->createUser('user2')
  223. ]);
  224. $result = $this->api->getGroup('group');
  225. $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
  226. }
  227. public function testGetGroupAsIrrelevantSubadmin(): void {
  228. $this->expectException(OCSException::class);
  229. $this->expectExceptionCode(403);
  230. $group = $this->createGroup('group');
  231. $otherGroup = $this->createGroup('otherGroup');
  232. $this->asSubAdminOfGroup($otherGroup);
  233. $this->groupManager
  234. ->method('get')
  235. ->with('group')
  236. ->willReturn($group);
  237. $this->groupManager
  238. ->method('groupExists')
  239. ->with('group')
  240. ->willReturn(true);
  241. $this->api->getGroup('group');
  242. }
  243. public function testGetGroupAsAdmin(): void {
  244. $group = $this->createGroup('group');
  245. $this->asAdmin();
  246. $this->groupManager
  247. ->method('get')
  248. ->with('group')
  249. ->willReturn($group);
  250. $this->groupManager
  251. ->method('groupExists')
  252. ->with('group')
  253. ->willReturn(true);
  254. $group
  255. ->method('getUsers')
  256. ->willReturn([
  257. $this->createUser('user1'),
  258. $this->createUser('user2')
  259. ]);
  260. $result = $this->api->getGroup('group');
  261. $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
  262. }
  263. public function testGetGroupNonExisting(): void {
  264. $this->expectException(OCSException::class);
  265. $this->expectExceptionMessage('The requested group could not be found');
  266. $this->expectExceptionCode(404);
  267. $this->asUser();
  268. $this->api->getGroup($this->getUniqueID());
  269. }
  270. public function testGetSubAdminsOfGroupsNotExists(): void {
  271. $this->expectException(OCSException::class);
  272. $this->expectExceptionMessage('Group does not exist');
  273. $this->expectExceptionCode(101);
  274. $this->api->getSubAdminsOfGroup('NonExistingGroup');
  275. }
  276. public function testGetSubAdminsOfGroup(): void {
  277. $group = $this->createGroup('GroupWithSubAdmins');
  278. $this->groupManager
  279. ->method('get')
  280. ->with('GroupWithSubAdmins')
  281. ->willReturn($group);
  282. $this->subAdminManager
  283. ->expects($this->once())
  284. ->method('getGroupsSubAdmins')
  285. ->with($group)
  286. ->willReturn([
  287. $this->createUser('SubAdmin1'),
  288. $this->createUser('SubAdmin2'),
  289. ]);
  290. $result = $this->api->getSubAdminsOfGroup('GroupWithSubAdmins');
  291. $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData());
  292. }
  293. public function testGetSubAdminsOfGroupEmptyList(): void {
  294. $group = $this->createGroup('GroupWithOutSubAdmins');
  295. $this->groupManager
  296. ->method('get')
  297. ->with('GroupWithOutSubAdmins')
  298. ->willReturn($group);
  299. $this->subAdminManager
  300. ->expects($this->once())
  301. ->method('getGroupsSubAdmins')
  302. ->with($group)
  303. ->willReturn([
  304. ]);
  305. $result = $this->api->getSubAdminsOfGroup('GroupWithOutSubAdmins');
  306. $this->assertEquals([], $result->getData());
  307. }
  308. public function testAddGroupEmptyGroup(): void {
  309. $this->expectException(OCSException::class);
  310. $this->expectExceptionMessage('Invalid group name');
  311. $this->expectExceptionCode(101);
  312. $this->api->addGroup('');
  313. }
  314. public function testAddGroupExistingGroup(): void {
  315. $this->expectException(OCSException::class);
  316. $this->expectExceptionCode(102);
  317. $this->groupManager
  318. ->method('groupExists')
  319. ->with('ExistingGroup')
  320. ->willReturn(true);
  321. $this->api->addGroup('ExistingGroup');
  322. }
  323. public function testAddGroup(): void {
  324. $this->groupManager
  325. ->method('groupExists')
  326. ->with('NewGroup')
  327. ->willReturn(false);
  328. $group = $this->createGroup('NewGroup');
  329. $this->groupManager
  330. ->expects($this->once())
  331. ->method('createGroup')
  332. ->with('NewGroup')
  333. ->willReturn($group);
  334. $this->api->addGroup('NewGroup');
  335. }
  336. public function testAddGroupWithSpecialChar(): void {
  337. $this->groupManager
  338. ->method('groupExists')
  339. ->with('Iñtërnâtiônàlizætiøn')
  340. ->willReturn(false);
  341. $group = $this->createGroup('Iñtërnâtiônàlizætiøn');
  342. $this->groupManager
  343. ->expects($this->once())
  344. ->method('createGroup')
  345. ->with('Iñtërnâtiônàlizætiøn')
  346. ->willReturn($group);
  347. $this->api->addGroup('Iñtërnâtiônàlizætiøn');
  348. }
  349. public function testDeleteGroupNonExisting(): void {
  350. $this->expectException(OCSException::class);
  351. $this->expectExceptionCode(101);
  352. $this->api->deleteGroup('NonExistingGroup');
  353. }
  354. public function testDeleteAdminGroup(): void {
  355. $this->expectException(OCSException::class);
  356. $this->expectExceptionCode(102);
  357. $this->groupManager
  358. ->method('groupExists')
  359. ->with('admin')
  360. ->willReturn('true');
  361. $this->api->deleteGroup('admin');
  362. }
  363. public function testDeleteGroup(): void {
  364. $this->groupManager
  365. ->method('groupExists')
  366. ->with('ExistingGroup')
  367. ->willReturn('true');
  368. $group = $this->createGroup('ExistingGroup');
  369. $this->groupManager
  370. ->method('get')
  371. ->with('ExistingGroup')
  372. ->willReturn($group);
  373. $group
  374. ->expects($this->once())
  375. ->method('delete')
  376. ->willReturn(true);
  377. $this->api->deleteGroup('ExistingGroup');
  378. }
  379. public function testDeleteGroupEncoding(): void {
  380. $this->groupManager
  381. ->method('groupExists')
  382. ->with('ExistingGroup A/B')
  383. ->willReturn('true');
  384. $group = $this->createGroup('ExistingGroup');
  385. $this->groupManager
  386. ->method('get')
  387. ->with('ExistingGroup A/B')
  388. ->willReturn($group);
  389. $group
  390. ->expects($this->once())
  391. ->method('delete')
  392. ->willReturn(true);
  393. $this->api->deleteGroup(urlencode('ExistingGroup A/B'));
  394. }
  395. public function testGetGroupUsersDetails(): void {
  396. $gid = 'ncg1';
  397. $this->asAdmin();
  398. $users = [
  399. 'ncu1' => $this->createUser('ncu1'), # regular
  400. 'ncu2' => $this->createUser('ncu2'), # the zombie
  401. ];
  402. $users['ncu2']->expects($this->atLeastOnce())
  403. ->method('getHome')
  404. ->willThrowException(new NoUserException());
  405. $this->userManager->expects($this->any())
  406. ->method('get')
  407. ->willReturnCallback(function (string $uid) use ($users) {
  408. return $users[$uid] ?? null;
  409. });
  410. $group = $this->createGroup($gid);
  411. $group->expects($this->once())
  412. ->method('searchUsers')
  413. ->with('', null, 0)
  414. ->willReturn(array_values($users));
  415. $this->groupManager
  416. ->method('get')
  417. ->with($gid)
  418. ->willReturn($group);
  419. $this->groupManager->expects($this->any())
  420. ->method('getUserGroups')
  421. ->willReturn([$group]);
  422. /** @var \PHPUnit\Framework\MockObject\MockObject */
  423. $this->subAdminManager->expects($this->any())
  424. ->method('isSubAdminOfGroup')
  425. ->willReturn(false);
  426. $this->subAdminManager->expects($this->any())
  427. ->method('getSubAdminsGroups')
  428. ->willReturn([]);
  429. $this->api->getGroupUsersDetails($gid);
  430. }
  431. public function testGetGroupUsersDetailsEncoded(): void {
  432. $gid = 'Department A/B C/D';
  433. $this->asAdmin();
  434. $users = [
  435. 'ncu1' => $this->createUser('ncu1'), # regular
  436. 'ncu2' => $this->createUser('ncu2'), # the zombie
  437. ];
  438. $users['ncu2']->expects($this->atLeastOnce())
  439. ->method('getHome')
  440. ->willThrowException(new NoUserException());
  441. $this->userManager->expects($this->any())
  442. ->method('get')
  443. ->willReturnCallback(function (string $uid) use ($users) {
  444. return $users[$uid] ?? null;
  445. });
  446. $group = $this->createGroup($gid);
  447. $group->expects($this->once())
  448. ->method('searchUsers')
  449. ->with('', null, 0)
  450. ->willReturn(array_values($users));
  451. $this->groupManager
  452. ->method('get')
  453. ->with($gid)
  454. ->willReturn($group);
  455. $this->groupManager->expects($this->any())
  456. ->method('getUserGroups')
  457. ->willReturn([$group]);
  458. /** @var \PHPUnit\Framework\MockObject\MockObject */
  459. $this->subAdminManager->expects($this->any())
  460. ->method('isSubAdminOfGroup')
  461. ->willReturn(false);
  462. $this->subAdminManager->expects($this->any())
  463. ->method('getSubAdminsGroups')
  464. ->willReturn([]);
  465. $this->api->getGroupUsersDetails(urlencode($gid));
  466. }
  467. }