UsersControllerTest.php 83 KB

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