1
0

GroupsControllerTest.php 13 KB

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