GroupsControllerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 OCA\Provisioning_API\Controller\GroupsController;
  31. use OCP\IConfig;
  32. use OCP\ILogger;
  33. use OCP\IRequest;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. use OCP\IUserSession;
  37. class GroupsControllerTest extends \Test\TestCase {
  38. /** @var IRequest|PHPUnit_Framework_MockObject_MockObject */
  39. protected $request;
  40. /** @var IUserManager|PHPUnit_Framework_MockObject_MockObject */
  41. protected $userManager;
  42. /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */
  43. protected $config;
  44. /** @var Manager|PHPUnit_Framework_MockObject_MockObject */
  45. protected $groupManager;
  46. /** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */
  47. protected $userSession;
  48. /** @var AccountManager|PHPUnit_Framework_MockObject_MockObject */
  49. protected $accountManager;
  50. /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */
  51. protected $logger;
  52. /** @var GroupsController|PHPUnit_Framework_MockObject_MockObject */
  53. protected $api;
  54. protected function setUp() {
  55. parent::setUp();
  56. $this->request = $this->createMock(IRequest::class);
  57. $this->userManager = $this->createMock(IUserManager::class);
  58. $this->config = $this->createMock(IConfig::class);
  59. $this->groupManager = $this->createMock(Manager::class);
  60. $this->userSession = $this->createMock(IUserSession::class);
  61. $this->accountManager = $this->createMock(AccountManager::class);
  62. $this->logger = $this->createMock(ILogger::class);
  63. $this->subAdminManager = $this->createMock(SubAdmin::class);
  64. $this->groupManager
  65. ->method('getSubAdmin')
  66. ->willReturn($this->subAdminManager);
  67. $this->api = $this->getMockBuilder(GroupsController::class)
  68. ->setConstructorArgs([
  69. 'provisioning_api',
  70. $this->request,
  71. $this->userManager,
  72. $this->config,
  73. $this->groupManager,
  74. $this->userSession,
  75. $this->accountManager,
  76. $this->logger
  77. ])
  78. ->setMethods(['fillStorageInfo'])
  79. ->getMock();
  80. }
  81. /**
  82. * @param string $gid
  83. * @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject
  84. */
  85. private function createGroup($gid) {
  86. $group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  87. $group
  88. ->method('getGID')
  89. ->willReturn($gid);
  90. $group
  91. ->method('getDisplayName')
  92. ->willReturn($gid.'-name');
  93. $group
  94. ->method('count')
  95. ->willReturn(123);
  96. $group
  97. ->method('countDisabled')
  98. ->willReturn(11);
  99. $group
  100. ->method('canAddUser')
  101. ->willReturn(true);
  102. $group
  103. ->method('canRemoveUser')
  104. ->willReturn(true);
  105. return $group;
  106. }
  107. /**
  108. * @param string $uid
  109. * @return \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject
  110. */
  111. private function createUser($uid) {
  112. $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  113. $user
  114. ->method('getUID')
  115. ->willReturn($uid);
  116. return $user;
  117. }
  118. private function asUser() {
  119. $user = $this->createUser('user');
  120. $this->userSession
  121. ->method('getUser')
  122. ->willReturn($user);
  123. }
  124. private function asAdmin() {
  125. $user = $this->createUser('admin');
  126. $this->userSession
  127. ->method('getUser')
  128. ->willReturn($user);
  129. $this->groupManager
  130. ->method('isAdmin')
  131. ->with('admin')
  132. ->willReturn(true);
  133. }
  134. private function asSubAdminOfGroup($group) {
  135. $user = $this->createUser('subAdmin');
  136. $this->userSession
  137. ->method('getUser')
  138. ->willReturn($user);
  139. $this->subAdminManager
  140. ->method('isSubAdminOfGroup')
  141. ->will($this->returnCallback(function($_user, $_group) use ($user, $group) {
  142. if ($_user === $user && $_group === $group) {
  143. return true;
  144. }
  145. return false;
  146. }));
  147. }
  148. public function dataGetGroups() {
  149. return [
  150. [null, 0, 0],
  151. ['foo', 0, 0],
  152. [null, 1, 0],
  153. [null, 0, 2],
  154. ['foo', 1, 2],
  155. ];
  156. }
  157. /**
  158. * @dataProvider dataGetGroups
  159. *
  160. * @param string|null $search
  161. * @param int|null $limit
  162. * @param int|null $offset
  163. */
  164. public function testGetGroups($search, $limit, $offset) {
  165. $groups = [$this->createGroup('group1'), $this->createGroup('group2')];
  166. $search = $search === null ? '' : $search;
  167. $this->groupManager
  168. ->expects($this->once())
  169. ->method('search')
  170. ->with($search, $limit, $offset)
  171. ->willReturn($groups);
  172. $result = $this->api->getGroups($search, $limit, $offset);
  173. $this->assertEquals(['groups' => ['group1', 'group2']], $result->getData());
  174. }
  175. /**
  176. * @dataProvider dataGetGroups
  177. *
  178. * @param string|null $search
  179. * @param int|null $limit
  180. * @param int|null $offset
  181. */
  182. public function testGetGroupsDetails($search, $limit, $offset) {
  183. $groups = [$this->createGroup('group1'), $this->createGroup('group2')];
  184. $search = $search === null ? '' : $search;
  185. $this->groupManager
  186. ->expects($this->once())
  187. ->method('search')
  188. ->with($search, $limit, $offset)
  189. ->willReturn($groups);
  190. $result = $this->api->getGroupsDetails($search, $limit, $offset);
  191. $this->assertEquals(['groups' => [
  192. Array(
  193. 'id' => 'group1',
  194. 'displayname' => 'group1-name',
  195. 'usercount' => 123,
  196. 'disabled' => 11,
  197. 'canAdd' => true,
  198. 'canRemove' => true
  199. ),
  200. Array(
  201. 'id' => 'group2',
  202. 'displayname' => 'group2-name',
  203. 'usercount' => 123,
  204. 'disabled' => 11,
  205. 'canAdd' => true,
  206. 'canRemove' => true
  207. )
  208. ]], $result->getData());
  209. }
  210. public function testGetGroupAsSubadmin() {
  211. $group = $this->createGroup('group');
  212. $this->asSubAdminOfGroup($group);
  213. $this->groupManager
  214. ->method('get')
  215. ->with('group')
  216. ->willReturn($group);
  217. $this->groupManager
  218. ->method('groupExists')
  219. ->with('group')
  220. ->willReturn(true);
  221. $group
  222. ->method('getUsers')
  223. ->willReturn([
  224. $this->createUser('user1'),
  225. $this->createUser('user2')
  226. ]);
  227. $result = $this->api->getGroup('group');
  228. $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
  229. }
  230. /**
  231. * @expectedException \OCP\AppFramework\OCS\OCSException
  232. * @expectedExceptionCode 403
  233. */
  234. public function testGetGroupAsIrrelevantSubadmin() {
  235. $group = $this->createGroup('group');
  236. $otherGroup = $this->createGroup('otherGroup');
  237. $this->asSubAdminOfGroup($otherGroup);
  238. $this->groupManager
  239. ->method('get')
  240. ->with('group')
  241. ->willReturn($group);
  242. $this->groupManager
  243. ->method('groupExists')
  244. ->with('group')
  245. ->willReturn(true);
  246. $this->api->getGroup('group');
  247. }
  248. public function testGetGroupAsAdmin() {
  249. $group = $this->createGroup('group');
  250. $this->asAdmin();
  251. $this->groupManager
  252. ->method('get')
  253. ->with('group')
  254. ->willReturn($group);
  255. $this->groupManager
  256. ->method('groupExists')
  257. ->with('group')
  258. ->willReturn(true);
  259. $group
  260. ->method('getUsers')
  261. ->willReturn([
  262. $this->createUser('user1'),
  263. $this->createUser('user2')
  264. ]);
  265. $result = $this->api->getGroup('group');
  266. $this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
  267. }
  268. /**
  269. * @expectedException \OCP\AppFramework\OCS\OCSException
  270. * @expectedExceptionCode 404
  271. * @expectedExceptionMessage The requested group could not be found
  272. */
  273. public function testGetGroupNonExisting() {
  274. $this->asUser();
  275. $this->api->getGroup($this->getUniqueID());
  276. }
  277. /**
  278. * @expectedException \OCP\AppFramework\OCS\OCSException
  279. * @expectedExceptionCode 101
  280. * @expectedExceptionMessage Group does not exist
  281. */
  282. public function testGetSubAdminsOfGroupsNotExists() {
  283. $this->api->getSubAdminsOfGroup('NonExistingGroup');
  284. }
  285. public function testGetSubAdminsOfGroup() {
  286. $group = $this->createGroup('GroupWithSubAdmins');
  287. $this->groupManager
  288. ->method('get')
  289. ->with('GroupWithSubAdmins')
  290. ->willReturn($group);
  291. $this->subAdminManager
  292. ->expects($this->once())
  293. ->method('getGroupsSubAdmins')
  294. ->with($group)
  295. ->willReturn([
  296. $this->createUser('SubAdmin1'),
  297. $this->createUser('SubAdmin2'),
  298. ]);
  299. $result = $this->api->getSubAdminsOfGroup('GroupWithSubAdmins');
  300. $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData());
  301. }
  302. public function testGetSubAdminsOfGroupEmptyList() {
  303. $group = $this->createGroup('GroupWithOutSubAdmins');
  304. $this->groupManager
  305. ->method('get')
  306. ->with('GroupWithOutSubAdmins')
  307. ->willReturn($group);
  308. $this->subAdminManager
  309. ->expects($this->once())
  310. ->method('getGroupsSubAdmins')
  311. ->with($group)
  312. ->willReturn([
  313. ]);
  314. $result = $this->api->getSubAdminsOfGroup('GroupWithOutSubAdmins');
  315. $this->assertEquals([], $result->getData());
  316. }
  317. /**
  318. * @expectedException \OCP\AppFramework\OCS\OCSException
  319. * @expectedExceptionCode 101
  320. * @expectedExceptionMessage Invalid group name
  321. */
  322. public function testAddGroupEmptyGroup() {
  323. $this->api->addGroup('');
  324. }
  325. /**
  326. * @expectedException \OCP\AppFramework\OCS\OCSException
  327. * @expectedExceptionCode 102
  328. */
  329. public function testAddGroupExistingGroup() {
  330. $this->groupManager
  331. ->method('groupExists')
  332. ->with('ExistingGroup')
  333. ->willReturn(true);
  334. $this->api->addGroup('ExistingGroup');
  335. }
  336. public function testAddGroup() {
  337. $this->groupManager
  338. ->method('groupExists')
  339. ->with('NewGroup')
  340. ->willReturn(false);
  341. $this->groupManager
  342. ->expects($this->once())
  343. ->method('createGroup')
  344. ->with('NewGroup');
  345. $this->api->addGroup('NewGroup');
  346. }
  347. public function testAddGroupWithSpecialChar() {
  348. $this->groupManager
  349. ->method('groupExists')
  350. ->with('Iñtërnâtiônàlizætiøn')
  351. ->willReturn(false);
  352. $this->groupManager
  353. ->expects($this->once())
  354. ->method('createGroup')
  355. ->with('Iñtërnâtiônàlizætiøn');
  356. $this->api->addGroup('Iñtërnâtiônàlizætiøn');
  357. }
  358. /**
  359. * @expectedException \OCP\AppFramework\OCS\OCSException
  360. * @expectedExceptionCode 101
  361. */
  362. public function testDeleteGroupNonExisting() {
  363. $this->api->deleteGroup('NonExistingGroup');
  364. }
  365. /**
  366. * @expectedException \OCP\AppFramework\OCS\OCSException
  367. * @expectedExceptionCode 102
  368. */
  369. public function testDeleteAdminGroup() {
  370. $this->groupManager
  371. ->method('groupExists')
  372. ->with('admin')
  373. ->willReturn('true');
  374. $this->api->deleteGroup('admin');
  375. }
  376. public function testDeleteGroup() {
  377. $this->groupManager
  378. ->method('groupExists')
  379. ->with('ExistingGroup')
  380. ->willReturn('true');
  381. $group = $this->createGroup('ExistingGroup');
  382. $this->groupManager
  383. ->method('get')
  384. ->with('ExistingGroup')
  385. ->willReturn($group);
  386. $group
  387. ->expects($this->once())
  388. ->method('delete')
  389. ->willReturn(true);
  390. $this->api->deleteGroup('ExistingGroup');
  391. }
  392. }