UsersControllerTest.php 65 KB

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