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.

2296 lines
70 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author michag86 <micha_g@arcor.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Tom Needham <tom@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Provisioning_API\Tests\Controller;
  30. use OCA\Provisioning_API\Controller\UsersController;
  31. use OCP\IUserManager;
  32. use OCP\IConfig;
  33. use OCP\IUserSession;
  34. use PHPUnit_Framework_MockObject_MockObject;
  35. use Test\TestCase as OriginalTest;
  36. use OCP\ILogger;
  37. class UsersControllerTest extends OriginalTest {
  38. /** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */
  39. protected $userManager;
  40. /** @var IConfig | PHPUnit_Framework_MockObject_MockObject */
  41. protected $config;
  42. /** @var \OC\Group\Manager | PHPUnit_Framework_MockObject_MockObject */
  43. protected $groupManager;
  44. /** @var IUserSession | PHPUnit_Framework_MockObject_MockObject */
  45. protected $userSession;
  46. /** @var ILogger | PHPUnit_Framework_MockObject_MockObject */
  47. protected $logger;
  48. /** @var UsersController | PHPUnit_Framework_MockObject_MockObject */
  49. protected $api;
  50. protected function tearDown() {
  51. parent::tearDown();
  52. }
  53. protected function setUp() {
  54. parent::setUp();
  55. $this->userManager = $this->getMockBuilder('OCP\IUserManager')
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->config = $this->getMockBuilder('OCP\IConfig')
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->groupManager = $this->getMockBuilder('OC\Group\Manager')
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->userSession = $this->getMockBuilder('OCP\IUserSession')
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->logger = $this->getMockBuilder('OCP\ILogger')
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $request = $this->getMockBuilder('OCP\IRequest')
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
  74. ->setConstructorArgs([
  75. 'provisioning_api',
  76. $request,
  77. $this->userManager,
  78. $this->config,
  79. $this->groupManager,
  80. $this->userSession,
  81. $this->logger,
  82. ])
  83. ->setMethods(['fillStorageInfo'])
  84. ->getMock();
  85. }
  86. public function testGetUsersAsAdmin() {
  87. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $loggedInUser
  91. ->expects($this->once())
  92. ->method('getUID')
  93. ->will($this->returnValue('admin'));
  94. $this->userSession
  95. ->expects($this->once())
  96. ->method('getUser')
  97. ->will($this->returnValue($loggedInUser));
  98. $this->groupManager
  99. ->expects($this->once())
  100. ->method('isAdmin')
  101. ->will($this->returnValue(true));
  102. $this->userManager
  103. ->expects($this->once())
  104. ->method('search')
  105. ->with('MyCustomSearch', null, null)
  106. ->will($this->returnValue(['Admin' => [], 'Foo' => [], 'Bar' => []]));
  107. $expected = ['users' => [
  108. 'Admin',
  109. 'Foo',
  110. 'Bar',
  111. ],
  112. ];
  113. $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData());
  114. }
  115. public function testGetUsersAsSubAdmin() {
  116. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $loggedInUser
  120. ->expects($this->once())
  121. ->method('getUID')
  122. ->will($this->returnValue('subadmin'));
  123. $this->userSession
  124. ->expects($this->once())
  125. ->method('getUser')
  126. ->will($this->returnValue($loggedInUser));
  127. $this->groupManager
  128. ->expects($this->once())
  129. ->method('isAdmin')
  130. ->will($this->returnValue(false));
  131. $firstGroup = $this->getMockBuilder('OCP\IGroup')
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $firstGroup
  135. ->expects($this->once())
  136. ->method('getGID')
  137. ->will($this->returnValue('FirstGroup'));
  138. $secondGroup = $this->getMockBuilder('OCP\IGroup')
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $secondGroup
  142. ->expects($this->once())
  143. ->method('getGID')
  144. ->will($this->returnValue('SecondGroup'));
  145. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  146. ->disableOriginalConstructor()->getMock();
  147. $subAdminManager
  148. ->expects($this->once())
  149. ->method('isSubAdmin')
  150. ->with($loggedInUser)
  151. ->will($this->returnValue(true));
  152. $subAdminManager
  153. ->expects($this->once())
  154. ->method('getSubAdminsGroups')
  155. ->with($loggedInUser)
  156. ->will($this->returnValue([$firstGroup, $secondGroup]));
  157. $this->groupManager
  158. ->expects($this->once())
  159. ->method('getSubAdmin')
  160. ->will($this->returnValue($subAdminManager));
  161. $this->groupManager
  162. ->expects($this->any())
  163. ->method('displayNamesInGroup')
  164. ->will($this->onConsecutiveCalls(['AnotherUserInTheFirstGroup' => []], ['UserInTheSecondGroup' => []]));
  165. $expected = [
  166. 'users' => [
  167. 'AnotherUserInTheFirstGroup',
  168. 'UserInTheSecondGroup',
  169. ],
  170. ];
  171. $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData());
  172. }
  173. /**
  174. * @expectedException \OCP\AppFramework\OCS\OCSException
  175. * @expectedExceptionCode 102
  176. */
  177. public function testAddUserAlreadyExisting() {
  178. $this->userManager
  179. ->expects($this->once())
  180. ->method('userExists')
  181. ->with('AlreadyExistingUser')
  182. ->will($this->returnValue(true));
  183. $this->logger
  184. ->expects($this->once())
  185. ->method('error')
  186. ->with('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
  187. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  188. ->disableOriginalConstructor()
  189. ->getMock();
  190. $loggedInUser
  191. ->expects($this->once())
  192. ->method('getUID')
  193. ->will($this->returnValue('adminUser'));
  194. $this->userSession
  195. ->expects($this->once())
  196. ->method('getUser')
  197. ->will($this->returnValue($loggedInUser));
  198. $this->groupManager
  199. ->expects($this->once())
  200. ->method('isAdmin')
  201. ->with('adminUser')
  202. ->willReturn(true);
  203. $this->api->addUser('AlreadyExistingUser', null, null);
  204. }
  205. /**
  206. * @expectedException \OCP\AppFramework\OCS\OCSException
  207. * @expectedExceptionCode 104
  208. * @expectedExceptionMessage group NonExistingGroup does not exist
  209. */
  210. public function testAddUserNonExistingGroup() {
  211. $this->userManager
  212. ->expects($this->once())
  213. ->method('userExists')
  214. ->with('NewUser')
  215. ->willReturn(false);
  216. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  217. ->disableOriginalConstructor()
  218. ->getMock();
  219. $loggedInUser
  220. ->expects($this->once())
  221. ->method('getUID')
  222. ->will($this->returnValue('adminUser'));
  223. $this->userSession
  224. ->expects($this->once())
  225. ->method('getUser')
  226. ->will($this->returnValue($loggedInUser));
  227. $this->groupManager
  228. ->expects($this->once())
  229. ->method('isAdmin')
  230. ->with('adminUser')
  231. ->willReturn(true);
  232. $this->groupManager
  233. ->expects($this->once())
  234. ->method('groupExists')
  235. ->with('NonExistingGroup')
  236. ->willReturn(false);
  237. $this->api->addUser('NewUser', 'pass', ['NonExistingGroup']);
  238. }
  239. /**
  240. * @expectedException \OCP\AppFramework\OCS\OCSException
  241. * @expectedExceptionCode 104
  242. * @expectedExceptionMessage group NonExistingGroup does not exist
  243. */
  244. public function testAddUserExistingGroupNonExistingGroup() {
  245. $this->userManager
  246. ->expects($this->once())
  247. ->method('userExists')
  248. ->with('NewUser')
  249. ->willReturn(false);
  250. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  251. ->disableOriginalConstructor()
  252. ->getMock();
  253. $loggedInUser
  254. ->expects($this->once())
  255. ->method('getUID')
  256. ->will($this->returnValue('adminUser'));
  257. $this->userSession
  258. ->expects($this->once())
  259. ->method('getUser')
  260. ->will($this->returnValue($loggedInUser));
  261. $this->groupManager
  262. ->expects($this->once())
  263. ->method('isAdmin')
  264. ->with('adminUser')
  265. ->willReturn(true);
  266. $this->groupManager
  267. ->expects($this->exactly(2))
  268. ->method('groupExists')
  269. ->withConsecutive(
  270. ['ExistingGroup'],
  271. ['NonExistingGroup']
  272. )
  273. ->will($this->returnValueMap([
  274. ['ExistingGroup', true],
  275. ['NonExistingGroup', false]
  276. ]));
  277. $this->api->addUser('NewUser', 'pass', ['ExistingGroup', 'NonExistingGroup']);
  278. }
  279. public function testAddUserSuccessful() {
  280. $this->userManager
  281. ->expects($this->once())
  282. ->method('userExists')
  283. ->with('NewUser')
  284. ->will($this->returnValue(false));
  285. $this->userManager
  286. ->expects($this->once())
  287. ->method('createUser')
  288. ->with('NewUser', 'PasswordOfTheNewUser');
  289. $this->logger
  290. ->expects($this->once())
  291. ->method('info')
  292. ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
  293. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  294. ->disableOriginalConstructor()
  295. ->getMock();
  296. $loggedInUser
  297. ->expects($this->once())
  298. ->method('getUID')
  299. ->will($this->returnValue('adminUser'));
  300. $this->userSession
  301. ->expects($this->once())
  302. ->method('getUser')
  303. ->will($this->returnValue($loggedInUser));
  304. $this->groupManager
  305. ->expects($this->once())
  306. ->method('isAdmin')
  307. ->with('adminUser')
  308. ->willReturn(true);
  309. $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser')->getData());
  310. }
  311. public function testAddUserExistingGroup() {
  312. $this->userManager
  313. ->expects($this->once())
  314. ->method('userExists')
  315. ->with('NewUser')
  316. ->willReturn(false);
  317. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  318. ->disableOriginalConstructor()
  319. ->getMock();
  320. $loggedInUser
  321. ->expects($this->once())
  322. ->method('getUID')
  323. ->will($this->returnValue('adminUser'));
  324. $this->userSession
  325. ->expects($this->once())
  326. ->method('getUser')
  327. ->will($this->returnValue($loggedInUser));
  328. $this->groupManager
  329. ->expects($this->once())
  330. ->method('isAdmin')
  331. ->with('adminUser')
  332. ->willReturn(true);
  333. $this->groupManager
  334. ->expects($this->once())
  335. ->method('groupExists')
  336. ->with('ExistingGroup')
  337. ->willReturn(true);
  338. $user = $this->getMockBuilder('OCP\IUser')
  339. ->disableOriginalConstructor()
  340. ->getMock();
  341. $this->userManager
  342. ->expects($this->once())
  343. ->method('createUser')
  344. ->with('NewUser', 'PasswordOfTheNewUser')
  345. ->willReturn($user);
  346. $group = $this->getMockBuilder('OCP\IGroup')
  347. ->disableOriginalConstructor()
  348. ->getMock();
  349. $group
  350. ->expects($this->once())
  351. ->method('addUser')
  352. ->with($user);
  353. $this->groupManager
  354. ->expects($this->once())
  355. ->method('get')
  356. ->with('ExistingGroup')
  357. ->willReturn($group);
  358. $this->logger
  359. ->expects($this->exactly(2))
  360. ->method('info')
  361. ->withConsecutive(
  362. ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
  363. ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']]
  364. );
  365. $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup'])->getData());
  366. }
  367. /**
  368. * @expectedException \OCP\AppFramework\OCS\OCSException
  369. * @expectedExceptionCode 101
  370. * @expectedExceptionMessage Bad request
  371. */
  372. public function testAddUserUnsuccessful() {
  373. $this->userManager
  374. ->expects($this->once())
  375. ->method('userExists')
  376. ->with('NewUser')
  377. ->will($this->returnValue(false));
  378. $this->userManager
  379. ->expects($this->once())
  380. ->method('createUser')
  381. ->with('NewUser', 'PasswordOfTheNewUser')
  382. ->will($this->throwException(new \Exception('User backend not found.')));
  383. $this->logger
  384. ->expects($this->once())
  385. ->method('error')
  386. ->with('Failed addUser attempt with exception: User backend not found.', ['app' => 'ocs_api']);
  387. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  388. ->disableOriginalConstructor()
  389. ->getMock();
  390. $loggedInUser
  391. ->expects($this->once())
  392. ->method('getUID')
  393. ->will($this->returnValue('adminUser'));
  394. $this->userSession
  395. ->expects($this->once())
  396. ->method('getUser')
  397. ->will($this->returnValue($loggedInUser));
  398. $this->groupManager
  399. ->expects($this->once())
  400. ->method('isAdmin')
  401. ->with('adminUser')
  402. ->willReturn(true);
  403. $this->api->addUser('NewUser', 'PasswordOfTheNewUser');
  404. }
  405. /**
  406. * @expectedException \OCP\AppFramework\OCS\OCSException
  407. * @expectedExceptionCode 106
  408. * @expectedExceptionMessage no group specified (required for subadmins)
  409. */
  410. public function testAddUserAsSubAdminNoGroup() {
  411. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  412. ->disableOriginalConstructor()
  413. ->getMock();
  414. $loggedInUser
  415. ->expects($this->once())
  416. ->method('getUID')
  417. ->will($this->returnValue('regularUser'));
  418. $this->userSession
  419. ->expects($this->once())
  420. ->method('getUser')
  421. ->will($this->returnValue($loggedInUser));
  422. $this->groupManager
  423. ->expects($this->once())
  424. ->method('isAdmin')
  425. ->with('regularUser')
  426. ->willReturn(false);
  427. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  428. ->disableOriginalConstructor()->getMock();
  429. $this->groupManager
  430. ->expects($this->once())
  431. ->method('getSubAdmin')
  432. ->with()
  433. ->willReturn($subAdminManager);
  434. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', null);
  435. }
  436. /**
  437. * @expectedException \OCP\AppFramework\OCS\OCSException
  438. * @expectedExceptionCode 105
  439. * @expectedExceptionMessage insufficient privileges for group ExistingGroup
  440. */
  441. public function testAddUserAsSubAdminValidGroupNotSubAdmin() {
  442. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  443. ->disableOriginalConstructor()
  444. ->getMock();
  445. $loggedInUser
  446. ->expects($this->once())
  447. ->method('getUID')
  448. ->will($this->returnValue('regularUser'));
  449. $this->userSession
  450. ->expects($this->once())
  451. ->method('getUser')
  452. ->will($this->returnValue($loggedInUser));
  453. $this->groupManager
  454. ->expects($this->once())
  455. ->method('isAdmin')
  456. ->with('regularUser')
  457. ->willReturn(false);
  458. $existingGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  459. $this->groupManager
  460. ->expects($this->once())
  461. ->method('get')
  462. ->with('ExistingGroup')
  463. ->willReturn($existingGroup);
  464. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  465. ->disableOriginalConstructor()->getMock();
  466. $subAdminManager
  467. ->expects($this->once())
  468. ->method('isSubAdminOfGroup')
  469. ->with($loggedInUser, $existingGroup)
  470. ->willReturn(false);
  471. $this->groupManager
  472. ->expects($this->once())
  473. ->method('getSubAdmin')
  474. ->with()
  475. ->willReturn($subAdminManager);
  476. $this->groupManager
  477. ->expects($this->once())
  478. ->method('groupExists')
  479. ->with('ExistingGroup')
  480. ->willReturn(true);
  481. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup'])->getData();
  482. }
  483. public function testAddUserAsSubAdminExistingGroups() {
  484. $this->userManager
  485. ->expects($this->once())
  486. ->method('userExists')
  487. ->with('NewUser')
  488. ->willReturn(false);
  489. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  490. ->disableOriginalConstructor()
  491. ->getMock();
  492. $loggedInUser
  493. ->expects($this->once())
  494. ->method('getUID')
  495. ->will($this->returnValue('subAdminUser'));
  496. $this->userSession
  497. ->expects($this->once())
  498. ->method('getUser')
  499. ->will($this->returnValue($loggedInUser));
  500. $this->groupManager
  501. ->expects($this->once())
  502. ->method('isAdmin')
  503. ->with('subAdminUser')
  504. ->willReturn(false);
  505. $this->groupManager
  506. ->expects($this->exactly(2))
  507. ->method('groupExists')
  508. ->withConsecutive(
  509. ['ExistingGroup1'],
  510. ['ExistingGroup2']
  511. )
  512. ->willReturn(true);
  513. $user = $this->getMockBuilder('OCP\IUser')
  514. ->disableOriginalConstructor()
  515. ->getMock();
  516. $this->userManager
  517. ->expects($this->once())
  518. ->method('createUser')
  519. ->with('NewUser', 'PasswordOfTheNewUser')
  520. ->willReturn($user);
  521. $existingGroup1 = $this->getMockBuilder('OCP\IGroup')
  522. ->disableOriginalConstructor()
  523. ->getMock();
  524. $existingGroup2 = $this->getMockBuilder('OCP\IGroup')
  525. ->disableOriginalConstructor()
  526. ->getMock();
  527. $existingGroup1
  528. ->expects($this->once())
  529. ->method('addUser')
  530. ->with($user);
  531. $existingGroup2
  532. ->expects($this->once())
  533. ->method('addUser')
  534. ->with($user);
  535. $this->groupManager
  536. ->expects($this->exactly(4))
  537. ->method('get')
  538. ->withConsecutive(
  539. ['ExistingGroup1'],
  540. ['ExistingGroup2'],
  541. ['ExistingGroup1'],
  542. ['ExistingGroup2']
  543. )
  544. ->will($this->returnValueMap([
  545. ['ExistingGroup1', $existingGroup1],
  546. ['ExistingGroup2', $existingGroup2]
  547. ]));
  548. $this->logger
  549. ->expects($this->exactly(3))
  550. ->method('info')
  551. ->withConsecutive(
  552. ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
  553. ['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']],
  554. ['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']]
  555. );
  556. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  557. ->disableOriginalConstructor()->getMock();
  558. $this->groupManager
  559. ->expects($this->once())
  560. ->method('getSubAdmin')
  561. ->willReturn($subAdminManager);
  562. $subAdminManager
  563. ->expects($this->exactly(2))
  564. ->method('isSubAdminOfGroup')
  565. ->withConsecutive(
  566. [$loggedInUser, $existingGroup1],
  567. [$loggedInUser, $existingGroup2]
  568. )
  569. ->willReturn(true);
  570. $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup1', 'ExistingGroup2'])->getData());
  571. }
  572. /**
  573. * @expectedException \OCP\AppFramework\OCS\OCSException
  574. * @expectedExceptionCode 998
  575. * @expectedExceptionMessage The requested user could not be found
  576. */
  577. public function testGetUserTargetDoesNotExist() {
  578. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  579. ->disableOriginalConstructor()
  580. ->getMock();
  581. $this->userSession
  582. ->expects($this->once())
  583. ->method('getUser')
  584. ->will($this->returnValue($loggedInUser));
  585. $this->userManager
  586. ->expects($this->once())
  587. ->method('get')
  588. ->with('UserToGet')
  589. ->will($this->returnValue(null));
  590. $this->api->getUser('UserToGet');
  591. }
  592. public function testGetUserAsAdmin() {
  593. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  594. ->disableOriginalConstructor()
  595. ->getMock();
  596. $loggedInUser
  597. ->expects($this->once())
  598. ->method('getUID')
  599. ->will($this->returnValue('admin'));
  600. $targetUser = $this->getMockBuilder('OCP\IUser')
  601. ->disableOriginalConstructor()
  602. ->getMock();
  603. $targetUser->expects($this->once())
  604. ->method('getEMailAddress')
  605. ->willReturn('demo@owncloud.org');
  606. $this->userSession
  607. ->expects($this->once())
  608. ->method('getUser')
  609. ->will($this->returnValue($loggedInUser));
  610. $this->userManager
  611. ->expects($this->once())
  612. ->method('get')
  613. ->with('UserToGet')
  614. ->will($this->returnValue($targetUser));
  615. $this->groupManager
  616. ->expects($this->once())
  617. ->method('isAdmin')
  618. ->with('admin')
  619. ->will($this->returnValue(true));
  620. $this->config
  621. ->expects($this->at(0))
  622. ->method('getUserValue')
  623. ->with('UserToGet', 'core', 'enabled', 'true')
  624. ->will($this->returnValue('true'));
  625. $this->api
  626. ->expects($this->once())
  627. ->method('fillStorageInfo')
  628. ->with('UserToGet')
  629. ->will($this->returnValue(['DummyValue']));
  630. $targetUser
  631. ->expects($this->once())
  632. ->method('getDisplayName')
  633. ->will($this->returnValue('Demo User'));
  634. $expected = [
  635. 'enabled' => 'true',
  636. 'quota' => ['DummyValue'],
  637. 'email' => 'demo@owncloud.org',
  638. 'displayname' => 'Demo User',
  639. ];
  640. $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
  641. }
  642. public function testGetUserAsSubAdminAndUserIsAccessible() {
  643. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  644. ->disableOriginalConstructor()
  645. ->getMock();
  646. $loggedInUser
  647. ->expects($this->once())
  648. ->method('getUID')
  649. ->will($this->returnValue('subadmin'));
  650. $targetUser = $this->getMockBuilder('OCP\IUser')
  651. ->disableOriginalConstructor()
  652. ->getMock();
  653. $targetUser
  654. ->expects($this->once())
  655. ->method('getEMailAddress')
  656. ->willReturn('demo@owncloud.org');
  657. $this->userSession
  658. ->expects($this->once())
  659. ->method('getUser')
  660. ->will($this->returnValue($loggedInUser));
  661. $this->userManager
  662. ->expects($this->once())
  663. ->method('get')
  664. ->with('UserToGet')
  665. ->will($this->returnValue($targetUser));
  666. $this->groupManager
  667. ->expects($this->once())
  668. ->method('isAdmin')
  669. ->with('subadmin')
  670. ->will($this->returnValue(false));
  671. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  672. ->disableOriginalConstructor()
  673. ->getMock();
  674. $subAdminManager
  675. ->expects($this->once())
  676. ->method('isUserAccessible')
  677. ->with($loggedInUser, $targetUser)
  678. ->will($this->returnValue(true));
  679. $this->groupManager
  680. ->expects($this->once())
  681. ->method('getSubAdmin')
  682. ->will($this->returnValue($subAdminManager));
  683. $this->config
  684. ->expects($this->at(0))
  685. ->method('getUserValue')
  686. ->with('UserToGet', 'core', 'enabled', 'true')
  687. ->will($this->returnValue('true'));
  688. $this->api
  689. ->expects($this->once())
  690. ->method('fillStorageInfo')
  691. ->with('UserToGet')
  692. ->will($this->returnValue(['DummyValue']));
  693. $targetUser
  694. ->expects($this->once())
  695. ->method('getDisplayName')
  696. ->will($this->returnValue('Demo User'));
  697. $expected = [
  698. 'enabled' => 'true',
  699. 'quota' => ['DummyValue'],
  700. 'email' => 'demo@owncloud.org',
  701. 'displayname' => 'Demo User',
  702. ];
  703. $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
  704. }
  705. /**
  706. * @expectedException \OCP\AppFramework\OCS\OCSException
  707. * @expectedExceptionCode 997
  708. */
  709. public function testGetUserAsSubAdminAndUserIsNotAccessible() {
  710. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  711. ->disableOriginalConstructor()
  712. ->getMock();
  713. $loggedInUser
  714. ->expects($this->exactly(2))
  715. ->method('getUID')
  716. ->will($this->returnValue('subadmin'));
  717. $targetUser = $this->getMockBuilder('OCP\IUser')
  718. ->disableOriginalConstructor()
  719. ->getMock();
  720. $this->userSession
  721. ->expects($this->once())
  722. ->method('getUser')
  723. ->will($this->returnValue($loggedInUser));
  724. $this->userManager
  725. ->expects($this->once())
  726. ->method('get')
  727. ->with('UserToGet')
  728. ->will($this->returnValue($targetUser));
  729. $this->groupManager
  730. ->expects($this->once())
  731. ->method('isAdmin')
  732. ->with('subadmin')
  733. ->will($this->returnValue(false));
  734. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  735. ->disableOriginalConstructor()
  736. ->getMock();
  737. $subAdminManager
  738. ->expects($this->once())
  739. ->method('isUserAccessible')
  740. ->with($loggedInUser, $targetUser)
  741. ->will($this->returnValue(false));
  742. $this->groupManager
  743. ->expects($this->once())
  744. ->method('getSubAdmin')
  745. ->will($this->returnValue($subAdminManager));
  746. $this->api->getUser('UserToGet');
  747. }
  748. public function testGetUserAsSubAdminSelfLookup() {
  749. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  750. ->disableOriginalConstructor()
  751. ->getMock();
  752. $loggedInUser
  753. ->expects($this->exactly(2))
  754. ->method('getUID')
  755. ->will($this->returnValue('subadmin'));
  756. $targetUser = $this->getMockBuilder('OCP\IUser')
  757. ->disableOriginalConstructor()
  758. ->getMock();
  759. $this->userSession
  760. ->expects($this->once())
  761. ->method('getUser')
  762. ->will($this->returnValue($loggedInUser));
  763. $this->userManager
  764. ->expects($this->once())
  765. ->method('get')
  766. ->with('subadmin')
  767. ->will($this->returnValue($targetUser));
  768. $this->groupManager
  769. ->expects($this->once())
  770. ->method('isAdmin')
  771. ->with('subadmin')
  772. ->will($this->returnValue(false));
  773. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  774. ->disableOriginalConstructor()
  775. ->getMock();
  776. $subAdminManager
  777. ->expects($this->once())
  778. ->method('isUserAccessible')
  779. ->with($loggedInUser, $targetUser)
  780. ->will($this->returnValue(false));
  781. $this->groupManager
  782. ->expects($this->once())
  783. ->method('getSubAdmin')
  784. ->will($this->returnValue($subAdminManager));
  785. $this->api
  786. ->expects($this->once())
  787. ->method('fillStorageInfo')
  788. ->with('subadmin')
  789. ->will($this->returnValue(['DummyValue']));
  790. $targetUser
  791. ->expects($this->once())
  792. ->method('getDisplayName')
  793. ->will($this->returnValue('Subadmin User'));
  794. $targetUser
  795. ->expects($this->once())
  796. ->method('getEMailAddress')
  797. ->will($this->returnValue('subadmin@owncloud.org'));
  798. $expected = [
  799. 'quota' => ['DummyValue'],
  800. 'email' => 'subadmin@owncloud.org',
  801. 'displayname' => 'Subadmin User',
  802. ];
  803. $this->assertEquals($expected, $this->api->getUser('subadmin')->getData());
  804. }
  805. public function testEditUserRegularUserSelfEditChangeDisplayName() {
  806. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  807. ->disableOriginalConstructor()
  808. ->getMock();
  809. $loggedInUser
  810. ->expects($this->any())
  811. ->method('getUID')
  812. ->will($this->returnValue('UserToEdit'));
  813. $targetUser = $this->getMockBuilder('OCP\IUser')
  814. ->disableOriginalConstructor()
  815. ->getMock();
  816. $this->userSession
  817. ->expects($this->once())
  818. ->method('getUser')
  819. ->will($this->returnValue($loggedInUser));
  820. $this->userManager
  821. ->expects($this->once())
  822. ->method('get')
  823. ->with('UserToEdit')
  824. ->will($this->returnValue($targetUser));
  825. $targetUser
  826. ->expects($this->once())
  827. ->method('setDisplayName')
  828. ->with('NewDisplayName');
  829. $this->assertEquals([], $this->api->editUser('UserToEdit', 'display', 'NewDisplayName')->getData());
  830. }
  831. public function testEditUserRegularUserSelfEditChangeEmailValid() {
  832. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  833. ->disableOriginalConstructor()
  834. ->getMock();
  835. $loggedInUser
  836. ->expects($this->any())
  837. ->method('getUID')
  838. ->will($this->returnValue('UserToEdit'));
  839. $targetUser = $this->getMockBuilder('OCP\IUser')
  840. ->disableOriginalConstructor()
  841. ->getMock();
  842. $this->userSession
  843. ->expects($this->once())
  844. ->method('getUser')
  845. ->will($this->returnValue($loggedInUser));
  846. $this->userManager
  847. ->expects($this->once())
  848. ->method('get')
  849. ->with('UserToEdit')
  850. ->will($this->returnValue($targetUser));
  851. $targetUser
  852. ->expects($this->once())
  853. ->method('setEMailAddress')
  854. ->with('demo@owncloud.org');
  855. $this->assertEquals([], $this->api->editUser('UserToEdit', 'email', 'demo@owncloud.org')->getData());
  856. }
  857. /**
  858. * @expectedException \OCP\AppFramework\OCS\OCSException
  859. * @expectedExceptionCode 102
  860. */
  861. public function testEditUserRegularUserSelfEditChangeEmailInvalid() {
  862. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  863. ->disableOriginalConstructor()
  864. ->getMock();
  865. $loggedInUser
  866. ->expects($this->any())
  867. ->method('getUID')
  868. ->will($this->returnValue('UserToEdit'));
  869. $targetUser = $this->getMockBuilder('OCP\IUser')
  870. ->disableOriginalConstructor()
  871. ->getMock();
  872. $this->userSession
  873. ->expects($this->once())
  874. ->method('getUser')
  875. ->will($this->returnValue($loggedInUser));
  876. $this->userManager
  877. ->expects($this->once())
  878. ->method('get')
  879. ->with('UserToEdit')
  880. ->will($this->returnValue($targetUser));
  881. $this->api->editUser('UserToEdit', 'email', 'demo.org');
  882. }
  883. public function testEditUserRegularUserSelfEditChangePassword() {
  884. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  885. ->disableOriginalConstructor()
  886. ->getMock();
  887. $loggedInUser
  888. ->expects($this->any())
  889. ->method('getUID')
  890. ->will($this->returnValue('UserToEdit'));
  891. $targetUser = $this->getMockBuilder('OCP\IUser')
  892. ->disableOriginalConstructor()
  893. ->getMock();
  894. $this->userSession
  895. ->expects($this->once())
  896. ->method('getUser')
  897. ->will($this->returnValue($loggedInUser));
  898. $this->userManager
  899. ->expects($this->once())
  900. ->method('get')
  901. ->with('UserToEdit')
  902. ->will($this->returnValue($targetUser));
  903. $targetUser
  904. ->expects($this->once())
  905. ->method('setPassword')
  906. ->with('NewPassword');
  907. $this->assertEquals([], $this->api->editUser('UserToEdit', 'password', 'NewPassword')->getData());
  908. }
  909. /**
  910. * @expectedException \OCP\AppFramework\OCS\OCSException
  911. * @expectedExceptionCode 997
  912. */
  913. public function testEditUserRegularUserSelfEditChangeQuota() {
  914. $loggedInUser = $this->getMockBuilder('OCP\IUser')
  915. ->disableOriginalConstructor()
  916. ->getMock();
  917. $loggedInUser
  918. ->expects($this->any())
  919. ->method('getUID')
  920. ->will($this->returnValue('UserToEdit'));
  921. $targetUser = $this->getMockBuilder('OCP\IUser')
  922. ->disableOriginalConstructor()
  923. ->getMock();
  924. $this->userSession
  925. ->expects($this->once())
  926. ->method('getUser')
  927. ->will($this->returnValue($loggedInUser));
  928. $this->userManager
  929. ->expects($this->once())
  930. ->method('get')
  931. ->with('UserToEdit')
  932. ->will($this->returnValue($targetUser));
  933. $this->api->editUser('UserToEdit', 'quota', 'NewQuota');
  934. }
  935. public function testEditUserAdminUserSelfEditChangeValidQuota() {
  936. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();;
  937. $loggedInUser
  938. ->expects($this->any())
  939. ->method('getUID')
  940. ->will($this->returnValue('UserToEdit'));
  941. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  942. $targetUser->expects($this->once())
  943. ->method('setQuota')
  944. ->with('2.9 MB');
  945. $this->userSession
  946. ->expects($this->once())
  947. ->method('getUser')
  948. ->will($this->returnValue($loggedInUser));
  949. $this->userManager
  950. ->expects($this->once())
  951. ->method('get')
  952. ->with('UserToEdit')
  953. ->will($this->returnValue($targetUser));
  954. $this->groupManager
  955. ->expects($this->once())
  956. ->method('isAdmin')
  957. ->with('UserToEdit')
  958. ->will($this->returnValue(true));
  959. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  960. }
  961. /**
  962. * @expectedException \OCP\AppFramework\OCS\OCSException
  963. * @expectedExceptionCode 103
  964. * @expectedExceptionMessage Invalid quota value ABC
  965. */
  966. public function testEditUserAdminUserSelfEditChangeInvalidQuota() {
  967. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  968. $loggedInUser
  969. ->expects($this->any())
  970. ->method('getUID')
  971. ->will($this->returnValue('UserToEdit'));
  972. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  973. $this->userSession
  974. ->expects($this->once())
  975. ->method('getUser')
  976. ->will($this->returnValue($loggedInUser));
  977. $this->userManager
  978. ->expects($this->once())
  979. ->method('get')
  980. ->with('UserToEdit')
  981. ->will($this->returnValue($targetUser));
  982. $this->groupManager
  983. ->expects($this->once())
  984. ->method('isAdmin')
  985. ->with('UserToEdit')
  986. ->will($this->returnValue(true));
  987. $this->api->editUser('UserToEdit', 'quota', 'ABC');
  988. }
  989. public function testEditUserAdminUserEditChangeValidQuota() {
  990. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  991. $loggedInUser
  992. ->expects($this->any())
  993. ->method('getUID')
  994. ->will($this->returnValue('admin'));
  995. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  996. $targetUser->expects($this->once())
  997. ->method('setQuota')
  998. ->with('2.9 MB');
  999. $this->userSession
  1000. ->expects($this->once())
  1001. ->method('getUser')
  1002. ->will($this->returnValue($loggedInUser));
  1003. $this->userManager
  1004. ->expects($this->once())
  1005. ->method('get')
  1006. ->with('UserToEdit')
  1007. ->will($this->returnValue($targetUser));
  1008. $this->groupManager
  1009. ->expects($this->once())
  1010. ->method('isAdmin')
  1011. ->with('admin')
  1012. ->will($this->returnValue(true));
  1013. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1014. ->disableOriginalConstructor()
  1015. ->getMock();
  1016. $this->groupManager
  1017. ->expects($this->once())
  1018. ->method('getSubAdmin')
  1019. ->will($this->returnValue($subAdminManager));
  1020. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  1021. }
  1022. public function testEditUserSubadminUserAccessible() {
  1023. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1024. $loggedInUser
  1025. ->expects($this->any())
  1026. ->method('getUID')
  1027. ->will($this->returnValue('subadmin'));
  1028. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1029. $targetUser->expects($this->once())
  1030. ->method('setQuota')
  1031. ->with('2.9 MB');
  1032. $this->userSession
  1033. ->expects($this->once())
  1034. ->method('getUser')
  1035. ->will($this->returnValue($loggedInUser));
  1036. $this->userManager
  1037. ->expects($this->once())
  1038. ->method('get')
  1039. ->with('UserToEdit')
  1040. ->will($this->returnValue($targetUser));
  1041. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1042. ->disableOriginalConstructor()
  1043. ->getMock();
  1044. $subAdminManager
  1045. ->expects($this->once())
  1046. ->method('isUserAccessible')
  1047. ->with($loggedInUser, $targetUser)
  1048. ->will($this->returnValue(true));
  1049. $this->groupManager
  1050. ->expects($this->once())
  1051. ->method('getSubAdmin')
  1052. ->will($this->returnValue($subAdminManager));
  1053. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  1054. }
  1055. /**
  1056. * @expectedException \OCP\AppFramework\OCS\OCSException
  1057. * @expectedExceptionCode 997
  1058. */
  1059. public function testEditUserSubadminUserInaccessible() {
  1060. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1061. $loggedInUser
  1062. ->expects($this->any())
  1063. ->method('getUID')
  1064. ->will($this->returnValue('subadmin'));
  1065. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1066. $this->userSession
  1067. ->expects($this->once())
  1068. ->method('getUser')
  1069. ->will($this->returnValue($loggedInUser));
  1070. $this->userManager
  1071. ->expects($this->once())
  1072. ->method('get')
  1073. ->with('UserToEdit')
  1074. ->will($this->returnValue($targetUser));
  1075. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1076. ->disableOriginalConstructor()
  1077. ->getMock();
  1078. $subAdminManager
  1079. ->expects($this->once())
  1080. ->method('isUserAccessible')
  1081. ->with($loggedInUser, $targetUser)
  1082. ->will($this->returnValue(false));
  1083. $this->groupManager
  1084. ->expects($this->once())
  1085. ->method('getSubAdmin')
  1086. ->will($this->returnValue($subAdminManager));
  1087. $this->api->editUser('UserToEdit', 'quota', 'value');
  1088. }
  1089. /**
  1090. * @expectedException \OCP\AppFramework\OCS\OCSException
  1091. * @expectedExceptionCode 101
  1092. */
  1093. public function testDeleteUserNotExistingUser() {
  1094. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1095. $loggedInUser
  1096. ->expects($this->any())
  1097. ->method('getUID')
  1098. ->will($this->returnValue('UserToEdit'));
  1099. $this->userSession
  1100. ->expects($this->once())
  1101. ->method('getUser')
  1102. ->will($this->returnValue($loggedInUser));
  1103. $this->userManager
  1104. ->expects($this->once())
  1105. ->method('get')
  1106. ->with('UserToDelete')
  1107. ->will($this->returnValue(null));
  1108. $this->api->deleteUser('UserToDelete');
  1109. }
  1110. /**
  1111. * @expectedException \OCP\AppFramework\OCS\OCSException
  1112. * @expectedExceptionCode 101
  1113. */
  1114. public function testDeleteUserSelf() {
  1115. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1116. $loggedInUser
  1117. ->expects($this->any())
  1118. ->method('getUID')
  1119. ->will($this->returnValue('UserToDelete'));
  1120. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1121. $targetUser
  1122. ->expects($this->once())
  1123. ->method('getUID')
  1124. ->will($this->returnValue('UserToDelete'));
  1125. $this->userSession
  1126. ->expects($this->once())
  1127. ->method('getUser')
  1128. ->will($this->returnValue($loggedInUser));
  1129. $this->userManager
  1130. ->expects($this->once())
  1131. ->method('get')
  1132. ->with('UserToDelete')
  1133. ->will($this->returnValue($targetUser));
  1134. $this->api->deleteUser('UserToDelete');
  1135. }
  1136. public function testDeleteSuccessfulUserAsAdmin() {
  1137. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1138. $loggedInUser
  1139. ->expects($this->any())
  1140. ->method('getUID')
  1141. ->will($this->returnValue('admin'));
  1142. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1143. $targetUser
  1144. ->expects($this->once())
  1145. ->method('getUID')
  1146. ->will($this->returnValue('UserToDelete'));
  1147. $this->userSession
  1148. ->expects($this->once())
  1149. ->method('getUser')
  1150. ->will($this->returnValue($loggedInUser));
  1151. $this->userManager
  1152. ->expects($this->once())
  1153. ->method('get')
  1154. ->with('UserToDelete')
  1155. ->will($this->returnValue($targetUser));
  1156. $this->groupManager
  1157. ->expects($this->once())
  1158. ->method('isAdmin')
  1159. ->with('admin')
  1160. ->will($this->returnValue(true));
  1161. $targetUser
  1162. ->expects($this->once())
  1163. ->method('delete')
  1164. ->will($this->returnValue(true));
  1165. $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData());
  1166. }
  1167. /**
  1168. * @expectedException \OCP\AppFramework\OCS\OCSException
  1169. * @expectedExceptionCode 101
  1170. */
  1171. public function testDeleteUnsuccessfulUserAsAdmin() {
  1172. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1173. $loggedInUser
  1174. ->expects($this->any())
  1175. ->method('getUID')
  1176. ->will($this->returnValue('admin'));
  1177. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1178. $targetUser
  1179. ->expects($this->once())
  1180. ->method('getUID')
  1181. ->will($this->returnValue('UserToDelete'));
  1182. $this->userSession
  1183. ->expects($this->once())
  1184. ->method('getUser')
  1185. ->will($this->returnValue($loggedInUser));
  1186. $this->userManager
  1187. ->expects($this->once())
  1188. ->method('get')
  1189. ->with('UserToDelete')
  1190. ->will($this->returnValue($targetUser));
  1191. $this->groupManager
  1192. ->expects($this->once())
  1193. ->method('isAdmin')
  1194. ->with('admin')
  1195. ->will($this->returnValue(true));
  1196. $targetUser
  1197. ->expects($this->once())
  1198. ->method('delete')
  1199. ->will($this->returnValue(false));
  1200. $this->api->deleteUser('UserToDelete');
  1201. }
  1202. public function testDeleteSuccessfulUserAsSubadmin() {
  1203. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1204. $loggedInUser
  1205. ->expects($this->any())
  1206. ->method('getUID')
  1207. ->will($this->returnValue('subadmin'));
  1208. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1209. $targetUser
  1210. ->expects($this->once())
  1211. ->method('getUID')
  1212. ->will($this->returnValue('UserToDelete'));
  1213. $this->userSession
  1214. ->expects($this->once())
  1215. ->method('getUser')
  1216. ->will($this->returnValue($loggedInUser));
  1217. $this->userManager
  1218. ->expects($this->once())
  1219. ->method('get')
  1220. ->with('UserToDelete')
  1221. ->will($this->returnValue($targetUser));
  1222. $this->groupManager
  1223. ->expects($this->once())
  1224. ->method('isAdmin')
  1225. ->with('subadmin')
  1226. ->will($this->returnValue(false));
  1227. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1228. ->disableOriginalConstructor()->getMock();
  1229. $subAdminManager
  1230. ->expects($this->once())
  1231. ->method('isUserAccessible')
  1232. ->with($loggedInUser, $targetUser)
  1233. ->will($this->returnValue(true));
  1234. $this->groupManager
  1235. ->expects($this->once())
  1236. ->method('getSubAdmin')
  1237. ->will($this->returnValue($subAdminManager));
  1238. $targetUser
  1239. ->expects($this->once())
  1240. ->method('delete')
  1241. ->will($this->returnValue(true));
  1242. $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData());
  1243. }
  1244. /**
  1245. * @expectedException \OCP\AppFramework\OCS\OCSException
  1246. * @expectedExceptionCode 101
  1247. */
  1248. public function testDeleteUnsuccessfulUserAsSubadmin() {
  1249. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1250. $loggedInUser
  1251. ->expects($this->any())
  1252. ->method('getUID')
  1253. ->will($this->returnValue('subadmin'));
  1254. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1255. $targetUser
  1256. ->expects($this->once())
  1257. ->method('getUID')
  1258. ->will($this->returnValue('UserToDelete'));
  1259. $this->userSession
  1260. ->expects($this->once())
  1261. ->method('getUser')
  1262. ->will($this->returnValue($loggedInUser));
  1263. $this->userManager
  1264. ->expects($this->once())
  1265. ->method('get')
  1266. ->with('UserToDelete')
  1267. ->will($this->returnValue($targetUser));
  1268. $this->groupManager
  1269. ->expects($this->once())
  1270. ->method('isAdmin')
  1271. ->with('subadmin')
  1272. ->will($this->returnValue(false));
  1273. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1274. ->disableOriginalConstructor()->getMock();
  1275. $subAdminManager
  1276. ->expects($this->once())
  1277. ->method('isUserAccessible')
  1278. ->with($loggedInUser, $targetUser)
  1279. ->will($this->returnValue(true));
  1280. $this->groupManager
  1281. ->expects($this->once())
  1282. ->method('getSubAdmin')
  1283. ->will($this->returnValue($subAdminManager));
  1284. $targetUser
  1285. ->expects($this->once())
  1286. ->method('delete')
  1287. ->will($this->returnValue(false));
  1288. $this->api->deleteUser('UserToDelete');
  1289. }
  1290. /**
  1291. * @expectedException \OCP\AppFramework\OCS\OCSException
  1292. * @expectedExceptionCode 997
  1293. */
  1294. public function testDeleteUserAsSubAdminAndUserIsNotAccessible() {
  1295. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1296. $loggedInUser
  1297. ->expects($this->any())
  1298. ->method('getUID')
  1299. ->will($this->returnValue('subadmin'));
  1300. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1301. $targetUser
  1302. ->expects($this->once())
  1303. ->method('getUID')
  1304. ->will($this->returnValue('UserToDelete'));
  1305. $this->userSession
  1306. ->expects($this->once())
  1307. ->method('getUser')
  1308. ->will($this->returnValue($loggedInUser));
  1309. $this->userManager
  1310. ->expects($this->once())
  1311. ->method('get')
  1312. ->with('UserToDelete')
  1313. ->will($this->returnValue($targetUser));
  1314. $this->groupManager
  1315. ->expects($this->once())
  1316. ->method('isAdmin')
  1317. ->with('subadmin')
  1318. ->will($this->returnValue(false));
  1319. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1320. ->disableOriginalConstructor()->getMock();
  1321. $subAdminManager
  1322. ->expects($this->once())
  1323. ->method('isUserAccessible')
  1324. ->with($loggedInUser, $targetUser)
  1325. ->will($this->returnValue(false));
  1326. $this->groupManager
  1327. ->expects($this->once())
  1328. ->method('getSubAdmin')
  1329. ->will($this->returnValue($subAdminManager));
  1330. $this->api->deleteUser('UserToDelete');
  1331. }
  1332. /**
  1333. * @expectedException \OCP\AppFramework\OCS\OCSException
  1334. * @expectedExceptionCode 998
  1335. */
  1336. public function testGetUsersGroupsTargetUserNotExisting() {
  1337. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1338. $this->userSession
  1339. ->expects($this->once())
  1340. ->method('getUser')
  1341. ->will($this->returnValue($loggedInUser));
  1342. $this->api->getUsersGroups('UserToLookup');
  1343. }
  1344. public function testGetUsersGroupsSelfTargetted() {
  1345. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1346. $loggedInUser
  1347. ->expects($this->once())
  1348. ->method('getUID')
  1349. ->will($this->returnValue('UserToLookup'));
  1350. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1351. $targetUser
  1352. ->expects($this->once())
  1353. ->method('getUID')
  1354. ->will($this->returnValue('UserToLookup'));
  1355. $this->userSession
  1356. ->expects($this->once())
  1357. ->method('getUser')
  1358. ->will($this->returnValue($loggedInUser));
  1359. $this->userManager
  1360. ->expects($this->once())
  1361. ->method('get')
  1362. ->with('UserToLookup')
  1363. ->will($this->returnValue($targetUser));
  1364. $this->groupManager
  1365. ->expects($this->once())
  1366. ->method('getUserGroupIds')
  1367. ->with($targetUser)
  1368. ->will($this->returnValue(['DummyValue']));
  1369. $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData());
  1370. }
  1371. public function testGetUsersGroupsForAdminUser() {
  1372. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1373. $loggedInUser
  1374. ->expects($this->exactly(2))
  1375. ->method('getUID')
  1376. ->will($this->returnValue('admin'));
  1377. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1378. $targetUser
  1379. ->expects($this->once())
  1380. ->method('getUID')
  1381. ->will($this->returnValue('UserToLookup'));
  1382. $this->userSession
  1383. ->expects($this->once())
  1384. ->method('getUser')
  1385. ->will($this->returnValue($loggedInUser));
  1386. $this->userManager
  1387. ->expects($this->once())
  1388. ->method('get')
  1389. ->with('UserToLookup')
  1390. ->will($this->returnValue($targetUser));
  1391. $this->groupManager
  1392. ->expects($this->once())
  1393. ->method('getUserGroupIds')
  1394. ->with($targetUser)
  1395. ->will($this->returnValue(['DummyValue']));
  1396. $this->groupManager
  1397. ->expects($this->once())
  1398. ->method('isAdmin')
  1399. ->with('admin')
  1400. ->will($this->returnValue(true));
  1401. $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData());
  1402. }
  1403. public function testGetUsersGroupsForSubAdminUserAndUserIsAccessible() {
  1404. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1405. $loggedInUser
  1406. ->expects($this->exactly(2))
  1407. ->method('getUID')
  1408. ->will($this->returnValue('subadmin'));
  1409. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1410. $targetUser
  1411. ->expects($this->once())
  1412. ->method('getUID')
  1413. ->will($this->returnValue('UserToLookup'));
  1414. $this->userSession
  1415. ->expects($this->once())
  1416. ->method('getUser')
  1417. ->will($this->returnValue($loggedInUser));
  1418. $this->userManager
  1419. ->expects($this->once())
  1420. ->method('get')
  1421. ->with('UserToLookup')
  1422. ->will($this->returnValue($targetUser));
  1423. $this->groupManager
  1424. ->expects($this->once())
  1425. ->method('isAdmin')
  1426. ->with('subadmin')
  1427. ->will($this->returnValue(false));
  1428. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1429. ->disableOriginalConstructor()->getMock();
  1430. $subAdminManager
  1431. ->expects($this->once())
  1432. ->method('isUserAccessible')
  1433. ->with($loggedInUser, $targetUser)
  1434. ->will($this->returnValue(true));
  1435. $this->groupManager
  1436. ->expects($this->once())
  1437. ->method('getSubAdmin')
  1438. ->will($this->returnValue($subAdminManager));
  1439. $group1 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1440. $group1
  1441. ->expects($this->any())
  1442. ->method('getGID')
  1443. ->will($this->returnValue('Group1'));
  1444. $group2 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1445. $group2
  1446. ->expects($this->any())
  1447. ->method('getGID')
  1448. ->will($this->returnValue('Group2'));
  1449. $subAdminManager
  1450. ->expects($this->once())
  1451. ->method('getSubAdminsGroups')
  1452. ->with($loggedInUser)
  1453. ->will($this->returnValue([$group1, $group2]));
  1454. $this->groupManager
  1455. ->expects($this->any())
  1456. ->method('getUserGroupIds')
  1457. ->with($targetUser)
  1458. ->will($this->returnValue(['Group1']));
  1459. $this->assertEquals(['groups' => ['Group1']], $this->api->getUsersGroups('UserToLookup')->getData());
  1460. }
  1461. /**
  1462. * @expectedException \OCP\AppFramework\OCS\OCSException
  1463. * @expectedExceptionCode 997
  1464. */
  1465. public function testGetUsersGroupsForSubAdminUserAndUserIsInaccessible() {
  1466. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1467. $loggedInUser
  1468. ->expects($this->exactly(2))
  1469. ->method('getUID')
  1470. ->will($this->returnValue('subadmin'));
  1471. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1472. $targetUser
  1473. ->expects($this->once())
  1474. ->method('getUID')
  1475. ->will($this->returnValue('UserToLookup'));
  1476. $this->userSession
  1477. ->expects($this->once())
  1478. ->method('getUser')
  1479. ->will($this->returnValue($loggedInUser));
  1480. $this->userManager
  1481. ->expects($this->once())
  1482. ->method('get')
  1483. ->with('UserToLookup')
  1484. ->will($this->returnValue($targetUser));
  1485. $this->groupManager
  1486. ->expects($this->once())
  1487. ->method('isAdmin')
  1488. ->with('subadmin')
  1489. ->will($this->returnValue(false));
  1490. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1491. ->disableOriginalConstructor()->getMock();
  1492. $subAdminManager
  1493. ->expects($this->once())
  1494. ->method('isUserAccessible')
  1495. ->with($loggedInUser, $targetUser)
  1496. ->will($this->returnValue(false));
  1497. $this->groupManager
  1498. ->expects($this->once())
  1499. ->method('getSubAdmin')
  1500. ->will($this->returnValue($subAdminManager));
  1501. $this->groupManager
  1502. ->expects($this->any())
  1503. ->method('getUserGroupIds')
  1504. ->with($targetUser)
  1505. ->will($this->returnValue(['Group1']));
  1506. $this->api->getUsersGroups('UserToLookup');
  1507. }
  1508. /**
  1509. * @expectedException \OCP\AppFramework\OCS\OCSException
  1510. * @expectedExceptionCode 102
  1511. */
  1512. public function testAddToGroupWithTargetGroupNotExisting() {
  1513. $this->groupManager
  1514. ->expects($this->once())
  1515. ->method('get')
  1516. ->with('GroupToAddTo')
  1517. ->will($this->returnValue(null));
  1518. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  1519. }
  1520. /**
  1521. * @expectedException \OCP\AppFramework\OCS\OCSException
  1522. * @expectedExceptionCode 101
  1523. */
  1524. public function testAddToGroupWithNoGroupSpecified() {
  1525. $this->api->addToGroup('TargetUser');
  1526. }
  1527. /**
  1528. * @expectedException \OCP\AppFramework\OCS\OCSException
  1529. * @expectedExceptionCode 103
  1530. */
  1531. public function testAddToGroupWithTargetUserNotExisting() {
  1532. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1533. $this->groupManager
  1534. ->expects($this->once())
  1535. ->method('get')
  1536. ->with('GroupToAddTo')
  1537. ->will($this->returnValue($targetGroup));
  1538. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  1539. }
  1540. /**
  1541. * @expectedException \OCP\AppFramework\OCS\OCSException
  1542. * @expectedExceptionCode 101
  1543. */
  1544. public function testRemoveFromGroupWithNoTargetGroup() {
  1545. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1546. $this->userSession
  1547. ->expects($this->once())
  1548. ->method('getUser')
  1549. ->will($this->returnValue($loggedInUser));
  1550. $this->api->removeFromGroup('TargetUser', null);
  1551. }
  1552. /**
  1553. * @expectedException \OCP\AppFramework\OCS\OCSException
  1554. * @expectedExceptionCode 102
  1555. */
  1556. public function testRemoveFromGroupWithNotExistingTargetGroup() {
  1557. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1558. $this->userSession
  1559. ->expects($this->once())
  1560. ->method('getUser')
  1561. ->will($this->returnValue($loggedInUser));
  1562. $this->groupManager
  1563. ->expects($this->once())
  1564. ->method('get')
  1565. ->with('TargetGroup')
  1566. ->will($this->returnValue(null));
  1567. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  1568. }
  1569. /**
  1570. * @expectedException \OCP\AppFramework\OCS\OCSException
  1571. * @expectedExceptionCode 103
  1572. */
  1573. public function testRemoveFromGroupWithNotExistingTargetUser() {
  1574. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1575. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1576. $this->userSession
  1577. ->expects($this->once())
  1578. ->method('getUser')
  1579. ->will($this->returnValue($loggedInUser));
  1580. $this->groupManager
  1581. ->expects($this->once())
  1582. ->method('get')
  1583. ->with('TargetGroup')
  1584. ->will($this->returnValue($targetGroup));
  1585. $this->userManager
  1586. ->expects($this->once())
  1587. ->method('get')
  1588. ->with('TargetUser')
  1589. ->will($this->returnValue(null));
  1590. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  1591. }
  1592. /**
  1593. * @expectedException \OCP\AppFramework\OCS\OCSException
  1594. * @expectedExceptionCode 104
  1595. */
  1596. public function testRemoveFromGroupWithoutPermission() {
  1597. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1598. $loggedInUser
  1599. ->expects($this->once())
  1600. ->method('getUID')
  1601. ->will($this->returnValue('unauthorizedUser'));
  1602. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1603. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1604. $this->userSession
  1605. ->expects($this->once())
  1606. ->method('getUser')
  1607. ->will($this->returnValue($loggedInUser));
  1608. $this->groupManager
  1609. ->expects($this->once())
  1610. ->method('get')
  1611. ->with('TargetGroup')
  1612. ->will($this->returnValue($targetGroup));
  1613. $this->userManager
  1614. ->expects($this->once())
  1615. ->method('get')
  1616. ->with('TargetUser')
  1617. ->will($this->returnValue($targetUser));
  1618. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1619. ->disableOriginalConstructor()->getMock();
  1620. $this->groupManager
  1621. ->expects($this->once())
  1622. ->method('getSubAdmin')
  1623. ->will($this->returnValue($subAdminManager));
  1624. $this->groupManager
  1625. ->expects($this->once())
  1626. ->method('isAdmin')
  1627. ->with('unauthorizedUser')
  1628. ->will($this->returnValue(false));
  1629. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  1630. }
  1631. /**
  1632. * @expectedException \OCP\AppFramework\OCS\OCSException
  1633. * @expectedExceptionCode 105
  1634. * @expectedExceptionMessage Cannot remove yourself from the admin group
  1635. */
  1636. public function testRemoveFromGroupAsAdminFromAdmin() {
  1637. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1638. $loggedInUser
  1639. ->expects($this->any())
  1640. ->method('getUID')
  1641. ->will($this->returnValue('admin'));
  1642. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1643. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1644. $targetGroup
  1645. ->expects($this->once())
  1646. ->method('getGID')
  1647. ->will($this->returnValue('admin'));
  1648. $this->userSession
  1649. ->expects($this->once())
  1650. ->method('getUser')
  1651. ->will($this->returnValue($loggedInUser));
  1652. $this->groupManager
  1653. ->expects($this->once())
  1654. ->method('get')
  1655. ->with('admin')
  1656. ->will($this->returnValue($targetGroup));
  1657. $this->userManager
  1658. ->expects($this->once())
  1659. ->method('get')
  1660. ->with('admin')
  1661. ->will($this->returnValue($targetUser));
  1662. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1663. ->disableOriginalConstructor()->getMock();
  1664. $this->groupManager
  1665. ->expects($this->once())
  1666. ->method('getSubAdmin')
  1667. ->will($this->returnValue($subAdminManager));
  1668. $this->groupManager
  1669. ->expects($this->any())
  1670. ->method('isAdmin')
  1671. ->with('admin')
  1672. ->will($this->returnValue(true));
  1673. $this->api->removeFromGroup('admin', 'admin');
  1674. }
  1675. /**
  1676. * @expectedException \OCP\AppFramework\OCS\OCSException
  1677. * @expectedExceptionCode 105
  1678. * @expectedExceptionMessage Cannot remove yourself from this group as you are a SubAdmin
  1679. */
  1680. public function testRemoveFromGroupAsSubAdminFromSubAdmin() {
  1681. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1682. $loggedInUser
  1683. ->expects($this->any())
  1684. ->method('getUID')
  1685. ->will($this->returnValue('subadmin'));
  1686. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1687. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1688. $targetGroup
  1689. ->expects($this->any())
  1690. ->method('getGID')
  1691. ->will($this->returnValue('subadmin'));
  1692. $this->userSession
  1693. ->expects($this->once())
  1694. ->method('getUser')
  1695. ->will($this->returnValue($loggedInUser));
  1696. $this->groupManager
  1697. ->expects($this->once())
  1698. ->method('get')
  1699. ->with('subadmin')
  1700. ->will($this->returnValue($targetGroup));
  1701. $this->userManager
  1702. ->expects($this->once())
  1703. ->method('get')
  1704. ->with('subadmin')
  1705. ->will($this->returnValue($targetUser));
  1706. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1707. ->disableOriginalConstructor()->getMock();
  1708. $subAdminManager
  1709. ->expects($this->once())
  1710. ->method('isSubAdminofGroup')
  1711. ->with($loggedInUser, $targetGroup)
  1712. ->will($this->returnValue(true));
  1713. $subAdminManager
  1714. ->expects($this->once())
  1715. ->method('getSubAdminsGroups')
  1716. ->with($loggedInUser)
  1717. ->will($this->returnValue([$targetGroup]));
  1718. $this->groupManager
  1719. ->expects($this->once())
  1720. ->method('getSubAdmin')
  1721. ->will($this->returnValue($subAdminManager));
  1722. $this->groupManager
  1723. ->expects($this->any())
  1724. ->method('isAdmin')
  1725. ->with('subadmin')
  1726. ->will($this->returnValue(false));
  1727. $this->api->removeFromGroup('subadmin', 'subadmin');
  1728. }
  1729. public function testRemoveFromGroupSuccessful() {
  1730. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1731. $loggedInUser
  1732. ->expects($this->any())
  1733. ->method('getUID')
  1734. ->will($this->returnValue('admin'));
  1735. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1736. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1737. $this->userSession
  1738. ->expects($this->once())
  1739. ->method('getUser')
  1740. ->will($this->returnValue($loggedInUser));
  1741. $this->groupManager
  1742. ->expects($this->once())
  1743. ->method('get')
  1744. ->with('admin')
  1745. ->will($this->returnValue($targetGroup));
  1746. $this->userManager
  1747. ->expects($this->once())
  1748. ->method('get')
  1749. ->with('AnotherUser')
  1750. ->will($this->returnValue($targetUser));
  1751. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1752. ->disableOriginalConstructor()->getMock();
  1753. $this->groupManager
  1754. ->expects($this->once())
  1755. ->method('getSubAdmin')
  1756. ->will($this->returnValue($subAdminManager));
  1757. $this->groupManager
  1758. ->expects($this->any())
  1759. ->method('isAdmin')
  1760. ->with('admin')
  1761. ->will($this->returnValue(true));
  1762. $targetGroup
  1763. ->expects($this->once())
  1764. ->method('removeUser')
  1765. ->with($targetUser);
  1766. $this->assertEquals([], $this->api->removeFromGroup('AnotherUser', 'admin')->getData());
  1767. }
  1768. /**
  1769. * @expectedException \OCP\AppFramework\OCS\OCSException
  1770. * @expectedExceptionCode 101
  1771. * @expectedExceptionMessage User does not exist
  1772. */
  1773. public function testAddSubAdminWithNotExistingTargetUser() {
  1774. $this->userManager
  1775. ->expects($this->once())
  1776. ->method('get')
  1777. ->with('NotExistingUser')
  1778. ->will($this->returnValue(null));
  1779. $this->api->addSubAdmin('NotExistingUser', null);
  1780. }
  1781. /**
  1782. * @expectedException \OCP\AppFramework\OCS\OCSException
  1783. * @expectedExceptionCode 102
  1784. * @expectedExceptionMessage Group:NotExistingGroup does not exist
  1785. */
  1786. public function testAddSubAdminWithNotExistingTargetGroup() {
  1787. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1788. $this->userManager
  1789. ->expects($this->once())
  1790. ->method('get')
  1791. ->with('ExistingUser')
  1792. ->will($this->returnValue($targetUser));
  1793. $this->groupManager
  1794. ->expects($this->once())
  1795. ->method('get')
  1796. ->with('NotExistingGroup')
  1797. ->will($this->returnValue(null));
  1798. $this->api->addSubAdmin('ExistingUser', 'NotExistingGroup');
  1799. }
  1800. /**
  1801. * @expectedException \OCP\AppFramework\OCS\OCSException
  1802. * @expectedExceptionCode 103
  1803. * @expectedExceptionMessage Cannot create subadmins for admin group
  1804. */
  1805. public function testAddSubAdminToAdminGroup() {
  1806. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1807. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1808. $this->userManager
  1809. ->expects($this->once())
  1810. ->method('get')
  1811. ->with('ExistingUser')
  1812. ->will($this->returnValue($targetUser));
  1813. $this->groupManager
  1814. ->expects($this->once())
  1815. ->method('get')
  1816. ->with('ADmiN')
  1817. ->will($this->returnValue($targetGroup));
  1818. $this->api->addSubAdmin('ExistingUser', 'ADmiN');
  1819. }
  1820. public function testAddSubAdminTwice() {
  1821. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1822. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1823. $this->userManager
  1824. ->expects($this->once())
  1825. ->method('get')
  1826. ->with('ExistingUser')
  1827. ->will($this->returnValue($targetUser));
  1828. $this->groupManager
  1829. ->expects($this->once())
  1830. ->method('get')
  1831. ->with('TargetGroup')
  1832. ->will($this->returnValue($targetGroup));
  1833. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1834. ->disableOriginalConstructor()->getMock();
  1835. $subAdminManager
  1836. ->expects($this->once())
  1837. ->method('isSubAdminOfGroup')
  1838. ->with($targetUser, $targetGroup)
  1839. ->will($this->returnValue(true));
  1840. $this->groupManager
  1841. ->expects($this->once())
  1842. ->method('getSubAdmin')
  1843. ->will($this->returnValue($subAdminManager));
  1844. $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData());
  1845. }
  1846. public function testAddSubAdminSuccessful() {
  1847. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1848. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1849. $this->userManager
  1850. ->expects($this->once())
  1851. ->method('get')
  1852. ->with('ExistingUser')
  1853. ->will($this->returnValue($targetUser));
  1854. $this->groupManager
  1855. ->expects($this->once())
  1856. ->method('get')
  1857. ->with('TargetGroup')
  1858. ->will($this->returnValue($targetGroup));
  1859. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1860. ->disableOriginalConstructor()->getMock();
  1861. $subAdminManager
  1862. ->expects($this->once())
  1863. ->method('isSubAdminOfGroup')
  1864. ->with($targetUser, $targetGroup)
  1865. ->will($this->returnValue(false));
  1866. $subAdminManager
  1867. ->expects($this->once())
  1868. ->method('createSubAdmin')
  1869. ->with($targetUser, $targetGroup)
  1870. ->will($this->returnValue(true));
  1871. $this->groupManager
  1872. ->expects($this->once())
  1873. ->method('getSubAdmin')
  1874. ->will($this->returnValue($subAdminManager));
  1875. $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData());
  1876. }
  1877. /**
  1878. * @expectedException \OCP\AppFramework\OCS\OCSException
  1879. * @expectedExceptionCode 103
  1880. * @expectedExceptionMessage Unknown error occurred
  1881. */
  1882. public function testAddSubAdminUnsuccessful() {
  1883. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1884. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1885. $this->userManager
  1886. ->expects($this->once())
  1887. ->method('get')
  1888. ->with('ExistingUser')
  1889. ->will($this->returnValue($targetUser));
  1890. $this->groupManager
  1891. ->expects($this->once())
  1892. ->method('get')
  1893. ->with('TargetGroup')
  1894. ->will($this->returnValue($targetGroup));
  1895. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1896. ->disableOriginalConstructor()->getMock();
  1897. $subAdminManager
  1898. ->expects($this->once())
  1899. ->method('isSubAdminOfGroup')
  1900. ->with($targetUser, $targetGroup)
  1901. ->will($this->returnValue(false));
  1902. $subAdminManager
  1903. ->expects($this->once())
  1904. ->method('createSubAdmin')
  1905. ->with($targetUser, $targetGroup)
  1906. ->will($this->returnValue(false));
  1907. $this->groupManager
  1908. ->expects($this->once())
  1909. ->method('getSubAdmin')
  1910. ->will($this->returnValue($subAdminManager));
  1911. $this->api->addSubAdmin('ExistingUser', 'TargetGroup');
  1912. }
  1913. /**
  1914. * @expectedException \OCP\AppFramework\OCS\OCSException
  1915. * @expectedExceptionCode 101
  1916. * @expectedExceptionMessage User does not exist
  1917. */
  1918. public function testRemoveSubAdminNotExistingTargetUser() {
  1919. $this->userManager
  1920. ->expects($this->once())
  1921. ->method('get')
  1922. ->with('NotExistingUser')
  1923. ->will($this->returnValue(null));
  1924. $this->api->removeSubAdmin('NotExistingUser', 'GroupToDeleteFrom');
  1925. }
  1926. /**
  1927. * @expectedException \OCP\AppFramework\OCS\OCSException
  1928. * @expectedExceptionCode 101
  1929. * @expectedExceptionMessage Group does not exist
  1930. */
  1931. public function testRemoveSubAdminNotExistingTargetGroup() {
  1932. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1933. $this->userManager
  1934. ->expects($this->once())
  1935. ->method('get')
  1936. ->with('ExistingUser')
  1937. ->will($this->returnValue($targetUser));
  1938. $this->groupManager
  1939. ->expects($this->once())
  1940. ->method('get')
  1941. ->with('GroupToDeleteFrom')
  1942. ->will($this->returnValue(null));
  1943. $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
  1944. }
  1945. /**
  1946. * @expectedException \OCP\AppFramework\OCS\OCSException
  1947. * @expectedExceptionCode 102
  1948. * @expectedExceptionMessage User is not a subadmin of this group
  1949. */
  1950. public function testRemoveSubAdminFromNotASubadmin() {
  1951. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1952. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1953. $this->userManager
  1954. ->expects($this->once())
  1955. ->method('get')
  1956. ->with('ExistingUser')
  1957. ->will($this->returnValue($targetUser));
  1958. $this->groupManager
  1959. ->expects($this->once())
  1960. ->method('get')
  1961. ->with('GroupToDeleteFrom')
  1962. ->will($this->returnValue($targetGroup));
  1963. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1964. ->disableOriginalConstructor()->getMock();
  1965. $subAdminManager
  1966. ->expects($this->once())
  1967. ->method('isSubAdminOfGroup')
  1968. ->with($targetUser, $targetGroup)
  1969. ->will($this->returnValue(false));
  1970. $this->groupManager
  1971. ->expects($this->once())
  1972. ->method('getSubAdmin')
  1973. ->will($this->returnValue($subAdminManager));
  1974. $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
  1975. }
  1976. public function testRemoveSubAdminSuccessful() {
  1977. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  1978. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  1979. $this->userManager
  1980. ->expects($this->once())
  1981. ->method('get')
  1982. ->with('ExistingUser')
  1983. ->will($this->returnValue($targetUser));
  1984. $this->groupManager
  1985. ->expects($this->once())
  1986. ->method('get')
  1987. ->with('GroupToDeleteFrom')
  1988. ->will($this->returnValue($targetGroup));
  1989. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1990. ->disableOriginalConstructor()->getMock();
  1991. $subAdminManager
  1992. ->expects($this->once())
  1993. ->method('isSubAdminOfGroup')
  1994. ->with($targetUser, $targetGroup)
  1995. ->will($this->returnValue(true));
  1996. $subAdminManager
  1997. ->expects($this->once())
  1998. ->method('deleteSubAdmin')
  1999. ->with($targetUser, $targetGroup)
  2000. ->will($this->returnValue(true));
  2001. $this->groupManager
  2002. ->expects($this->once())
  2003. ->method('getSubAdmin')
  2004. ->will($this->returnValue($subAdminManager));
  2005. $this->assertEquals([], $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom')->getData());
  2006. }
  2007. /**
  2008. * @expectedException \OCP\AppFramework\OCS\OCSException
  2009. * @expectedExceptionCode 103
  2010. * @expectedExceptionMessage Unknown error occurred
  2011. */
  2012. public function testRemoveSubAdminUnsuccessful() {
  2013. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  2014. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2015. $this->userManager
  2016. ->expects($this->once())
  2017. ->method('get')
  2018. ->with('ExistingUser')
  2019. ->will($this->returnValue($targetUser));
  2020. $this->groupManager
  2021. ->expects($this->once())
  2022. ->method('get')
  2023. ->with('GroupToDeleteFrom')
  2024. ->will($this->returnValue($targetGroup));
  2025. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2026. ->disableOriginalConstructor()->getMock();
  2027. $subAdminManager
  2028. ->expects($this->once())
  2029. ->method('isSubAdminOfGroup')
  2030. ->with($targetUser, $targetGroup)
  2031. ->will($this->returnValue(true));
  2032. $subAdminManager
  2033. ->expects($this->once())
  2034. ->method('deleteSubAdmin')
  2035. ->with($targetUser, $targetGroup)
  2036. ->will($this->returnValue(false));
  2037. $this->groupManager
  2038. ->expects($this->once())
  2039. ->method('getSubAdmin')
  2040. ->will($this->returnValue($subAdminManager));
  2041. $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
  2042. }
  2043. /**
  2044. * @expectedException \OCP\AppFramework\OCS\OCSException
  2045. * @expectedExceptionCode 101
  2046. * @expectedExceptionMessage User does not exist
  2047. */
  2048. public function testGetUserSubAdminGroupsNotExistingTargetUser() {
  2049. $this->userManager
  2050. ->expects($this->once())
  2051. ->method('get')
  2052. ->with('RequestedUser')
  2053. ->will($this->returnValue(null));
  2054. $this->api->getUserSubAdminGroups('RequestedUser');
  2055. }
  2056. public function testGetUserSubAdminGroupsWithGroups() {
  2057. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  2058. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2059. $targetGroup
  2060. ->expects($this->once())
  2061. ->method('getGID')
  2062. ->will($this->returnValue('TargetGroup'));
  2063. $this->userManager
  2064. ->expects($this->once())
  2065. ->method('get')
  2066. ->with('RequestedUser')
  2067. ->will($this->returnValue($targetUser));
  2068. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2069. ->disableOriginalConstructor()->getMock();
  2070. $subAdminManager
  2071. ->expects($this->once())
  2072. ->method('getSubAdminsGroups')
  2073. ->with($targetUser)
  2074. ->will($this->returnValue([$targetGroup]));
  2075. $this->groupManager
  2076. ->expects($this->once())
  2077. ->method('getSubAdmin')
  2078. ->will($this->returnValue($subAdminManager));
  2079. $this->assertEquals(['TargetGroup'], $this->api->getUserSubAdminGroups('RequestedUser')->getData());
  2080. }
  2081. /**
  2082. * @expectedException \OCP\AppFramework\OCS\OCSException
  2083. * @expectedExceptionCode 102
  2084. * @expectedExceptionMessage Unknown error occurred
  2085. */
  2086. public function testGetUserSubAdminGroupsWithoutGroups() {
  2087. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  2088. $this->userManager
  2089. ->expects($this->once())
  2090. ->method('get')
  2091. ->with('RequestedUser')
  2092. ->will($this->returnValue($targetUser));
  2093. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2094. ->disableOriginalConstructor()->getMock();
  2095. $subAdminManager
  2096. ->expects($this->once())
  2097. ->method('getSubAdminsGroups')
  2098. ->with($targetUser)
  2099. ->will($this->returnValue([]));
  2100. $this->groupManager
  2101. ->expects($this->once())
  2102. ->method('getSubAdmin')
  2103. ->will($this->returnValue($subAdminManager));
  2104. $this->api->getUserSubAdminGroups('RequestedUser');
  2105. }
  2106. public function testEnableUser() {
  2107. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  2108. $targetUser->expects($this->once())
  2109. ->method('setEnabled')
  2110. ->with(true);
  2111. $this->userManager
  2112. ->expects($this->once())
  2113. ->method('get')
  2114. ->with('RequestedUser')
  2115. ->will($this->returnValue($targetUser));
  2116. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  2117. $loggedInUser
  2118. ->expects($this->exactly(2))
  2119. ->method('getUID')
  2120. ->will($this->returnValue('admin'));
  2121. $this->userSession
  2122. ->expects($this->once())
  2123. ->method('getUser')
  2124. ->will($this->returnValue($loggedInUser));
  2125. $this->groupManager
  2126. ->expects($this->once())
  2127. ->method('isAdmin')
  2128. ->will($this->returnValue(true));
  2129. $this->assertEquals([], $this->api->enableUser('RequestedUser')->getData());
  2130. }
  2131. public function testDisableUser() {
  2132. $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  2133. $targetUser->expects($this->once())
  2134. ->method('setEnabled')
  2135. ->with(false);
  2136. $this->userManager
  2137. ->expects($this->once())
  2138. ->method('get')
  2139. ->with('RequestedUser')
  2140. ->will($this->returnValue($targetUser));
  2141. $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();
  2142. $loggedInUser
  2143. ->expects($this->exactly(2))
  2144. ->method('getUID')
  2145. ->will($this->returnValue('admin'));
  2146. $this->userSession
  2147. ->expects($this->once())
  2148. ->method('getUser')
  2149. ->will($this->returnValue($loggedInUser));
  2150. $this->groupManager
  2151. ->expects($this->once())
  2152. ->method('isAdmin')
  2153. ->will($this->returnValue(true));
  2154. $this->assertEquals([], $this->api->disableUser('RequestedUser')->getData());
  2155. }
  2156. }