UsersControllerTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. <?php
  2. /**
  3. * @copyright 2014-2015 Lukas Reschke lukas@owncloud.com
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Settings\Tests\Controller;
  30. use OC\Accounts\AccountManager;
  31. use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
  32. use OC\Group\Manager;
  33. use OC\KnownUser\KnownUserService;
  34. use OCA\Settings\Controller\UsersController;
  35. use OCP\Accounts\IAccountManager;
  36. use OCP\App\IAppManager;
  37. use OCP\AppFramework\Http;
  38. use OCP\BackgroundJob\IJobList;
  39. use OCP\Encryption\IEncryptionModule;
  40. use OCP\Encryption\IManager;
  41. use OCP\EventDispatcher\IEventDispatcher;
  42. use OCP\IAvatarManager;
  43. use OCP\IConfig;
  44. use OCP\IGroupManager;
  45. use OCP\IL10N;
  46. use OCP\ILogger;
  47. use OCP\IRequest;
  48. use OCP\IUser;
  49. use OCP\IUserManager;
  50. use OCP\IUserSession;
  51. use OCP\L10N\IFactory;
  52. use OCP\Mail\IMailer;
  53. use OCP\Security\ISecureRandom;
  54. /**
  55. * @group DB
  56. *
  57. * @package Tests\Settings\Controller
  58. */
  59. class UsersControllerTest extends \Test\TestCase {
  60. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  61. private $groupManager;
  62. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  63. private $userManager;
  64. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  65. private $userSession;
  66. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  67. private $config;
  68. /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
  69. private $logger;
  70. /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
  71. private $mailer;
  72. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  73. private $l10nFactory;
  74. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  75. private $appManager;
  76. /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
  77. private $avatarManager;
  78. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  79. private $l;
  80. /** @var AccountManager | \PHPUnit\Framework\MockObject\MockObject */
  81. private $accountManager;
  82. /** @var ISecureRandom | \PHPUnit\Framework\MockObject\MockObject */
  83. private $secureRandom;
  84. /** @var \OCA\Settings\Mailer\NewUserMailHelper|\PHPUnit\Framework\MockObject\MockObject */
  85. private $newUserMailHelper;
  86. /** @var IJobList | \PHPUnit\Framework\MockObject\MockObject */
  87. private $jobList;
  88. /** @var \OC\Security\IdentityProof\Manager |\PHPUnit\Framework\MockObject\MockObject */
  89. private $securityManager;
  90. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  91. private $encryptionManager;
  92. /** @var KnownUserService|\PHPUnit\Framework\MockObject\MockObject */
  93. private $knownUserService;
  94. /** @var IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject */
  95. private $encryptionModule;
  96. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  97. private $dispatcher;
  98. protected function setUp(): void {
  99. parent::setUp();
  100. $this->userManager = $this->createMock(IUserManager::class);
  101. $this->groupManager = $this->createMock(Manager::class);
  102. $this->userSession = $this->createMock(IUserSession::class);
  103. $this->config = $this->createMock(IConfig::class);
  104. $this->l = $this->createMock(IL10N::class);
  105. $this->mailer = $this->createMock(IMailer::class);
  106. $this->l10nFactory = $this->createMock(IFactory::class);
  107. $this->appManager = $this->createMock(IAppManager::class);
  108. $this->accountManager = $this->createMock(AccountManager::class);
  109. $this->securityManager = $this->getMockBuilder(\OC\Security\IdentityProof\Manager::class)->disableOriginalConstructor()->getMock();
  110. $this->jobList = $this->createMock(IJobList::class);
  111. $this->encryptionManager = $this->createMock(IManager::class);
  112. $this->knownUserService = $this->createMock(KnownUserService::class);
  113. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  114. $this->l->method('t')
  115. ->willReturnCallback(function ($text, $parameters = []) {
  116. return vsprintf($text, $parameters);
  117. });
  118. $this->encryptionModule = $this->createMock(IEncryptionModule::class);
  119. $this->encryptionManager->expects($this->any())->method('getEncryptionModules')
  120. ->willReturn(['encryptionModule' => ['callback' => function () {
  121. return $this->encryptionModule;
  122. }]]);
  123. }
  124. /**
  125. * @param bool $isAdmin
  126. * @return UsersController | \PHPUnit\Framework\MockObject\MockObject
  127. */
  128. protected function getController($isAdmin = false, $mockedMethods = []) {
  129. if (empty($mockedMethods)) {
  130. return new UsersController(
  131. 'settings',
  132. $this->createMock(IRequest::class),
  133. $this->userManager,
  134. $this->groupManager,
  135. $this->userSession,
  136. $this->config,
  137. $isAdmin,
  138. $this->l,
  139. $this->mailer,
  140. $this->l10nFactory,
  141. $this->appManager,
  142. $this->accountManager,
  143. $this->securityManager,
  144. $this->jobList,
  145. $this->encryptionManager,
  146. $this->knownUserService,
  147. $this->dispatcher
  148. );
  149. } else {
  150. return $this->getMockBuilder(UsersController::class)
  151. ->setConstructorArgs(
  152. [
  153. 'settings',
  154. $this->createMock(IRequest::class),
  155. $this->userManager,
  156. $this->groupManager,
  157. $this->userSession,
  158. $this->config,
  159. $isAdmin,
  160. $this->l,
  161. $this->mailer,
  162. $this->l10nFactory,
  163. $this->appManager,
  164. $this->accountManager,
  165. $this->securityManager,
  166. $this->jobList,
  167. $this->encryptionManager,
  168. $this->knownUserService,
  169. $this->dispatcher
  170. ]
  171. )->setMethods($mockedMethods)->getMock();
  172. }
  173. }
  174. protected function getDefaultAccountManagerUserData() {
  175. return [
  176. IAccountManager::PROPERTY_DISPLAYNAME =>
  177. [
  178. 'value' => 'Default display name',
  179. 'scope' => IAccountManager::SCOPE_FEDERATED,
  180. 'verified' => IAccountManager::VERIFIED,
  181. ],
  182. IAccountManager::PROPERTY_ADDRESS =>
  183. [
  184. 'value' => 'Default address',
  185. 'scope' => IAccountManager::SCOPE_LOCAL,
  186. 'verified' => IAccountManager::VERIFIED,
  187. ],
  188. IAccountManager::PROPERTY_WEBSITE =>
  189. [
  190. 'value' => 'Default website',
  191. 'scope' => IAccountManager::SCOPE_LOCAL,
  192. 'verified' => IAccountManager::VERIFIED,
  193. ],
  194. IAccountManager::PROPERTY_EMAIL =>
  195. [
  196. 'value' => 'Default email',
  197. 'scope' => IAccountManager::SCOPE_FEDERATED,
  198. 'verified' => IAccountManager::VERIFIED,
  199. ],
  200. IAccountManager::PROPERTY_AVATAR =>
  201. [
  202. 'scope' => IAccountManager::SCOPE_FEDERATED
  203. ],
  204. IAccountManager::PROPERTY_PHONE =>
  205. [
  206. 'value' => 'Default phone',
  207. 'scope' => IAccountManager::SCOPE_LOCAL,
  208. 'verified' => IAccountManager::VERIFIED,
  209. ],
  210. IAccountManager::PROPERTY_TWITTER =>
  211. [
  212. 'value' => 'Default twitter',
  213. 'scope' => IAccountManager::SCOPE_LOCAL,
  214. 'verified' => IAccountManager::VERIFIED,
  215. ],
  216. ];
  217. }
  218. /**
  219. * @dataProvider dataTestSetUserSettings
  220. *
  221. * @param string $email
  222. * @param bool $validEmail
  223. * @param $expectedStatus
  224. */
  225. public function testSetUserSettings($email, $validEmail, $expectedStatus) {
  226. $controller = $this->getController(false, ['saveUserSettings']);
  227. $user = $this->createMock(IUser::class);
  228. $user->method('getUID')->willReturn('johndoe');
  229. $this->userSession->method('getUser')->willReturn($user);
  230. if (!empty($email) && $validEmail) {
  231. $this->mailer->expects($this->once())->method('validateMailAddress')
  232. ->willReturn($validEmail);
  233. }
  234. $saveData = (!empty($email) && $validEmail) || empty($email);
  235. if ($saveData) {
  236. $this->accountManager->expects($this->once())
  237. ->method('getUser')
  238. ->with($user)
  239. ->willReturn($this->getDefaultAccountManagerUserData());
  240. $controller->expects($this->once())
  241. ->method('saveUserSettings')
  242. ->willReturnArgument(1);
  243. } else {
  244. $controller->expects($this->never())->method('saveUserSettings');
  245. }
  246. $result = $controller->setUserSettings(//
  247. AccountManager::SCOPE_FEDERATED,
  248. 'displayName',
  249. AccountManager::SCOPE_FEDERATED,
  250. '47658468',
  251. AccountManager::SCOPE_FEDERATED,
  252. $email,
  253. AccountManager::SCOPE_FEDERATED,
  254. 'nextcloud.com',
  255. AccountManager::SCOPE_FEDERATED,
  256. 'street and city',
  257. AccountManager::SCOPE_FEDERATED,
  258. '@nextclouders',
  259. AccountManager::SCOPE_FEDERATED
  260. );
  261. $this->assertSame($expectedStatus, $result->getStatus());
  262. }
  263. public function dataTestSetUserSettings() {
  264. return [
  265. ['', true, Http::STATUS_OK],
  266. ['', false, Http::STATUS_OK],
  267. ['example.com', false, Http::STATUS_UNPROCESSABLE_ENTITY],
  268. ['john@example.com', true, Http::STATUS_OK],
  269. ];
  270. }
  271. public function testSetUserSettingsWhenUserDisplayNameChangeNotAllowed() {
  272. $controller = $this->getController(false, ['saveUserSettings']);
  273. $user = $this->createMock(IUser::class);
  274. $user->method('getUID')->willReturn('johndoe');
  275. $this->userSession->method('getUser')->willReturn($user);
  276. $defaultProperties = $this->getDefaultAccountManagerUserData();
  277. $this->accountManager->expects($this->once())
  278. ->method('getUser')
  279. ->with($user)
  280. ->willReturn($defaultProperties);
  281. $this->config->expects($this->once())
  282. ->method('getSystemValue')
  283. ->with('allow_user_to_change_display_name')
  284. ->willReturn(false);
  285. $this->appManager->expects($this->any())
  286. ->method('isEnabledForUser')
  287. ->with('federatedfilesharing')
  288. ->willReturn(true);
  289. $avatarScope = IAccountManager::SCOPE_PUBLISHED;
  290. $displayName = 'Display name';
  291. $displayNameScope = IAccountManager::SCOPE_PUBLISHED;
  292. $phone = '47658468';
  293. $phoneScope = IAccountManager::SCOPE_PUBLISHED;
  294. $email = 'john@example.com';
  295. $emailScope = IAccountManager::SCOPE_PUBLISHED;
  296. $website = 'nextcloud.com';
  297. $websiteScope = IAccountManager::SCOPE_PUBLISHED;
  298. $address = 'street and city';
  299. $addressScope = IAccountManager::SCOPE_PUBLISHED;
  300. $twitter = '@nextclouders';
  301. $twitterScope = IAccountManager::SCOPE_PUBLISHED;
  302. // Display name and email are not changed.
  303. $expectedProperties = $defaultProperties;
  304. $expectedProperties[IAccountManager::PROPERTY_AVATAR]['scope'] = $avatarScope;
  305. $expectedProperties[IAccountManager::PROPERTY_PHONE]['value'] = $phone;
  306. $expectedProperties[IAccountManager::PROPERTY_PHONE]['scope'] = $phoneScope;
  307. $expectedProperties[IAccountManager::PROPERTY_WEBSITE]['value'] = $website;
  308. $expectedProperties[IAccountManager::PROPERTY_WEBSITE]['scope'] = $websiteScope;
  309. $expectedProperties[IAccountManager::PROPERTY_ADDRESS]['value'] = $address;
  310. $expectedProperties[IAccountManager::PROPERTY_ADDRESS]['scope'] = $addressScope;
  311. $expectedProperties[IAccountManager::PROPERTY_TWITTER]['value'] = $twitter;
  312. $expectedProperties[IAccountManager::PROPERTY_TWITTER]['scope'] = $twitterScope;
  313. $this->mailer->expects($this->once())->method('validateMailAddress')
  314. ->willReturn(true);
  315. $controller->expects($this->once())
  316. ->method('saveUserSettings')
  317. ->with($user, $expectedProperties)
  318. ->willReturnArgument(1);
  319. $result = $controller->setUserSettings(
  320. $avatarScope,
  321. $displayName,
  322. $displayNameScope,
  323. $phone,
  324. $phoneScope,
  325. $email,
  326. $emailScope,
  327. $website,
  328. $websiteScope,
  329. $address,
  330. $addressScope,
  331. $twitter,
  332. $twitterScope
  333. );
  334. }
  335. public function testSetUserSettingsWhenFederatedFilesharingNotEnabled() {
  336. $controller = $this->getController(false, ['saveUserSettings']);
  337. $user = $this->createMock(IUser::class);
  338. $user->method('getUID')->willReturn('johndoe');
  339. $this->userSession->method('getUser')->willReturn($user);
  340. $defaultProperties = $this->getDefaultAccountManagerUserData();
  341. $this->accountManager->expects($this->once())
  342. ->method('getUser')
  343. ->with($user)
  344. ->willReturn($defaultProperties);
  345. $this->appManager->expects($this->any())
  346. ->method('isEnabledForUser')
  347. ->with('federatedfilesharing')
  348. ->willReturn(false);
  349. $avatarScope = IAccountManager::SCOPE_PUBLISHED;
  350. $displayName = 'Display name';
  351. $displayNameScope = IAccountManager::SCOPE_PUBLISHED;
  352. $phone = '47658468';
  353. $phoneScope = IAccountManager::SCOPE_PUBLISHED;
  354. $email = 'john@example.com';
  355. $emailScope = IAccountManager::SCOPE_PUBLISHED;
  356. $website = 'nextcloud.com';
  357. $websiteScope = IAccountManager::SCOPE_PUBLISHED;
  358. $address = 'street and city';
  359. $addressScope = IAccountManager::SCOPE_PUBLISHED;
  360. $twitter = '@nextclouders';
  361. $twitterScope = IAccountManager::SCOPE_PUBLISHED;
  362. // All settings are changed (in the past phone, website, address and
  363. // twitter were not changed).
  364. $expectedProperties = $defaultProperties;
  365. $expectedProperties[IAccountManager::PROPERTY_AVATAR]['scope'] = $avatarScope;
  366. $expectedProperties[IAccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
  367. $expectedProperties[IAccountManager::PROPERTY_DISPLAYNAME]['scope'] = $displayNameScope;
  368. $expectedProperties[IAccountManager::PROPERTY_EMAIL]['value'] = $email;
  369. $expectedProperties[IAccountManager::PROPERTY_EMAIL]['scope'] = $emailScope;
  370. $expectedProperties[IAccountManager::PROPERTY_PHONE]['value'] = $phone;
  371. $expectedProperties[IAccountManager::PROPERTY_PHONE]['scope'] = $phoneScope;
  372. $expectedProperties[IAccountManager::PROPERTY_WEBSITE]['value'] = $website;
  373. $expectedProperties[IAccountManager::PROPERTY_WEBSITE]['scope'] = $websiteScope;
  374. $expectedProperties[IAccountManager::PROPERTY_ADDRESS]['value'] = $address;
  375. $expectedProperties[IAccountManager::PROPERTY_ADDRESS]['scope'] = $addressScope;
  376. $expectedProperties[IAccountManager::PROPERTY_TWITTER]['value'] = $twitter;
  377. $expectedProperties[IAccountManager::PROPERTY_TWITTER]['scope'] = $twitterScope;
  378. $this->mailer->expects($this->once())->method('validateMailAddress')
  379. ->willReturn(true);
  380. $controller->expects($this->once())
  381. ->method('saveUserSettings')
  382. ->with($user, $expectedProperties)
  383. ->willReturnArgument(1);
  384. $result = $controller->setUserSettings(
  385. $avatarScope,
  386. $displayName,
  387. $displayNameScope,
  388. $phone,
  389. $phoneScope,
  390. $email,
  391. $emailScope,
  392. $website,
  393. $websiteScope,
  394. $address,
  395. $addressScope,
  396. $twitter,
  397. $twitterScope
  398. );
  399. }
  400. /**
  401. * @dataProvider dataTestSetUserSettingsSubset
  402. *
  403. * @param string $property
  404. * @param string $propertyValue
  405. */
  406. public function testSetUserSettingsSubset($property, $propertyValue) {
  407. $controller = $this->getController(false, ['saveUserSettings']);
  408. $user = $this->createMock(IUser::class);
  409. $user->method('getUID')->willReturn('johndoe');
  410. $this->userSession->method('getUser')->willReturn($user);
  411. $defaultProperties = $this->getDefaultAccountManagerUserData();
  412. $this->accountManager->expects($this->once())
  413. ->method('getUser')
  414. ->with($user)
  415. ->willReturn($defaultProperties);
  416. $avatarScope = ($property === 'avatarScope') ? $propertyValue : null;
  417. $displayName = ($property === 'displayName') ? $propertyValue : null;
  418. $displayNameScope = ($property === 'displayNameScope') ? $propertyValue : null;
  419. $phone = ($property === 'phone') ? $propertyValue : null;
  420. $phoneScope = ($property === 'phoneScope') ? $propertyValue : null;
  421. $email = ($property === 'email') ? $propertyValue : null;
  422. $emailScope = ($property === 'emailScope') ? $propertyValue : null;
  423. $website = ($property === 'website') ? $propertyValue : null;
  424. $websiteScope = ($property === 'websiteScope') ? $propertyValue : null;
  425. $address = ($property === 'address') ? $propertyValue : null;
  426. $addressScope = ($property === 'addressScope') ? $propertyValue : null;
  427. $twitter = ($property === 'twitter') ? $propertyValue : null;
  428. $twitterScope = ($property === 'twitterScope') ? $propertyValue : null;
  429. $expectedProperties = $defaultProperties;
  430. if ($property === 'avatarScope') {
  431. $expectedProperties[IAccountManager::PROPERTY_AVATAR]['scope'] = $propertyValue;
  432. }
  433. if ($property === 'displayName') {
  434. $expectedProperties[IAccountManager::PROPERTY_DISPLAYNAME]['value'] = $propertyValue;
  435. }
  436. if ($property === 'displayNameScope') {
  437. $expectedProperties[IAccountManager::PROPERTY_DISPLAYNAME]['scope'] = $propertyValue;
  438. }
  439. if ($property === 'phone') {
  440. $expectedProperties[IAccountManager::PROPERTY_PHONE]['value'] = $propertyValue;
  441. }
  442. if ($property === 'phoneScope') {
  443. $expectedProperties[IAccountManager::PROPERTY_PHONE]['scope'] = $propertyValue;
  444. }
  445. if ($property === 'email') {
  446. $expectedProperties[IAccountManager::PROPERTY_EMAIL]['value'] = $propertyValue;
  447. }
  448. if ($property === 'emailScope') {
  449. $expectedProperties[IAccountManager::PROPERTY_EMAIL]['scope'] = $propertyValue;
  450. }
  451. if ($property === 'website') {
  452. $expectedProperties[IAccountManager::PROPERTY_WEBSITE]['value'] = $propertyValue;
  453. }
  454. if ($property === 'websiteScope') {
  455. $expectedProperties[IAccountManager::PROPERTY_WEBSITE]['scope'] = $propertyValue;
  456. }
  457. if ($property === 'address') {
  458. $expectedProperties[IAccountManager::PROPERTY_ADDRESS]['value'] = $propertyValue;
  459. }
  460. if ($property === 'addressScope') {
  461. $expectedProperties[IAccountManager::PROPERTY_ADDRESS]['scope'] = $propertyValue;
  462. }
  463. if ($property === 'twitter') {
  464. $expectedProperties[IAccountManager::PROPERTY_TWITTER]['value'] = $propertyValue;
  465. }
  466. if ($property === 'twitterScope') {
  467. $expectedProperties[IAccountManager::PROPERTY_TWITTER]['scope'] = $propertyValue;
  468. }
  469. if (!empty($email)) {
  470. $this->mailer->expects($this->once())->method('validateMailAddress')
  471. ->willReturn(true);
  472. }
  473. $controller->expects($this->once())
  474. ->method('saveUserSettings')
  475. ->with($user, $expectedProperties)
  476. ->willReturnArgument(1);
  477. $result = $controller->setUserSettings(
  478. $avatarScope,
  479. $displayName,
  480. $displayNameScope,
  481. $phone,
  482. $phoneScope,
  483. $email,
  484. $emailScope,
  485. $website,
  486. $websiteScope,
  487. $address,
  488. $addressScope,
  489. $twitter,
  490. $twitterScope
  491. );
  492. }
  493. public function dataTestSetUserSettingsSubset() {
  494. return [
  495. ['avatarScope', IAccountManager::SCOPE_PUBLISHED],
  496. ['displayName', 'Display name'],
  497. ['displayNameScope', IAccountManager::SCOPE_PUBLISHED],
  498. ['phone', '47658468'],
  499. ['phoneScope', IAccountManager::SCOPE_PUBLISHED],
  500. ['email', 'john@example.com'],
  501. ['emailScope', IAccountManager::SCOPE_PUBLISHED],
  502. ['website', 'nextcloud.com'],
  503. ['websiteScope', IAccountManager::SCOPE_PUBLISHED],
  504. ['address', 'street and city'],
  505. ['addressScope', IAccountManager::SCOPE_PUBLISHED],
  506. ['twitter', '@nextclouders'],
  507. ['twitterScope', IAccountManager::SCOPE_PUBLISHED],
  508. ];
  509. }
  510. /**
  511. * @dataProvider dataTestSaveUserSettings
  512. *
  513. * @param array $data
  514. * @param string $oldEmailAddress
  515. * @param string $oldDisplayName
  516. */
  517. public function testSaveUserSettings($data,
  518. $oldEmailAddress,
  519. $oldDisplayName
  520. ) {
  521. $controller = $this->getController();
  522. $user = $this->createMock(IUser::class);
  523. $user->method('getDisplayName')->willReturn($oldDisplayName);
  524. $user->method('getEMailAddress')->willReturn($oldEmailAddress);
  525. $user->method('canChangeDisplayName')->willReturn(true);
  526. if ($data[IAccountManager::PROPERTY_EMAIL]['value'] === $oldEmailAddress ||
  527. ($oldEmailAddress === null && $data[IAccountManager::PROPERTY_EMAIL]['value'] === '')) {
  528. $user->expects($this->never())->method('setEMailAddress');
  529. } else {
  530. $user->expects($this->once())->method('setEMailAddress')
  531. ->with($data[IAccountManager::PROPERTY_EMAIL]['value'])
  532. ->willReturn(true);
  533. }
  534. if ($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'] === $oldDisplayName ||
  535. ($oldDisplayName === null && $data[IAccountManager::PROPERTY_DISPLAYNAME]['value'] === '')) {
  536. $user->expects($this->never())->method('setDisplayName');
  537. } else {
  538. $user->expects($this->once())->method('setDisplayName')
  539. ->with($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'])
  540. ->willReturn(true);
  541. }
  542. $this->accountManager->expects($this->once())->method('updateUser')
  543. ->with($user, $data);
  544. $this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
  545. }
  546. public function dataTestSaveUserSettings() {
  547. return [
  548. [
  549. [
  550. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  551. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  552. ],
  553. 'john@example.com',
  554. 'john doe'
  555. ],
  556. [
  557. [
  558. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  559. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  560. ],
  561. 'johnNew@example.com',
  562. 'john New doe'
  563. ],
  564. [
  565. [
  566. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  567. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  568. ],
  569. 'johnNew@example.com',
  570. 'john doe'
  571. ],
  572. [
  573. [
  574. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  575. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  576. ],
  577. 'john@example.com',
  578. 'john New doe'
  579. ],
  580. [
  581. [
  582. IAccountManager::PROPERTY_EMAIL => ['value' => ''],
  583. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  584. ],
  585. null,
  586. 'john New doe'
  587. ],
  588. [
  589. [
  590. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  591. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  592. ],
  593. 'john@example.com',
  594. null
  595. ],
  596. ];
  597. }
  598. /**
  599. * @dataProvider dataTestSaveUserSettingsException
  600. *
  601. * @param array $data
  602. * @param string $oldEmailAddress
  603. * @param string $oldDisplayName
  604. * @param bool $setDisplayNameResult
  605. * @param bool $canChangeEmail
  606. *
  607. */
  608. public function testSaveUserSettingsException($data,
  609. $oldEmailAddress,
  610. $oldDisplayName,
  611. $setDisplayNameResult,
  612. $canChangeEmail
  613. ) {
  614. $this->expectException(\OC\ForbiddenException::class);
  615. $controller = $this->getController();
  616. $user = $this->createMock(IUser::class);
  617. $user->method('getDisplayName')->willReturn($oldDisplayName);
  618. $user->method('getEMailAddress')->willReturn($oldEmailAddress);
  619. if ($data[IAccountManager::PROPERTY_EMAIL]['value'] !== $oldEmailAddress) {
  620. $user->method('canChangeDisplayName')
  621. ->willReturn($canChangeEmail);
  622. }
  623. if ($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'] !== $oldDisplayName) {
  624. $user->method('setDisplayName')
  625. ->with($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'])
  626. ->willReturn($setDisplayNameResult);
  627. }
  628. $this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
  629. }
  630. public function dataTestSaveUserSettingsException() {
  631. return [
  632. [
  633. [
  634. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  635. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  636. ],
  637. 'johnNew@example.com',
  638. 'john New doe',
  639. true,
  640. false
  641. ],
  642. [
  643. [
  644. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  645. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  646. ],
  647. 'johnNew@example.com',
  648. 'john New doe',
  649. false,
  650. true
  651. ],
  652. [
  653. [
  654. IAccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'],
  655. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'],
  656. ],
  657. 'johnNew@example.com',
  658. 'john New doe',
  659. false,
  660. false
  661. ],
  662. ];
  663. }
  664. /**
  665. * @param string $account
  666. * @param string $type
  667. * @param array $dataBefore
  668. * @param array $expectedData
  669. *
  670. * @dataProvider dataTestGetVerificationCode
  671. */
  672. public function testGetVerificationCode($account, $type, $dataBefore, $expectedData, $onlyVerificationCode) {
  673. $message = 'Use my Federated Cloud ID to share with me: user@nextcloud.com';
  674. $signature = 'theSignature';
  675. $code = $message . ' ' . $signature;
  676. if ($type === IAccountManager::PROPERTY_TWITTER) {
  677. $code = $message . ' ' . md5($signature);
  678. }
  679. $controller = $this->getController(false, ['signMessage', 'getCurrentTime']);
  680. $user = $this->createMock(IUser::class);
  681. $this->userSession->expects($this->once())->method('getUser')->willReturn($user);
  682. $this->accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($dataBefore);
  683. $user->expects($this->any())->method('getCloudId')->willReturn('user@nextcloud.com');
  684. $user->expects($this->any())->method('getUID')->willReturn('uid');
  685. $controller->expects($this->once())->method('signMessage')->with($user, $message)->willReturn($signature);
  686. $controller->expects($this->any())->method('getCurrentTime')->willReturn(1234567);
  687. if ($onlyVerificationCode === false) {
  688. $this->accountManager->expects($this->once())->method('updateUser')->with($user, $expectedData)->willReturnArgument(1);
  689. $this->jobList->expects($this->once())->method('add')
  690. ->with('OCA\Settings\BackgroundJobs\VerifyUserData',
  691. [
  692. 'verificationCode' => $code,
  693. 'data' => $dataBefore[$type]['value'],
  694. 'type' => $type,
  695. 'uid' => 'uid',
  696. 'try' => 0,
  697. 'lastRun' => 1234567
  698. ]);
  699. }
  700. $result = $controller->getVerificationCode($account, $onlyVerificationCode);
  701. $data = $result->getData();
  702. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  703. $this->assertSame($code, $data['code']);
  704. }
  705. public function dataTestGetVerificationCode() {
  706. $accountDataBefore = [
  707. IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => IAccountManager::NOT_VERIFIED],
  708. IAccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => IAccountManager::NOT_VERIFIED, 'signature' => 'theSignature'],
  709. ];
  710. $accountDataAfterWebsite = [
  711. IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => IAccountManager::VERIFICATION_IN_PROGRESS, 'signature' => 'theSignature'],
  712. IAccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => IAccountManager::NOT_VERIFIED, 'signature' => 'theSignature'],
  713. ];
  714. $accountDataAfterTwitter = [
  715. IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://nextcloud.com', 'verified' => IAccountManager::NOT_VERIFIED],
  716. IAccountManager::PROPERTY_TWITTER => ['value' => '@nextclouders', 'verified' => IAccountManager::VERIFICATION_IN_PROGRESS, 'signature' => 'theSignature'],
  717. ];
  718. return [
  719. ['verify-twitter', IAccountManager::PROPERTY_TWITTER, $accountDataBefore, $accountDataAfterTwitter, false],
  720. ['verify-website', IAccountManager::PROPERTY_WEBSITE, $accountDataBefore, $accountDataAfterWebsite, false],
  721. ['verify-twitter', IAccountManager::PROPERTY_TWITTER, $accountDataBefore, $accountDataAfterTwitter, true],
  722. ['verify-website', IAccountManager::PROPERTY_WEBSITE, $accountDataBefore, $accountDataAfterWebsite, true],
  723. ];
  724. }
  725. /**
  726. * test get verification code in case no valid user was given
  727. */
  728. public function testGetVerificationCodeInvalidUser() {
  729. $controller = $this->getController();
  730. $this->userSession->expects($this->once())->method('getUser')->willReturn(null);
  731. $result = $controller->getVerificationCode('account', false);
  732. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  733. }
  734. /**
  735. * @dataProvider dataTestCanAdminChangeUserPasswords
  736. *
  737. * @param bool $encryptionEnabled
  738. * @param bool $encryptionModuleLoaded
  739. * @param bool $masterKeyEnabled
  740. * @param bool $expected
  741. */
  742. public function testCanAdminChangeUserPasswords($encryptionEnabled,
  743. $encryptionModuleLoaded,
  744. $masterKeyEnabled,
  745. $expected) {
  746. $controller = $this->getController();
  747. $this->encryptionManager->expects($this->any())
  748. ->method('isEnabled')
  749. ->willReturn($encryptionEnabled);
  750. $this->encryptionManager->expects($this->any())
  751. ->method('getEncryptionModule')
  752. ->willReturnCallback(function () use ($encryptionModuleLoaded) {
  753. if ($encryptionModuleLoaded) {
  754. return $this->encryptionModule;
  755. } else {
  756. throw new ModuleDoesNotExistsException();
  757. }
  758. });
  759. $this->encryptionModule->expects($this->any())
  760. ->method('needDetailedAccessList')
  761. ->willReturn(!$masterKeyEnabled);
  762. $result = $this->invokePrivate($controller, 'canAdminChangeUserPasswords', []);
  763. $this->assertSame($expected, $result);
  764. }
  765. public function dataTestCanAdminChangeUserPasswords() {
  766. return [
  767. // encryptionEnabled, encryptionModuleLoaded, masterKeyEnabled, expectedResult
  768. [true, true, true, true],
  769. [false, true, true, true],
  770. [true, false, true, false],
  771. [false, false, true, true],
  772. [true, true, false, false],
  773. [false, true, false, false],
  774. [true, false, false, false],
  775. [false, false, false, true],
  776. ];
  777. }
  778. }