userstest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Tom Needham <tom@owncloud.com>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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;
  27. use OCP\IUserManager;
  28. use OCP\IConfig;
  29. use OCP\IGroupManager;
  30. use OCP\IUserSession;
  31. class UsersTest extends TestCase {
  32. /** @var IUserManager */
  33. protected $userManager;
  34. /** @var IConfig */
  35. protected $config;
  36. /** @var IGroupManager */
  37. protected $groupManager;
  38. /** @var IUserSession */
  39. protected $userSession;
  40. protected function resetParams() {
  41. $_GET = null;
  42. $_POST = null;
  43. }
  44. protected function setup() {
  45. parent::setup();
  46. $this->userManager = \OC::$server->getUserManager();
  47. $this->config = \OC::$server->getConfig();
  48. $this->groupManager = \OC::$server->getGroupManager();
  49. $this->userSession = \OC::$server->getUserSession();
  50. $this->api = new \OCA\Provisioning_Api\Users(
  51. $this->userManager,
  52. $this->config,
  53. $this->groupManager,
  54. $this->userSession
  55. );
  56. }
  57. // Test getting the list of users
  58. public function testGetUsers() {
  59. $result = $this->api->getUsers();
  60. $this->assertInstanceOf('OC_OCS_Result', $result);
  61. $this->assertTrue($result->succeeded());
  62. $count = $result->getData();
  63. $count = count($count['users']);
  64. $this->assertEquals(count($this->userManager->search('', null, null)), $count);
  65. $user = $this->generateUsers();
  66. $_GET['search'] = $user->getUID();
  67. $result = $this->api->getUsers();
  68. $this->assertInstanceOf('OC_OCS_Result', $result);
  69. $this->assertTrue($result->succeeded());
  70. $data = $result->getData();
  71. $this->assertEquals($user->getUID(), reset($data['users']));
  72. // Add several users
  73. $this->generateUsers(10);
  74. $this->resetParams();
  75. $_GET['limit'] = 2;
  76. $result = $this->api->getUsers();
  77. $this->assertInstanceOf('OC_OCS_Result', $result);
  78. $this->assertTrue($result->succeeded());
  79. $count = $result->getData();
  80. $count = count($count['users']);
  81. $this->assertEquals(2, $count);
  82. $this->resetParams();
  83. $_GET['limit'] = 1;
  84. $_GET['offset'] = 1;
  85. $result = $this->api->getUsers(array());
  86. $this->assertInstanceOf('OC_OCS_Result', $result);
  87. $this->assertTrue($result->succeeded());
  88. $data = $result->getData();
  89. $this->assertEquals(array_keys($this->userManager->search('', 1, 1)), $data['users']);
  90. }
  91. public function testAddUser() {
  92. $this->resetParams();
  93. $_POST['userid'] = $this->getUniqueID();
  94. $_POST['password'] = 'password';
  95. $result = $this->api->addUser();
  96. $this->assertInstanceOf('OC_OCS_Result', $result);
  97. $this->assertTrue($result->succeeded());
  98. $this->assertTrue($this->userManager->userExists($_POST['userid']));
  99. $this->assertEquals($_POST['userid'], $this->userManager->checkPassword($_POST['userid'], $_POST['password'])->getUID());
  100. $this->users[] = $this->userManager->get($_POST['userid']);
  101. }
  102. public function testAddUserTwice() {
  103. $this->resetParams();
  104. $_POST['userid'] = $this->getUniqueID();
  105. $_POST['password'] = 'password';
  106. $this->api->addUser();
  107. $result = $this->api->addUser();
  108. $this->assertInstanceOf('OC_OCS_Result', $result);
  109. $this->assertFalse($result->succeeded());
  110. $this->assertEquals(102, $result->getStatusCode());
  111. $this->assertEquals('User already exists', $result->getMeta()['message']);
  112. }
  113. public function testAddUserFails() {
  114. $uid = $this->getUniqueID();
  115. $userManager = $this->getMockBuilder('\OCP\IUserManager')
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $userManager->expects($this->once())
  119. ->method('userExists')
  120. ->with($uid)
  121. ->willReturn(false);
  122. $userManager->expects($this->once())
  123. ->method('createUser')
  124. ->with($uid, 'password')
  125. ->will($this->throwException(new \Exception));
  126. $api = new \OCA\Provisioning_Api\Users(
  127. $userManager,
  128. $this->config,
  129. $this->groupManager,
  130. $this->userSession
  131. );
  132. $this->resetParams();
  133. $_POST['userid'] = $uid;
  134. $_POST['password'] = 'password';
  135. $result = $api->addUser();
  136. $this->assertInstanceOf('OC_OCS_Result', $result);
  137. $this->assertFalse($result->succeeded());
  138. $this->assertEquals(101, $result->getStatusCode());
  139. $this->assertEquals('Bad request', $result->getMeta()['message']);
  140. }
  141. public function testGetUserOnSelf() {
  142. $user = $this->generateUsers();
  143. $user->setDisplayName('foobar');
  144. $this->userSession->setUser($user);
  145. $params['userid'] = $user->getUID();
  146. $result = $this->api->getUser($params);
  147. $this->assertInstanceOf('OC_OCS_Result', $result);
  148. $this->assertTrue($result->succeeded());
  149. $data = $result->getData();
  150. $this->assertEquals('foobar', $data['displayname']);
  151. }
  152. public function testGetUserOnNonExistingUser() {
  153. $user = $this->generateUsers();
  154. $this->groupManager->get('admin')->addUser($user);
  155. $this->userSession->setUser($user);
  156. $params = array();
  157. $params['userid'] = $this->getUniqueID();
  158. while($this->userManager->userExists($params['userid'])) {
  159. $params['userid'] = $this->getUniqueID();
  160. }
  161. $result = $this->api->getUser($params);
  162. $this->assertInstanceOf('OC_OCS_Result', $result);
  163. $this->assertFalse($result->succeeded());
  164. $this->assertEquals(\OCP\API::RESPOND_NOT_FOUND, $result->getStatusCode());
  165. }
  166. public function testGetUserOnOtherUser() {
  167. $users = $this->generateUsers(2);
  168. $params['userid'] = $users[0];
  169. $this->userSession->setUser($users[1]);
  170. $result = $this->api->getUser($params);
  171. $this->assertInstanceOf('OC_OCS_Result', $result);
  172. $this->assertFalse($result->succeeded());
  173. // Now as as admin
  174. $users = $this->generateUsers(2);
  175. $params['userid'] = $users[0]->getUID();
  176. // login to generate home
  177. $this->userSession->setUser($users[0]);
  178. $this->groupManager->get('admin')->addUser($users[1]);
  179. $this->userSession->setUser($users[1]);
  180. $result = $this->api->getUser($params);
  181. $this->assertInstanceOf('OC_OCS_Result', $result);
  182. $this->assertTrue($result->succeeded());
  183. $data = $result->getData();
  184. $this->assertEquals(\OC::$server->getConfig()->getUserValue($users[0]->getUID(), 'core', 'enabled', 'true'), $data['enabled']);
  185. }
  186. public function testEditOwnDisplayName() {
  187. // Test editing own name
  188. $user = $this->generateUsers();
  189. $this->userSession->setUser($user);
  190. $result = $this->api->editUser(
  191. array(
  192. 'userid' => $user->getUID(),
  193. '_put' => array(
  194. 'key' => 'display',
  195. 'value' => 'newname',
  196. ),
  197. )
  198. );
  199. $this->assertInstanceOf('OC_OCS_Result', $result);
  200. $this->assertTrue($result->succeeded());
  201. $this->assertEquals('newname', $user->getDisplayName());
  202. }
  203. public function testAdminEditDisplayNameOfUser() {
  204. // Test admin editing users name
  205. $user = $this->generateUsers();
  206. $this->groupManager->get('admin')->addUser($user);
  207. $this->userSession->setUser($user);
  208. $user2 = $this->generateUsers();
  209. $result = $this->api->editUser(
  210. [
  211. 'userid' => $user2->getUID(),
  212. '_put' => [
  213. 'key' => 'display',
  214. 'value' => 'newname',
  215. ],
  216. ]
  217. );
  218. $this->assertInstanceOf('OC_OCS_Result', $result);
  219. $this->assertTrue($result->succeeded());
  220. $this->assertEquals('newname', $user2->getDisplayName());
  221. }
  222. public function testUserEditOtherUserDisplayName() {
  223. // Test editing other users name
  224. $user = $this->generateUsers();
  225. $this->userSession->setUser($user);
  226. $user2 = $this->generateUsers();
  227. $result = $this->api->editUser(
  228. array(
  229. 'userid' => $user2->getUID(),
  230. '_put' => array(
  231. 'key' => 'display',
  232. 'value' => 'newname',
  233. ),
  234. )
  235. );
  236. $this->assertInstanceOf('OC_OCS_Result', $result);
  237. $this->assertFalse($result->succeeded());
  238. }
  239. /**
  240. * @dataProvider providesQuotas
  241. * @param $expected
  242. * @param $quota
  243. */
  244. public function testEditOwnQuota($expected, $quota) {
  245. $user = $this->generateUsers();
  246. $this->userSession->setUser($user);
  247. $result = $this->api->editUser(
  248. [
  249. 'userid' => $user->getUID(),
  250. '_put' => [
  251. 'key' => 'quota',
  252. 'value' => $quota,
  253. ],
  254. ]
  255. );
  256. $this->assertInstanceOf('OC_OCS_Result', $result);
  257. $this->assertFalse($result->succeeded());
  258. $this->assertEquals(997, $result->getStatusCode());
  259. }
  260. /**
  261. * @dataProvider providesQuotas
  262. * @param $expected
  263. * @param $quota
  264. */
  265. public function testEditOwnQuotaAsAdmin($expected, $quota) {
  266. $user = $this->generateUsers();
  267. $this->groupManager->get('admin')->addUser($user);
  268. $this->userSession->setUser($user);
  269. $result = $this->api->editUser(
  270. [
  271. 'userid' => $user->getUID(),
  272. '_put' => [
  273. 'key' => 'quota',
  274. 'value' => $quota,
  275. ],
  276. ]
  277. );
  278. $this->assertInstanceOf('OC_OCS_Result', $result);
  279. $this->assertEquals($expected, $result->succeeded());
  280. }
  281. public function providesQuotas() {
  282. return [
  283. [true, '20G'],
  284. [true, '1234567'],
  285. [true, 'none'],
  286. [true, 'default'],
  287. [false, 'qwertzu'],
  288. [true, 0],
  289. [true, -1]
  290. ];
  291. }
  292. public function testAdminEditOwnQuota() {
  293. $user = $this->generateUsers();
  294. $this->groupManager->get('admin')->addUser($user);
  295. $this->userSession->setUser($user);
  296. $result = $this->api->editUser(
  297. array(
  298. 'userid' => $user->getUID(),
  299. '_put' => array(
  300. 'key' => 'quota',
  301. 'value' => '20G',
  302. ),
  303. )
  304. );
  305. $this->assertInstanceOf('OC_OCS_Result', $result);
  306. $this->assertTrue($result->succeeded());
  307. }
  308. public function testAdminEditOtherUserQuota() {
  309. $user = $this->generateUsers();
  310. $this->groupManager->get('admin')->addUser($user);
  311. $this->userSession->setUser($user);
  312. $user2 = $this->generateUsers();
  313. $result = $this->api->editUser(
  314. array(
  315. 'userid' => $user2->getUID(),
  316. '_put' => array(
  317. 'key' => 'quota',
  318. 'value' => '20G',
  319. ),
  320. )
  321. );
  322. $this->assertInstanceOf('OC_OCS_Result', $result);
  323. $this->assertTrue($result->succeeded());
  324. }
  325. public function testUserEditOtherUserQuota() {
  326. $user = $this->generateUsers();
  327. $this->userSession->setUser($user);
  328. $user2 = $this->generateUsers();
  329. $result = $this->api->editUser(
  330. array(
  331. 'userid' => $user2->getUID(),
  332. '_put' => array(
  333. 'key' => 'quota',
  334. 'value' => '20G',
  335. ),
  336. )
  337. );
  338. $this->assertInstanceOf('OC_OCS_Result', $result);
  339. $this->assertFalse($result->succeeded());
  340. }
  341. public function testUserEditOwnEmail() {
  342. $user = $this->generateUsers();
  343. $email = 'test@example.com';
  344. $this->userSession->setUser($user);
  345. $result = $this->api->editUser(
  346. array(
  347. 'userid' => $user->getUID(),
  348. '_put' => array(
  349. 'key' => 'email',
  350. 'value' => $email,
  351. ),
  352. )
  353. );
  354. $this->assertInstanceOf('OC_OCS_Result', $result);
  355. $this->assertTrue($result->succeeded());
  356. $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($user->getUID(), 'settings', 'email', null));
  357. }
  358. public function testUserEditOwnEmailInvalid() {
  359. $user = $this->generateUsers();
  360. $email = 'test@example';
  361. $this->userSession->setUser($user);
  362. $result = $this->api->editUser([
  363. 'userid' => $user->getUID(),
  364. '_put' => [
  365. 'key' => 'email',
  366. 'value' => $email,
  367. ],
  368. ]);
  369. $this->assertInstanceOf('OC_OCS_Result', $result);
  370. $this->assertFalse($result->succeeded());
  371. $this->assertEquals(102, $result->getStatusCode());
  372. }
  373. public function testUserEditOtherUserEmailAsUser() {
  374. $users = $this->generateUsers(2);
  375. $email = 'test@example.com';
  376. $this->userSession->setUser($users[0]);
  377. $result = $this->api->editUser(
  378. array(
  379. 'userid' => $users[1]->getUID(),
  380. '_put' => array(
  381. 'key' => 'email',
  382. 'value' => $email,
  383. ),
  384. )
  385. );
  386. $this->assertInstanceOf('OC_OCS_Result', $result);
  387. $this->assertFalse($result->succeeded());
  388. }
  389. public function testUserEditOtherUserEmailAsAdmin() {
  390. $users = $this->generateUsers(2);
  391. $email = 'test@example.com';
  392. $this->userSession->setUser($users[0]);
  393. $this->groupManager->get('admin')->addUser($users[0]);
  394. $result = $this->api->editUser(
  395. array(
  396. 'userid' => $users[1]->getUID(),
  397. '_put' => array(
  398. 'key' => 'email',
  399. 'value' => $email,
  400. ),
  401. )
  402. );
  403. $this->assertInstanceOf('OC_OCS_Result', $result);
  404. $this->assertTrue($result->succeeded());
  405. $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($users[1]->getUID(), 'settings', 'email', null));
  406. }
  407. public function testUserEditOwnPassword() {
  408. $user = $this->generateUsers();
  409. $password = 'foo';
  410. $this->userSession->setUser($user);
  411. $result = $this->api->editUser([
  412. 'userid' => $user->getUID(),
  413. '_put' => [
  414. 'key' => 'password',
  415. 'value' => $password,
  416. ],
  417. ]);
  418. $this->assertInstanceOf('OC_OCS_Result', $result);
  419. $this->assertTrue($result->succeeded());
  420. }
  421. public function testUserEditOtherUserPasswordAsUser() {
  422. $users = $this->generateUsers(2);
  423. $password = 'foo';
  424. $this->userSession->setUser($users[0]);
  425. $result = $this->api->editUser([
  426. 'userid' => $users[1]->getUID(),
  427. '_put' => [
  428. 'key' => 'password',
  429. 'value' => $password,
  430. ],
  431. ]);
  432. $this->assertInstanceOf('OC_OCS_Result', $result);
  433. $this->assertFalse($result->succeeded());
  434. }
  435. public function testUserEditOtherUserPasswordAsAdmin() {
  436. $users = $this->generateUsers(2);
  437. $password = 'foo';
  438. $this->userSession->setUser($users[0]);
  439. $this->groupManager->get('admin')->addUser($users[0]);
  440. $result = $this->api->editUser([
  441. 'userid' => $users[1]->getUID(),
  442. '_put' => [
  443. 'key' => 'password',
  444. 'value' => $password,
  445. ],
  446. ]);
  447. $this->assertInstanceOf('OC_OCS_Result', $result);
  448. $this->assertTrue($result->succeeded());
  449. }
  450. public function testDeleteSelf() {
  451. $user = $this->generateUsers();
  452. $this->userSession->setUser($user);
  453. $result = $this->api->deleteUser(array(
  454. 'userid' => $user->getUID(),
  455. ));
  456. $this->assertInstanceOf('OC_OCS_Result', $result);
  457. $this->assertFalse($result->succeeded());
  458. }
  459. public function testDeleteOtherAsUser() {
  460. $user = $this->generateUsers();
  461. $this->userSession->setUser($user);
  462. $user2 = $this->generateUsers();
  463. $result = $this->api->deleteUser(array(
  464. 'userid' => $user2->getUID(),
  465. ));
  466. $this->assertInstanceOf('OC_OCS_Result', $result);
  467. $this->assertFalse($result->succeeded());
  468. }
  469. public function testDeleteOtherAsSubAdmin() {
  470. $user = $this->generateUsers();
  471. $this->userSession->setUser($user);
  472. $user2 = $this->generateUsers();
  473. $group = $this->groupManager->createGroup($this->getUniqueID());
  474. $group->addUser($user);
  475. $group->addUser($user2);
  476. \OC_SubAdmin::createSubAdmin($user->getUID(), $group->getGID());
  477. $result = $this->api->deleteUser(array(
  478. 'userid' => $user2->getUID(),
  479. ));
  480. $this->assertInstanceOf('OC_OCS_Result', $result);
  481. $this->assertTrue($result->succeeded());
  482. $group->delete();
  483. }
  484. public function testDeleteOtherAsIrelevantSubAdmin() {
  485. $user = $this->generateUsers();
  486. $this->userSession->setUser($user);
  487. $user2 = $this->generateUsers();
  488. $group = $this->groupManager->createGroup($this->getUniqueID());
  489. $group2 = $this->groupManager->createGroup($this->getUniqueID());
  490. $group->addUser($user);
  491. $group2->addUser($user2);
  492. \OC_SubAdmin::createSubAdmin($user->getUID(), $group->getGID());
  493. $result = $this->api->deleteUser(array(
  494. 'userid' => $user2->getUID(),
  495. ));
  496. $this->assertInstanceOf('OC_OCS_Result', $result);
  497. $this->assertFalse($result->succeeded());
  498. $group->delete();
  499. $group2->delete();
  500. }
  501. public function testDeleteOtherAsAdmin() {
  502. $user = $this->generateUsers();
  503. $this->groupManager->get('admin')->addUser($user);
  504. $this->userSession->setUser($user);
  505. $user2 = $this->generateUsers();
  506. $result = $this->api->deleteUser(array(
  507. 'userid' => $user2->getUID(),
  508. ));
  509. $this->assertInstanceOf('OC_OCS_Result', $result);
  510. $this->assertTrue($result->succeeded());
  511. }
  512. public function testDeleteSelfAsAdmin() {
  513. $user = $this->generateUsers();
  514. $this->groupManager->get('admin')->addUser($user);
  515. $this->userSession->setUser($user);
  516. $result = $this->api->deleteUser(array(
  517. 'userid' => $user->getUID(),
  518. ));
  519. $this->assertInstanceOf('OC_OCS_Result', $result);
  520. $this->assertFalse($result->succeeded());
  521. }
  522. public function testDeleteFails() {
  523. $user = $this->getMockBuilder('\OCP\IUser')
  524. ->disableOriginalConstructor()
  525. ->getMock();
  526. $user->expects($this->once())
  527. ->method('delete')
  528. ->willReturn(false);
  529. $user2 = $this->getMockBuilder('\OCP\IUser')
  530. ->disableOriginalConstructor()
  531. ->getMock();
  532. $user2->expects($this->any())
  533. ->method('getUID')
  534. ->willReturn('user2');
  535. $userManager = $this->getMockBuilder('\OCP\IUserManager')
  536. ->disableOriginalConstructor()
  537. ->getMock();
  538. $userManager->expects($this->once())
  539. ->method('userExists')
  540. ->with('user')
  541. ->willReturn(true);
  542. $userManager->expects($this->once())
  543. ->method('get')
  544. ->with('user')
  545. ->willReturn($user);
  546. $userSession = $this->getMockBuilder('\OCP\IUserSession')
  547. ->disableOriginalConstructor()
  548. ->getMock();
  549. $userSession->expects($this->once())
  550. ->method('getUser')
  551. ->willReturn($user2);
  552. $groupManager = $this->getMockBuilder('\OCP\IGroupManager')
  553. ->disableOriginalConstructor()
  554. ->getMock();
  555. $groupManager->expects($this->once())
  556. ->method('isAdmin')
  557. ->with('user2')
  558. ->willReturn(true);
  559. $api = new \OCA\Provisioning_Api\Users(
  560. $userManager,
  561. $this->config,
  562. $groupManager,
  563. $userSession
  564. );
  565. $result = $api->deleteUser([
  566. 'userid' => 'user',
  567. ]);
  568. $this->assertInstanceOf('OC_OCS_Result', $result);
  569. $this->assertFalse($result->succeeded());
  570. $this->assertEquals(101, $result->getStatusCode());
  571. }
  572. public function testGetUsersGroupsOnSelf() {
  573. $user = $this->generateUsers();
  574. $this->userSession->setUser($user);
  575. $group = $this->getUniqueID();
  576. $group = $this->groupManager->createGroup($group);
  577. $group->addUser($user);
  578. $result = $this->api->getUsersGroups(array(
  579. 'userid' => $user->getUID(),
  580. ));
  581. $this->assertInstanceOf('OC_OCS_Result', $result);
  582. $this->assertTrue($result->succeeded());
  583. $data = $result->getData();
  584. $this->assertEquals($group->getGID(), reset($data['groups']));
  585. $this->assertEquals(1, count($data['groups']));
  586. $group->delete();
  587. }
  588. public function testGetUsersGroupOnOther() {
  589. $user1 = $this->generateUsers();
  590. $user2 = $this->generateUsers();
  591. $this->userSession->setUser($user1);
  592. $group = $this->getUniqueID();
  593. $group = $this->groupManager->createGroup($group);
  594. $group->addUser($user2);
  595. $result = $this->api->getUsersGroups(array(
  596. 'userid' => $user2->getUID(),
  597. ));
  598. $this->assertInstanceOf('OC_OCS_Result', $result);
  599. $this->assertFalse($result->succeeded());
  600. $group->delete();
  601. }
  602. public function testGetUsersGroupOnOtherAsAdmin() {
  603. $user1 = $this->generateUsers();
  604. $this->groupManager->get('admin')->addUser($user1);
  605. $user2 = $this->generateUsers();
  606. $this->userSession->setUser($user1);
  607. $group = $this->getUniqueID();
  608. $group = $this->groupManager->createGroup($group);
  609. $group->addUser($user2);
  610. $result = $this->api->getUsersGroups(array(
  611. 'userid' => $user2->getUID(),
  612. ));
  613. $this->assertInstanceOf('OC_OCS_Result', $result);
  614. $this->assertTrue($result->succeeded());
  615. $data = $result->getData();
  616. $this->assertEquals($group->getGID(), reset($data['groups']));
  617. $this->assertEquals(1, count($data['groups']));
  618. $group->delete();
  619. }
  620. public function testGetUsersGroupsOnOtherAsSubAdmin() {
  621. $user1 = $this->generateUsers();
  622. $user2 = $this->generateUsers();
  623. $this->userSession->setUser($user1);
  624. $group1 = $this->getUniqueID();
  625. $group2 = $this->getUniqueID();
  626. $group1 = $this->groupManager->createGroup($group1);
  627. $group2 = $this->groupManager->createGroup($group2);
  628. $group1->addUser($user2);
  629. $group2->addUser($user2);
  630. $group1->addUser($user1);
  631. \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID());
  632. $result = $this->api->getUsersGroups(array(
  633. 'userid' => $user2->getUID(),
  634. ));
  635. $this->assertInstanceOf('OC_OCS_Result', $result);
  636. $this->assertTrue($result->succeeded());
  637. $data = $result->getData();
  638. $this->assertEquals($group1->getGID(), reset($data['groups']));
  639. $this->assertEquals(1, count($data['groups']));
  640. $group1->delete();
  641. $group2->delete();
  642. }
  643. public function testGetUsersGroupsOnOtherAsIrelevantSubAdmin() {
  644. $user1 = $this->generateUsers();
  645. $user2 = $this->generateUsers();
  646. $this->userSession->setUser($user1);
  647. $group1 = $this->getUniqueID();
  648. $group2 = $this->getUniqueID();
  649. $group1 = $this->groupManager->createGroup($group1);
  650. $group2 = $this->groupManager->createGroup($group2);
  651. $group2->addUser($user2);
  652. $group1->addUser($user1);
  653. \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID());
  654. $result = $this->api->getUsersGroups(array(
  655. 'userid' => $user2->getUID(),
  656. ));
  657. $this->assertInstanceOf('OC_OCS_Result', $result);
  658. $this->assertFalse($result->succeeded());
  659. $group1->delete();
  660. $group2->delete();
  661. }
  662. public function testAddToGroup() {
  663. $user = $this->generateUsers();
  664. $group = $this->getUniqueID();
  665. $group = $this->groupManager->createGroup($group);
  666. $this->userSession->setUser($user);
  667. $_POST['groupid'] = $group->getGID();
  668. $result = $this->api->addToGroup(array(
  669. 'userid' => $user->getUID(),
  670. ));
  671. $this->assertInstanceOf('OC_OCS_Result', $result);
  672. $this->assertFalse($result->succeeded());
  673. $this->assertFalse($group->inGroup($user));
  674. $group->delete();
  675. }
  676. public function testAddToGroupAsAdmin() {
  677. $user = $this->generateUsers();
  678. $this->groupManager->get('admin')->addUser($user);
  679. $group = $this->getUniqueID();
  680. $group = $this->groupManager->createGroup($group);
  681. $user2 = $this->generateUsers();
  682. $this->userSession->setUser($user);
  683. $_POST['groupid'] = $group->getGID();
  684. $result = $this->api->addToGroup(array(
  685. 'userid' => $user2->getUID(),
  686. ));
  687. $this->assertInstanceOf('OC_OCS_Result', $result);
  688. $this->assertTrue($result->succeeded());
  689. $this->assertTrue($group->inGroup($user2));
  690. $group->delete();
  691. }
  692. public function testAddToGroupAsSubAdmin() {
  693. $user1 = $this->generateUsers();
  694. $user2 = $this->generateUsers();
  695. $this->userSession->setUser($user1);
  696. $group1 = $this->getUniqueID();
  697. $group1 = $this->groupManager->createGroup($group1);
  698. \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID());
  699. $_POST['groupid'] = $group1->getGID();
  700. $result = $this->api->addToGroup(array(
  701. 'userid' => $user2->getUID(),
  702. ));
  703. $this->assertInstanceOf('OC_OCS_Result', $result);
  704. $this->assertFalse($result->succeeded());
  705. $this->assertFalse($group1->inGroup($user2));
  706. $group1->delete();
  707. }
  708. public function testAddToGroupAsIrelevantSubAdmin() {
  709. $user1 = $this->generateUsers();
  710. $user2 = $this->generateUsers();
  711. $this->userSession->setUser($user1);
  712. $group1 = $this->getUniqueID();
  713. $group2 = $this->getUniqueID();
  714. $group1 = $this->groupManager->createGroup($group1);
  715. $group2 = $this->groupManager->createGroup($group2);
  716. \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID());
  717. $_POST['groupid'] = $group2->getGID();
  718. $result = $this->api->addToGroup(array(
  719. 'userid' => $user2->getUID(),
  720. ));
  721. $this->assertInstanceOf('OC_OCS_Result', $result);
  722. $this->assertFalse($result->succeeded());
  723. $this->assertFalse($group2->inGroup($user2));
  724. $group1->delete();
  725. $group2->delete();
  726. }
  727. public function testAddToGroupNoGroupId() {
  728. $_POST['groupid'] = '';
  729. $result = $this->api->addToGroup([
  730. 'userid' => $this->getUniqueID(),
  731. ]);
  732. $this->assertInstanceOf('OC_OCS_Result', $result);
  733. $this->assertFalse($result->succeeded());
  734. $this->assertEquals(101, $result->getStatusCode());
  735. }
  736. public function testAddToNonExistingGroup() {
  737. $user = $this->generateUsers();
  738. $this->groupManager->get('admin')->addUser($user);
  739. $this->userSession->setUser($user);
  740. $group = $this->groupManager->createGroup($this->getUniqueID());
  741. $_POST['groupid'] = $group->getGID();
  742. $result = $this->api->addToGroup([
  743. 'userid' => $this->getUniqueID(),
  744. ]);
  745. $this->assertInstanceOf('OC_OCS_Result', $result);
  746. $this->assertFalse($result->succeeded());
  747. $this->assertEquals(103, $result->getStatusCode());
  748. }
  749. public function testAddNonExistingUserToGroup() {
  750. $user = $this->generateUsers();
  751. $this->groupManager->get('admin')->addUser($user);
  752. $this->userSession->setUser($user);
  753. $_POST['groupid'] = $this->getUniqueID();
  754. $result = $this->api->addToGroup([
  755. 'userid' => $this->getUniqueID(),
  756. ]);
  757. $this->assertInstanceOf('OC_OCS_Result', $result);
  758. $this->assertFalse($result->succeeded());
  759. $this->assertEquals(102, $result->getStatusCode());
  760. }
  761. // test delete /cloud/users/{userid}/groups
  762. public function testRemoveFromGroupAsSelf() {
  763. $user1 = $this->generateUsers();
  764. $this->userSession->setUser($user1);
  765. $group1 = $this->getUniqueID();
  766. $group1 = $this->groupManager->createGroup($group1);
  767. $group1->addUser($user1);
  768. $result = $this->api->removeFromGroup(array(
  769. 'userid' => $user1->getUID(),
  770. '_delete' => array(
  771. 'groupid' => $group1->getGID(),
  772. ),
  773. ));
  774. $this->assertInstanceOf('OC_OCS_Result', $result);
  775. $this->assertFalse($result->succeeded());
  776. $this->assertTrue($group1->inGroup($user1));
  777. $group1->delete();
  778. }
  779. public function testRemoveFromGroupAsAdmin() {
  780. $user1 = $this->generateUsers();
  781. $user2 = $this->generateUsers();
  782. $this->userSession->setUser($user1);
  783. $group1 = $this->getUniqueID();
  784. $group1 = $this->groupManager->createGroup($group1);
  785. $group1->addUser($user2);
  786. $this->groupManager->get('admin')->addUser($user1);
  787. $result = $this->api->removeFromGroup(array(
  788. 'userid' => $user2->getUID(),
  789. '_delete' => array(
  790. 'groupid' => $group1->getGID(),
  791. ),
  792. ));
  793. $this->assertInstanceOf('OC_OCS_Result', $result);
  794. $this->assertTrue($result->succeeded());
  795. $this->assertFalse($group1->inGroup($user2));
  796. $group1->delete();
  797. }
  798. public function testRemoveSelfFromGroupAsAdmin() {
  799. $user1 = $this->generateUsers();
  800. $this->userSession->setUser($user1);
  801. $group1 = $this->groupManager->createGroup($this->getUniqueID());
  802. $group1->addUser($user1);
  803. $this->groupManager->get('admin')->addUser($user1);
  804. $result = $this->api->removeFromGroup([
  805. 'userid' => $user1->getUID(),
  806. '_delete' => [
  807. 'groupid' => $group1->getGID(),
  808. ],
  809. ]);
  810. $this->assertInstanceOf('OC_OCS_Result', $result);
  811. $this->assertTrue($result->succeeded());
  812. $this->assertFalse($group1->inGroup($user1));
  813. $group1->delete();
  814. }
  815. public function testRemoveFromGroupAsSubAdmin() {
  816. $user1 = $this->generateUsers();
  817. $this->userSession->setUser($user1);
  818. $user2 = $this->generateUsers();
  819. $group1 = $this->getUniqueID();
  820. $group1 = $this->groupManager->createGroup($group1);
  821. $group1->addUser($user1);
  822. $group1->addUser($user2);
  823. \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID());
  824. $result = $this->api->removeFromGroup(array(
  825. 'userid' => $user2->getUID(),
  826. '_delete' => array(
  827. 'groupid' => $group1->getGID(),
  828. ),
  829. ));
  830. $this->assertInstanceOf('OC_OCS_Result', $result);
  831. $this->assertTrue($result->succeeded());
  832. $this->assertFalse($group1->inGroup($user2));
  833. $group1->delete();
  834. }
  835. public function testRemoveFromGroupAsIrelevantSubAdmin() {
  836. $user1 = $this->generateUsers();
  837. $this->userSession->setUser($user1);
  838. $user2 = $this->generateUsers();
  839. $group1 = $this->getUniqueID();
  840. $group2 = $this->getUniqueID();
  841. $group1 = $this->groupManager->createGroup($group1);
  842. $group2 = $this->groupManager->createGroup($group2);
  843. $group1->addUser($user1);
  844. $group2->addUser($user2);
  845. \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID());
  846. $result = $this->api->removeFromGroup(array(
  847. 'userid' => $user2->getUID(),
  848. '_delete' => array(
  849. 'groupid' => $group2->getGID(),
  850. ),
  851. ));
  852. $this->assertInstanceOf('OC_OCS_Result', $result);
  853. $this->assertFalse($result->succeeded());
  854. $this->assertTrue($group2->inGroup($user2));
  855. $group1->delete();
  856. $group2->delete();
  857. }
  858. public function testRemoveFromGroupNoGroupId() {
  859. $result = $this->api->removeFromGroup([
  860. '_delete' => [
  861. 'groupid' => ''
  862. ],
  863. ]);
  864. $this->assertInstanceOf('OC_OCS_Result', $result);
  865. $this->assertFalse($result->succeeded());
  866. $this->assertEquals(101, $result->getStatusCode());
  867. }
  868. public function testRemoveSelfFromAdminAsAdmin() {
  869. $user = $this->generateUsers();
  870. $this->userSession->setUser($user);
  871. $this->groupManager->get('admin')->addUser($user);
  872. $result = $this->api->removeFromGroup([
  873. 'userid' => $user->getUID(),
  874. '_delete' => [
  875. 'groupid' => 'admin'
  876. ],
  877. ]);
  878. $this->assertInstanceOf('OC_OCS_Result', $result);
  879. $this->assertFalse($result->succeeded());
  880. $this->assertEquals(105, $result->getStatusCode());
  881. $this->assertEquals('Cannot remove yourself from the admin group', $result->getMeta()['message']);
  882. }
  883. public function testRemoveSelfFromSubAdminGroupAsSubAdmin() {
  884. $user = $this->generateUsers();
  885. $this->userSession->setUser($user);
  886. $group = $this->groupManager->createGroup($this->getUniqueID());
  887. \OC_SubAdmin::createSubAdmin($user->getUID(), $group->getGID());
  888. $result = $this->api->removeFromGroup([
  889. 'userid' => $user->getUID(),
  890. '_delete' => [
  891. 'groupid' => $group->getGID()
  892. ],
  893. ]);
  894. $this->assertInstanceOf('OC_OCS_Result', $result);
  895. $this->assertFalse($result->succeeded());
  896. $this->assertEquals(105, $result->getStatusCode());
  897. $this->assertEquals('Cannot remove yourself from this group as you are a SubAdmin', $result->getMeta()['message']);
  898. $group->delete();
  899. }
  900. public function testRemoveFromNonExistingGroup() {
  901. $user1 = $this->generateUsers();
  902. $this->userSession->setUser($user1);
  903. $this->groupManager->get('admin')->addUser($user1);
  904. $user2 = $this->generateUsers();
  905. $result = $this->api->removeFromGroup([
  906. 'userid' => $user2->getUID(),
  907. '_delete' => [
  908. 'groupid' => $this->getUniqueID()
  909. ],
  910. ]);
  911. $this->assertInstanceOf('OC_OCS_Result', $result);
  912. $this->assertFalse($result->succeeded());
  913. $this->assertEquals(102, $result->getStatusCode());
  914. }
  915. public function testRemoveFromNonGroupNonExistingUser() {
  916. $user = $this->generateUsers();
  917. $this->userSession->setUser($user);
  918. $this->groupManager->get('admin')->addUser($user);
  919. $group = $this->groupManager->createGroup($this->getUniqueID());
  920. $result = $this->api->removeFromGroup([
  921. 'userid' => $this->getUniqueID(),
  922. '_delete' => [
  923. 'groupid' => $group->getGID()
  924. ],
  925. ]);
  926. $this->assertInstanceOf('OC_OCS_Result', $result);
  927. $this->assertFalse($result->succeeded());
  928. $this->assertEquals(103, $result->getStatusCode());
  929. }
  930. public function testCreateSubAdmin() {
  931. $user1 = $this->generateUsers();
  932. $user2 = $this->generateUsers();
  933. $this->userSession->setUser($user1);
  934. $this->groupManager->get('admin')->addUser($user1);
  935. $group1 = $this->getUniqueID();
  936. $group1 = $this->groupManager->createGroup($group1);
  937. $_POST['groupid'] = $group1->getGID();
  938. $result = $this->api->addSubAdmin(array(
  939. 'userid' => $user2->getUID(),
  940. ));
  941. $this->assertInstanceOf('OC_OCS_Result', $result);
  942. $this->assertTrue($result->succeeded());
  943. $this->assertTrue(\OC_SubAdmin::isSubAdminofGroup($user2->getUID(), $group1->getGID()));
  944. $group1->delete();
  945. $this->resetParams();
  946. $user1 = $this->generateUsers();
  947. $user2 = $this->generateUsers();
  948. $this->userSession->setUser($user1);
  949. $this->groupManager->get('admin')->addUser($user1);
  950. $_POST['groupid'] = 'admin';
  951. $result = $this->api->addSubAdmin(array(
  952. 'userid' => $user2->getUID(),
  953. ));
  954. $this->assertInstanceOf('OC_OCS_Result', $result);
  955. $this->assertEquals(103, $result->getStatusCode());
  956. $this->assertFalse($result->succeeded());
  957. $this->resetParams();
  958. $user1 = $this->generateUsers();
  959. $this->userSession->setUser($user1);
  960. $this->groupManager->get('admin')->addUser($user1);
  961. $group1 = $this->getUniqueID();
  962. $group1 = $this->groupManager->createGroup($group1);
  963. $_POST['groupid'] = $group1->getGID();
  964. $result = $this->api->addSubAdmin(array(
  965. 'userid' => $this->getUniqueID(),
  966. ));
  967. $this->assertInstanceOf('OC_OCS_Result', $result);
  968. $this->assertFalse($result->succeeded());
  969. $this->assertEquals(101, $result->getStatusCode());
  970. $group1->delete();
  971. $user1 = $this->generateUsers();
  972. $this->userSession->setUser($user1);
  973. $group = $this->getUniqueID();
  974. $_POST['groupid'] = $group;
  975. $result = $this->api->addSubAdmin([
  976. 'userid' => $user1->getUID()
  977. ]);
  978. $this->assertInstanceOf('OC_OCS_Result', $result);
  979. $this->assertFalse($result->succeeded());
  980. $this->assertEquals(102, $result->getStatusCode());
  981. $this->assertEquals('Group:'.$group.' does not exist', $result->getMeta()['message']);
  982. }
  983. public function testRemoveSubAdmin() {
  984. $user1 = $this->generateUsers();
  985. $user2 = $this->generateUsers();
  986. $this->userSession->setUser($user1);
  987. $this->groupManager->get('admin')->addUser($user1);
  988. $group1 = $this->getUniqueID();
  989. $group1 = $this->groupManager->createGroup($group1);
  990. \OC_SubAdmin::createSubAdmin($user2->getUID(), $group1->getGID());
  991. $result = $this->api->removeSubAdmin(array(
  992. 'userid' => $user2->getUID(),
  993. '_delete' => array(
  994. 'groupid' => $group1->getGID(),
  995. ),
  996. ));
  997. $this->assertInstanceOf('OC_OCS_Result', $result);
  998. $this->assertTrue($result->succeeded());
  999. $this->assertTrue(!\OC_SubAdmin::isSubAdminofGroup($user2->getUID(), $group1->getGID()));
  1000. $group1->delete();
  1001. $user1 = $this->generateUsers();
  1002. $this->userSession->setUser($user1);
  1003. $this->groupManager->get('admin')->addUser($user1);
  1004. $result = $this->api->removeSubAdmin(array(
  1005. 'userid' => $this->getUniqueID(),
  1006. '_delete' => array(
  1007. 'groupid' => $group1->getGID(),
  1008. ),
  1009. ));
  1010. $this->assertInstanceOf('OC_OCS_Result', $result);
  1011. $this->assertEquals(101, $result->getStatusCode());
  1012. $this->assertFalse($result->succeeded());
  1013. $this->resetParams();
  1014. $user1 = $this->generateUsers();
  1015. $user2 = $this->generateUsers();
  1016. $this->userSession->setUser($user1);
  1017. $this->groupManager->get('admin')->addUser($user1);
  1018. $group1 = $this->getUniqueID();
  1019. $group1 = $this->groupManager->createGroup($group1);
  1020. $_POST['groupid'] = $group1->getGID();
  1021. $result = $this->api->removeSubAdmin(array(
  1022. 'userid' => $user2->getUID(),
  1023. '_delete' => array(
  1024. 'groupid' => $group1->getGID(),
  1025. ),
  1026. ));
  1027. $this->assertInstanceOf('OC_OCS_Result', $result);
  1028. $this->assertFalse($result->succeeded());
  1029. $this->assertEquals(102, $result->getStatusCode());
  1030. $group1->delete();
  1031. }
  1032. public function testGetSubAdminGroups() {
  1033. $user1 = $this->generateUsers();
  1034. $user2 = $this->generateUsers();
  1035. $this->userSession->setUser($user1);
  1036. $this->groupManager->get('admin')->addUser($user1);
  1037. $group1 = $this->getUniqueID();
  1038. $group1 = $this->groupManager->createGroup($group1);
  1039. \OC_SubAdmin::createSubAdmin($user2->getUID(), $group1->getGID());
  1040. $result = $this->api->getUserSubAdminGroups(array(
  1041. 'userid' => $user2->getUID(),
  1042. ));
  1043. $this->assertInstanceOf('OC_OCS_Result', $result);
  1044. $this->assertTrue($result->succeeded());
  1045. $data = $result->getData();
  1046. $this->assertEquals($group1->getGID(), reset($data));
  1047. $group1->delete();
  1048. $user1 = $this->generateUsers();
  1049. $this->userSession->setUser($user1);
  1050. $this->groupManager->get('admin')->addUser($user1);
  1051. $result = $this->api->getUserSubAdminGroups(array(
  1052. 'userid' => $this->getUniqueID(),
  1053. ));
  1054. $this->assertInstanceOf('OC_OCS_Result', $result);
  1055. $this->assertFalse($result->succeeded());
  1056. $this->assertEquals(101, $result->getStatusCode());
  1057. }
  1058. public function testSubAdminOfGroupAlreadySubAdmin() {
  1059. $user1 = $this->generateUsers();
  1060. $user2 = $this->generateUsers();
  1061. $this->userSession->setUser($user1);
  1062. $this->groupManager->get('admin')->addUser($user1);
  1063. $group1 = $this->groupManager->createGroup($this->getUniqueID());
  1064. //Make user2 subadmin of group1
  1065. $_POST['groupid'] = $group1->getGID();
  1066. $result = $this->api->addSubAdmin([
  1067. 'userid' => $user2->getUID(),
  1068. ]);
  1069. $this->assertInstanceOf('OC_OCS_Result', $result);
  1070. $this->assertTrue($result->succeeded());
  1071. //Make user2 subadmin of group1 again
  1072. $_POST['groupid'] = $group1->getGID();
  1073. $result = $this->api->addSubAdmin([
  1074. 'userid' => $user2->getUID(),
  1075. ]);
  1076. $this->assertInstanceOf('OC_OCS_Result', $result);
  1077. $this->assertTrue($result->succeeded());
  1078. $group1->delete();
  1079. }
  1080. }