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.

GroupsControllerTest.php 9.3KB

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