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.

2871 lines
79 KiB

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
11 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
9 years ago
9 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. /**
  3. * @author Lukas Reschke
  4. * @copyright 2014-2015 Lukas Reschke lukas@owncloud.com
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later.
  8. * See the COPYING-README file.
  9. */
  10. namespace Tests\Settings\Controller;
  11. use OC\Accounts\AccountManager;
  12. use OC\Group\Manager;
  13. use OC\Settings\Controller\UsersController;
  14. use OC\Settings\Mailer\NewUserMailHelper;
  15. use OCP\App\IAppManager;
  16. use OCP\AppFramework\Http;
  17. use OCP\AppFramework\Http\DataResponse;
  18. use OCP\AppFramework\Utility\ITimeFactory;
  19. use OCP\BackgroundJob\IJobList;
  20. use OCP\IAvatar;
  21. use OCP\IAvatarManager;
  22. use OCP\IConfig;
  23. use OCP\IGroup;
  24. use OCP\IGroupManager;
  25. use OCP\IL10N;
  26. use OCP\ILogger;
  27. use OCP\IRequest;
  28. use OCP\IURLGenerator;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use OCP\IUserSession;
  32. use OCP\Mail\IEMailTemplate;
  33. use OCP\Mail\IMailer;
  34. use OCP\Security\ICrypto;
  35. use OCP\Security\ISecureRandom;
  36. use OC\User\User;
  37. use Test\Util\User\Dummy;
  38. /**
  39. * @group DB
  40. *
  41. * @package Tests\Settings\Controller
  42. */
  43. class UsersControllerTest extends \Test\TestCase {
  44. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  45. private $groupManager;
  46. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  47. private $userManager;
  48. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  49. private $userSession;
  50. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  51. private $config;
  52. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  53. private $logger;
  54. /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
  55. private $mailer;
  56. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  57. private $urlGenerator;
  58. /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
  59. private $appManager;
  60. /** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */
  61. private $avatarManager;
  62. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  63. private $l;
  64. /** @var AccountManager | \PHPUnit_Framework_MockObject_MockObject */
  65. private $accountManager;
  66. /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject */
  67. private $secureRandom;
  68. /** @var ITimeFactory | \PHPUnit_Framework_MockObject_MockObject */
  69. private $timeFactory;
  70. /** @var NewUserMailHelper|\PHPUnit_Framework_MockObject_MockObject */
  71. private $newUserMailHelper;
  72. /** @var ICrypto | \PHPUnit_Framework_MockObject_MockObject */
  73. private $crypto;
  74. /** @var IJobList | \PHPUnit_Framework_MockObject_MockObject */
  75. private $jobList;
  76. /** @var \OC\Security\IdentityProof\Manager |\PHPUnit_Framework_MockObject_MockObject */
  77. private $securityManager;
  78. protected function setUp() {
  79. parent::setUp();
  80. $this->groupManager = $this->createMock(Manager::class);
  81. $this->userManager = $this->createMock(IUserManager::class);
  82. $this->userSession = $this->createMock(IUserSession::class);
  83. $this->config = $this->createMock(IConfig::class);
  84. $this->logger = $this->createMock(ILogger::class);
  85. $this->mailer = $this->createMock(IMailer::class);
  86. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  87. $this->appManager = $this->createMock(IAppManager::class);
  88. $this->avatarManager = $this->createMock(IAvatarManager::class);
  89. $this->accountManager = $this->createMock(AccountManager::class);
  90. $this->secureRandom = $this->createMock(ISecureRandom::class);
  91. $this->timeFactory = $this->createMock(ITimeFactory::class);
  92. $this->crypto = $this->createMock(ICrypto::class);
  93. $this->newUserMailHelper = $this->createMock(NewUserMailHelper::class);
  94. $this->timeFactory = $this->createMock(ITimeFactory::class);
  95. $this->crypto = $this->createMock(ICrypto::class);
  96. $this->securityManager = $this->getMockBuilder(\OC\Security\IdentityProof\Manager::class)->disableOriginalConstructor()->getMock();
  97. $this->jobList = $this->createMock(IJobList::class);
  98. $this->l = $this->createMock(IL10N::class);
  99. $this->l->method('t')
  100. ->will($this->returnCallback(function ($text, $parameters = []) {
  101. return vsprintf($text, $parameters);
  102. }));
  103. /*
  104. * Set default avatar behaviour for whole test suite
  105. */
  106. $avatarExists = $this->createMock(IAvatar::class);
  107. $avatarExists->method('exists')->willReturn(true);
  108. $avatarNotExists = $this->createMock(IAvatar::class);
  109. $avatarNotExists->method('exists')->willReturn(false);
  110. $this->avatarManager->method('getAvatar')
  111. ->will($this->returnValueMap([
  112. ['foo', $avatarExists],
  113. ['bar', $avatarExists],
  114. ['admin', $avatarNotExists],
  115. ]));
  116. }
  117. /**
  118. * @param bool $isAdmin
  119. * @return UsersController | \PHPUnit_Framework_MockObject_MockObject
  120. */
  121. protected function getController($isAdmin = false, $mockedMethods = []) {
  122. if (empty($mockedMethods)) {
  123. return new UsersController(
  124. 'settings',
  125. $this->createMock(IRequest::class),
  126. $this->userManager,
  127. $this->groupManager,
  128. $this->userSession,
  129. $this->config,
  130. $isAdmin,
  131. $this->l,
  132. $this->logger,
  133. $this->mailer,
  134. $this->urlGenerator,
  135. $this->appManager,
  136. $this->avatarManager,
  137. $this->accountManager,
  138. $this->secureRandom,
  139. $this->newUserMailHelper,
  140. $this->timeFactory,
  141. $this->crypto,
  142. $this->securityManager,
  143. $this->jobList
  144. );
  145. } else {
  146. return $this->getMockBuilder(UsersController::class)
  147. ->setConstructorArgs(
  148. [
  149. 'settings',
  150. $this->createMock(IRequest::class),
  151. $this->userManager,
  152. $this->groupManager,
  153. $this->userSession,
  154. $this->config,
  155. $isAdmin,
  156. $this->l,
  157. $this->logger,
  158. $this->mailer,
  159. $this->urlGenerator,
  160. $this->appManager,
  161. $this->avatarManager,
  162. $this->accountManager,
  163. $this->secureRandom,
  164. $this->newUserMailHelper,
  165. $this->timeFactory,
  166. $this->crypto,
  167. $this->securityManager,
  168. $this->jobList
  169. ]
  170. )->setMethods($mockedMethods)->getMock();
  171. }
  172. }
  173. public function testIndexAdmin() {
  174. $controller = $this->getController(true);
  175. $foo = $this->createMock(User::class);
  176. $foo
  177. ->expects($this->exactly(2))
  178. ->method('getUID')
  179. ->will($this->returnValue('foo'));
  180. $foo
  181. ->expects($this->once())
  182. ->method('getDisplayName')
  183. ->will($this->returnValue('M. Foo'));
  184. $foo
  185. ->expects($this->once())
  186. ->method('getEMailAddress')
  187. ->will($this->returnValue('foo@bar.com'));
  188. $foo
  189. ->expects($this->once())
  190. ->method('getQuota')
  191. ->will($this->returnValue('1024'));
  192. $foo
  193. ->method('getLastLogin')
  194. ->will($this->returnValue(500));
  195. $foo
  196. ->method('getHome')
  197. ->will($this->returnValue('/home/foo'));
  198. $foo
  199. ->expects($this->once())
  200. ->method('getBackendClassName')
  201. ->will($this->returnValue('OC_User_Database'));
  202. $foo->expects($this->any())
  203. ->method('isEnabled')
  204. ->willReturn(true);
  205. $admin = $this->createMock(User::class);
  206. $admin
  207. ->expects($this->exactly(2))
  208. ->method('getUID')
  209. ->will($this->returnValue('admin'));
  210. $admin
  211. ->expects($this->once())
  212. ->method('getDisplayName')
  213. ->will($this->returnValue('S. Admin'));
  214. $admin
  215. ->expects($this->once())
  216. ->method('getEMailAddress')
  217. ->will($this->returnValue('admin@bar.com'));
  218. $admin
  219. ->expects($this->once())
  220. ->method('getQuota')
  221. ->will($this->returnValue('404'));
  222. $admin
  223. ->expects($this->once())
  224. ->method('getLastLogin')
  225. ->will($this->returnValue(12));
  226. $admin
  227. ->expects($this->once())
  228. ->method('getHome')
  229. ->will($this->returnValue('/home/admin'));
  230. $admin
  231. ->expects($this->once())
  232. ->method('getBackendClassName')
  233. ->willReturn(Dummy::class);
  234. $admin->expects($this->any())
  235. ->method('isEnabled')
  236. ->willReturn(true);
  237. $bar = $this->createMock(User::class);
  238. $bar
  239. ->expects($this->exactly(2))
  240. ->method('getUID')
  241. ->will($this->returnValue('bar'));
  242. $bar
  243. ->expects($this->once())
  244. ->method('getDisplayName')
  245. ->will($this->returnValue('B. Ar'));
  246. $bar
  247. ->expects($this->once())
  248. ->method('getEMailAddress')
  249. ->will($this->returnValue('bar@dummy.com'));
  250. $bar
  251. ->expects($this->once())
  252. ->method('getQuota')
  253. ->will($this->returnValue('2323'));
  254. $bar
  255. ->method('getLastLogin')
  256. ->will($this->returnValue(3999));
  257. $bar
  258. ->method('getHome')
  259. ->will($this->returnValue('/home/bar'));
  260. $bar
  261. ->expects($this->once())
  262. ->method('getBackendClassName')
  263. ->willReturn(Dummy::class);
  264. $bar->expects($this->at(0))
  265. ->method('isEnabled')
  266. ->willReturn(true);
  267. $bar->expects($this->at(1))
  268. ->method('isEnabled')
  269. ->willReturn(true);
  270. $bar->expects($this->at(2))
  271. ->method('isEnabled')
  272. ->willReturn(false);
  273. $this->groupManager
  274. ->expects($this->once())
  275. ->method('displayNamesInGroup')
  276. ->with('gid', 'pattern')
  277. ->will($this->returnValue(array('foo' => 'M. Foo', 'admin' => 'S. Admin', 'bar' => 'B. Ar')));
  278. $this->groupManager
  279. ->expects($this->exactly(3))
  280. ->method('getUserGroupIds')
  281. ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users')));
  282. $this->userManager
  283. ->expects($this->at(0))
  284. ->method('get')
  285. ->with('foo')
  286. ->will($this->returnValue($foo));
  287. $this->userManager
  288. ->expects($this->at(1))
  289. ->method('get')
  290. ->with('admin')
  291. ->will($this->returnValue($admin));
  292. $this->userManager
  293. ->expects($this->at(2))
  294. ->method('get')
  295. ->with('bar')
  296. ->will($this->returnValue($bar));
  297. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  298. ->disableOriginalConstructor()
  299. ->getMock();
  300. $subadmin
  301. ->expects($this->any())
  302. ->method('getSubAdminsGroups')
  303. ->with($foo)
  304. ->will($this->returnValue([]));
  305. $subadmin
  306. ->expects($this->any())
  307. ->method('getSubAdminsGroups')
  308. ->with($admin)
  309. ->will($this->returnValue([]));
  310. $subadmin
  311. ->expects($this->any())
  312. ->method('getSubAdminsGroups')
  313. ->with($bar)
  314. ->will($this->returnValue([]));
  315. $this->groupManager
  316. ->expects($this->any())
  317. ->method('getSubAdmin')
  318. ->will($this->returnValue($subadmin));
  319. $expectedResponse = new DataResponse(
  320. array(
  321. 0 => array(
  322. 'name' => 'foo',
  323. 'displayname' => 'M. Foo',
  324. 'groups' => array('Users', 'Support'),
  325. 'subadmin' => array(),
  326. 'quota' => 1024,
  327. 'storageLocation' => '/home/foo',
  328. 'lastLogin' => 500000,
  329. 'backend' => 'OC_User_Database',
  330. 'email' => 'foo@bar.com',
  331. 'isRestoreDisabled' => false,
  332. 'isAvatarAvailable' => true,
  333. 'isEnabled' => true,
  334. ),
  335. 1 => array(
  336. 'name' => 'admin',
  337. 'displayname' => 'S. Admin',
  338. 'groups' => array('admins', 'Support'),
  339. 'subadmin' => array(),
  340. 'quota' => 404,
  341. 'storageLocation' => '/home/admin',
  342. 'lastLogin' => 12000,
  343. 'backend' => Dummy::class,
  344. 'email' => 'admin@bar.com',
  345. 'isRestoreDisabled' => false,
  346. 'isAvatarAvailable' => false,
  347. 'isEnabled' => true,
  348. ),
  349. 2 => array(
  350. 'name' => 'bar',
  351. 'displayname' => 'B. Ar',
  352. 'groups' => array('External Users'),
  353. 'subadmin' => array(),
  354. 'quota' => 2323,
  355. 'storageLocation' => '/home/bar',
  356. 'lastLogin' => 3999000,
  357. 'backend' => Dummy::class,
  358. 'email' => 'bar@dummy.com',
  359. 'isRestoreDisabled' => false,
  360. 'isAvatarAvailable' => true,
  361. 'isEnabled' => false,
  362. ),
  363. )
  364. );
  365. $response = $controller->index(0, 10, 'gid', 'pattern');
  366. $this->assertEquals($expectedResponse, $response);
  367. }
  368. public function testIndexSubAdmin() {
  369. $controller = $this->getController(false);
  370. $user = $this->createMock(User::class);
  371. $this->userSession
  372. ->expects($this->once())
  373. ->method('getUser')
  374. ->will($this->returnValue($user));
  375. $foo = $this->createMock(User::class);
  376. $foo
  377. ->expects($this->exactly(2))
  378. ->method('getUID')
  379. ->will($this->returnValue('foo'));
  380. $foo
  381. ->expects($this->once())
  382. ->method('getDisplayName')
  383. ->will($this->returnValue('M. Foo'));
  384. $foo
  385. ->expects($this->once())
  386. ->method('getEMailAddress')
  387. ->will($this->returnValue('foo@bar.com'));
  388. $foo
  389. ->expects($this->once())
  390. ->method('getQuota')
  391. ->will($this->returnValue('1024'));
  392. $foo
  393. ->method('getLastLogin')
  394. ->will($this->returnValue(500));
  395. $foo
  396. ->method('getHome')
  397. ->will($this->returnValue('/home/foo'));
  398. $foo
  399. ->expects($this->once())
  400. ->method('getBackendClassName')
  401. ->will($this->returnValue('OC_User_Database'));
  402. $foo->expects($this->any())
  403. ->method('isEnabled')
  404. ->willReturn(true);
  405. $admin = $this->createMock(User::class);
  406. $admin
  407. ->expects($this->exactly(2))
  408. ->method('getUID')
  409. ->will($this->returnValue('admin'));
  410. $admin
  411. ->expects($this->once())
  412. ->method('getDisplayName')
  413. ->will($this->returnValue('S. Admin'));
  414. $admin
  415. ->expects($this->once())
  416. ->method('getEMailAddress')
  417. ->will($this->returnValue('admin@bar.com'));
  418. $admin
  419. ->expects($this->once())
  420. ->method('getQuota')
  421. ->will($this->returnValue('404'));
  422. $admin
  423. ->expects($this->once())
  424. ->method('getLastLogin')
  425. ->will($this->returnValue(12));
  426. $admin
  427. ->expects($this->once())
  428. ->method('getHome')
  429. ->will($this->returnValue('/home/admin'));
  430. $admin
  431. ->expects($this->once())
  432. ->method('getBackendClassName')
  433. ->willReturn(Dummy::class);
  434. $admin->expects($this->any())
  435. ->method('isEnabled')
  436. ->willReturn(true);
  437. $bar = $this->createMock(User::class);
  438. $bar
  439. ->expects($this->exactly(2))
  440. ->method('getUID')
  441. ->will($this->returnValue('bar'));
  442. $bar
  443. ->expects($this->once())
  444. ->method('getDisplayName')
  445. ->will($this->returnValue('B. Ar'));
  446. $bar
  447. ->expects($this->once())
  448. ->method('getEMailAddress')
  449. ->will($this->returnValue('bar@dummy.com'));
  450. $bar
  451. ->expects($this->once())
  452. ->method('getQuota')
  453. ->will($this->returnValue('2323'));
  454. $bar
  455. ->method('getLastLogin')
  456. ->will($this->returnValue(3999));
  457. $bar
  458. ->method('getHome')
  459. ->will($this->returnValue('/home/bar'));
  460. $bar
  461. ->expects($this->once())
  462. ->method('getBackendClassName')
  463. ->willReturn(Dummy::class);
  464. $bar->expects($this->any())
  465. ->method('isEnabled')
  466. ->willReturn(true);
  467. $this->groupManager
  468. ->expects($this->at(2))
  469. ->method('displayNamesInGroup')
  470. ->with('SubGroup2', 'pattern')
  471. ->will($this->returnValue(['foo' => 'M. Foo', 'admin' => 'S. Admin']));
  472. $this->groupManager
  473. ->expects($this->at(1))
  474. ->method('displayNamesInGroup')
  475. ->with('SubGroup1', 'pattern')
  476. ->will($this->returnValue(['bar' => 'B. Ar']));
  477. $this->groupManager
  478. ->expects($this->exactly(3))
  479. ->method('getUserGroupIds')
  480. ->will($this->onConsecutiveCalls(
  481. ['admin', 'SubGroup1', 'testGroup'],
  482. ['SubGroup2', 'SubGroup1'],
  483. ['SubGroup2', 'Foo']
  484. ));
  485. $this->userManager
  486. ->expects($this->at(0))
  487. ->method('get')
  488. ->with('bar')
  489. ->will($this->returnValue($bar));
  490. $this->userManager
  491. ->expects($this->at(1))
  492. ->method('get')
  493. ->with('foo')
  494. ->will($this->returnValue($foo));
  495. $this->userManager
  496. ->expects($this->at(2))
  497. ->method('get')
  498. ->with('admin')
  499. ->will($this->returnValue($admin));
  500. $subgroup1 = $this->getMockBuilder('\OCP\IGroup')
  501. ->disableOriginalConstructor()
  502. ->getMock();
  503. $subgroup1->expects($this->any())
  504. ->method('getGID')
  505. ->will($this->returnValue('SubGroup1'));
  506. $subgroup2 = $this->getMockBuilder('\OCP\IGroup')
  507. ->disableOriginalConstructor()
  508. ->getMock();
  509. $subgroup2->expects($this->any())
  510. ->method('getGID')
  511. ->will($this->returnValue('SubGroup2'));
  512. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  513. ->disableOriginalConstructor()
  514. ->getMock();
  515. $subadmin
  516. ->expects($this->at(0))
  517. ->method('getSubAdminsGroups')
  518. ->will($this->returnValue([$subgroup1, $subgroup2]));
  519. $subadmin
  520. ->expects($this->any())
  521. ->method('getSubAdminsGroups')
  522. ->will($this->returnValue([]));
  523. $this->groupManager
  524. ->expects($this->any())
  525. ->method('getSubAdmin')
  526. ->will($this->returnValue($subadmin));
  527. $expectedResponse = new DataResponse(
  528. [
  529. 0 => [
  530. 'name' => 'bar',
  531. 'displayname' => 'B. Ar',
  532. 'groups' => ['SubGroup1'],
  533. 'subadmin' => [],
  534. 'quota' => 2323,
  535. 'storageLocation' => '/home/bar',
  536. 'lastLogin' => 3999000,
  537. 'backend' => Dummy::class,
  538. 'email' => 'bar@dummy.com',
  539. 'isRestoreDisabled' => false,
  540. 'isAvatarAvailable' => true,
  541. 'isEnabled' => true,
  542. ],
  543. 1=> [
  544. 'name' => 'foo',
  545. 'displayname' => 'M. Foo',
  546. 'groups' => ['SubGroup2', 'SubGroup1'],
  547. 'subadmin' => [],
  548. 'quota' => 1024,
  549. 'storageLocation' => '/home/foo',
  550. 'lastLogin' => 500000,
  551. 'backend' => 'OC_User_Database',
  552. 'email' => 'foo@bar.com',
  553. 'isRestoreDisabled' => false,
  554. 'isAvatarAvailable' => true,
  555. 'isEnabled' => true,
  556. ],
  557. 2 => [
  558. 'name' => 'admin',
  559. 'displayname' => 'S. Admin',
  560. 'groups' => ['SubGroup2'],
  561. 'subadmin' => [],
  562. 'quota' => 404,
  563. 'storageLocation' => '/home/admin',
  564. 'lastLogin' => 12000,
  565. 'backend' => Dummy::class,
  566. 'email' => 'admin@bar.com',
  567. 'isRestoreDisabled' => false,
  568. 'isAvatarAvailable' => false,
  569. 'isEnabled' => true,
  570. ],
  571. ]
  572. );
  573. $response = $controller->index(0, 10, '', 'pattern');
  574. $this->assertEquals($expectedResponse, $response);
  575. }
  576. /**
  577. * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
  578. * to test for subadmins. Thus the test always assumes you have admin permissions...
  579. */
  580. public function testIndexWithSearch() {
  581. $controller = $this->getController(true);
  582. $foo = $this->createMock(User::class);
  583. $foo
  584. ->expects($this->exactly(2))
  585. ->method('getUID')
  586. ->will($this->returnValue('foo'));
  587. $foo
  588. ->expects($this->once())
  589. ->method('getDisplayName')
  590. ->will($this->returnValue('M. Foo'));
  591. $foo
  592. ->expects($this->once())
  593. ->method('getEMailAddress')
  594. ->will($this->returnValue('foo@bar.com'));
  595. $foo
  596. ->expects($this->once())
  597. ->method('getQuota')
  598. ->will($this->returnValue('1024'));
  599. $foo
  600. ->method('getLastLogin')
  601. ->will($this->returnValue(500));
  602. $foo
  603. ->method('getHome')
  604. ->will($this->returnValue('/home/foo'));
  605. $foo
  606. ->expects($this->once())
  607. ->method('getBackendClassName')
  608. ->will($this->returnValue('OC_User_Database'));
  609. $foo->expects($this->any())
  610. ->method('isEnabled')
  611. ->willReturn(true);
  612. $admin = $this->createMock(User::class);
  613. $admin
  614. ->expects($this->exactly(2))
  615. ->method('getUID')
  616. ->will($this->returnValue('admin'));
  617. $admin
  618. ->expects($this->once())
  619. ->method('getDisplayName')
  620. ->will($this->returnValue('S. Admin'));
  621. $admin
  622. ->expects($this->once())
  623. ->method('getEMailAddress')
  624. ->will($this->returnValue('admin@bar.com'));
  625. $admin
  626. ->expects($this->once())
  627. ->method('getQuota')
  628. ->will($this->returnValue('404'));
  629. $admin
  630. ->expects($this->once())
  631. ->method('getLastLogin')
  632. ->will($this->returnValue(12));
  633. $admin
  634. ->expects($this->once())
  635. ->method('getHome')
  636. ->will($this->returnValue('/home/admin'));
  637. $admin
  638. ->expects($this->once())
  639. ->method('getBackendClassName')
  640. ->willReturn(Dummy::class);
  641. $admin->expects($this->any())
  642. ->method('isEnabled')
  643. ->willReturn(true);
  644. $bar = $this->createMock(User::class);
  645. $bar
  646. ->expects($this->exactly(2))
  647. ->method('getUID')
  648. ->will($this->returnValue('bar'));
  649. $bar
  650. ->expects($this->once())
  651. ->method('getDisplayName')
  652. ->will($this->returnValue('B. Ar'));
  653. $bar
  654. ->expects($this->once())
  655. ->method('getEMailAddress')
  656. ->will($this->returnValue('bar@dummy.com'));
  657. $bar
  658. ->expects($this->once())
  659. ->method('getQuota')
  660. ->will($this->returnValue('2323'));
  661. $bar
  662. ->method('getLastLogin')
  663. ->will($this->returnValue(3999));
  664. $bar
  665. ->method('getHome')
  666. ->will($this->returnValue('/home/bar'));
  667. $bar
  668. ->expects($this->once())
  669. ->method('getBackendClassName')
  670. ->willReturn(Dummy::class);
  671. $bar->expects($this->any())
  672. ->method('isEnabled')
  673. ->willReturn(true);
  674. $this->userManager
  675. ->expects($this->once())
  676. ->method('search')
  677. ->with('pattern', 10, 0)
  678. ->will($this->returnValue([$foo, $admin, $bar]));
  679. $this->groupManager
  680. ->expects($this->exactly(3))
  681. ->method('getUserGroupIds')
  682. ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users')));
  683. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  684. ->disableOriginalConstructor()
  685. ->getMock();
  686. $subadmin->expects($this->any())
  687. ->method('getSubAdminsGroups')
  688. ->will($this->returnValue([]));
  689. $this->groupManager
  690. ->expects($this->any())
  691. ->method('getSubAdmin')
  692. ->will($this->returnValue($subadmin));
  693. $expectedResponse = new DataResponse(
  694. array(
  695. 0 => array(
  696. 'name' => 'foo',
  697. 'displayname' => 'M. Foo',
  698. 'groups' => array('Users', 'Support'),
  699. 'subadmin' => array(),
  700. 'quota' => 1024,
  701. 'storageLocation' => '/home/foo',
  702. 'lastLogin' => 500000,
  703. 'backend' => 'OC_User_Database',
  704. 'email' => 'foo@bar.com',
  705. 'isRestoreDisabled' => false,
  706. 'isAvatarAvailable' => true,
  707. 'isEnabled' => true,
  708. ),
  709. 1 => array(
  710. 'name' => 'admin',
  711. 'displayname' => 'S. Admin',
  712. 'groups' => array('admins', 'Support'),
  713. 'subadmin' => array(),
  714. 'quota' => 404,
  715. 'storageLocation' => '/home/admin',
  716. 'lastLogin' => 12000,
  717. 'backend' => Dummy::class,
  718. 'email' => 'admin@bar.com',
  719. 'isRestoreDisabled' => false,
  720. 'isAvatarAvailable' => false,
  721. 'isEnabled' => true,
  722. ),
  723. 2 => array(
  724. 'name' => 'bar',
  725. 'displayname' => 'B. Ar',
  726. 'groups' => array('External Users'),
  727. 'subadmin' => array(),
  728. 'quota' => 2323,
  729. 'storageLocation' => '/home/bar',
  730. 'lastLogin' => 3999000,
  731. 'backend' => Dummy::class,
  732. 'email' => 'bar@dummy.com',
  733. 'isRestoreDisabled' => false,
  734. 'isAvatarAvailable' => true,
  735. 'isEnabled' => true,
  736. ),
  737. )
  738. );
  739. $response = $controller->index(0, 10, '', 'pattern');
  740. $this->assertEquals($expectedResponse, $response);
  741. }
  742. public function testIndexWithBackend() {
  743. $controller = $this->getController(true);
  744. $user = $this->createMock(User::class);
  745. $user
  746. ->expects($this->exactly(2))
  747. ->method('getUID')
  748. ->will($this->returnValue('foo'));
  749. $user
  750. ->expects($this->once())
  751. ->method('getDisplayName')
  752. ->will($this->returnValue('M. Foo'));
  753. $user
  754. ->expects($this->once())
  755. ->method('getEMailAddress')
  756. ->will($this->returnValue(null));
  757. $user
  758. ->expects($this->once())
  759. ->method('getQuota')
  760. ->will($this->returnValue('none'));
  761. $user
  762. ->method('getLastLogin')
  763. ->will($this->returnValue(500));
  764. $user
  765. ->method('getHome')
  766. ->will($this->returnValue('/home/foo'));
  767. $user
  768. ->expects($this->once())
  769. ->method('getBackendClassName')
  770. ->will($this->returnValue('OC_User_Database'));
  771. $user->expects($this->any())
  772. ->method('isEnabled')
  773. ->willReturn(true);
  774. $this->userManager
  775. ->expects($this->once())
  776. ->method('getBackends')
  777. ->will($this->returnValue([new Dummy(), new \OC\User\Database()]));
  778. $this->userManager
  779. ->expects($this->once())
  780. ->method('clearBackends');
  781. $this->userManager
  782. ->expects($this->once())
  783. ->method('search')
  784. ->with('')
  785. ->will($this->returnValue([$user]));
  786. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  787. ->disableOriginalConstructor()
  788. ->getMock();
  789. $subadmin->expects($this->once())
  790. ->method('getSubAdminsGroups')
  791. ->will($this->returnValue([]));
  792. $this->groupManager
  793. ->expects($this->any())
  794. ->method('getSubAdmin')
  795. ->will($this->returnValue($subadmin));
  796. $expectedResponse = new DataResponse(
  797. array(
  798. 0 => array(
  799. 'name' => 'foo',
  800. 'displayname' => 'M. Foo',
  801. 'groups' => null,
  802. 'subadmin' => array(),
  803. 'quota' => 'none',
  804. 'storageLocation' => '/home/foo',
  805. 'lastLogin' => 500000,
  806. 'backend' => 'OC_User_Database',
  807. 'email' => null,
  808. 'isRestoreDisabled' => false,
  809. 'isAvatarAvailable' => true,
  810. 'isEnabled' => true,
  811. )
  812. )
  813. );
  814. $response = $controller->index(0, 10, '','', Dummy::class);
  815. $this->assertEquals($expectedResponse, $response);
  816. }
  817. public function testIndexWithBackendNoUser() {
  818. $controller = $this->getController(true);
  819. $this->userManager
  820. ->expects($this->once())
  821. ->method('getBackends')
  822. ->will($this->returnValue([new Dummy(), new \OC\User\Database()]));
  823. $this->userManager
  824. ->expects($this->once())
  825. ->method('search')
  826. ->with('')
  827. ->will($this->returnValue([]));
  828. $expectedResponse = new DataResponse([]);
  829. $response = $controller->index(0, 10, '','', Dummy::class);
  830. $this->assertEquals($expectedResponse, $response);
  831. }
  832. public function testCreateSuccessfulWithoutGroupAdmin() {
  833. $controller = $this->getController(true);
  834. $user = $this->createMock(User::class);
  835. $user
  836. ->method('getHome')
  837. ->will($this->returnValue('/home/user'));
  838. $user
  839. ->method('getUID')
  840. ->will($this->returnValue('foo'));
  841. $user
  842. ->expects($this->once())
  843. ->method('getBackendClassName')
  844. ->will($this->returnValue('bar'));
  845. $user->expects($this->any())
  846. ->method('isEnabled')
  847. ->willReturn(true);
  848. $this->userManager
  849. ->expects($this->once())
  850. ->method('createUser')
  851. ->will($this->onConsecutiveCalls($user));
  852. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  853. ->disableOriginalConstructor()
  854. ->getMock();
  855. $subadmin
  856. ->expects($this->any())
  857. ->method('getSubAdminsGroups')
  858. ->with($user)
  859. ->will($this->returnValue([]));
  860. $this->groupManager
  861. ->expects($this->any())
  862. ->method('getSubAdmin')
  863. ->will($this->returnValue($subadmin));
  864. $expectedResponse = new DataResponse(
  865. array(
  866. 'name' => 'foo',
  867. 'groups' => null,
  868. 'storageLocation' => '/home/user',
  869. 'backend' => 'bar',
  870. 'lastLogin' => null,
  871. 'displayname' => null,
  872. 'quota' => null,
  873. 'subadmin' => array(),
  874. 'email' => null,
  875. 'isRestoreDisabled' => false,
  876. 'isAvatarAvailable' => true,
  877. 'isEnabled' => true,
  878. ),
  879. Http::STATUS_CREATED
  880. );
  881. $response = $controller->create('foo', 'password', array());
  882. $this->assertEquals($expectedResponse, $response);
  883. }
  884. public function testCreateSuccessfulWithGroupAdmin() {
  885. $controller = $this->getController(true);
  886. $user = $this->createMock(User::class);
  887. $user
  888. ->method('getHome')
  889. ->will($this->returnValue('/home/user'));
  890. $user
  891. ->method('getHome')
  892. ->will($this->returnValue('/home/user'));
  893. $user
  894. ->method('getUID')
  895. ->will($this->returnValue('foo'));
  896. $user
  897. ->expects($this->once())
  898. ->method('getBackendClassName')
  899. ->will($this->returnValue('bar'));
  900. $user->expects($this->any())
  901. ->method('isEnabled')
  902. ->willReturn(true);
  903. $existingGroup = $this->getMockBuilder('\OCP\IGroup')
  904. ->disableOriginalConstructor()->getMock();
  905. $existingGroup
  906. ->expects($this->once())
  907. ->method('addUser')
  908. ->with($user);
  909. $newGroup = $this->getMockBuilder('\OCP\IGroup')
  910. ->disableOriginalConstructor()->getMock();
  911. $newGroup
  912. ->expects($this->once())
  913. ->method('addUser')
  914. ->with($user);
  915. $this->userManager
  916. ->expects($this->once())
  917. ->method('createUser')
  918. ->will($this->onConsecutiveCalls($user));
  919. $this->groupManager
  920. ->expects($this->exactly(2))
  921. ->method('get')
  922. ->will($this->onConsecutiveCalls(null, $existingGroup));
  923. $this->groupManager
  924. ->expects($this->once())
  925. ->method('createGroup')
  926. ->with('NewGroup')
  927. ->will($this->onConsecutiveCalls($newGroup));
  928. $this->groupManager
  929. ->expects($this->once())
  930. ->method('getUserGroupIds')
  931. ->with($user)
  932. ->will($this->onConsecutiveCalls(array('NewGroup', 'ExistingGroup')));
  933. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  934. ->disableOriginalConstructor()
  935. ->getMock();
  936. $subadmin
  937. ->expects($this->once())
  938. ->method('getSubAdminsGroups')
  939. ->with($user)
  940. ->will($this->returnValue([]));
  941. $this->groupManager
  942. ->expects($this->any())
  943. ->method('getSubAdmin')
  944. ->will($this->returnValue($subadmin));
  945. $expectedResponse = new DataResponse(
  946. array(
  947. 'name' => 'foo',
  948. 'groups' => array('NewGroup', 'ExistingGroup'),
  949. 'storageLocation' => '/home/user',
  950. 'backend' => 'bar',
  951. 'lastLogin' => null,
  952. 'displayname' => null,
  953. 'quota' => null,
  954. 'subadmin' => array(),
  955. 'email' => null,
  956. 'isRestoreDisabled' => false,
  957. 'isAvatarAvailable' => true,
  958. 'isEnabled' => true,
  959. ),
  960. Http::STATUS_CREATED
  961. );
  962. $response = $controller->create('foo', 'password', array('NewGroup', 'ExistingGroup'));
  963. $this->assertEquals($expectedResponse, $response);
  964. }
  965. public function testCreateSuccessfulWithGroupSubAdmin() {
  966. $controller = $this->getController(false);
  967. $user = $this->createMock(IUser::class);
  968. $this->userSession
  969. ->expects($this->once())
  970. ->method('getUser')
  971. ->will($this->returnValue($user));
  972. $newUser = $this->createMock(IUser::class);
  973. $newUser
  974. ->method('getHome')
  975. ->will($this->returnValue('/home/user'));
  976. $newUser
  977. ->method('getHome')
  978. ->will($this->returnValue('/home/user'));
  979. $newUser
  980. ->method('getUID')
  981. ->will($this->returnValue('foo'));
  982. $newUser
  983. ->expects($this->once())
  984. ->method('getBackendClassName')
  985. ->will($this->returnValue('bar'));
  986. $subGroup1 = $this->createMock(IGroup::class);
  987. $newUser->expects($this->any())
  988. ->method('isEnabled')
  989. ->willReturn(true);
  990. $subGroup1
  991. ->expects($this->any())
  992. ->method('getGID')
  993. ->will($this->returnValue('SubGroup1'));
  994. $subGroup1
  995. ->expects($this->once())
  996. ->method('addUser')
  997. ->with($user);
  998. $this->userManager
  999. ->expects($this->once())
  1000. ->method('createUser')
  1001. ->will($this->returnValue($newUser));
  1002. $this->groupManager
  1003. ->expects($this->once())
  1004. ->method('getUserGroupIds')
  1005. ->with($user)
  1006. ->will($this->onConsecutiveCalls(['SubGroup1']));
  1007. $this->groupManager
  1008. ->expects($this->once())
  1009. ->method('getUserGroupIds')
  1010. ->with($newUser)
  1011. ->will($this->onConsecutiveCalls(['SubGroup1']));
  1012. $subadmin = $this->createMock(\OC\SubAdmin::class);
  1013. $subadmin->expects($this->atLeastOnce())
  1014. ->method('getSubAdminsGroups')
  1015. ->with($user)
  1016. ->willReturnMap([
  1017. [$user, [$subGroup1]],
  1018. [$newUser, []],
  1019. ]);
  1020. $subadmin->expects($this->atLeastOnce())
  1021. ->method('isSubAdminofGroup')
  1022. ->willReturnMap([
  1023. [$user, $subGroup1, true],
  1024. ]);
  1025. $this->groupManager
  1026. ->expects($this->any())
  1027. ->method('getSubAdmin')
  1028. ->will($this->returnValue($subadmin));
  1029. $this->groupManager->expects($this->atLeastOnce())
  1030. ->method('get')
  1031. ->willReturnMap([
  1032. ['SubGroup1', $subGroup1],
  1033. ]);
  1034. $expectedResponse = new DataResponse(
  1035. array(
  1036. 'name' => 'foo',
  1037. 'groups' => ['SubGroup1'],
  1038. 'storageLocation' => '/home/user',
  1039. 'backend' => 'bar',
  1040. 'lastLogin' => 0,
  1041. 'displayname' => null,
  1042. 'quota' => null,
  1043. 'subadmin' => [],
  1044. 'email' => null,
  1045. 'isRestoreDisabled' => false,
  1046. 'isAvatarAvailable' => true,
  1047. 'isEnabled' => true,
  1048. ),
  1049. Http::STATUS_CREATED
  1050. );
  1051. $response = $controller->create('foo', 'password', ['SubGroup1', 'ExistingGroup']);
  1052. $this->assertEquals($expectedResponse, $response);
  1053. }
  1054. public function testCreateUnsuccessfulAdmin() {
  1055. $controller = $this->getController(true);
  1056. $this->userManager
  1057. ->method('createUser')
  1058. ->will($this->throwException(new \Exception()));
  1059. $expectedResponse = new DataResponse(
  1060. array(
  1061. 'message' => 'Unable to create user.'
  1062. ),
  1063. Http::STATUS_FORBIDDEN
  1064. );
  1065. $response = $controller->create('foo', 'password', array());
  1066. $this->assertEquals($expectedResponse, $response);
  1067. }
  1068. public function testCreateUnsuccessfulSubAdminNoGroup() {
  1069. $controller = $this->getController(false);
  1070. $user = $this->createMock(IUser::class);
  1071. $user->expects($this->any())
  1072. ->method('getUID')
  1073. ->will($this->returnValue('username'));
  1074. $this->userSession->expects($this->once())
  1075. ->method('getUser')
  1076. ->will($this->returnValue($user));
  1077. $this->userManager->expects($this->never())
  1078. ->method('createUser');
  1079. $expectedResponse = new DataResponse(
  1080. [
  1081. 'message' => 'No valid group selected'
  1082. ],
  1083. Http::STATUS_FORBIDDEN
  1084. );
  1085. $response = $controller->create('foo', 'password', []);
  1086. $this->assertEquals($expectedResponse, $response);
  1087. }
  1088. public function testCreateUnsuccessfulSubAdmin() {
  1089. $controller = $this->getController(false);
  1090. $user = $this->createMock(IUser::class);
  1091. $user->expects($this->any())
  1092. ->method('getUID')
  1093. ->will($this->returnValue('username'));
  1094. $this->userSession->expects($this->once())
  1095. ->method('getUser')
  1096. ->will($this->returnValue($user));
  1097. $this->userManager
  1098. ->method('createUser')
  1099. ->will($this->throwException(new \Exception()));
  1100. $subgroup1 = $this->createMock(IGroup::class);
  1101. $subgroup2 = $this->createMock(IGroup::class);
  1102. $subadmin = $this->createMock(\OC\SubAdmin::class);
  1103. $subadmin->expects($this->atLeastOnce())
  1104. ->method('isSubAdminofGroup')
  1105. ->willReturnMap([
  1106. [$user, $subgroup1, true],
  1107. [$user, $subgroup2, true],
  1108. ]);
  1109. $this->groupManager->expects($this->any())
  1110. ->method('getSubAdmin')
  1111. ->willReturn($subadmin);
  1112. $this->groupManager->expects($this->atLeastOnce())
  1113. ->method('get')
  1114. ->willReturnMap([
  1115. ['SubGroup1', $subgroup1],
  1116. ['SubGroup2', $subgroup2],
  1117. ]);
  1118. $expectedResponse = new DataResponse(
  1119. [
  1120. 'message' => 'Unable to create user.'
  1121. ],
  1122. Http::STATUS_FORBIDDEN
  1123. );
  1124. $response = $controller->create('foo', 'password', array('SubGroup1', 'SubGroup2'));
  1125. $this->assertEquals($expectedResponse, $response);
  1126. }
  1127. public function testDestroySelfAdmin() {
  1128. $controller = $this->getController(true);
  1129. $user = $this->createMock(User::class);
  1130. $user
  1131. ->expects($this->once())
  1132. ->method('getUID')
  1133. ->will($this->returnValue('myself'));
  1134. $this->userSession
  1135. ->method('getUser')
  1136. ->will($this->returnValue($user));
  1137. $expectedResponse = new DataResponse(
  1138. array(
  1139. 'status' => 'error',
  1140. 'data' => array(
  1141. 'message' => 'Unable to delete user.'
  1142. )
  1143. ),
  1144. Http::STATUS_FORBIDDEN
  1145. );
  1146. $response = $controller->destroy('myself');
  1147. $this->assertEquals($expectedResponse, $response);
  1148. }
  1149. public function testDestroySelfSubadmin() {
  1150. $controller = $this->getController(false);
  1151. $user = $this->createMock(User::class);
  1152. $user
  1153. ->expects($this->once())
  1154. ->method('getUID')
  1155. ->will($this->returnValue('myself'));
  1156. $this->userSession
  1157. ->method('getUser')
  1158. ->will($this->returnValue($user));
  1159. $expectedResponse = new DataResponse(
  1160. array(
  1161. 'status' => 'error',
  1162. 'data' => array(
  1163. 'message' => 'Unable to delete user.'
  1164. )
  1165. ),
  1166. Http::STATUS_FORBIDDEN
  1167. );
  1168. $response = $controller->destroy('myself');
  1169. $this->assertEquals($expectedResponse, $response);
  1170. }
  1171. public function testDestroyAdmin() {
  1172. $controller = $this->getController(true);
  1173. $user = $this->createMock(User::class);
  1174. $user
  1175. ->expects($this->once())
  1176. ->method('getUID')
  1177. ->will($this->returnValue('Admin'));
  1178. $toDeleteUser = $this->createMock(User::class);
  1179. $toDeleteUser
  1180. ->expects($this->once())
  1181. ->method('delete')
  1182. ->will($this->returnValue(true));
  1183. $this->userSession
  1184. ->method('getUser')
  1185. ->will($this->returnValue($user));
  1186. $this->userManager
  1187. ->method('get')
  1188. ->with('UserToDelete')
  1189. ->will($this->returnValue($toDeleteUser));
  1190. $expectedResponse = new DataResponse(
  1191. array(
  1192. 'status' => 'success',
  1193. 'data' => array(
  1194. 'username' => 'UserToDelete'
  1195. )
  1196. ),
  1197. Http::STATUS_NO_CONTENT
  1198. );
  1199. $response = $controller->destroy('UserToDelete');
  1200. $this->assertEquals($expectedResponse, $response);
  1201. }
  1202. public function testDestroySubAdmin() {
  1203. $controller = $this->getController(false);
  1204. $user = $this->createMock(User::class);
  1205. $user
  1206. ->expects($this->once())
  1207. ->method('getUID')
  1208. ->will($this->returnValue('myself'));
  1209. $this->userSession
  1210. ->method('getUser')
  1211. ->will($this->returnValue($user));
  1212. $user = $this->createMock(User::class);
  1213. $toDeleteUser = $this->createMock(User::class);
  1214. $toDeleteUser
  1215. ->expects($this->once())
  1216. ->method('delete')
  1217. ->will($this->returnValue(true));
  1218. $this->userSession
  1219. ->method('getUser')
  1220. ->will($this->returnValue($user));
  1221. $this->userManager
  1222. ->method('get')
  1223. ->with('UserToDelete')
  1224. ->will($this->returnValue($toDeleteUser));
  1225. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1226. ->disableOriginalConstructor()
  1227. ->getMock();
  1228. $subadmin->expects($this->once())
  1229. ->method('isUserAccessible')
  1230. ->with($user, $toDeleteUser)
  1231. ->will($this->returnValue(true));
  1232. $this->groupManager
  1233. ->expects($this->any())
  1234. ->method('getSubAdmin')
  1235. ->will($this->returnValue($subadmin));
  1236. $expectedResponse = new DataResponse(
  1237. [
  1238. 'status' => 'success',
  1239. 'data' => [
  1240. 'username' => 'UserToDelete'
  1241. ]
  1242. ],
  1243. Http::STATUS_NO_CONTENT
  1244. );
  1245. $response = $controller->destroy('UserToDelete');
  1246. $this->assertEquals($expectedResponse, $response);
  1247. }
  1248. public function testDestroyUnsuccessfulAdmin() {
  1249. $controller = $this->getController(true);
  1250. $user = $this->createMock(User::class);
  1251. $user
  1252. ->expects($this->once())
  1253. ->method('getUID')
  1254. ->will($this->returnValue('Admin'));
  1255. $toDeleteUser = $this->createMock(User::class);
  1256. $toDeleteUser
  1257. ->expects($this->once())
  1258. ->method('delete')
  1259. ->will($this->returnValue(false));
  1260. $this->userSession
  1261. ->method('getUser')
  1262. ->will($this->returnValue($user));
  1263. $this->userManager
  1264. ->method('get')
  1265. ->with('UserToDelete')
  1266. ->will($this->returnValue($toDeleteUser));
  1267. $expectedResponse = new DataResponse(
  1268. array(
  1269. 'status' => 'error',
  1270. 'data' => array(
  1271. 'message' => 'Unable to delete user.'
  1272. )
  1273. ),
  1274. Http::STATUS_FORBIDDEN
  1275. );
  1276. $response = $controller->destroy('UserToDelete');
  1277. $this->assertEquals($expectedResponse, $response);
  1278. }
  1279. public function testDestroyUnsuccessfulSubAdmin() {
  1280. $controller = $this->getController(false);
  1281. $user = $this->createMock(User::class);
  1282. $user
  1283. ->expects($this->once())
  1284. ->method('getUID')
  1285. ->will($this->returnValue('myself'));
  1286. $this->userSession
  1287. ->method('getUser')
  1288. ->will($this->returnValue($user));
  1289. $toDeleteUser = $this->createMock(User::class);
  1290. $toDeleteUser
  1291. ->expects($this->once())
  1292. ->method('delete')
  1293. ->will($this->returnValue(false));
  1294. $this->userSession
  1295. ->method('getUser')
  1296. ->will($this->returnValue($user));
  1297. $this->userManager
  1298. ->method('get')
  1299. ->with('UserToDelete')
  1300. ->will($this->returnValue($toDeleteUser));
  1301. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1302. ->disableOriginalConstructor()
  1303. ->getMock();
  1304. $subadmin->expects($this->once())
  1305. ->method('isUserAccessible')
  1306. ->with($user, $toDeleteUser)
  1307. ->will($this->returnValue(true));
  1308. $this->groupManager
  1309. ->expects($this->any())
  1310. ->method('getSubAdmin')
  1311. ->will($this->returnValue($subadmin));
  1312. $expectedResponse = new DataResponse(
  1313. [
  1314. 'status' => 'error',
  1315. 'data' => [
  1316. 'message' => 'Unable to delete user.'
  1317. ]
  1318. ],
  1319. Http::STATUS_FORBIDDEN
  1320. );
  1321. $response = $controller->destroy('UserToDelete');
  1322. $this->assertEquals($expectedResponse, $response);
  1323. }
  1324. public function testDestroyNotAccessibleToSubAdmin() {
  1325. $controller = $this->getController(false);
  1326. $user = $this->createMock(User::class);
  1327. $user
  1328. ->expects($this->once())
  1329. ->method('getUID')
  1330. ->will($this->returnValue('myself'));
  1331. $this->userSession
  1332. ->method('getUser')
  1333. ->will($this->returnValue($user));
  1334. $toDeleteUser = $this->createMock(User::class);
  1335. $this->userSession
  1336. ->method('getUser')
  1337. ->will($this->returnValue($user));
  1338. $this->userManager
  1339. ->method('get')
  1340. ->with('UserToDelete')
  1341. ->will($this->returnValue($toDeleteUser));
  1342. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1343. ->disableOriginalConstructor()
  1344. ->getMock();
  1345. $subadmin->expects($this->once())
  1346. ->method('isUserAccessible')
  1347. ->with($user, $toDeleteUser)
  1348. ->will($this->returnValue(false));
  1349. $this->groupManager
  1350. ->expects($this->any())
  1351. ->method('getSubAdmin')
  1352. ->will($this->returnValue($subadmin));
  1353. $expectedResponse = new DataResponse(
  1354. [
  1355. 'status' => 'error',
  1356. 'data' => [
  1357. 'message' => 'Authentication error'
  1358. ]
  1359. ],
  1360. Http::STATUS_FORBIDDEN
  1361. );
  1362. $response = $controller->destroy('UserToDelete');
  1363. $this->assertEquals($expectedResponse, $response);
  1364. }
  1365. /**
  1366. * test if an invalid mail result in a failure response
  1367. */
  1368. public function testCreateUnsuccessfulWithInvalidEmailAdmin() {
  1369. $controller = $this->getController(true);
  1370. $expectedResponse = new DataResponse([
  1371. 'message' => 'Invalid mail address',
  1372. ],
  1373. Http::STATUS_UNPROCESSABLE_ENTITY
  1374. );
  1375. $response = $controller->create('foo', 'password', [], 'invalidMailAdress');
  1376. $this->assertEquals($expectedResponse, $response);
  1377. }
  1378. /**
  1379. * test if a valid mail result in a successful mail send
  1380. */
  1381. public function testCreateSuccessfulWithValidEmailAdmin() {
  1382. $controller = $this->getController(true);
  1383. $this->mailer
  1384. ->expects($this->at(0))
  1385. ->method('validateMailAddress')
  1386. ->with('validMail@Adre.ss')
  1387. ->will($this->returnValue(true));
  1388. $user = $this->createMock(User::class);
  1389. $user
  1390. ->method('getHome')
  1391. ->will($this->returnValue('/home/user'));
  1392. $user
  1393. ->method('getHome')
  1394. ->will($this->returnValue('/home/user'));
  1395. $user
  1396. ->method('getUID')
  1397. ->will($this->returnValue('foo'));
  1398. $user
  1399. ->method('getDisplayName')
  1400. ->will($this->returnValue('foo'));
  1401. $user
  1402. ->expects($this->once())
  1403. ->method('getBackendClassName')
  1404. ->will($this->returnValue('bar'));
  1405. $emailTemplate = $this->createMock(IEMailTemplate::class);
  1406. $this->newUserMailHelper
  1407. ->expects($this->at(0))
  1408. ->method('generateTemplate')
  1409. ->with($user, false)
  1410. ->willReturn($emailTemplate);
  1411. $this->newUserMailHelper
  1412. ->expects($this->at(1))
  1413. ->method('sendMail')
  1414. ->with($user, $emailTemplate);
  1415. $this->userManager
  1416. ->expects($this->once())
  1417. ->method('createUser')
  1418. ->will($this->onConsecutiveCalls($user));
  1419. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1420. ->disableOriginalConstructor()
  1421. ->getMock();
  1422. $subadmin->expects($this->once())
  1423. ->method('getSubAdminsGroups')
  1424. ->with($user)
  1425. ->will($this->returnValue([]));
  1426. $this->groupManager
  1427. ->expects($this->any())
  1428. ->method('getSubAdmin')
  1429. ->will($this->returnValue($subadmin));
  1430. $response = $controller->create('foo', 'password', [], 'validMail@Adre.ss');
  1431. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  1432. }
  1433. private function mockUser($userId = 'foo', $displayName = 'M. Foo',
  1434. $lastLogin = 500, $home = '/home/foo',
  1435. $backend = 'OC_User_Database', $enabled = true) {
  1436. $user = $this->createMock(User::class);
  1437. $user
  1438. ->expects($this->any())
  1439. ->method('getUID')
  1440. ->will($this->returnValue($userId));
  1441. $user
  1442. ->expects($this->once())
  1443. ->method('getDisplayName')
  1444. ->will($this->returnValue($displayName));
  1445. $user
  1446. ->method('getLastLogin')
  1447. ->will($this->returnValue($lastLogin));
  1448. $user
  1449. ->method('getHome')
  1450. ->will($this->returnValue($home));
  1451. $user
  1452. ->expects($this->once())
  1453. ->method('getBackendClassName')
  1454. ->will($this->returnValue($backend));
  1455. $user->expects($this->any())
  1456. ->method('isEnabled')
  1457. ->willReturn($enabled);
  1458. $result = [
  1459. 'name' => $userId,
  1460. 'displayname' => $displayName,
  1461. 'groups' => null,
  1462. 'subadmin' => array(),
  1463. 'quota' => null,
  1464. 'storageLocation' => $home,
  1465. 'lastLogin' => $lastLogin * 1000,
  1466. 'backend' => $backend,
  1467. 'email' => null,
  1468. 'isRestoreDisabled' => false,
  1469. 'isAvatarAvailable' => true,
  1470. 'isEnabled' => $enabled,
  1471. ];
  1472. return [$user, $result];
  1473. }
  1474. public function testRestorePossibleWithoutEncryption() {
  1475. $controller = $this->getController(true);
  1476. list($user, $expectedResult) = $this->mockUser();
  1477. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1478. ->disableOriginalConstructor()
  1479. ->getMock();
  1480. $subadmin->expects($this->once())
  1481. ->method('getSubAdminsGroups')
  1482. ->with($user)
  1483. ->will($this->returnValue([]));
  1484. $this->groupManager
  1485. ->expects($this->any())
  1486. ->method('getSubAdmin')
  1487. ->will($this->returnValue($subadmin));
  1488. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1489. $this->assertEquals($expectedResult, $result);
  1490. }
  1491. public function testRestorePossibleWithAdminAndUserRestore() {
  1492. list($user, $expectedResult) = $this->mockUser();
  1493. $this->appManager
  1494. ->expects($this->once())
  1495. ->method('isEnabledForUser')
  1496. ->with(
  1497. $this->equalTo('encryption')
  1498. )
  1499. ->will($this->returnValue(true));
  1500. $this->config
  1501. ->expects($this->once())
  1502. ->method('getAppValue')
  1503. ->with(
  1504. $this->equalTo('encryption'),
  1505. $this->equalTo('recoveryAdminEnabled'),
  1506. $this->anything()
  1507. )
  1508. ->will($this->returnValue('1'));
  1509. $this->config
  1510. ->expects($this->at(1))
  1511. ->method('getUserValue')
  1512. ->with(
  1513. $this->anything(),
  1514. $this->equalTo('encryption'),
  1515. $this->equalTo('recoveryEnabled'),
  1516. $this->anything()
  1517. )
  1518. ->will($this->returnValue('1'));
  1519. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1520. ->disableOriginalConstructor()
  1521. ->getMock();
  1522. $subadmin->expects($this->once())
  1523. ->method('getSubAdminsGroups')
  1524. ->with($user)
  1525. ->will($this->returnValue([]));
  1526. $this->groupManager
  1527. ->expects($this->any())
  1528. ->method('getSubAdmin')
  1529. ->will($this->returnValue($subadmin));
  1530. $controller = $this->getController(true);
  1531. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1532. $this->assertEquals($expectedResult, $result);
  1533. }
  1534. public function testRestoreNotPossibleWithoutAdminRestore() {
  1535. list($user, $expectedResult) = $this->mockUser();
  1536. $this->appManager
  1537. ->method('isEnabledForUser')
  1538. ->with(
  1539. $this->equalTo('encryption')
  1540. )
  1541. ->will($this->returnValue(true));
  1542. $expectedResult['isRestoreDisabled'] = true;
  1543. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1544. ->disableOriginalConstructor()
  1545. ->getMock();
  1546. $subadmin->expects($this->once())
  1547. ->method('getSubAdminsGroups')
  1548. ->with($user)
  1549. ->will($this->returnValue([]));
  1550. $this->groupManager
  1551. ->expects($this->any())
  1552. ->method('getSubAdmin')
  1553. ->will($this->returnValue($subadmin));
  1554. $controller = $this->getController(true);
  1555. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1556. $this->assertEquals($expectedResult, $result);
  1557. }
  1558. public function testRestoreNotPossibleWithoutUserRestore() {
  1559. list($user, $expectedResult) = $this->mockUser();
  1560. $this->appManager
  1561. ->expects($this->once())
  1562. ->method('isEnabledForUser')
  1563. ->with(
  1564. $this->equalTo('encryption')
  1565. )
  1566. ->will($this->returnValue(true));
  1567. $this->config
  1568. ->expects($this->once())
  1569. ->method('getAppValue')
  1570. ->with(
  1571. $this->equalTo('encryption'),
  1572. $this->equalTo('recoveryAdminEnabled'),
  1573. $this->anything()
  1574. )
  1575. ->will($this->returnValue('1'));
  1576. $this->config
  1577. ->expects($this->at(1))
  1578. ->method('getUserValue')
  1579. ->with(
  1580. $this->anything(),
  1581. $this->equalTo('encryption'),
  1582. $this->equalTo('recoveryEnabled'),
  1583. $this->anything()
  1584. )
  1585. ->will($this->returnValue('0'));
  1586. $expectedResult['isRestoreDisabled'] = true;
  1587. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1588. ->disableOriginalConstructor()
  1589. ->getMock();
  1590. $subadmin->expects($this->once())
  1591. ->method('getSubAdminsGroups')
  1592. ->with($user)
  1593. ->will($this->returnValue([]));
  1594. $this->groupManager
  1595. ->expects($this->any())
  1596. ->method('getSubAdmin')
  1597. ->will($this->returnValue($subadmin));
  1598. $controller = $this->getController(true);
  1599. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1600. $this->assertEquals($expectedResult, $result);
  1601. }
  1602. public function testNoAvatar() {
  1603. $controller = $this->getController(true);
  1604. list($user, $expectedResult) = $this->mockUser();
  1605. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1606. ->disableOriginalConstructor()
  1607. ->getMock();
  1608. $subadmin->expects($this->once())
  1609. ->method('getSubAdminsGroups')
  1610. ->with($user)
  1611. ->will($this->returnValue([]));
  1612. $this->groupManager
  1613. ->expects($this->any())
  1614. ->method('getSubAdmin')
  1615. ->will($this->returnValue($subadmin));
  1616. $this->avatarManager
  1617. ->method('getAvatar')
  1618. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  1619. $expectedResult['isAvatarAvailable'] = false;
  1620. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1621. $this->assertEquals($expectedResult, $result);
  1622. }
  1623. public function testStatsAdmin() {
  1624. $controller = $this->getController(true);
  1625. $this->userManager
  1626. ->expects($this->at(0))
  1627. ->method('countUsers')
  1628. ->will($this->returnValue([128, 44]));
  1629. $expectedResponse = new DataResponse(
  1630. [
  1631. 'totalUsers' => 172
  1632. ]
  1633. );
  1634. $response = $controller->stats();
  1635. $this->assertEquals($expectedResponse, $response);
  1636. }
  1637. /**
  1638. * Tests that the subadmin stats return unique users, even
  1639. * when a user appears in several groups.
  1640. */
  1641. public function testStatsSubAdmin() {
  1642. $controller = $this->getController(false);
  1643. $user = $this->createMock(User::class);
  1644. $this->userSession
  1645. ->expects($this->once())
  1646. ->method('getUser')
  1647. ->will($this->returnValue($user));
  1648. $group1 = $this->getMockBuilder('\OC\Group\Group')
  1649. ->disableOriginalConstructor()->getMock();
  1650. $group1
  1651. ->expects($this->once())
  1652. ->method('getUsers')
  1653. ->will($this->returnValue(['foo' => 'M. Foo', 'admin' => 'S. Admin']));
  1654. $group2 = $this->getMockBuilder('\OC\Group\Group')
  1655. ->disableOriginalConstructor()->getMock();
  1656. $group2
  1657. ->expects($this->once())
  1658. ->method('getUsers')
  1659. ->will($this->returnValue(['bar' => 'B. Ar']));
  1660. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1661. ->disableOriginalConstructor()
  1662. ->getMock();
  1663. $subadmin
  1664. ->expects($this->at(0))
  1665. ->method('getSubAdminsGroups')
  1666. ->will($this->returnValue([$group1, $group2]));
  1667. $this->groupManager
  1668. ->expects($this->any())
  1669. ->method('getSubAdmin')
  1670. ->will($this->returnValue($subadmin));
  1671. $expectedResponse = new DataResponse(
  1672. [
  1673. 'totalUsers' => 3
  1674. ]
  1675. );
  1676. $response = $controller->stats();
  1677. $this->assertEquals($expectedResponse, $response);
  1678. }
  1679. public function testSetDisplayNameNull() {
  1680. $user = $this->createMock(IUser::class);
  1681. $user->method('getUID')->willReturn('userName');
  1682. $this->userSession
  1683. ->expects($this->once())
  1684. ->method('getUser')
  1685. ->willReturn($user);
  1686. $expectedResponse = new DataResponse(
  1687. [
  1688. 'status' => 'error',
  1689. 'data' => [
  1690. 'message' => 'Authentication error',
  1691. ],
  1692. ]
  1693. );
  1694. $controller = $this->getController(true);
  1695. $response = $controller->setDisplayName(null, 'displayName');
  1696. $this->assertEquals($expectedResponse, $response);
  1697. }
  1698. public function dataSetDisplayName() {
  1699. $data = [];
  1700. $user1 = $this->createMock(IUser::class);
  1701. $user1->method('getUID')->willReturn('user1');
  1702. $user1->method('canChangeDisplayName')->willReturn(true);
  1703. $data[] = [$user1, $user1, false, false, true];
  1704. $user1 = $this->createMock(IUser::class);
  1705. $user1->method('getUID')->willReturn('user1');
  1706. $user1->method('canChangeDisplayName')->willReturn(false);
  1707. $data[] = [$user1, $user1, false, false, false];
  1708. $user1 = $this->createMock(IUser::class);
  1709. $user1->method('getUID')->willReturn('user1');
  1710. $user2 = $this->createMock(IUser::class);
  1711. $user2->method('getUID')->willReturn('user2');
  1712. $user2->method('canChangeDisplayName')->willReturn(true);
  1713. $data[] = [$user1, $user2, false, false, false];
  1714. $user1 = $this->createMock(IUser::class);
  1715. $user1->method('getUID')->willReturn('user1');
  1716. $user2 = $this->createMock(IUser::class);
  1717. $user2->method('getUID')->willReturn('user2');
  1718. $user2->method('canChangeDisplayName')->willReturn(true);
  1719. $data[] = [$user1, $user2, true, false, true];
  1720. $user1 = $this->createMock(IUser::class);
  1721. $user1->method('getUID')->willReturn('user1');
  1722. $user2 = $this->createMock(IUser::class);
  1723. $user2->method('getUID')->willReturn('user2');
  1724. $user2->method('canChangeDisplayName')->willReturn(true);
  1725. $data[] = [$user1, $user2, false, true, true];
  1726. return $data;
  1727. }
  1728. /**
  1729. * @dataProvider dataSetDisplayName
  1730. *
  1731. * @param IUser|\PHPUnit_Framework_MockObject_MockObject $currentUser
  1732. * @param IUser|\PHPUnit_Framework_MockObject_MockObject $editUser
  1733. * @param bool $isAdmin
  1734. * @param bool $isSubAdmin
  1735. * @param bool $valid
  1736. */
  1737. public function testSetDisplayName($currentUser, $editUser, $isAdmin, $isSubAdmin, $valid) {
  1738. $this->userSession
  1739. ->expects($this->once())
  1740. ->method('getUser')
  1741. ->willReturn($currentUser);
  1742. $this->userManager
  1743. ->expects($this->once())
  1744. ->method('get')
  1745. ->with($editUser->getUID())
  1746. ->willReturn($editUser);
  1747. $this->accountManager->expects($this->any())->method('getUser')->willReturn([]);
  1748. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1749. ->disableOriginalConstructor()
  1750. ->getMock();
  1751. $subadmin
  1752. ->method('isUserAccessible')
  1753. ->with($currentUser, $editUser)
  1754. ->willReturn($isSubAdmin);
  1755. $this->groupManager
  1756. ->method('getSubAdmin')
  1757. ->willReturn($subadmin);
  1758. $this->groupManager
  1759. ->method('isAdmin')
  1760. ->with($currentUser->getUID())
  1761. ->willReturn($isAdmin);
  1762. if ($valid === true) {
  1763. $expectedResponse = new DataResponse(
  1764. [
  1765. 'status' => 'success',
  1766. 'data' => [
  1767. 'message' => 'Your full name has been changed.',
  1768. 'username' => $editUser->getUID(),
  1769. 'displayName' => 'newDisplayName',
  1770. ],
  1771. ]
  1772. );
  1773. } else {
  1774. $expectedResponse = new DataResponse(
  1775. [
  1776. 'status' => 'error',
  1777. 'data' => [
  1778. 'message' => 'Authentication error',
  1779. ],
  1780. ]
  1781. );
  1782. }
  1783. $controller = $this->getController(true);
  1784. $response = $controller->setDisplayName($editUser->getUID(), 'newDisplayName');
  1785. $this->assertEquals($expectedResponse, $response);
  1786. }
  1787. public function testSetDisplayNameFails() {
  1788. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  1789. $user = $this->createMock(IUser::class);
  1790. $user->method('canChangeDisplayname')->willReturn(true);
  1791. $user->method('getUID')->willReturn('user');
  1792. $user->expects($this->once())
  1793. ->method('setDisplayName')
  1794. ->with('newDisplayName')
  1795. ->willReturn(false);
  1796. $user->method('getDisplayName')->willReturn('oldDisplayName');
  1797. $this->userSession
  1798. ->expects($this->once())
  1799. ->method('getUser')
  1800. ->willReturn($user);
  1801. $this->userManager
  1802. ->expects($this->once())
  1803. ->method('get')
  1804. ->with($user->getUID())
  1805. ->willReturn($user);
  1806. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1807. ->disableOriginalConstructor()
  1808. ->getMock();
  1809. $subadmin
  1810. ->method('isUserAccessible')
  1811. ->with($user, $user)
  1812. ->willReturn(false);
  1813. $this->groupManager
  1814. ->method('getSubAdmin')
  1815. ->willReturn($subadmin);
  1816. $this->groupManager
  1817. ->expects($this->once())
  1818. ->method('isAdmin')
  1819. ->with($user->getUID())
  1820. ->willReturn(false);
  1821. $expectedResponse = new DataResponse(
  1822. [
  1823. 'status' => 'error',
  1824. 'data' => [
  1825. 'message' => 'Unable to change full name',
  1826. 'displayName' => 'oldDisplayName',
  1827. ],
  1828. ]
  1829. );
  1830. $controller = $this->getController(true);
  1831. $response = $controller->setDisplayName($user->getUID(), 'newDisplayName');
  1832. $this->assertEquals($expectedResponse, $response);
  1833. }
  1834. /**
  1835. * @dataProvider dataTestSetUserSettings
  1836. *
  1837. * @param string $email
  1838. * @param bool $validEmail
  1839. * @param $expectedStatus
  1840. */
  1841. public function testSetUserSettings($email, $validEmail, $expectedStatus) {
  1842. $controller = $this->getController(false, ['saveUserSettings']);
  1843. $user = $this->createMock(IUser::class);
  1844. $this->userSession->method('getUser')->willReturn($user);
  1845. if (!empty($email) && $validEmail) {
  1846. $this->mailer->expects($this->once())->method('validateMailAddress')
  1847. ->willReturn($validEmail);
  1848. }
  1849. $saveData = (!empty($email) && $validEmail) || empty($email);
  1850. if ($saveData) {
  1851. $controller->expects($this->once())->method('saveUserSettings');
  1852. } else {
  1853. $controller->expects($this->never())->method('saveUserSettings');
  1854. }
  1855. $result = $controller->setUserSettings(
  1856. AccountManager::VISIBILITY_CONTACTS_ONLY,
  1857. 'displayName',
  1858. AccountManager::VISIBILITY_CONTACTS_ONLY,
  1859. '47658468',
  1860. AccountManager::VISIBILITY_CONTACTS_ONLY,
  1861. $email,
  1862. AccountManager::VISIBILITY_CONTACTS_ONLY,
  1863. 'nextcloud.com',
  1864. AccountManager::VISIBILITY_CONTACTS_ONLY,
  1865. 'street and city',
  1866. AccountManager::VISIBILITY_CONTACTS_ONLY,
  1867. '@nextclouders',
  1868. AccountManager::VISIBILITY_CONTACTS_ONLY
  1869. );
  1870. $this->assertSame($expectedStatus, $result->getStatus());
  1871. }
  1872. public function dataTestSetUserSettings() {
  1873. return [
  1874. ['', true, Http::STATUS_OK],
  1875. ['', false, Http::STATUS_OK],
  1876. ['example.com', false, Http::STATUS_UNPROCESSABLE_ENTITY],
  1877. ['john@example.com', true, Http::STATUS_OK],
  1878. ];
  1879. }
  1880. /**
  1881. * @dataProvider dataTestSaveUserSettings
  1882. *
  1883. * @param array $data
  1884. * @param string $oldEmailAddress
  1885. * @param string $oldDisplayName
  1886. */
  1887. public function testSaveUserSettings($data,
  1888. $oldEmailAddress,
  1889. $oldDisplayName
  1890. ) {
  1891. $controller = $this->getController();
  1892. $user = $this->createMock(IUser::class);
  1893. $user->method('getDisplayName')->willReturn($oldDisplayName);
  1894. $user->method('getEMailAddress')->willReturn($oldEmailAddress);
  1895. $user->method('canChangeDisplayName')->willReturn(true);
  1896. if ($data[AccountManager::PROPERTY_EMAIL]['value'] === $oldEmailAddress ||
  1897. ($oldEmailAddress === null && $data[AccountManager::PROPERTY_EMAIL]['value'] === '')) {
  1898. $user->expects($this->never())->method('setEMailAddress');
  1899. } else {
  1900. $user->expects($this->once())->method('setEMailAddress')
  1901. ->with($data[AccountManager::PROPERTY_EMAIL]['value'])
  1902. ->willReturn(true);
  1903. }
  1904. if ($data[AccountManager::PROPERTY_DISPLAYNAME]['value'] === $oldDisplayName ||
  1905. ($oldDisplayName === null && $data[AccountManager::PROPERTY_DISPLAYNAME]['value'] === '')) {
  1906. $user->expects($this->never())->method('setDisplayName');
  1907. } else {
  1908. $user->expects($this->once())->method('setDisplayName')
  1909. ->with($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
  1910. ->willReturn(true);
  1911. }
  1912. $this->accountManager->expects($this->once())->method('updateUser')
  1913. ->with($user, $data);
  1914. $this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
  1915. }
  1916. public function dataTestSaveUserSettings() {
  1917. return [
  1918. [
  1919. [
  1920. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  1921. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  1922. ],
  1923. 'john@example.com',
  1924. 'john doe'
  1925. ],
  1926. [
  1927. [
  1928. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  1929. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  1930. ],
  1931. 'johnNew@example.com',
  1932. 'john New doe'
  1933. ],
  1934. [
  1935. [
  1936. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  1937. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  1938. ],
  1939. 'johnNew@example.com',
  1940. 'john doe'
  1941. ],
  1942. [
  1943. [
  1944. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  1945. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  1946. ],
  1947. 'john@example.com',
  1948. 'john New doe'
  1949. ],
  1950. [
  1951. [
  1952. AccountManager::PROPERTY_EMAIL => ['value' => ''],
  1953. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  1954. ],
  1955. null,
  1956. 'john New doe'
  1957. ],
  1958. [
  1959. [
  1960. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  1961. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  1962. ],
  1963. 'john@example.com',
  1964. null
  1965. ],
  1966. ];
  1967. }
  1968. /**
  1969. * @dataProvider dataTestSaveUserSettingsException
  1970. *
  1971. * @param array $data
  1972. * @param string $oldEmailAddress
  1973. * @param string $oldDisplayName
  1974. * @param bool $setDisplayNameResult
  1975. * @param bool $canChangeEmail
  1976. *
  1977. * @expectedException \OC\ForbiddenException
  1978. */
  1979. public function testSaveUserSettingsException($data,
  1980. $oldEmailAddress,
  1981. $oldDisplayName,
  1982. $setDisplayNameResult,
  1983. $canChangeEmail
  1984. ) {
  1985. $controller = $this->getController();
  1986. $user = $this->createMock(IUser::class);
  1987. $user->method('getDisplayName')->willReturn($oldDisplayName);
  1988. $user->method('getEMailAddress')->willReturn($oldEmailAddress);
  1989. if ($data[AccountManager::PROPERTY_EMAIL]['value'] !== $oldEmailAddress) {
  1990. $user->method('canChangeDisplayName')
  1991. ->willReturn($canChangeEmail);
  1992. }
  1993. if ($data[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $oldDisplayName) {
  1994. $user->method('setDisplayName')
  1995. ->with($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
  1996. ->willReturn($setDisplayNameResult);
  1997. }
  1998. $this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
  1999. }
  2000. public function dataTestSaveUserSettingsException() {
  2001. return [
  2002. [
  2003. [
  2004. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  2005. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  2006. ],
  2007. 'johnNew@example.com',
  2008. 'john New doe',
  2009. true,
  2010. false
  2011. ],
  2012. [
  2013. [
  2014. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  2015. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  2016. ],
  2017. 'johnNew@example.com',
  2018. 'john New doe',
  2019. false,
  2020. true
  2021. ],
  2022. [
  2023. [
  2024. AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  2025. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  2026. ],
  2027. 'johnNew@example.com',
  2028. 'john New doe',
  2029. false,
  2030. false
  2031. ],
  2032. ];
  2033. }
  2034. /**
  2035. * @return array
  2036. */
  2037. public function setEmailAddressData() {
  2038. return [
  2039. /* mailAddress, isValid, expectsUpdate, canChangeDisplayName, responseCode */
  2040. [ '', true, true, true, Http::STATUS_OK ],
  2041. [ 'foo@local', true, true, true, Http::STATUS_OK],
  2042. [ 'foo@bar@local', false, false, true, Http::STATUS_UNPROCESSABLE_ENTITY],
  2043. [ 'foo@local', true, false, false, Http::STATUS_FORBIDDEN],
  2044. ];
  2045. }
  2046. /**
  2047. * @dataProvider setEmailAddressData
  2048. *
  2049. * @param string $mailAddress
  2050. * @param bool $isValid
  2051. * @param bool $expectsUpdate
  2052. * @param bool $canChangeDisplayName
  2053. * @param int $responseCode
  2054. */
  2055. public function testSetEMailAddress($mailAddress, $isValid, $expectsUpdate, $canChangeDisplayName, $responseCode) {
  2056. $user = $this->createMock(User::class);
  2057. $user
  2058. ->expects($this->any())
  2059. ->method('getUID')
  2060. ->will($this->returnValue('foo'));
  2061. $user
  2062. ->expects($this->any())
  2063. ->method('canChangeDisplayName')
  2064. ->will($this->returnValue($canChangeDisplayName));
  2065. $user
  2066. ->expects($expectsUpdate ? $this->once() : $this->never())
  2067. ->method('setEMailAddress')
  2068. ->with(
  2069. $this->equalTo($mailAddress)
  2070. );
  2071. $user->method('getEMailAddress')->willReturn('oldEmailAddress');
  2072. $this->mailer
  2073. ->expects($this->any())
  2074. ->method('validateMailAddress')
  2075. ->with($mailAddress)
  2076. ->willReturn($isValid);
  2077. if ($isValid) {
  2078. $user->expects($this->atLeastOnce())
  2079. ->method('canChangeDisplayName')
  2080. ->willReturn(true);
  2081. $this->userManager
  2082. ->expects($this->atLeastOnce())
  2083. ->method('get')
  2084. ->with('foo')
  2085. ->will($this->returnValue($user));
  2086. }
  2087. $controller = $this->getController(true);
  2088. $response = $controller->setEMailAddress($user->getUID(), $mailAddress);
  2089. $this->assertSame($responseCode, $response->getStatus());
  2090. }
  2091. public function testCreateUnsuccessfulWithoutPasswordAndEmail() {
  2092. $controller = $this->getController(true);
  2093. $expectedResponse = new DataResponse(
  2094. array(
  2095. 'message' => 'To send a password link to the user an email address is required.'
  2096. ),
  2097. Http::STATUS_UNPROCESSABLE_ENTITY
  2098. );
  2099. $response = $controller->create('foo', '', array(), '');
  2100. $this->assertEquals($expectedResponse, $response);
  2101. }
  2102. public function testCreateSuccessfulWithoutPasswordAndWithEmail() {
  2103. $user = $this->createMock(User::class);
  2104. $user
  2105. ->method('getHome')
  2106. ->willReturn('/home/user');
  2107. $user
  2108. ->method('getUID')
  2109. ->willReturn('foo');
  2110. $user
  2111. ->method('getDisplayName')
  2112. ->willReturn('John Doe');
  2113. $user
  2114. ->method('getEmailAddress')
  2115. ->willReturn('abc@example.org');
  2116. $user
  2117. ->expects($this->once())
  2118. ->method('getBackendClassName')
  2119. ->willReturn('bar');
  2120. $this->userManager
  2121. ->expects($this->once())
  2122. ->method('createUser')
  2123. ->will($this->onConsecutiveCalls($user));
  2124. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  2125. ->disableOriginalConstructor()
  2126. ->getMock();
  2127. $subadmin
  2128. ->expects($this->any())
  2129. ->method('getSubAdminsGroups')
  2130. ->with($user)
  2131. ->will($this->returnValue([]));
  2132. $this->groupManager
  2133. ->expects($this->any())
  2134. ->method('getSubAdmin')
  2135. ->will($this->returnValue($subadmin));
  2136. $controller = $this->getController(true);
  2137. $this->mailer
  2138. ->expects($this->at(0))
  2139. ->method('validateMailAddress')
  2140. ->with('abc@example.org')
  2141. ->will($this->returnValue(true));
  2142. $emailTemplate = $this->createMock(IEMailTemplate::class);
  2143. $this->newUserMailHelper
  2144. ->expects($this->at(0))
  2145. ->method('generateTemplate')
  2146. ->with($user, true)
  2147. ->willReturn($emailTemplate);
  2148. $this->newUserMailHelper
  2149. ->expects($this->at(1))
  2150. ->method('sendMail')
  2151. ->with($user, $emailTemplate);
  2152. $expectedResponse = new DataResponse(
  2153. [
  2154. 'name' => 'foo',
  2155. 'groups' => null,
  2156. 'storageLocation' => '/home/user',
  2157. 'backend' => 'bar',
  2158. 'lastLogin' => 0,
  2159. 'displayname' => 'John Doe',
  2160. 'quota' => null,
  2161. 'subadmin' => array(),
  2162. 'email' => 'abc@example.org',
  2163. 'isRestoreDisabled' => false,
  2164. 'isAvatarAvailable' => true,
  2165. ],
  2166. Http::STATUS_CREATED
  2167. );
  2168. $response = $controller->create('foo', '', array(), 'abc@example.org');
  2169. $this->assertEquals($expectedResponse, $response);
  2170. }
  2171. /**
  2172. * @param string $account
  2173. * @param string $type
  2174. * @param array $dataBefore
  2175. * @param array $expectedData
  2176. *
  2177. * @dataProvider dataTestGetVerificationCode
  2178. */
  2179. public function testGetVerificationCode($account, $type, $dataBefore, $expectedData, $onlyVerificationCode) {
  2180. $message = 'Use my Federated Cloud ID to share with me: user@nextcloud.com';
  2181. $signature = 'theSignature';
  2182. $code = $message . ' ' . $signature;
  2183. if($type === AccountManager::PROPERTY_TWITTER) {
  2184. $code = $message . ' ' . md5($signature);
  2185. }
  2186. $controller = $this->getController(false, ['signMessage', 'getCurrentTime']);
  2187. $user = $this->createMock(IUser::class);
  2188. $this->userSession->expects($this->once())->method('getUser')->willReturn($user);
  2189. $this->accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($dataBefore);
  2190. $user->expects($this->any())->method('getCloudId')->willReturn('user@nextcloud.com');
  2191. $user->expects($this->any())->method('getUID')->willReturn('uid');
  2192. $controller->expects($this->once())->method('signMessage')->with($user, $message)->willReturn($signature);
  2193. $controller->expects($this->any())->method('getCurrentTime')->willReturn(1234567);
  2194. if ($onlyVerificationCode === false) {
  2195. $this->accountManager->expects($this->once())->method('updateUser')->with($user, $expectedData);
  2196. $this->jobList->expects($this->once())->method('add')
  2197. ->with('OC\Settings\BackgroundJobs\VerifyUserData',
  2198. [
  2199. 'verificationCode' => $code,
  2200. 'data' => $dataBefore[$type]['value'],
  2201. 'type' => $type,
  2202. 'uid' => 'uid',
  2203. 'try' => 0,
  2204. 'lastRun' => 1234567
  2205. ]);
  2206. }
  2207. $result = $controller->getVerificationCode($account, $onlyVerificationCode);
  2208. $data = $result->getData();
  2209. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  2210. $this->assertSame($code, $data['code']);
  2211. }
  2212. public function dataTestGetVerificationCode() {
  2213. $accountDataBefore = [
  2214. AccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => AccountManager::NOT_VERIFIED],
  2215. AccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => AccountManager::NOT_VERIFIED, 'signature' => 'theSignature'],
  2216. ];
  2217. $accountDataAfterWebsite = [
  2218. AccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => AccountManager::VERIFICATION_IN_PROGRESS, 'signature' => 'theSignature'],
  2219. AccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => AccountManager::NOT_VERIFIED, 'signature' => 'theSignature'],
  2220. ];
  2221. $accountDataAfterTwitter = [
  2222. AccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => AccountManager::NOT_VERIFIED],
  2223. AccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => AccountManager::VERIFICATION_IN_PROGRESS, 'signature' => 'theSignature'],
  2224. ];
  2225. return [
  2226. ['verify-twitter', AccountManager::PROPERTY_TWITTER, $accountDataBefore, $accountDataAfterTwitter, false],
  2227. ['verify-website', AccountManager::PROPERTY_WEBSITE, $accountDataBefore, $accountDataAfterWebsite, false],
  2228. ['verify-twitter', AccountManager::PROPERTY_TWITTER, $accountDataBefore, $accountDataAfterTwitter, true],
  2229. ['verify-website', AccountManager::PROPERTY_WEBSITE, $accountDataBefore, $accountDataAfterWebsite, true],
  2230. ];
  2231. }
  2232. /**
  2233. * test get verification code in case no valid user was given
  2234. */
  2235. public function testGetVerificationCodeInvalidUser() {
  2236. $controller = $this->getController();
  2237. $this->userSession->expects($this->once())->method('getUser')->willReturn(null);
  2238. $result = $controller->getVerificationCode('account', false);
  2239. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  2240. }
  2241. public function testDisableUserFailsDueSameUser() {
  2242. $user = $this->getMockBuilder('\OC\User\User')
  2243. ->disableOriginalConstructor()->getMock();
  2244. $user->expects($this->once())
  2245. ->method('getUID')
  2246. ->will($this->returnValue('abc'));
  2247. $this->userSession
  2248. ->expects($this->once())
  2249. ->method('getUser')
  2250. ->will($this->returnValue($user));
  2251. $expectedResponse = new DataResponse(
  2252. [
  2253. 'status' => 'error',
  2254. 'data' => [
  2255. 'message' => 'Error while disabling user.',
  2256. ],
  2257. ],
  2258. Http::STATUS_FORBIDDEN
  2259. );
  2260. $response = $this->getController(true)->disable('abc');
  2261. $this->assertEquals($expectedResponse, $response);
  2262. }
  2263. public function testDisableUserFailsDueNoAdminAndNoSubadmin() {
  2264. $user = $this->getMockBuilder('\OC\User\User')
  2265. ->disableOriginalConstructor()->getMock();
  2266. $user->expects($this->once())
  2267. ->method('getUID')
  2268. ->will($this->returnValue('def'));
  2269. $this->userSession
  2270. ->expects($this->exactly(2))
  2271. ->method('getUser')
  2272. ->will($this->returnValue($user));
  2273. $user2 = $this->getMockBuilder('\OC\User\User')
  2274. ->disableOriginalConstructor()->getMock();
  2275. $user2->expects($this->never())
  2276. ->method('setEnabled');
  2277. $this->userManager
  2278. ->expects($this->once())
  2279. ->method('get')
  2280. ->with('abc')
  2281. ->willReturn($user2);
  2282. $subadmin = $this->createMock('\OC\SubAdmin');
  2283. $subadmin->expects($this->once())
  2284. ->method('isUserAccessible')
  2285. ->will($this->returnValue(false));
  2286. $this->groupManager
  2287. ->expects($this->once())
  2288. ->method('getSubAdmin')
  2289. ->willReturn($subadmin);
  2290. $expectedResponse = new DataResponse(
  2291. [
  2292. 'status' => 'error',
  2293. 'data' => [
  2294. 'message' => 'Authentication error',
  2295. ],
  2296. ],
  2297. Http::STATUS_FORBIDDEN
  2298. );
  2299. $response = $this->getController(false)->disable('abc');
  2300. $this->assertEquals($expectedResponse, $response);
  2301. }
  2302. public function testDisableUserFailsDueNoUser() {
  2303. $user = $this->getMockBuilder('\OC\User\User')
  2304. ->disableOriginalConstructor()->getMock();
  2305. $user->expects($this->once())
  2306. ->method('getUID')
  2307. ->will($this->returnValue('def'));
  2308. $this->userSession
  2309. ->expects($this->exactly(1))
  2310. ->method('getUser')
  2311. ->will($this->returnValue($user));
  2312. $this->userManager
  2313. ->expects($this->once())
  2314. ->method('get')
  2315. ->with('abc')
  2316. ->willReturn(null);
  2317. $this->groupManager
  2318. ->expects($this->never())
  2319. ->method('getSubAdmin');
  2320. $expectedResponse = new DataResponse(
  2321. [
  2322. 'status' => 'error',
  2323. 'data' => [
  2324. 'message' => 'Error while disabling user.',
  2325. ],
  2326. ]
  2327. );
  2328. $response = $this->getController(true)->disable('abc');
  2329. $this->assertEquals($expectedResponse, $response);
  2330. }
  2331. public function testDisableUserFailsDueNoUserForSubAdmin() {
  2332. $user = $this->getMockBuilder('\OC\User\User')
  2333. ->disableOriginalConstructor()->getMock();
  2334. $user->expects($this->once())
  2335. ->method('getUID')
  2336. ->will($this->returnValue('def'));
  2337. $this->userSession
  2338. ->expects($this->exactly(2))
  2339. ->method('getUser')
  2340. ->will($this->returnValue($user));
  2341. $this->userManager
  2342. ->expects($this->once())
  2343. ->method('get')
  2344. ->with('abc')
  2345. ->willReturn(null);
  2346. $subadmin = $this->createMock('\OC\SubAdmin');
  2347. $subadmin->expects($this->once())
  2348. ->method('isUserAccessible')
  2349. ->will($this->returnValue(true));
  2350. $this->groupManager
  2351. ->expects($this->once())
  2352. ->method('getSubAdmin')
  2353. ->willReturn($subadmin);
  2354. $expectedResponse = new DataResponse(
  2355. [
  2356. 'status' => 'error',
  2357. 'data' => [
  2358. 'message' => 'Error while disabling user.',
  2359. ],
  2360. ]
  2361. );
  2362. $response = $this->getController(false)->disable('abc');
  2363. $this->assertEquals($expectedResponse, $response);
  2364. }
  2365. public function testDisableUserSuccessForAdmin() {
  2366. $user = $this->getMockBuilder('\OC\User\User')
  2367. ->disableOriginalConstructor()->getMock();
  2368. $user->expects($this->once())
  2369. ->method('getUID')
  2370. ->will($this->returnValue('def'));
  2371. $this->userSession
  2372. ->expects($this->exactly(1))
  2373. ->method('getUser')
  2374. ->will($this->returnValue($user));
  2375. $user2 = $this->getMockBuilder('\OC\User\User')
  2376. ->disableOriginalConstructor()->getMock();
  2377. $user2->expects($this->once())
  2378. ->method('setEnabled')
  2379. ->with(false);
  2380. $this->userManager
  2381. ->expects($this->once())
  2382. ->method('get')
  2383. ->with('abc')
  2384. ->willReturn($user2);
  2385. $this->groupManager
  2386. ->expects($this->never())
  2387. ->method('getSubAdmin');
  2388. $expectedResponse = new DataResponse(
  2389. [
  2390. 'status' => 'success',
  2391. 'data' => [
  2392. 'username' => 'abc',
  2393. 'enabled' => 0,
  2394. ],
  2395. ]
  2396. );
  2397. $response = $this->getController(true)->disable('abc');
  2398. $this->assertEquals($expectedResponse, $response);
  2399. }
  2400. public function testDisableUserSuccessForSubAdmin() {
  2401. $user = $this->getMockBuilder('\OC\User\User')
  2402. ->disableOriginalConstructor()->getMock();
  2403. $user->expects($this->once())
  2404. ->method('getUID')
  2405. ->will($this->returnValue('def'));
  2406. $this->userSession
  2407. ->expects($this->exactly(2))
  2408. ->method('getUser')
  2409. ->will($this->returnValue($user));
  2410. $user2 = $this->getMockBuilder('\OC\User\User')
  2411. ->disableOriginalConstructor()->getMock();
  2412. $user2->expects($this->once())
  2413. ->method('setEnabled');
  2414. $this->userManager
  2415. ->expects($this->once())
  2416. ->method('get')
  2417. ->with('abc')
  2418. ->willReturn($user2);
  2419. $subadmin = $this->createMock('\OC\SubAdmin');
  2420. $subadmin->expects($this->once())
  2421. ->method('isUserAccessible')
  2422. ->will($this->returnValue(true));
  2423. $this->groupManager
  2424. ->expects($this->once())
  2425. ->method('getSubAdmin')
  2426. ->willReturn($subadmin);
  2427. $expectedResponse = new DataResponse(
  2428. [
  2429. 'status' => 'success',
  2430. 'data' => [
  2431. 'username' => 'abc',
  2432. 'enabled' => 0,
  2433. ],
  2434. ]
  2435. );
  2436. $response = $this->getController(false)->disable('abc');
  2437. $this->assertEquals($expectedResponse, $response);
  2438. }
  2439. public function testEnableUserFailsDueSameUser() {
  2440. $user = $this->getMockBuilder('\OC\User\User')
  2441. ->disableOriginalConstructor()->getMock();
  2442. $user->expects($this->once())
  2443. ->method('getUID')
  2444. ->will($this->returnValue('abc'));
  2445. $this->userSession
  2446. ->expects($this->once())
  2447. ->method('getUser')
  2448. ->will($this->returnValue($user));
  2449. $expectedResponse = new DataResponse(
  2450. [
  2451. 'status' => 'error',
  2452. 'data' => [
  2453. 'message' => 'Error while enabling user.',
  2454. ],
  2455. ],
  2456. Http::STATUS_FORBIDDEN
  2457. );
  2458. $response = $this->getController(true)->enable('abc');
  2459. $this->assertEquals($expectedResponse, $response);
  2460. }
  2461. public function testEnableUserFailsDueNoAdminAndNoSubadmin() {
  2462. $user = $this->getMockBuilder('\OC\User\User')
  2463. ->disableOriginalConstructor()->getMock();
  2464. $user->expects($this->once())
  2465. ->method('getUID')
  2466. ->will($this->returnValue('def'));
  2467. $this->userSession
  2468. ->expects($this->exactly(2))
  2469. ->method('getUser')
  2470. ->will($this->returnValue($user));
  2471. $user2 = $this->getMockBuilder('\OC\User\User')
  2472. ->disableOriginalConstructor()->getMock();
  2473. $user2->expects($this->never())
  2474. ->method('setEnabled');
  2475. $this->userManager
  2476. ->expects($this->once())
  2477. ->method('get')
  2478. ->with('abc')
  2479. ->willReturn($user2);
  2480. $subadmin = $this->createMock('\OC\SubAdmin');
  2481. $subadmin->expects($this->once())
  2482. ->method('isUserAccessible')
  2483. ->will($this->returnValue(false));
  2484. $this->groupManager
  2485. ->expects($this->once())
  2486. ->method('getSubAdmin')
  2487. ->willReturn($subadmin);
  2488. $expectedResponse = new DataResponse(
  2489. [
  2490. 'status' => 'error',
  2491. 'data' => [
  2492. 'message' => 'Authentication error',
  2493. ],
  2494. ],
  2495. Http::STATUS_FORBIDDEN
  2496. );
  2497. $response = $this->getController(false)->enable('abc');
  2498. $this->assertEquals($expectedResponse, $response);
  2499. }
  2500. public function testEnableUserFailsDueNoUser() {
  2501. $user = $this->getMockBuilder('\OC\User\User')
  2502. ->disableOriginalConstructor()->getMock();
  2503. $user->expects($this->once())
  2504. ->method('getUID')
  2505. ->will($this->returnValue('def'));
  2506. $this->userSession
  2507. ->expects($this->exactly(1))
  2508. ->method('getUser')
  2509. ->will($this->returnValue($user));
  2510. $this->userManager
  2511. ->expects($this->once())
  2512. ->method('get')
  2513. ->with('abc')
  2514. ->willReturn(null);
  2515. $this->groupManager
  2516. ->expects($this->never())
  2517. ->method('getSubAdmin');
  2518. $expectedResponse = new DataResponse(
  2519. [
  2520. 'status' => 'error',
  2521. 'data' => [
  2522. 'message' => 'Error while enabling user.',
  2523. ],
  2524. ]
  2525. );
  2526. $response = $this->getController(true)->enable('abc');
  2527. $this->assertEquals($expectedResponse, $response);
  2528. }
  2529. public function testEnableUserFailsDueNoUserForSubAdmin() {
  2530. $user = $this->getMockBuilder('\OC\User\User')
  2531. ->disableOriginalConstructor()->getMock();
  2532. $user->expects($this->once())
  2533. ->method('getUID')
  2534. ->will($this->returnValue('def'));
  2535. $this->userSession
  2536. ->expects($this->exactly(2))
  2537. ->method('getUser')
  2538. ->will($this->returnValue($user));
  2539. $this->userManager
  2540. ->expects($this->once())
  2541. ->method('get')
  2542. ->with('abc')
  2543. ->willReturn(null);
  2544. $subadmin = $this->createMock('\OC\SubAdmin');
  2545. $subadmin->expects($this->once())
  2546. ->method('isUserAccessible')
  2547. ->will($this->returnValue(true));
  2548. $this->groupManager
  2549. ->expects($this->once())
  2550. ->method('getSubAdmin')
  2551. ->willReturn($subadmin);
  2552. $expectedResponse = new DataResponse(
  2553. [
  2554. 'status' => 'error',
  2555. 'data' => [
  2556. 'message' => 'Error while enabling user.',
  2557. ],
  2558. ]
  2559. );
  2560. $response = $this->getController(false)->enable('abc');
  2561. $this->assertEquals($expectedResponse, $response);
  2562. }
  2563. public function testEnableUserSuccessForAdmin() {
  2564. $user = $this->getMockBuilder('\OC\User\User')
  2565. ->disableOriginalConstructor()->getMock();
  2566. $user->expects($this->once())
  2567. ->method('getUID')
  2568. ->will($this->returnValue('def'));
  2569. $this->userSession
  2570. ->expects($this->exactly(1))
  2571. ->method('getUser')
  2572. ->will($this->returnValue($user));
  2573. $user2 = $this->getMockBuilder('\OC\User\User')
  2574. ->disableOriginalConstructor()->getMock();
  2575. $user2->expects($this->once())
  2576. ->method('setEnabled');
  2577. $this->userManager
  2578. ->expects($this->once())
  2579. ->method('get')
  2580. ->with('abc')
  2581. ->willReturn($user2);
  2582. $this->groupManager
  2583. ->expects($this->never())
  2584. ->method('getSubAdmin');
  2585. $expectedResponse = new DataResponse(
  2586. [
  2587. 'status' => 'success',
  2588. 'data' => [
  2589. 'username' => 'abc',
  2590. 'enabled' => 1,
  2591. ],
  2592. ]
  2593. );
  2594. $response = $this->getController(true)->enable('abc');
  2595. $this->assertEquals($expectedResponse, $response);
  2596. }
  2597. public function testEnableUserSuccessForSubAdmin() {
  2598. $user = $this->getMockBuilder('\OC\User\User')
  2599. ->disableOriginalConstructor()->getMock();
  2600. $user->expects($this->once())
  2601. ->method('getUID')
  2602. ->will($this->returnValue('def'));
  2603. $this->userSession
  2604. ->expects($this->exactly(2))
  2605. ->method('getUser')
  2606. ->will($this->returnValue($user));
  2607. $user2 = $this->getMockBuilder('\OC\User\User')
  2608. ->disableOriginalConstructor()->getMock();
  2609. $user2->expects($this->once())
  2610. ->method('setEnabled')
  2611. ->with(true);
  2612. $this->userManager
  2613. ->expects($this->once())
  2614. ->method('get')
  2615. ->with('abc')
  2616. ->willReturn($user2);
  2617. $subadmin = $this->createMock('\OC\SubAdmin');
  2618. $subadmin->expects($this->once())
  2619. ->method('isUserAccessible')
  2620. ->will($this->returnValue(true));
  2621. $this->groupManager
  2622. ->expects($this->once())
  2623. ->method('getSubAdmin')
  2624. ->willReturn($subadmin);
  2625. $expectedResponse = new DataResponse(
  2626. [
  2627. 'status' => 'success',
  2628. 'data' => [
  2629. 'username' => 'abc',
  2630. 'enabled' => 1,
  2631. ],
  2632. ]
  2633. );
  2634. $response = $this->getController(false)->enable('abc');
  2635. $this->assertEquals($expectedResponse, $response);
  2636. }
  2637. }