groupstest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Tom Needham <tom@owncloud.com>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Provisioning_API\Tests;
  25. use OCP\IUserManager;
  26. use OCP\IGroupManager;
  27. use OCP\IUserSession;
  28. class GroupsTest extends TestCase {
  29. /** @var IUserManager */
  30. protected $userManager;
  31. /** @var IGroupManager */
  32. protected $groupManager;
  33. /** @var IUserSession */
  34. protected $userSession;
  35. protected function setup() {
  36. parent::setup();
  37. $this->userManager = \OC::$server->getUserManager();
  38. $this->groupManager = \OC::$server->getGroupManager();
  39. $this->userSession = \OC::$server->getUserSession();
  40. $this->api = new \OCA\Provisioning_API\Groups(
  41. $this->groupManager,
  42. $this->userSession
  43. );
  44. }
  45. public function testGetGroups() {
  46. $groups = [];
  47. $id = $this->getUniqueID();
  48. for ($i=0; $i < 10; $i++) {
  49. $groups[] = $this->groupManager->createGroup($id . '_' . $i);
  50. }
  51. $_GET = [];
  52. $result = $this->api->getGroups([]);
  53. $this->assertInstanceOf('OC_OCS_Result', $result);
  54. $this->assertTrue($result->succeeded());
  55. $this->assertCount(count($this->groupManager->search('')), $result->getData()['groups']);
  56. $this->assertContains('admin', $result->getData()['groups']);
  57. foreach ($groups as $group) {
  58. $this->assertContains($group->getGID(), $result->getData()['groups']);
  59. }
  60. $_GET = [
  61. 'search' => $id,
  62. 'limit' => 5,
  63. 'offset' => 2
  64. ];
  65. $result = $this->api->getGroups([]);
  66. $this->assertInstanceOf('OC_OCS_Result', $result);
  67. $this->assertTrue($result->succeeded());
  68. $this->assertCount(5, $result->getData()['groups']);
  69. foreach (array_splice($groups, 2, 5) as $group) {
  70. $this->assertContains($group->getGID(), $result->getData()['groups']);
  71. }
  72. foreach ($groups as $group) {
  73. $group->delete();
  74. }
  75. }
  76. public function testGetGroupAsUser() {
  77. $users = $this->generateUsers(2);
  78. $this->userSession->setUser($users[0]);
  79. $group = $this->groupManager->createGroup($this->getUniqueID());
  80. $group->addUser($users[1]);
  81. $result = $this->api->getGroup(array(
  82. 'groupid' => $group->getGID(),
  83. ));
  84. $this->assertInstanceOf('OC_OCS_Result', $result);
  85. $this->assertFalse($result->succeeded());
  86. $this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode());
  87. }
  88. public function testGetGroupAsSubadmin() {
  89. $users = $this->generateUsers(2);
  90. $this->userSession->setUser($users[0]);
  91. $group = $this->groupManager->createGroup($this->getUniqueID());
  92. $group->addUser($users[0]);
  93. $group->addUser($users[1]);
  94. \OC_SubAdmin::createSubAdmin($users[0]->getUID(), $group->getGID());
  95. $result = $this->api->getGroup([
  96. 'groupid' => $group->getGID(),
  97. ]);
  98. $this->assertInstanceOf('OC_OCS_Result', $result);
  99. $this->assertTrue($result->succeeded());
  100. $this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key');
  101. $this->assertArrayHasKey('users', $result->getData());
  102. $resultData = $result->getData();
  103. $resultData = $resultData['users'];
  104. $users = array_map(function($user) {
  105. return $user->getUID();
  106. }, $users);
  107. sort($users);
  108. sort($resultData);
  109. $this->assertEquals($users, $resultData);
  110. }
  111. public function testGetGroupAsIrrelevantSubadmin() {
  112. $users = $this->generateUsers(2);
  113. $this->userSession->setUser($users[0]);
  114. $group1 = $this->groupManager->createGroup($this->getUniqueID());
  115. $group2 = $this->groupManager->createGroup($this->getUniqueID());
  116. $group1->addUser($users[1]);
  117. $group2->addUser($users[0]);
  118. \OC_SubAdmin::createSubAdmin($users[0]->getUID(), $group2->getGID());
  119. $result = $this->api->getGroup([
  120. 'groupid' => $group1->getGID(),
  121. ]);
  122. $this->assertInstanceOf('OC_OCS_Result', $result);
  123. $this->assertFalse($result->succeeded());
  124. $this->assertEquals(\OCP\API::RESPOND_UNAUTHORISED, $result->getStatusCode());
  125. }
  126. public function testGetGroupAsAdmin() {
  127. $users = $this->generateUsers(2);
  128. $this->userSession->setUser($users[0]);
  129. $group = $this->groupManager->createGroup($this->getUniqueID());
  130. $group->addUser($users[1]);
  131. $this->groupManager->get('admin')->addUser($users[0]);
  132. $result = $this->api->getGroup([
  133. 'groupid' => $group->getGID(),
  134. ]);
  135. $this->assertInstanceOf('OC_OCS_Result', $result);
  136. $this->assertTrue($result->succeeded());
  137. $this->assertEquals(['users' => [$users[1]->getUID()]], $result->getData());
  138. }
  139. public function testGetGroupNonExisting() {
  140. $result = $this->api->getGroup([
  141. 'groupid' => $this->getUniqueId()
  142. ]);
  143. $this->assertInstanceOf('OC_OCS_Result', $result);
  144. $this->assertFalse($result->succeeded());
  145. $this->assertEquals(\OCP\API::RESPOND_NOT_FOUND, $result->getStatusCode());
  146. $this->assertEquals('The requested group could not be found', $result->getMeta()['message']);
  147. }
  148. public function testGetSubAdminsOfGroup() {
  149. $user1 = $this->generateUsers();
  150. $user2 = $this->generateUsers();
  151. $this->userSession->setUser($user1);
  152. $this->groupManager->get('admin')->addUser($user1);
  153. $group1 = $this->groupManager->createGroup($this->getUniqueID());
  154. \OC_SubAdmin::createSubAdmin($user2->getUID(), $group1->getGID());
  155. $result = $this->api->getSubAdminsOfGroup([
  156. 'groupid' => $group1->getGID(),
  157. ]);
  158. $this->assertInstanceOf('OC_OCS_Result', $result);
  159. $this->assertTrue($result->succeeded());
  160. $data = $result->getData();
  161. $this->assertEquals($user2->getUID(), reset($data));
  162. $group1->delete();
  163. $user1 = $this->generateUsers();
  164. $this->userSession->setUser($user1);
  165. $this->groupManager->get('admin')->addUser($user1);
  166. $result = $this->api->getSubAdminsOfGroup([
  167. 'groupid' => $this->getUniqueID(),
  168. ]);
  169. $this->assertInstanceOf('OC_OCS_Result', $result);
  170. $this->assertFalse($result->succeeded());
  171. $this->assertEquals(101, $result->getStatusCode());
  172. }
  173. public function testAddGroupEmptyGroup() {
  174. $_POST = [];
  175. $result = $this->api->addGroup([]);
  176. $this->assertInstanceOf('OC_OCS_Result', $result);
  177. $this->assertFalse($result->succeeded());
  178. $this->assertEquals(101, $result->getStatusCode());
  179. $this->assertEquals('Invalid group name', $result->getMeta()['message']);
  180. }
  181. public function testAddGroupExistingGroup() {
  182. $group = $this->groupManager->createGroup($this->getUniqueID());
  183. $_POST = [
  184. 'groupid' => $group->getGID()
  185. ];
  186. $result = $this->api->addGroup([]);
  187. $this->assertInstanceOf('OC_OCS_Result', $result);
  188. $this->assertFalse($result->succeeded());
  189. $this->assertEquals(102, $result->getStatusCode());
  190. $group->delete();
  191. }
  192. public function testAddGroup() {
  193. $group = $this->getUniqueId();
  194. $_POST = [
  195. 'groupid' => $group
  196. ];
  197. $result = $this->api->addGroup([]);
  198. $this->assertInstanceOf('OC_OCS_Result', $result);
  199. $this->assertTrue($result->succeeded());
  200. $this->assertTrue($this->groupManager->groupExists($group));
  201. $this->groupManager->get($group)->delete();
  202. }
  203. public function testDeleteGroupNonExisting() {
  204. $group = $this->getUniqueId();
  205. $result = $this->api->deleteGroup([
  206. 'groupid' => $group
  207. ]);
  208. $this->assertInstanceOf('OC_OCS_Result', $result);
  209. $this->assertFalse($result->succeeded());
  210. $this->assertEquals(101, $result->getStatusCode());
  211. }
  212. public function testDeleteAdminGroup() {
  213. $result = $this->api->deleteGroup([
  214. 'groupid' => 'admin'
  215. ]);
  216. $this->assertInstanceOf('OC_OCS_Result', $result);
  217. $this->assertFalse($result->succeeded());
  218. $this->assertEquals(102, $result->getStatusCode());
  219. }
  220. public function testDeleteGroup() {
  221. $group = $this->groupManager->createGroup($this->getUniqueId());
  222. $result = $this->api->deleteGroup([
  223. 'groupid' => $group->getGID()
  224. ]);
  225. $this->assertInstanceOf('OC_OCS_Result', $result);
  226. $this->assertTrue($result->succeeded());
  227. $this->assertFalse($this->groupManager->groupExists($group->getGID()));
  228. }
  229. }