userstest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Provisioning_API\Tests;
  24. class UsersTest extends TestCase {
  25. protected function resetParams() {
  26. $_GET = null;
  27. $_POST = null;
  28. }
  29. // Test getting the list of users
  30. public function testGetUsers() {
  31. $result = \OCA\provisioning_API\Users::getUsers(array());
  32. $this->assertInstanceOf('OC_OCS_Result', $result);
  33. $this->assertTrue($result->succeeded());
  34. $count = $result->getData();
  35. $count = count($count['users']);
  36. $this->assertEquals(count(\OC_User::getUsers()), $count);
  37. $user = $this->generateUsers();
  38. $_GET['search'] = $user;
  39. $result = \OCA\provisioning_API\Users::getUsers(array());
  40. $this->assertInstanceOf('OC_OCS_Result', $result);
  41. $this->assertTrue($result->succeeded());
  42. $data = $result->getData();
  43. $this->assertEquals($user, reset($data['users']));
  44. // Add several users
  45. $this->generateUsers(10);
  46. $this->resetParams();
  47. $_GET['limit'] = 2;
  48. $result = \OCA\provisioning_API\Users::getUsers(array());
  49. $this->assertInstanceOf('OC_OCS_Result', $result);
  50. $this->assertTrue($result->succeeded());
  51. $count = $result->getData();
  52. $count = count($count['users']);
  53. $this->assertEquals(2, $count);
  54. $this->resetParams();
  55. $_GET['limit'] = 1;
  56. $_GET['offset'] = 1;
  57. $result = \OCA\provisioning_API\Users::getUsers(array());
  58. $this->assertInstanceOf('OC_OCS_Result', $result);
  59. $this->assertTrue($result->succeeded());
  60. $data = $result->getData();
  61. $this->assertEquals(\OC_User::getUsers('', 1, 1), $data['users']);
  62. }
  63. public function testAddUser() {
  64. $this->resetParams();
  65. $_POST['userid'] = $this->getUniqueID();
  66. $_POST['password'] = 'password';
  67. $result = \OCA\provisioning_API\Users::addUser(array());
  68. $this->assertInstanceOf('OC_OCS_Result', $result);
  69. $this->assertTrue($result->succeeded());
  70. $this->assertTrue(\OC_User::userExists($_POST['userid']));
  71. $this->assertEquals($_POST['userid'], \OC_User::checkPassword($_POST['userid'], $_POST['password']));
  72. $this->users[] = $_POST['userid'];
  73. }
  74. public function testGetUserOnSelf() {
  75. $user = $this->generateUsers();
  76. \OC_User::setUserId($user);
  77. $params['userid'] = $user;
  78. $result = \OCA\provisioning_API\Users::getUser($params);
  79. $this->assertInstanceOf('OC_OCS_Result', $result);
  80. $this->assertTrue($result->succeeded());
  81. $data = $result->getData();
  82. }
  83. public function testGetUserOnNonExistingUser() {
  84. $user = $this->generateUsers();
  85. \OC_Group::addToGroup($user, 'admin');
  86. \OC_User::setUserId($user);
  87. $params = array();
  88. $params['userid'] = $this->getUniqueID();
  89. while(\OC_User::userExists($params['userid'])) {
  90. $params['userid'] = $this->getUniqueID();
  91. }
  92. $result = \OCA\provisioning_API\Users::getUser($params);
  93. $this->assertInstanceOf('OC_OCS_Result', $result);
  94. $this->assertFalse($result->succeeded());
  95. $this->assertEquals(\OC_API::RESPOND_NOT_FOUND, $result->getStatusCode());
  96. }
  97. public function testGetUserOnOtherUser() {
  98. $users = $this->generateUsers(2);
  99. $params['userid'] = $users[0];
  100. \OC_User::setUserId($users[1]);
  101. $result = \OCA\provisioning_API\Users::getUser($params);
  102. $this->assertInstanceOf('OC_OCS_Result', $result);
  103. $this->assertFalse($result->succeeded());
  104. // Now as as admin
  105. $users = $this->generateUsers(2);
  106. $params['userid'] = $users[0];
  107. \OC_Group::addToGroup($users[1], 'admin');
  108. \OC_User::setUserId($users[1]);
  109. $result = \OCA\provisioning_API\Users::getUser($params);
  110. $this->assertInstanceOf('OC_OCS_Result', $result);
  111. $this->assertTrue($result->succeeded());
  112. $data = $result->getData();
  113. $this->assertEquals(\OC::$server->getConfig()->getUserValue($users[0], 'core', 'enabled', 'true'), $data['enabled']);
  114. }
  115. public function testEditOwnDisplayName() {
  116. // Test editing own name
  117. $user = $this->generateUsers();
  118. \OC_User::setUserId($user);
  119. $result = \OCA\provisioning_API\Users::editUser(
  120. array(
  121. 'userid' => $user,
  122. '_put' => array(
  123. 'key' => 'display',
  124. 'value' => 'newname',
  125. ),
  126. )
  127. );
  128. $this->assertInstanceOf('OC_OCS_Result', $result);
  129. $this->assertTrue($result->succeeded());
  130. $this->assertEquals('newname', \OC_User::getDisplayName($user));
  131. }
  132. public function testAdminEditDisplayNameOfUser() {
  133. // Test admin editing users name
  134. $user = $this->generateUsers();
  135. \OC_Group::addToGroup($user, 'admin');
  136. \OC_User::setUserId($user);
  137. $user2 = $this->generateUsers();
  138. $result = \OCA\provisioning_API\Users::editUser(
  139. array(
  140. 'userid' => $user2,
  141. '_put' => array(
  142. 'key' => 'display',
  143. 'value' => 'newname',
  144. ),
  145. )
  146. );
  147. $this->assertInstanceOf('OC_OCS_Result', $result);
  148. $this->assertTrue($result->succeeded());
  149. $this->assertEquals('newname', \OC_User::getDisplayName($user2));
  150. }
  151. public function testUserEditOtherUserDisplayName() {
  152. // Test editing other users name
  153. $user = $this->generateUsers();
  154. \OC_User::setUserId($user);
  155. $user2 = $this->generateUsers();
  156. $result = \OCA\provisioning_API\Users::editUser(
  157. array(
  158. 'userid' => $user2,
  159. '_put' => array(
  160. 'key' => 'display',
  161. 'value' => 'newname',
  162. ),
  163. )
  164. );
  165. $this->assertInstanceOf('OC_OCS_Result', $result);
  166. $this->assertFalse($result->succeeded());
  167. }
  168. public function testEditOwnQuota() {
  169. $user = $this->generateUsers();
  170. \OC_User::setUserId($user);
  171. $result = \OCA\provisioning_API\Users::editUser(
  172. array(
  173. 'userid' => $user,
  174. '_put' => array(
  175. 'key' => 'quota',
  176. 'value' => '20G',
  177. ),
  178. )
  179. );
  180. $this->assertInstanceOf('OC_OCS_Result', $result);
  181. $this->assertFalse($result->succeeded());
  182. }
  183. public function testAdminEditOwnQuota() {
  184. $user = $this->generateUsers();
  185. \OC_Group::addToGroup($user, 'admin');
  186. \OC_User::setUserId($user);
  187. $result = \OCA\provisioning_API\Users::editUser(
  188. array(
  189. 'userid' => $user,
  190. '_put' => array(
  191. 'key' => 'quota',
  192. 'value' => '20G',
  193. ),
  194. )
  195. );
  196. $this->assertInstanceOf('OC_OCS_Result', $result);
  197. $this->assertTrue($result->succeeded());
  198. }
  199. public function testAdminEditOtherUserQuota() {
  200. $user = $this->generateUsers();
  201. \OC_Group::addToGroup($user, 'admin');
  202. \OC_User::setUserId($user);
  203. $user2 = $this->generateUsers();
  204. $result = \OCA\provisioning_API\Users::editUser(
  205. array(
  206. 'userid' => $user2,
  207. '_put' => array(
  208. 'key' => 'quota',
  209. 'value' => '20G',
  210. ),
  211. )
  212. );
  213. $this->assertInstanceOf('OC_OCS_Result', $result);
  214. $this->assertTrue($result->succeeded());
  215. }
  216. public function testUserEditOtherUserQuota() {
  217. $user = $this->generateUsers();
  218. \OC_User::setUserId($user);
  219. $user2 = $this->generateUsers();
  220. $result = \OCA\provisioning_API\Users::editUser(
  221. array(
  222. 'userid' => $user2,
  223. '_put' => array(
  224. 'key' => 'quota',
  225. 'value' => '20G',
  226. ),
  227. )
  228. );
  229. $this->assertInstanceOf('OC_OCS_Result', $result);
  230. $this->assertFalse($result->succeeded());
  231. }
  232. public function testUserEditOwnEmail() {
  233. $user = $this->generateUsers();
  234. $email = 'test@example.com';
  235. \OC_User::setUserId($user);
  236. $result = \OCA\provisioning_API\Users::editUser(
  237. array(
  238. 'userid' => $user,
  239. '_put' => array(
  240. 'key' => 'email',
  241. 'value' => $email,
  242. ),
  243. )
  244. );
  245. $this->assertInstanceOf('OC_OCS_Result', $result);
  246. $this->assertTrue($result->succeeded());
  247. $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($user, 'settings', 'email', null));
  248. }
  249. public function testUserEditOtherUserEmailAsUser() {
  250. $users = $this->generateUsers(2);
  251. $email = 'test@example.com';
  252. \OC_User::setUserId($users[0]);
  253. $result = \OCA\provisioning_API\Users::editUser(
  254. array(
  255. 'userid' => $users[1],
  256. '_put' => array(
  257. 'key' => 'email',
  258. 'value' => $email,
  259. ),
  260. )
  261. );
  262. $this->assertInstanceOf('OC_OCS_Result', $result);
  263. $this->assertFalse($result->succeeded());
  264. }
  265. public function testUserEditOtherUserEmailAsAdmin() {
  266. $users = $this->generateUsers(2);
  267. $email = 'test@example.com';
  268. \OC_User::setUserId($users[0]);
  269. \OC_Group::addToGroup($users[0], 'admin');
  270. $result = \OCA\provisioning_API\Users::editUser(
  271. array(
  272. 'userid' => $users[1],
  273. '_put' => array(
  274. 'key' => 'email',
  275. 'value' => $email,
  276. ),
  277. )
  278. );
  279. $this->assertInstanceOf('OC_OCS_Result', $result);
  280. $this->assertTrue($result->succeeded());
  281. $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($users[1], 'settings', 'email', null));
  282. }
  283. public function testDeleteSelf() {
  284. $user = $this->generateUsers();
  285. \OC_User::setUserId($user);
  286. $result = \OCA\provisioning_API\Users::deleteUser(array(
  287. 'userid' => $user,
  288. ));
  289. $this->assertInstanceOf('OC_OCS_Result', $result);
  290. $this->assertFalse($result->succeeded());
  291. }
  292. public function testDeleteOtherAsUser() {
  293. $user = $this->generateUsers();
  294. \OC_User::setUserId($user);
  295. $user2 = $this->generateUsers();
  296. $result = \OCA\provisioning_API\Users::deleteUser(array(
  297. 'userid' => $user2,
  298. ));
  299. $this->assertInstanceOf('OC_OCS_Result', $result);
  300. $this->assertFalse($result->succeeded());
  301. }
  302. public function testDeleteOtherAsSubAdmin() {
  303. $user = $this->generateUsers();
  304. \OC_User::setUserId($user);
  305. $user2 = $this->generateUsers();
  306. $group = $this->getUniqueID();
  307. \OC_Group::createGroup($group);
  308. \OC_Group::addToGroup($user, $group);
  309. \OC_Group::addToGroup($user2, $group);
  310. \OC_SubAdmin::createSubAdmin($user, $group);
  311. $result = \OCA\provisioning_API\Users::deleteUser(array(
  312. 'userid' => $user2,
  313. ));
  314. $this->assertInstanceOf('OC_OCS_Result', $result);
  315. $this->assertTrue($result->succeeded());
  316. \OC_Group::deleteGroup($group);
  317. }
  318. public function testDeleteOtherAsIrelevantSubAdmin() {
  319. $user = $this->generateUsers();
  320. \OC_User::setUserId($user);
  321. $user2 = $this->generateUsers();
  322. $group = $this->getUniqueID();
  323. $group2 = $this->getUniqueID();
  324. \OC_Group::createGroup($group);
  325. \OC_Group::createGroup($group2);
  326. \OC_Group::addToGroup($user, $group);
  327. \OC_Group::addToGroup($user2, $group2);
  328. \OC_SubAdmin::createSubAdmin($user, $group);
  329. $result = \OCA\provisioning_API\Users::deleteUser(array(
  330. 'userid' => $user2,
  331. ));
  332. $this->assertInstanceOf('OC_OCS_Result', $result);
  333. $this->assertFalse($result->succeeded());
  334. \OC_Group::deleteGroup($group);
  335. \OC_Group::deleteGroup($group2);
  336. }
  337. public function testDeleteOtherAsAdmin() {
  338. $user = $this->generateUsers();
  339. \OC_Group::addToGroup($user, 'admin');
  340. \OC_User::setUserId($user);
  341. $user2 = $this->generateUsers();
  342. $result = \OCA\provisioning_API\Users::deleteUser(array(
  343. 'userid' => $user2,
  344. ));
  345. $this->assertInstanceOf('OC_OCS_Result', $result);
  346. $this->assertTrue($result->succeeded());
  347. }
  348. public function testDeleteSelfAsAdmin() {
  349. $user = $this->generateUsers();
  350. \OC_Group::addToGroup($user, 'admin');
  351. \OC_User::setUserId($user);
  352. $result = \OCA\provisioning_API\Users::deleteUser(array(
  353. 'userid' => $user,
  354. ));
  355. $this->assertInstanceOf('OC_OCS_Result', $result);
  356. $this->assertFalse($result->succeeded());
  357. }
  358. public function testGetUsersGroupsOnSelf() {
  359. $user = $this->generateUsers();
  360. \OC_User::setUserId($user);
  361. $group = $this->getUniqueID();
  362. \OC_Group::createGroup($group);
  363. \OC_Group::addToGroup($user, $group);
  364. $result = \OCA\provisioning_API\Users::getUsersGroups(array(
  365. 'userid' => $user,
  366. ));
  367. $this->assertInstanceOf('OC_OCS_Result', $result);
  368. $this->assertTrue($result->succeeded());
  369. $data = $result->getData();
  370. $this->assertEquals($group, reset($data['groups']));
  371. $this->assertEquals(1, count($data['groups']));
  372. \OC_Group::deleteGroup($group);
  373. }
  374. public function testGetUsersGroupOnOther() {
  375. $user1 = $this->generateUsers();
  376. $user2 = $this->generateUsers();
  377. \OC_User::setUserId($user1);
  378. $group = $this->getUniqueID();
  379. \OC_Group::createGroup($group);
  380. \OC_Group::addToGroup($user2, $group);
  381. $result = \OCA\provisioning_API\Users::getUsersGroups(array(
  382. 'userid' => $user2,
  383. ));
  384. $this->assertInstanceOf('OC_OCS_Result', $result);
  385. $this->assertFalse($result->succeeded());
  386. \OC_Group::deleteGroup($group);
  387. }
  388. public function testGetUsersGroupOnOtherAsAdmin() {
  389. $user1 = $this->generateUsers();
  390. \OC_Group::addToGroup($user1, 'admin');
  391. $user2 = $this->generateUsers();
  392. \OC_User::setUserId($user1);
  393. $group = $this->getUniqueID();
  394. \OC_Group::createGroup($group);
  395. \OC_Group::addToGroup($user2, $group);
  396. $result = \OCA\provisioning_API\Users::getUsersGroups(array(
  397. 'userid' => $user2,
  398. ));
  399. $this->assertInstanceOf('OC_OCS_Result', $result);
  400. $this->assertTrue($result->succeeded());
  401. $data = $result->getData();
  402. $this->assertEquals($group, reset($data['groups']));
  403. $this->assertEquals(1, count($data['groups']));
  404. \OC_Group::deleteGroup($group);
  405. }
  406. public function testGetUsersGroupsOnOtherAsSubAdmin() {
  407. $user1 = $this->generateUsers();
  408. $user2 = $this->generateUsers();
  409. \OC_User::setUserId($user1);
  410. $group1 = $this->getUniqueID();
  411. $group2 = $this->getUniqueID();
  412. \OC_Group::createGroup($group1);
  413. \OC_Group::createGroup($group2);
  414. \OC_Group::addToGroup($user2, $group1);
  415. \OC_Group::addToGroup($user2, $group2);
  416. \OC_Group::addToGroup($user1, $group1);
  417. \OC_SubAdmin::createSubAdmin($user1, $group1);
  418. $result = \OCA\provisioning_API\Users::getUsersGroups(array(
  419. 'userid' => $user2,
  420. ));
  421. $this->assertInstanceOf('OC_OCS_Result', $result);
  422. $this->assertTrue($result->succeeded());
  423. $data = $result->getData();
  424. $this->assertEquals($group1, reset($data['groups']));
  425. $this->assertEquals(1, count($data['groups']));
  426. \OC_Group::deleteGroup($group1);
  427. \OC_Group::deleteGroup($group2);
  428. }
  429. public function testGetUsersGroupsOnOtherAsIrelevantSubAdmin() {
  430. $user1 = $this->generateUsers();
  431. $user2 = $this->generateUsers();
  432. \OC_User::setUserId($user1);
  433. $group1 = $this->getUniqueID();
  434. $group2 = $this->getUniqueID();
  435. \OC_Group::createGroup($group1);
  436. \OC_Group::createGroup($group2);
  437. \OC_Group::addToGroup($user2, $group2);
  438. \OC_Group::addToGroup($user1, $group1);
  439. \OC_SubAdmin::createSubAdmin($user1, $group1);
  440. $result = \OCA\provisioning_API\Users::getUsersGroups(array(
  441. 'userid' => $user2,
  442. ));
  443. $this->assertInstanceOf('OC_OCS_Result', $result);
  444. $this->assertFalse($result->succeeded());
  445. \OC_Group::deleteGroup($group1);
  446. \OC_Group::deleteGroup($group2);
  447. }
  448. public function testAddToGroup() {
  449. $user = $this->generateUsers();
  450. $group = $this->getUniqueID();
  451. \OC_Group::createGroup($group);
  452. \OC_User::setUserId($user);
  453. $_POST['groupid'] = $group;
  454. $result = \OCA\provisioning_API\Users::addToGroup(array(
  455. 'userid' => $user,
  456. ));
  457. $this->assertInstanceOf('OC_OCS_Result', $result);
  458. $this->assertFalse($result->succeeded());
  459. $this->assertFalse(\OC_Group::inGroup($user, $group));
  460. \OC_Group::deleteGroup($group);
  461. }
  462. public function testAddToGroupAsAdmin() {
  463. $user = $this->generateUsers();
  464. \OC_Group::addToGroup($user, 'admin');
  465. $group = $this->getUniqueID();
  466. \OC_Group::createGroup($group);
  467. $user2 = $this->generateUsers();
  468. \OC_User::setUserId($user);
  469. $_POST['groupid'] = $group;
  470. $result = \OCA\provisioning_API\Users::addToGroup(array(
  471. 'userid' => $user2,
  472. ));
  473. $this->assertInstanceOf('OC_OCS_Result', $result);
  474. $this->assertTrue($result->succeeded());
  475. $this->assertTrue(\OC_Group::inGroup($user2, $group));
  476. \OC_Group::deleteGroup($group);
  477. }
  478. public function testAddToGroupAsSubAdmin() {
  479. $user1 = $this->generateUsers();
  480. $user2 = $this->generateUsers();
  481. \OC_User::setUserId($user1);
  482. $group1 = $this->getUniqueID();
  483. \OC_Group::createGroup($group1);
  484. \OC_SubAdmin::createSubAdmin($user1, $group1);
  485. $_POST['groupid'] = $group1;
  486. $result = \OCA\provisioning_API\Users::addToGroup(array(
  487. 'userid' => $user2,
  488. ));
  489. $this->assertInstanceOf('OC_OCS_Result', $result);
  490. $this->assertFalse($result->succeeded());
  491. $this->assertFalse(\OC_Group::inGroup($user2, $group1));
  492. \OC_Group::deleteGroup($group1);
  493. }
  494. public function testAddToGroupAsIrelevantSubAdmin() {
  495. $user1 = $this->generateUsers();
  496. $user2 = $this->generateUsers();
  497. \OC_User::setUserId($user1);
  498. $group1 = $this->getUniqueID();
  499. $group2 = $this->getUniqueID();
  500. \OC_Group::createGroup($group1);
  501. \OC_Group::createGroup($group2);
  502. \OC_SubAdmin::createSubAdmin($user1, $group1);
  503. $_POST['groupid'] = $group2;
  504. $result = \OCA\provisioning_API\Users::addToGroup(array(
  505. 'userid' => $user2,
  506. ));
  507. $this->assertInstanceOf('OC_OCS_Result', $result);
  508. $this->assertFalse($result->succeeded());
  509. $this->assertFalse(\OC_Group::inGroup($user2, $group2));
  510. \OC_Group::deleteGroup($group1);
  511. \OC_Group::deleteGroup($group2);
  512. }
  513. // test delete /cloud/users/{userid}/groups
  514. public function testRemoveFromGroupAsSelf() {
  515. $user1 = $this->generateUsers();
  516. \OC_User::setUserId($user1);
  517. $group1 = $this->getUniqueID();
  518. \OC_Group::createGroup($group1);
  519. \OC_Group::addToGroup($user1, $group1);
  520. $result = \OCA\provisioning_api\Users::removeFromGroup(array(
  521. 'userid' => $user1,
  522. '_delete' => array(
  523. 'groupid' => $group1,
  524. ),
  525. ));
  526. $this->assertInstanceOf('OC_OCS_Result', $result);
  527. $this->assertFalse($result->succeeded());
  528. $this->assertTrue(\OC_Group::inGroup($user1, $group1));
  529. \OC_Group::deleteGroup($group1);
  530. }
  531. public function testRemoveFromGroupAsAdmin() {
  532. $user1 = $this->generateUsers();
  533. $user2 = $this->generateUsers();
  534. \OC_User::setUserId($user1);
  535. $group1 = $this->getUniqueID();
  536. \OC_Group::createGroup($group1);
  537. \OC_Group::addToGroup($user2, $group1);
  538. \OC_Group::addToGroup($user1, 'admin');
  539. $result = \OCA\provisioning_api\Users::removeFromGroup(array(
  540. 'userid' => $user2,
  541. '_delete' => array(
  542. 'groupid' => $group1,
  543. ),
  544. ));
  545. $this->assertInstanceOf('OC_OCS_Result', $result);
  546. $this->assertTrue($result->succeeded());
  547. $this->assertFalse(\OC_Group::inGroup($user2, $group1));
  548. \OC_Group::deleteGroup($group1);
  549. }
  550. public function testRemoveFromGroupAsSubAdmin() {
  551. $user1 = $this->generateUsers();
  552. \OC_User::setUserId($user1);
  553. $user2 = $this->generateUsers();
  554. $group1 = $this->getUniqueID();
  555. \OC_Group::createGroup($group1);
  556. \OC_Group::addToGroup($user1, $group1);
  557. \OC_Group::addToGroup($user2, $group1);
  558. \OC_SubAdmin::createSubAdmin($user1, $group1);
  559. $result = \OCA\provisioning_api\Users::removeFromGroup(array(
  560. 'userid' => $user2,
  561. '_delete' => array(
  562. 'groupid' => $group1,
  563. ),
  564. ));
  565. $this->assertInstanceOf('OC_OCS_Result', $result);
  566. $this->assertTrue($result->succeeded());
  567. $this->assertFalse(\OC_Group::inGroup($user2, $group1));
  568. \OC_Group::deleteGroup($group1);
  569. }
  570. public function testRemoveFromGroupAsIrelevantSubAdmin() {
  571. $user1 = $this->generateUsers();
  572. \OC_User::setUserId($user1);
  573. $user2 = $this->generateUsers();
  574. $group1 = $this->getUniqueID();
  575. $group2 = $this->getUniqueID();
  576. \OC_Group::createGroup($group1);
  577. \OC_Group::createGroup($group2);
  578. \OC_Group::addToGroup($user1, $group1);
  579. \OC_Group::addToGroup($user2, $group2);
  580. \OC_SubAdmin::createSubAdmin($user1, $group1);
  581. $result = \OCA\provisioning_api\Users::removeFromGroup(array(
  582. 'userid' => $user2,
  583. '_delete' => array(
  584. 'groupid' => $group2,
  585. ),
  586. ));
  587. $this->assertInstanceOf('OC_OCS_Result', $result);
  588. $this->assertFalse($result->succeeded());
  589. $this->assertTrue(\OC_Group::inGroup($user2, $group2));
  590. \OC_Group::deleteGroup($group1);
  591. \OC_Group::deleteGroup($group2);
  592. }
  593. public function testCreateSubAdmin() {
  594. $user1 = $this->generateUsers();
  595. $user2 = $this->generateUsers();
  596. \OC_User::setUserId($user1);
  597. \OC_Group::addToGroup($user1, 'admin');
  598. $group1 = $this->getUniqueID();
  599. \OC_Group::createGroup($group1);
  600. $_POST['groupid'] = $group1;
  601. $result = \OCA\provisioning_api\Users::addSubAdmin(array(
  602. 'userid' => $user2,
  603. ));
  604. $this->assertInstanceOf('OC_OCS_Result', $result);
  605. $this->assertTrue($result->succeeded());
  606. $this->assertTrue(\OC_SubAdmin::isSubAdminofGroup($user2, $group1));
  607. \OC_Group::deleteGroup($group1);
  608. $this->resetParams();
  609. $user1 = $this->generateUsers();
  610. $user2 = $this->generateUsers();
  611. \OC_User::setUserId($user1);
  612. \OC_Group::addToGroup($user1, 'admin');
  613. $_POST['groupid'] = 'admin';
  614. $result = \OCA\provisioning_api\Users::addSubAdmin(array(
  615. 'userid' => $user2,
  616. ));
  617. $this->assertInstanceOf('OC_OCS_Result', $result);
  618. $this->assertEquals(103, $result->getStatusCode());
  619. $this->assertFalse($result->succeeded());
  620. $this->resetParams();
  621. $user1 = $this->generateUsers();
  622. \OC_User::setUserId($user1);
  623. \OC_Group::addToGroup($user1, 'admin');
  624. $group1 = $this->getUniqueID();
  625. \OC_Group::createGroup($group1);
  626. $_POST['groupid'] = $group1;
  627. $result = \OCA\provisioning_api\Users::addSubAdmin(array(
  628. 'userid' => $this->getUniqueID(),
  629. ));
  630. $this->assertInstanceOf('OC_OCS_Result', $result);
  631. $this->assertFalse($result->succeeded());
  632. $this->assertEquals(101, $result->getStatusCode());
  633. \OC_Group::deleteGroup($group1);
  634. }
  635. public function testRemoveSubAdmin() {
  636. $user1 = $this->generateUsers();
  637. $user2 = $this->generateUsers();
  638. \OC_User::setUserId($user1);
  639. \OC_Group::addToGroup($user1, 'admin');
  640. $group1 = $this->getUniqueID();
  641. \OC_Group::createGroup($group1);
  642. \OC_SubAdmin::createSubAdmin($user2, $group1);
  643. $result = \OCA\provisioning_api\Users::removeSubAdmin(array(
  644. 'userid' => $user2,
  645. '_delete' => array(
  646. 'groupid' => $group1,
  647. ),
  648. ));
  649. $this->assertInstanceOf('OC_OCS_Result', $result);
  650. $this->assertTrue($result->succeeded());
  651. $this->assertTrue(!\OC_SubAdmin::isSubAdminofGroup($user2, $group1));
  652. \OC_Group::deleteGroup($group1);
  653. $user1 = $this->generateUsers();
  654. \OC_User::setUserId($user1);
  655. \OC_Group::addToGroup($user1, 'admin');
  656. $result = \OCA\provisioning_api\Users::removeSubAdmin(array(
  657. 'userid' => $this->getUniqueID(),
  658. '_delete' => array(
  659. 'groupid' => $group1,
  660. ),
  661. ));
  662. $this->assertInstanceOf('OC_OCS_Result', $result);
  663. $this->assertEquals(101, $result->getStatusCode());
  664. $this->assertFalse($result->succeeded());
  665. $this->resetParams();
  666. $user1 = $this->generateUsers();
  667. $user2 = $this->generateUsers();
  668. \OC_User::setUserId($user1);
  669. \OC_Group::addToGroup($user1, 'admin');
  670. $group1 = $this->getUniqueID();
  671. \OC_Group::createGroup($group1);
  672. $_POST['groupid'] = $group1;
  673. $result = \OCA\provisioning_api\Users::removeSubAdmin(array(
  674. 'userid' => $user2,
  675. '_delete' => array(
  676. 'groupid' => $group1,
  677. ),
  678. ));
  679. $this->assertInstanceOf('OC_OCS_Result', $result);
  680. $this->assertFalse($result->succeeded());
  681. $this->assertEquals(102, $result->getStatusCode());
  682. \OC_Group::deleteGroup($group1);
  683. }
  684. public function testGetSubAdminGroups() {
  685. $user1 = $this->generateUsers();
  686. $user2 = $this->generateUsers();
  687. \OC_User::setUserId($user1);
  688. \OC_Group::addToGroup($user1, 'admin');
  689. $group1 = $this->getUniqueID();
  690. \OC_Group::createGroup($group1);
  691. \OC_SubAdmin::createSubAdmin($user2, $group1);
  692. $result = \OCA\provisioning_api\Users::getUserSubAdminGroups(array(
  693. 'userid' => $user2,
  694. ));
  695. $this->assertInstanceOf('OC_OCS_Result', $result);
  696. $this->assertTrue($result->succeeded());
  697. $data = $result->getData();
  698. $this->assertEquals($group1, reset($data));
  699. \OC_Group::deleteGroup($group1);
  700. $user1 = $this->generateUsers();
  701. \OC_User::setUserId($user1);
  702. \OC_Group::addToGroup($user1, 'admin');
  703. $group1 = $this->getUniqueID();
  704. $result = \OCA\provisioning_api\Users::getUserSubAdminGroups(array(
  705. 'userid' => $this->getUniqueID(),
  706. ));
  707. $this->assertInstanceOf('OC_OCS_Result', $result);
  708. $this->assertFalse($result->succeeded());
  709. $this->assertEquals(101, $result->getStatusCode());
  710. }
  711. }