UsersControllerTest.php 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Georg Ehrke <oc.list@georgehrke.com>
  11. * @author GretaD <gretadoci@gmail.com>
  12. * @author Joas Schilling <coding@schilljs.com>
  13. * @author John Molakvoæ <skjnldsv@protonmail.com>
  14. * @author Julius Härtl <jus@bitgrid.net>
  15. * @author Lukas Reschke <lukas@statuscode.ch>
  16. * @author michag86 <micha_g@arcor.de>
  17. * @author Morris Jobke <hey@morrisjobke.de>
  18. * @author Roeland Jago Douma <roeland@famdouma.nl>
  19. * @author Sujith Haridasan <sujith.h@gmail.com>
  20. * @author Thomas Citharel <nextcloud@tcit.fr>
  21. * @author Thomas Müller <thomas.mueller@tmit.eu>
  22. * @author Tom Needham <tom@owncloud.com>
  23. * @author Vincent Petry <vincent@nextcloud.com>
  24. * @author zulan <git@zulan.net>
  25. *
  26. * @license AGPL-3.0
  27. *
  28. * This code is free software: you can redistribute it and/or modify
  29. * it under the terms of the GNU Affero General Public License, version 3,
  30. * as published by the Free Software Foundation.
  31. *
  32. * This program is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. * GNU Affero General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU Affero General Public License, version 3,
  38. * along with this program. If not, see <http://www.gnu.org/licenses/>
  39. *
  40. */
  41. namespace OCA\Provisioning_API\Tests\Controller;
  42. use Exception;
  43. use OC\Authentication\Token\RemoteWipe;
  44. use OC\Group\Manager;
  45. use OC\KnownUser\KnownUserService;
  46. use OC\SubAdmin;
  47. use OCA\Provisioning_API\Controller\UsersController;
  48. use OCA\Settings\Mailer\NewUserMailHelper;
  49. use OCP\Accounts\IAccount;
  50. use OCP\Accounts\IAccountManager;
  51. use OCP\Accounts\IAccountProperty;
  52. use OCP\AppFramework\Http\DataResponse;
  53. use OCP\EventDispatcher\IEventDispatcher;
  54. use OCP\IConfig;
  55. use OCP\IGroup;
  56. use OCP\IL10N;
  57. use OCP\IRequest;
  58. use OCP\IURLGenerator;
  59. use OCP\IUser;
  60. use OCP\IUserManager;
  61. use OCP\IUserSession;
  62. use OCP\L10N\IFactory;
  63. use OCP\Mail\IEMailTemplate;
  64. use OCP\Security\Events\GenerateSecurePasswordEvent;
  65. use OCP\Security\ISecureRandom;
  66. use OCP\User\Backend\ISetDisplayNameBackend;
  67. use OCP\UserInterface;
  68. use PHPUnit\Framework\MockObject\MockObject;
  69. use Psr\Log\LoggerInterface;
  70. use Test\TestCase;
  71. class UsersControllerTest extends TestCase {
  72. /** @var IUserManager|MockObject */
  73. protected $userManager;
  74. /** @var IConfig|MockObject */
  75. protected $config;
  76. /** @var Manager|MockObject */
  77. protected $groupManager;
  78. /** @var IUserSession|MockObject */
  79. protected $userSession;
  80. /** @var LoggerInterface|MockObject */
  81. protected $logger;
  82. /** @var UsersController|MockObject */
  83. protected $api;
  84. /** @var IAccountManager|MockObject */
  85. protected $accountManager;
  86. /** @var IURLGenerator|MockObject */
  87. protected $urlGenerator;
  88. /** @var IRequest|MockObject */
  89. protected $request;
  90. /** @var IFactory|MockObject */
  91. private $l10nFactory;
  92. /** @var NewUserMailHelper|MockObject */
  93. private $newUserMailHelper;
  94. /** @var ISecureRandom|MockObject */
  95. private $secureRandom;
  96. /** @var RemoteWipe|MockObject */
  97. private $remoteWipe;
  98. /** @var KnownUserService|MockObject */
  99. private $knownUserService;
  100. /** @var IEventDispatcher */
  101. private $eventDispatcher;
  102. protected function setUp(): void {
  103. parent::setUp();
  104. $this->userManager = $this->createMock(IUserManager::class);
  105. $this->config = $this->createMock(IConfig::class);
  106. $this->groupManager = $this->createMock(Manager::class);
  107. $this->userSession = $this->createMock(IUserSession::class);
  108. $this->logger = $this->createMock(LoggerInterface::class);
  109. $this->request = $this->createMock(IRequest::class);
  110. $this->accountManager = $this->createMock(IAccountManager::class);
  111. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  112. $this->l10nFactory = $this->createMock(IFactory::class);
  113. $this->newUserMailHelper = $this->createMock(NewUserMailHelper::class);
  114. $this->secureRandom = $this->createMock(ISecureRandom::class);
  115. $this->remoteWipe = $this->createMock(RemoteWipe::class);
  116. $this->knownUserService = $this->createMock(KnownUserService::class);
  117. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  118. $this->api = $this->getMockBuilder(UsersController::class)
  119. ->setConstructorArgs([
  120. 'provisioning_api',
  121. $this->request,
  122. $this->userManager,
  123. $this->config,
  124. $this->groupManager,
  125. $this->userSession,
  126. $this->accountManager,
  127. $this->urlGenerator,
  128. $this->logger,
  129. $this->l10nFactory,
  130. $this->newUserMailHelper,
  131. $this->secureRandom,
  132. $this->remoteWipe,
  133. $this->knownUserService,
  134. $this->eventDispatcher,
  135. ])
  136. ->setMethods(['fillStorageInfo'])
  137. ->getMock();
  138. }
  139. public function testGetUsersAsAdmin() {
  140. $loggedInUser = $this->getMockBuilder(IUser::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $loggedInUser
  144. ->expects($this->once())
  145. ->method('getUID')
  146. ->willReturn('admin');
  147. $this->userSession
  148. ->expects($this->once())
  149. ->method('getUser')
  150. ->willReturn($loggedInUser);
  151. $this->groupManager
  152. ->expects($this->once())
  153. ->method('isAdmin')
  154. ->willReturn(true);
  155. $this->userManager
  156. ->expects($this->once())
  157. ->method('search')
  158. ->with('MyCustomSearch')
  159. ->willReturn(['Admin' => [], 'Foo' => [], 'Bar' => []]);
  160. $expected = [
  161. 'users' => [
  162. 'Admin',
  163. 'Foo',
  164. 'Bar',
  165. ],
  166. ];
  167. $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData());
  168. }
  169. public function testGetUsersAsSubAdmin() {
  170. $loggedInUser = $this->getMockBuilder(IUser::class)
  171. ->disableOriginalConstructor()
  172. ->getMock();
  173. $loggedInUser
  174. ->expects($this->once())
  175. ->method('getUID')
  176. ->willReturn('subadmin');
  177. $this->userSession
  178. ->expects($this->once())
  179. ->method('getUser')
  180. ->willReturn($loggedInUser);
  181. $this->groupManager
  182. ->expects($this->once())
  183. ->method('isAdmin')
  184. ->willReturn(false);
  185. $firstGroup = $this->getMockBuilder('OCP\IGroup')
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. $firstGroup
  189. ->expects($this->once())
  190. ->method('getGID')
  191. ->willReturn('FirstGroup');
  192. $secondGroup = $this->getMockBuilder('OCP\IGroup')
  193. ->disableOriginalConstructor()
  194. ->getMock();
  195. $secondGroup
  196. ->expects($this->once())
  197. ->method('getGID')
  198. ->willReturn('SecondGroup');
  199. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  200. ->disableOriginalConstructor()->getMock();
  201. $subAdminManager
  202. ->expects($this->once())
  203. ->method('isSubAdmin')
  204. ->with($loggedInUser)
  205. ->willReturn(true);
  206. $subAdminManager
  207. ->expects($this->once())
  208. ->method('getSubAdminsGroups')
  209. ->with($loggedInUser)
  210. ->willReturn([$firstGroup, $secondGroup]);
  211. $this->groupManager
  212. ->expects($this->once())
  213. ->method('getSubAdmin')
  214. ->willReturn($subAdminManager);
  215. $this->groupManager
  216. ->expects($this->any())
  217. ->method('displayNamesInGroup')
  218. ->will($this->onConsecutiveCalls(['AnotherUserInTheFirstGroup' => []], ['UserInTheSecondGroup' => []]));
  219. $expected = [
  220. 'users' => [
  221. 'AnotherUserInTheFirstGroup',
  222. 'UserInTheSecondGroup',
  223. ],
  224. ];
  225. $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData());
  226. }
  227. public function testAddUserAlreadyExisting() {
  228. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  229. $this->expectExceptionCode(102);
  230. $this->userManager
  231. ->expects($this->once())
  232. ->method('userExists')
  233. ->with('AlreadyExistingUser')
  234. ->willReturn(true);
  235. $this->logger
  236. ->expects($this->once())
  237. ->method('error')
  238. ->with('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
  239. $loggedInUser = $this->getMockBuilder(IUser::class)
  240. ->disableOriginalConstructor()
  241. ->getMock();
  242. $loggedInUser
  243. ->expects($this->once())
  244. ->method('getUID')
  245. ->willReturn('adminUser');
  246. $this->userSession
  247. ->expects($this->once())
  248. ->method('getUser')
  249. ->willReturn($loggedInUser);
  250. $this->groupManager
  251. ->expects($this->once())
  252. ->method('isAdmin')
  253. ->with('adminUser')
  254. ->willReturn(true);
  255. $l10n = $this->createMock(IL10N::class);
  256. $this->l10nFactory
  257. ->expects($this->once())
  258. ->method('get')
  259. ->with('provisioning_api')
  260. ->willReturn($l10n);
  261. $this->api->addUser('AlreadyExistingUser', 'password', '', '', []);
  262. }
  263. public function testAddUserNonExistingGroup() {
  264. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  265. $this->expectExceptionMessage('group NonExistingGroup does not exist');
  266. $this->expectExceptionCode(104);
  267. $this->userManager
  268. ->expects($this->once())
  269. ->method('userExists')
  270. ->with('NewUser')
  271. ->willReturn(false);
  272. $loggedInUser = $this->getMockBuilder(IUser::class)
  273. ->disableOriginalConstructor()
  274. ->getMock();
  275. $loggedInUser
  276. ->expects($this->once())
  277. ->method('getUID')
  278. ->willReturn('adminUser');
  279. $this->userSession
  280. ->expects($this->once())
  281. ->method('getUser')
  282. ->willReturn($loggedInUser);
  283. $this->groupManager
  284. ->expects($this->once())
  285. ->method('isAdmin')
  286. ->with('adminUser')
  287. ->willReturn(true);
  288. $this->groupManager
  289. ->expects($this->once())
  290. ->method('groupExists')
  291. ->with('NonExistingGroup')
  292. ->willReturn(false);
  293. $this->api->addUser('NewUser', 'pass', '', '', ['NonExistingGroup']);
  294. }
  295. public function testAddUserExistingGroupNonExistingGroup() {
  296. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  297. $this->expectExceptionMessage('group NonExistingGroup does not exist');
  298. $this->expectExceptionCode(104);
  299. $this->userManager
  300. ->expects($this->once())
  301. ->method('userExists')
  302. ->with('NewUser')
  303. ->willReturn(false);
  304. $loggedInUser = $this->getMockBuilder(IUser::class)
  305. ->disableOriginalConstructor()
  306. ->getMock();
  307. $loggedInUser
  308. ->expects($this->once())
  309. ->method('getUID')
  310. ->willReturn('adminUser');
  311. $this->userSession
  312. ->expects($this->once())
  313. ->method('getUser')
  314. ->willReturn($loggedInUser);
  315. $this->groupManager
  316. ->expects($this->once())
  317. ->method('isAdmin')
  318. ->with('adminUser')
  319. ->willReturn(true);
  320. $this->groupManager
  321. ->expects($this->exactly(2))
  322. ->method('groupExists')
  323. ->withConsecutive(
  324. ['ExistingGroup'],
  325. ['NonExistingGroup']
  326. )
  327. ->willReturnMap([
  328. ['ExistingGroup', true],
  329. ['NonExistingGroup', false]
  330. ]);
  331. $this->api->addUser('NewUser', 'pass', '', '', ['ExistingGroup', 'NonExistingGroup']);
  332. }
  333. public function testAddUserSuccessful() {
  334. $this->userManager
  335. ->expects($this->once())
  336. ->method('userExists')
  337. ->with('NewUser')
  338. ->willReturn(false);
  339. $this->userManager
  340. ->expects($this->once())
  341. ->method('createUser')
  342. ->with('NewUser', 'PasswordOfTheNewUser');
  343. $this->logger
  344. ->expects($this->once())
  345. ->method('info')
  346. ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
  347. $loggedInUser = $this->getMockBuilder(IUser::class)
  348. ->disableOriginalConstructor()
  349. ->getMock();
  350. $loggedInUser
  351. ->expects($this->once())
  352. ->method('getUID')
  353. ->willReturn('adminUser');
  354. $this->userSession
  355. ->expects($this->once())
  356. ->method('getUser')
  357. ->willReturn($loggedInUser);
  358. $this->groupManager
  359. ->expects($this->once())
  360. ->method('isAdmin')
  361. ->with('adminUser')
  362. ->willReturn(true);
  363. $this->assertTrue(key_exists(
  364. 'id',
  365. $this->api->addUser('NewUser', 'PasswordOfTheNewUser')->getData()
  366. ));
  367. }
  368. public function testAddUserSuccessfulWithDisplayName() {
  369. $api = $this->getMockBuilder(UsersController::class)
  370. ->setConstructorArgs([
  371. 'provisioning_api',
  372. $this->request,
  373. $this->userManager,
  374. $this->config,
  375. $this->groupManager,
  376. $this->userSession,
  377. $this->accountManager,
  378. $this->urlGenerator,
  379. $this->logger,
  380. $this->l10nFactory,
  381. $this->newUserMailHelper,
  382. $this->secureRandom,
  383. $this->remoteWipe,
  384. $this->knownUserService,
  385. $this->eventDispatcher,
  386. ])
  387. ->setMethods(['editUser'])
  388. ->getMock();
  389. $this->userManager
  390. ->expects($this->once())
  391. ->method('userExists')
  392. ->with('NewUser')
  393. ->willReturn(false);
  394. $this->userManager
  395. ->expects($this->once())
  396. ->method('createUser')
  397. ->with('NewUser', 'PasswordOfTheNewUser');
  398. $this->logger
  399. ->expects($this->once())
  400. ->method('info')
  401. ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
  402. $loggedInUser = $this->getMockBuilder(IUser::class)
  403. ->disableOriginalConstructor()
  404. ->getMock();
  405. $loggedInUser
  406. ->expects($this->any())
  407. ->method('getUID')
  408. ->willReturn('adminUser');
  409. $this->userSession
  410. ->expects($this->any())
  411. ->method('getUser')
  412. ->willReturn($loggedInUser);
  413. $this->groupManager
  414. ->expects($this->once())
  415. ->method('isAdmin')
  416. ->with('adminUser')
  417. ->willReturn(true);
  418. $api
  419. ->expects($this->once())
  420. ->method('editUser')
  421. ->with('NewUser', 'display', 'DisplayNameOfTheNewUser');
  422. $this->assertTrue(key_exists(
  423. 'id',
  424. $api->addUser('NewUser', 'PasswordOfTheNewUser', 'DisplayNameOfTheNewUser')->getData()
  425. ));
  426. }
  427. public function testAddUserSuccessfulGenerateUserID() {
  428. $this->config
  429. ->expects($this->any())
  430. ->method('getAppValue')
  431. ->willReturnCallback(function ($appid, $key, $default) {
  432. if ($key === 'newUser.generateUserID') {
  433. return 'yes';
  434. }
  435. return null;
  436. });
  437. $this->userManager
  438. ->expects($this->any())
  439. ->method('userExists')
  440. ->with($this->anything())
  441. ->willReturn(false);
  442. $this->userManager
  443. ->expects($this->once())
  444. ->method('createUser')
  445. ->with($this->anything(), 'PasswordOfTheNewUser');
  446. $this->logger
  447. ->expects($this->once())
  448. ->method('info')
  449. ->with($this->stringStartsWith('Successful addUser call with userid: '), ['app' => 'ocs_api']);
  450. $loggedInUser = $this->getMockBuilder(IUser::class)
  451. ->disableOriginalConstructor()
  452. ->getMock();
  453. $loggedInUser
  454. ->expects($this->once())
  455. ->method('getUID')
  456. ->willReturn('adminUser');
  457. $this->userSession
  458. ->expects($this->once())
  459. ->method('getUser')
  460. ->willReturn($loggedInUser);
  461. $this->groupManager
  462. ->expects($this->once())
  463. ->method('isAdmin')
  464. ->with('adminUser')
  465. ->willReturn(true);
  466. $this->secureRandom->expects($this->any())
  467. ->method('generate')
  468. ->with(10)
  469. ->willReturnCallback(function () {
  470. return (string)rand(100000000, 999999999);
  471. });
  472. $this->assertTrue(key_exists(
  473. 'id',
  474. $this->api->addUser('', 'PasswordOfTheNewUser')->getData()
  475. ));
  476. }
  477. public function testAddUserSuccessfulGeneratePassword() {
  478. $this->userManager
  479. ->expects($this->once())
  480. ->method('userExists')
  481. ->with('NewUser')
  482. ->willReturn(false);
  483. $newUser = $this->createMock(IUser::class);
  484. $newUser->expects($this->once())
  485. ->method('setEMailAddress');
  486. $this->userManager
  487. ->expects($this->once())
  488. ->method('createUser')
  489. ->willReturn($newUser);
  490. $this->logger
  491. ->expects($this->once())
  492. ->method('info')
  493. ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
  494. $loggedInUser = $this->getMockBuilder(IUser::class)
  495. ->disableOriginalConstructor()
  496. ->getMock();
  497. $loggedInUser
  498. ->expects($this->once())
  499. ->method('getUID')
  500. ->willReturn('adminUser');
  501. $this->userSession
  502. ->expects($this->once())
  503. ->method('getUser')
  504. ->willReturn($loggedInUser);
  505. $this->groupManager
  506. ->expects($this->once())
  507. ->method('isAdmin')
  508. ->with('adminUser')
  509. ->willReturn(true);
  510. $this->eventDispatcher
  511. ->expects($this->once())
  512. ->method('dispatchTyped')
  513. ->with(new GenerateSecurePasswordEvent());
  514. $this->assertTrue(key_exists(
  515. 'id',
  516. $this->api->addUser('NewUser', '', '', 'foo@bar')->getData()
  517. ));
  518. }
  519. public function testAddUserFailedToGenerateUserID() {
  520. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  521. $this->expectExceptionMessage('Could not create non-existing user id');
  522. $this->expectExceptionCode(111);
  523. $this->config
  524. ->expects($this->any())
  525. ->method('getAppValue')
  526. ->willReturnCallback(function ($appid, $key, $default) {
  527. if ($key === 'newUser.generateUserID') {
  528. return 'yes';
  529. }
  530. return null;
  531. });
  532. $this->userManager
  533. ->expects($this->any())
  534. ->method('userExists')
  535. ->with($this->anything())
  536. ->willReturn(true);
  537. $this->userManager
  538. ->expects($this->never())
  539. ->method('createUser');
  540. $loggedInUser = $this->getMockBuilder(IUser::class)
  541. ->disableOriginalConstructor()
  542. ->getMock();
  543. $loggedInUser
  544. ->expects($this->once())
  545. ->method('getUID')
  546. ->willReturn('adminUser');
  547. $this->userSession
  548. ->expects($this->once())
  549. ->method('getUser')
  550. ->willReturn($loggedInUser);
  551. $this->groupManager
  552. ->expects($this->once())
  553. ->method('isAdmin')
  554. ->with('adminUser')
  555. ->willReturn(true);
  556. $this->api->addUser('', 'PasswordOfTheNewUser')->getData();
  557. }
  558. public function testAddUserEmailRequired() {
  559. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  560. $this->expectExceptionMessage('Required email address was not provided');
  561. $this->expectExceptionCode(110);
  562. $this->config
  563. ->expects($this->any())
  564. ->method('getAppValue')
  565. ->willReturnCallback(function ($appid, $key, $default) {
  566. if ($key === 'newUser.requireEmail') {
  567. return 'yes';
  568. }
  569. return null;
  570. });
  571. $this->userManager
  572. ->expects($this->once())
  573. ->method('userExists')
  574. ->with('NewUser')
  575. ->willReturn(false);
  576. $this->userManager
  577. ->expects($this->never())
  578. ->method('createUser');
  579. $loggedInUser = $this->getMockBuilder(IUser::class)
  580. ->disableOriginalConstructor()
  581. ->getMock();
  582. $loggedInUser
  583. ->expects($this->once())
  584. ->method('getUID')
  585. ->willReturn('adminUser');
  586. $this->userSession
  587. ->expects($this->once())
  588. ->method('getUser')
  589. ->willReturn($loggedInUser);
  590. $this->groupManager
  591. ->expects($this->once())
  592. ->method('isAdmin')
  593. ->with('adminUser')
  594. ->willReturn(true);
  595. $this->assertTrue(key_exists(
  596. 'id',
  597. $this->api->addUser('NewUser', 'PasswordOfTheNewUser')->getData()
  598. ));
  599. }
  600. public function testAddUserExistingGroup() {
  601. $this->userManager
  602. ->expects($this->once())
  603. ->method('userExists')
  604. ->with('NewUser')
  605. ->willReturn(false);
  606. $loggedInUser = $this->getMockBuilder(IUser::class)
  607. ->disableOriginalConstructor()
  608. ->getMock();
  609. $loggedInUser
  610. ->expects($this->once())
  611. ->method('getUID')
  612. ->willReturn('adminUser');
  613. $this->userSession
  614. ->expects($this->once())
  615. ->method('getUser')
  616. ->willReturn($loggedInUser);
  617. $this->groupManager
  618. ->expects($this->once())
  619. ->method('isAdmin')
  620. ->with('adminUser')
  621. ->willReturn(true);
  622. $this->groupManager
  623. ->expects($this->once())
  624. ->method('groupExists')
  625. ->with('ExistingGroup')
  626. ->willReturn(true);
  627. $user = $this->getMockBuilder(IUser::class)
  628. ->disableOriginalConstructor()
  629. ->getMock();
  630. $this->userManager
  631. ->expects($this->once())
  632. ->method('createUser')
  633. ->with('NewUser', 'PasswordOfTheNewUser')
  634. ->willReturn($user);
  635. $group = $this->getMockBuilder('OCP\IGroup')
  636. ->disableOriginalConstructor()
  637. ->getMock();
  638. $group
  639. ->expects($this->once())
  640. ->method('addUser')
  641. ->with($user);
  642. $this->groupManager
  643. ->expects($this->once())
  644. ->method('get')
  645. ->with('ExistingGroup')
  646. ->willReturn($group);
  647. $this->logger
  648. ->expects($this->exactly(2))
  649. ->method('info')
  650. ->withConsecutive(
  651. ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
  652. ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']]
  653. );
  654. $this->assertTrue(key_exists(
  655. 'id',
  656. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData()
  657. ));
  658. }
  659. public function testAddUserUnsuccessful() {
  660. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  661. $this->expectExceptionMessage('Bad request');
  662. $this->expectExceptionCode(101);
  663. $exception = new Exception('User backend not found.');
  664. $this->userManager
  665. ->expects($this->once())
  666. ->method('userExists')
  667. ->with('NewUser')
  668. ->willReturn(false);
  669. $this->userManager
  670. ->expects($this->once())
  671. ->method('createUser')
  672. ->with('NewUser', 'PasswordOfTheNewUser')
  673. ->will($this->throwException($exception));
  674. $this->logger
  675. ->expects($this->once())
  676. ->method('error')
  677. ->with(
  678. 'Failed addUser attempt with exception.',
  679. [
  680. 'app' => 'ocs_api',
  681. 'exception' => $exception
  682. ]
  683. );
  684. $loggedInUser = $this->getMockBuilder(IUser::class)
  685. ->disableOriginalConstructor()
  686. ->getMock();
  687. $loggedInUser
  688. ->expects($this->once())
  689. ->method('getUID')
  690. ->willReturn('adminUser');
  691. $this->userSession
  692. ->expects($this->once())
  693. ->method('getUser')
  694. ->willReturn($loggedInUser);
  695. $this->groupManager
  696. ->expects($this->once())
  697. ->method('isAdmin')
  698. ->with('adminUser')
  699. ->willReturn(true);
  700. $this->api->addUser('NewUser', 'PasswordOfTheNewUser');
  701. }
  702. public function testAddUserAsSubAdminNoGroup() {
  703. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  704. $this->expectExceptionMessage('no group specified (required for subadmins)');
  705. $this->expectExceptionCode(106);
  706. $loggedInUser = $this->getMockBuilder(IUser::class)
  707. ->disableOriginalConstructor()
  708. ->getMock();
  709. $loggedInUser
  710. ->expects($this->once())
  711. ->method('getUID')
  712. ->willReturn('regularUser');
  713. $this->userSession
  714. ->expects($this->once())
  715. ->method('getUser')
  716. ->willReturn($loggedInUser);
  717. $this->groupManager
  718. ->expects($this->once())
  719. ->method('isAdmin')
  720. ->with('regularUser')
  721. ->willReturn(false);
  722. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  723. ->disableOriginalConstructor()->getMock();
  724. $this->groupManager
  725. ->expects($this->once())
  726. ->method('getSubAdmin')
  727. ->with()
  728. ->willReturn($subAdminManager);
  729. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', []);
  730. }
  731. public function testAddUserAsSubAdminValidGroupNotSubAdmin() {
  732. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  733. $this->expectExceptionMessage('insufficient privileges for group ExistingGroup');
  734. $this->expectExceptionCode(105);
  735. $loggedInUser = $this->getMockBuilder(IUser::class)
  736. ->disableOriginalConstructor()
  737. ->getMock();
  738. $loggedInUser
  739. ->expects($this->once())
  740. ->method('getUID')
  741. ->willReturn('regularUser');
  742. $this->userSession
  743. ->expects($this->once())
  744. ->method('getUser')
  745. ->willReturn($loggedInUser);
  746. $this->groupManager
  747. ->expects($this->once())
  748. ->method('isAdmin')
  749. ->with('regularUser')
  750. ->willReturn(false);
  751. $existingGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  752. $this->groupManager
  753. ->expects($this->once())
  754. ->method('get')
  755. ->with('ExistingGroup')
  756. ->willReturn($existingGroup);
  757. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  758. ->disableOriginalConstructor()->getMock();
  759. $subAdminManager
  760. ->expects($this->once())
  761. ->method('isSubAdminOfGroup')
  762. ->with($loggedInUser, $existingGroup)
  763. ->willReturn(false);
  764. $this->groupManager
  765. ->expects($this->once())
  766. ->method('getSubAdmin')
  767. ->with()
  768. ->willReturn($subAdminManager);
  769. $this->groupManager
  770. ->expects($this->once())
  771. ->method('groupExists')
  772. ->with('ExistingGroup')
  773. ->willReturn(true);
  774. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData();
  775. }
  776. public function testAddUserAsSubAdminExistingGroups() {
  777. $this->userManager
  778. ->expects($this->once())
  779. ->method('userExists')
  780. ->with('NewUser')
  781. ->willReturn(false);
  782. $loggedInUser = $this->getMockBuilder(IUser::class)
  783. ->disableOriginalConstructor()
  784. ->getMock();
  785. $loggedInUser
  786. ->expects($this->once())
  787. ->method('getUID')
  788. ->willReturn('subAdminUser');
  789. $this->userSession
  790. ->expects($this->once())
  791. ->method('getUser')
  792. ->willReturn($loggedInUser);
  793. $this->groupManager
  794. ->expects($this->once())
  795. ->method('isAdmin')
  796. ->with('subAdminUser')
  797. ->willReturn(false);
  798. $this->groupManager
  799. ->expects($this->exactly(2))
  800. ->method('groupExists')
  801. ->withConsecutive(
  802. ['ExistingGroup1'],
  803. ['ExistingGroup2']
  804. )
  805. ->willReturn(true);
  806. $user = $this->getMockBuilder(IUser::class)
  807. ->disableOriginalConstructor()
  808. ->getMock();
  809. $this->userManager
  810. ->expects($this->once())
  811. ->method('createUser')
  812. ->with('NewUser', 'PasswordOfTheNewUser')
  813. ->willReturn($user);
  814. $existingGroup1 = $this->getMockBuilder('OCP\IGroup')
  815. ->disableOriginalConstructor()
  816. ->getMock();
  817. $existingGroup2 = $this->getMockBuilder('OCP\IGroup')
  818. ->disableOriginalConstructor()
  819. ->getMock();
  820. $existingGroup1
  821. ->expects($this->once())
  822. ->method('addUser')
  823. ->with($user);
  824. $existingGroup2
  825. ->expects($this->once())
  826. ->method('addUser')
  827. ->with($user);
  828. $this->groupManager
  829. ->expects($this->exactly(4))
  830. ->method('get')
  831. ->withConsecutive(
  832. ['ExistingGroup1'],
  833. ['ExistingGroup2'],
  834. ['ExistingGroup1'],
  835. ['ExistingGroup2']
  836. )
  837. ->willReturnMap([
  838. ['ExistingGroup1', $existingGroup1],
  839. ['ExistingGroup2', $existingGroup2]
  840. ]);
  841. $this->logger
  842. ->expects($this->exactly(3))
  843. ->method('info')
  844. ->withConsecutive(
  845. ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
  846. ['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']],
  847. ['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']]
  848. );
  849. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  850. ->disableOriginalConstructor()->getMock();
  851. $this->groupManager
  852. ->expects($this->once())
  853. ->method('getSubAdmin')
  854. ->willReturn($subAdminManager);
  855. $subAdminManager
  856. ->expects($this->exactly(2))
  857. ->method('isSubAdminOfGroup')
  858. ->withConsecutive(
  859. [$loggedInUser, $existingGroup1],
  860. [$loggedInUser, $existingGroup2]
  861. )
  862. ->willReturn(true);
  863. $this->assertTrue(key_exists(
  864. 'id',
  865. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup1', 'ExistingGroup2'])->getData()
  866. ));
  867. }
  868. public function testGetUserTargetDoesNotExist() {
  869. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  870. $this->expectExceptionMessage('User does not exist');
  871. $this->expectExceptionCode(404);
  872. $loggedInUser = $this->getMockBuilder(IUser::class)
  873. ->disableOriginalConstructor()
  874. ->getMock();
  875. $this->userSession
  876. ->method('getUser')
  877. ->willReturn($loggedInUser);
  878. $this->userManager
  879. ->expects($this->once())
  880. ->method('get')
  881. ->with('UserToGet')
  882. ->willReturn(null);
  883. $this->api->getUser('UserToGet');
  884. }
  885. public function testGetUserDataAsAdmin() {
  886. $group0 = $this->createMock(IGroup::class);
  887. $group1 = $this->createMock(IGroup::class);
  888. $group2 = $this->createMock(IGroup::class);
  889. $group3 = $this->createMock(IGroup::class);
  890. $loggedInUser = $this->getMockBuilder(IUser::class)
  891. ->disableOriginalConstructor()
  892. ->getMock();
  893. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  894. ->disableOriginalConstructor()
  895. ->getMock();
  896. $loggedInUser
  897. ->expects($this->once())
  898. ->method('getUID')
  899. ->willReturn('admin');
  900. $targetUser = $this->getMockBuilder(IUser::class)
  901. ->disableOriginalConstructor()
  902. ->getMock();
  903. $targetUser->expects($this->once())
  904. ->method('getSystemEMailAddress')
  905. ->willReturn('demo@nextcloud.com');
  906. $this->userSession
  907. ->expects($this->once())
  908. ->method('getUser')
  909. ->willReturn($loggedInUser);
  910. $this->userManager
  911. ->expects($this->exactly(2))
  912. ->method('get')
  913. ->with('UID')
  914. ->willReturn($targetUser);
  915. $this->groupManager
  916. ->expects($this->once())
  917. ->method('isAdmin')
  918. ->with('admin')
  919. ->willReturn(true);
  920. $this->groupManager
  921. ->expects($this->any())
  922. ->method('getUserGroups')
  923. ->willReturn([$group0, $group1, $group2]);
  924. $this->groupManager
  925. ->expects($this->once())
  926. ->method('getSubAdmin')
  927. ->willReturn($subAdminManager);
  928. $subAdminManager
  929. ->expects($this->once())
  930. ->method('getSubAdminsGroups')
  931. ->willReturn([$group3]);
  932. $group0->expects($this->once())
  933. ->method('getGID')
  934. ->willReturn('group0');
  935. $group1->expects($this->once())
  936. ->method('getGID')
  937. ->willReturn('group1');
  938. $group2->expects($this->once())
  939. ->method('getGID')
  940. ->willReturn('group2');
  941. $group3->expects($this->once())
  942. ->method('getGID')
  943. ->willReturn('group3');
  944. $this->mockAccount($targetUser, [
  945. IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
  946. IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
  947. IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
  948. IAccountManager::PROPERTY_FEDIVERSE => ['value' => 'fediverse'],
  949. IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
  950. IAccountManager::PROPERTY_ORGANISATION => ['value' => 'organisation'],
  951. IAccountManager::PROPERTY_ROLE => ['value' => 'role'],
  952. IAccountManager::PROPERTY_HEADLINE => ['value' => 'headline'],
  953. IAccountManager::PROPERTY_BIOGRAPHY => ['value' => 'biography'],
  954. IAccountManager::PROPERTY_PROFILE_ENABLED => ['value' => '1'],
  955. ]);
  956. $this->config
  957. ->method('getUserValue')
  958. ->willReturnMap([
  959. ['UID', 'core', 'enabled', 'true', 'true'],
  960. ]);
  961. $this->api
  962. ->expects($this->once())
  963. ->method('fillStorageInfo')
  964. ->with('UID')
  965. ->willReturn(['DummyValue']);
  966. $backend = $this->createMock(UserInterface::class);
  967. $backend->expects($this->any())
  968. ->method('implementsActions')
  969. ->willReturn(true);
  970. $targetUser
  971. ->expects($this->once())
  972. ->method('getDisplayName')
  973. ->willReturn('Demo User');
  974. $targetUser
  975. ->expects($this->once())
  976. ->method('getHome')
  977. ->willReturn('/var/www/newtcloud/data/UID');
  978. $targetUser
  979. ->expects($this->once())
  980. ->method('getLastLogin')
  981. ->willReturn(1521191471);
  982. $targetUser
  983. ->expects($this->once())
  984. ->method('getBackendClassName')
  985. ->willReturn('Database');
  986. $targetUser
  987. ->expects($this->once())
  988. ->method('getBackend')
  989. ->willReturn($backend);
  990. $targetUser
  991. ->method('getUID')
  992. ->willReturn('UID');
  993. $this->l10nFactory
  994. ->expects($this->once())
  995. ->method('getUserLanguage')
  996. ->with($targetUser)
  997. ->willReturn('de');
  998. $expected = [
  999. 'id' => 'UID',
  1000. 'enabled' => true,
  1001. 'storageLocation' => '/var/www/newtcloud/data/UID',
  1002. 'lastLogin' => 1521191471000,
  1003. 'backend' => 'Database',
  1004. 'subadmin' => ['group3'],
  1005. 'quota' => ['DummyValue'],
  1006. 'email' => 'demo@nextcloud.com',
  1007. 'displayname' => 'Demo User',
  1008. 'phone' => 'phone',
  1009. 'address' => 'address',
  1010. 'website' => 'website',
  1011. 'twitter' => 'twitter',
  1012. 'fediverse' => 'fediverse',
  1013. 'groups' => ['group0', 'group1', 'group2'],
  1014. 'language' => 'de',
  1015. 'locale' => null,
  1016. 'backendCapabilities' => [
  1017. 'setDisplayName' => true,
  1018. 'setPassword' => true,
  1019. ],
  1020. 'additional_mail' => [],
  1021. 'organisation' => 'organisation',
  1022. 'role' => 'role',
  1023. 'headline' => 'headline',
  1024. 'biography' => 'biography',
  1025. 'profile_enabled' => '1',
  1026. 'notify_email' => null,
  1027. ];
  1028. $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID']));
  1029. }
  1030. public function testGetUserDataAsSubAdminAndUserIsAccessible() {
  1031. $loggedInUser = $this->getMockBuilder(IUser::class)
  1032. ->disableOriginalConstructor()
  1033. ->getMock();
  1034. $loggedInUser
  1035. ->expects($this->once())
  1036. ->method('getUID')
  1037. ->willReturn('subadmin');
  1038. $targetUser = $this->getMockBuilder(IUser::class)
  1039. ->disableOriginalConstructor()
  1040. ->getMock();
  1041. $targetUser
  1042. ->expects($this->once())
  1043. ->method('getSystemEMailAddress')
  1044. ->willReturn('demo@nextcloud.com');
  1045. $this->userSession
  1046. ->expects($this->once())
  1047. ->method('getUser')
  1048. ->willReturn($loggedInUser);
  1049. $this->userManager
  1050. ->expects($this->exactly(2))
  1051. ->method('get')
  1052. ->with('UID')
  1053. ->willReturn($targetUser);
  1054. $this->groupManager
  1055. ->expects($this->once())
  1056. ->method('isAdmin')
  1057. ->with('subadmin')
  1058. ->willReturn(false);
  1059. $this->groupManager
  1060. ->expects($this->any())
  1061. ->method('getUserGroups')
  1062. ->willReturn([]);
  1063. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1064. ->disableOriginalConstructor()
  1065. ->getMock();
  1066. $subAdminManager
  1067. ->expects($this->once())
  1068. ->method('isUserAccessible')
  1069. ->with($loggedInUser, $targetUser)
  1070. ->willReturn(true);
  1071. $subAdminManager
  1072. ->expects($this->once())
  1073. ->method('getSubAdminsGroups')
  1074. ->willReturn([]);
  1075. $this->groupManager
  1076. ->expects($this->exactly(2))
  1077. ->method('getSubAdmin')
  1078. ->willReturn($subAdminManager);
  1079. $this->config
  1080. ->method('getUserValue')
  1081. ->willReturnMap([
  1082. ['UID', 'core', 'enabled', 'true', 'true'],
  1083. ]);
  1084. $this->api
  1085. ->expects($this->once())
  1086. ->method('fillStorageInfo')
  1087. ->with('UID')
  1088. ->willReturn(['DummyValue']);
  1089. $backend = $this->createMock(UserInterface::class);
  1090. $backend->expects($this->any())
  1091. ->method('implementsActions')
  1092. ->willReturn(true);
  1093. $targetUser
  1094. ->expects($this->once())
  1095. ->method('getDisplayName')
  1096. ->willReturn('Demo User');
  1097. $targetUser
  1098. ->expects($this->never())
  1099. ->method('getHome');
  1100. $targetUser
  1101. ->expects($this->once())
  1102. ->method('getLastLogin')
  1103. ->willReturn(1521191471);
  1104. $targetUser
  1105. ->expects($this->once())
  1106. ->method('getBackendClassName')
  1107. ->willReturn('Database');
  1108. $targetUser
  1109. ->expects($this->once())
  1110. ->method('getBackend')
  1111. ->willReturn($backend);
  1112. $targetUser
  1113. ->method('getUID')
  1114. ->willReturn('UID');
  1115. $this->mockAccount($targetUser, [
  1116. IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
  1117. IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
  1118. IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
  1119. IAccountManager::PROPERTY_FEDIVERSE => ['value' => 'fediverse'],
  1120. IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
  1121. IAccountManager::PROPERTY_ORGANISATION => ['value' => 'organisation'],
  1122. IAccountManager::PROPERTY_ROLE => ['value' => 'role'],
  1123. IAccountManager::PROPERTY_HEADLINE => ['value' => 'headline'],
  1124. IAccountManager::PROPERTY_BIOGRAPHY => ['value' => 'biography'],
  1125. IAccountManager::PROPERTY_PROFILE_ENABLED => ['value' => '1'],
  1126. ]);
  1127. $this->l10nFactory
  1128. ->expects($this->once())
  1129. ->method('getUserLanguage')
  1130. ->with($targetUser)
  1131. ->willReturn('da');
  1132. $expected = [
  1133. 'id' => 'UID',
  1134. 'enabled' => true,
  1135. 'lastLogin' => 1521191471000,
  1136. 'backend' => 'Database',
  1137. 'subadmin' => [],
  1138. 'quota' => ['DummyValue'],
  1139. 'email' => 'demo@nextcloud.com',
  1140. 'displayname' => 'Demo User',
  1141. 'phone' => 'phone',
  1142. 'address' => 'address',
  1143. 'website' => 'website',
  1144. 'twitter' => 'twitter',
  1145. 'fediverse' => 'fediverse',
  1146. 'groups' => [],
  1147. 'language' => 'da',
  1148. 'locale' => null,
  1149. 'backendCapabilities' => [
  1150. 'setDisplayName' => true,
  1151. 'setPassword' => true,
  1152. ],
  1153. 'additional_mail' => [],
  1154. 'organisation' => 'organisation',
  1155. 'role' => 'role',
  1156. 'headline' => 'headline',
  1157. 'biography' => 'biography',
  1158. 'profile_enabled' => '1',
  1159. 'notify_email' => null,
  1160. ];
  1161. $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID']));
  1162. }
  1163. public function testGetUserDataAsSubAdminAndUserIsNotAccessible() {
  1164. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  1165. $this->expectExceptionCode(998);
  1166. $loggedInUser = $this->getMockBuilder(IUser::class)
  1167. ->disableOriginalConstructor()
  1168. ->getMock();
  1169. $loggedInUser
  1170. ->expects($this->exactly(3))
  1171. ->method('getUID')
  1172. ->willReturn('subadmin');
  1173. $targetUser = $this->getMockBuilder(IUser::class)
  1174. ->disableOriginalConstructor()
  1175. ->getMock();
  1176. $this->userSession
  1177. ->method('getUser')
  1178. ->willReturn($loggedInUser);
  1179. $this->userManager
  1180. ->expects($this->once())
  1181. ->method('get')
  1182. ->with('UserToGet')
  1183. ->willReturn($targetUser);
  1184. $this->groupManager
  1185. ->expects($this->once())
  1186. ->method('isAdmin')
  1187. ->with('subadmin')
  1188. ->willReturn(false);
  1189. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1190. ->disableOriginalConstructor()
  1191. ->getMock();
  1192. $subAdminManager
  1193. ->expects($this->once())
  1194. ->method('isUserAccessible')
  1195. ->with($loggedInUser, $targetUser)
  1196. ->willReturn(false);
  1197. $this->groupManager
  1198. ->expects($this->once())
  1199. ->method('getSubAdmin')
  1200. ->willReturn($subAdminManager);
  1201. $this->invokePrivate($this->api, 'getUser', ['UserToGet']);
  1202. }
  1203. public function testGetUserDataAsSubAdminSelfLookup() {
  1204. $loggedInUser = $this->getMockBuilder(IUser::class)
  1205. ->disableOriginalConstructor()
  1206. ->getMock();
  1207. $loggedInUser
  1208. ->expects($this->exactly(2))
  1209. ->method('getUID')
  1210. ->willReturn('UID');
  1211. $targetUser = $this->getMockBuilder(IUser::class)
  1212. ->disableOriginalConstructor()
  1213. ->getMock();
  1214. $this->userSession
  1215. ->expects($this->once())
  1216. ->method('getUser')
  1217. ->willReturn($loggedInUser);
  1218. $this->userManager
  1219. ->expects($this->exactly(2))
  1220. ->method('get')
  1221. ->with('UID')
  1222. ->willReturn($targetUser);
  1223. $this->groupManager
  1224. ->expects($this->once())
  1225. ->method('isAdmin')
  1226. ->with('UID')
  1227. ->willReturn(false);
  1228. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1229. ->disableOriginalConstructor()
  1230. ->getMock();
  1231. $subAdminManager
  1232. ->expects($this->once())
  1233. ->method('isUserAccessible')
  1234. ->with($loggedInUser, $targetUser)
  1235. ->willReturn(false);
  1236. $subAdminManager
  1237. ->expects($this->once())
  1238. ->method('getSubAdminsGroups')
  1239. ->willReturn([]);
  1240. $this->groupManager
  1241. ->expects($this->exactly(2))
  1242. ->method('getSubAdmin')
  1243. ->willReturn($subAdminManager);
  1244. $this->groupManager
  1245. ->expects($this->any())
  1246. ->method('getUserGroups')
  1247. ->willReturn([]);
  1248. $this->api
  1249. ->expects($this->once())
  1250. ->method('fillStorageInfo')
  1251. ->with('UID')
  1252. ->willReturn(['DummyValue']);
  1253. $backend = $this->createMock(UserInterface::class);
  1254. $backend->expects($this->atLeastOnce())
  1255. ->method('implementsActions')
  1256. ->willReturn(false);
  1257. $targetUser
  1258. ->expects($this->once())
  1259. ->method('getDisplayName')
  1260. ->willReturn('Subadmin User');
  1261. $targetUser
  1262. ->expects($this->once())
  1263. ->method('getSystemEMailAddress')
  1264. ->willReturn('subadmin@nextcloud.com');
  1265. $targetUser
  1266. ->method('getUID')
  1267. ->willReturn('UID');
  1268. $targetUser
  1269. ->expects($this->never())
  1270. ->method('getHome');
  1271. $targetUser
  1272. ->expects($this->once())
  1273. ->method('getLastLogin')
  1274. ->willReturn(1521191471);
  1275. $targetUser
  1276. ->expects($this->once())
  1277. ->method('getBackendClassName')
  1278. ->willReturn('Database');
  1279. $targetUser
  1280. ->expects($this->once())
  1281. ->method('getBackend')
  1282. ->willReturn($backend);
  1283. $this->mockAccount($targetUser, [
  1284. IAccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
  1285. IAccountManager::PROPERTY_PHONE => ['value' => 'phone'],
  1286. IAccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
  1287. IAccountManager::PROPERTY_FEDIVERSE => ['value' => 'fediverse'],
  1288. IAccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
  1289. IAccountManager::PROPERTY_ORGANISATION => ['value' => 'organisation'],
  1290. IAccountManager::PROPERTY_ROLE => ['value' => 'role'],
  1291. IAccountManager::PROPERTY_HEADLINE => ['value' => 'headline'],
  1292. IAccountManager::PROPERTY_BIOGRAPHY => ['value' => 'biography'],
  1293. IAccountManager::PROPERTY_PROFILE_ENABLED => ['value' => '1'],
  1294. ]);
  1295. $this->l10nFactory
  1296. ->expects($this->once())
  1297. ->method('getUserLanguage')
  1298. ->with($targetUser)
  1299. ->willReturn('ru');
  1300. $expected = [
  1301. 'id' => 'UID',
  1302. 'lastLogin' => 1521191471000,
  1303. 'backend' => 'Database',
  1304. 'subadmin' => [],
  1305. 'quota' => ['DummyValue'],
  1306. 'email' => 'subadmin@nextcloud.com',
  1307. 'displayname' => 'Subadmin User',
  1308. 'phone' => 'phone',
  1309. 'address' => 'address',
  1310. 'website' => 'website',
  1311. 'twitter' => 'twitter',
  1312. 'fediverse' => 'fediverse',
  1313. 'groups' => [],
  1314. 'language' => 'ru',
  1315. 'locale' => null,
  1316. 'backendCapabilities' => [
  1317. 'setDisplayName' => false,
  1318. 'setPassword' => false,
  1319. ],
  1320. 'additional_mail' => [],
  1321. 'organisation' => 'organisation',
  1322. 'role' => 'role',
  1323. 'headline' => 'headline',
  1324. 'biography' => 'biography',
  1325. 'profile_enabled' => '1',
  1326. 'notify_email' => null,
  1327. ];
  1328. $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID']));
  1329. }
  1330. public function dataSearchByPhoneNumbers(): array {
  1331. return [
  1332. 'Invalid country' => ['Not a country code', ['12345' => ['NaN']], 400, null, null, []],
  1333. 'No number to search' => ['DE', ['12345' => ['NaN']], 200, null, null, []],
  1334. 'Valid number but no match' => ['DE', ['12345' => ['0711 / 25 24 28-90']], 200, ['+4971125242890'], [], []],
  1335. 'Invalid number' => ['FR', ['12345' => ['0711 / 25 24 28-90']], 200, null, null, []],
  1336. 'Invalid and valid number' => ['DE', ['12345' => ['NaN', '0711 / 25 24 28-90']], 200, ['+4971125242890'], [], []],
  1337. 'Valid and invalid number' => ['DE', ['12345' => ['0711 / 25 24 28-90', 'NaN']], 200, ['+4971125242890'], [], []],
  1338. 'Valid number and a match' => ['DE', ['12345' => ['0711 / 25 24 28-90']], 200, ['+4971125242890'], ['+4971125242890' => 'admin'], ['12345' => 'admin@localhost']],
  1339. 'Same number twice, later hits' => ['DE', ['12345' => ['0711 / 25 24 28-90'], '23456' => ['0711 / 25 24 28-90']], 200, ['+4971125242890'], ['+4971125242890' => 'admin'], ['23456' => 'admin@localhost']],
  1340. ];
  1341. }
  1342. /**
  1343. * @dataProvider dataSearchByPhoneNumbers
  1344. * @param string $location
  1345. * @param array $search
  1346. * @param int $status
  1347. * @param array $expected
  1348. */
  1349. public function testSearchByPhoneNumbers(string $location, array $search, int $status, ?array $searchUsers, ?array $userMatches, array $expected) {
  1350. $knownTo = 'knownTo';
  1351. $user = $this->createMock(IUser::class);
  1352. $user->method('getUID')
  1353. ->willReturn($knownTo);
  1354. $this->userSession->method('getUser')
  1355. ->willReturn($user);
  1356. if ($searchUsers === null) {
  1357. $this->accountManager->expects($this->never())
  1358. ->method('searchUsers');
  1359. } else {
  1360. $this->accountManager->expects($this->once())
  1361. ->method('searchUsers')
  1362. ->with(IAccountManager::PROPERTY_PHONE, $searchUsers)
  1363. ->willReturn($userMatches);
  1364. $this->knownUserService->expects($this->once())
  1365. ->method('deleteKnownTo')
  1366. ->with($knownTo);
  1367. $this->knownUserService->expects($this->exactly(count($expected)))
  1368. ->method('storeIsKnownToUser')
  1369. ->with($knownTo, $this->anything());
  1370. }
  1371. $this->urlGenerator->method('getAbsoluteURL')
  1372. ->with('/')
  1373. ->willReturn('https://localhost/');
  1374. $response = $this->api->searchByPhoneNumbers($location, $search);
  1375. self::assertEquals($status, $response->getStatus());
  1376. self::assertEquals($expected, $response->getData());
  1377. }
  1378. public function testEditUserRegularUserSelfEditChangeDisplayName() {
  1379. $loggedInUser = $this->getMockBuilder(IUser::class)
  1380. ->disableOriginalConstructor()
  1381. ->getMock();
  1382. $loggedInUser
  1383. ->expects($this->any())
  1384. ->method('getUID')
  1385. ->willReturn('UID');
  1386. $targetUser = $this->getMockBuilder(IUser::class)
  1387. ->disableOriginalConstructor()
  1388. ->getMock();
  1389. $this->userSession
  1390. ->expects($this->once())
  1391. ->method('getUser')
  1392. ->willReturn($loggedInUser);
  1393. $this->userManager
  1394. ->expects($this->once())
  1395. ->method('get')
  1396. ->with('UserToEdit')
  1397. ->willReturn($targetUser);
  1398. $targetUser
  1399. ->expects($this->once())
  1400. ->method('getBackend')
  1401. ->willReturn($this->createMock(ISetDisplayNameBackend::class));
  1402. $targetUser
  1403. ->expects($this->once())
  1404. ->method('setDisplayName')
  1405. ->with('NewDisplayName')
  1406. ->willReturn(true);
  1407. $targetUser
  1408. ->expects($this->any())
  1409. ->method('getUID')
  1410. ->willReturn('UID');
  1411. $this->assertEquals([], $this->api->editUser('UserToEdit', 'display', 'NewDisplayName')->getData());
  1412. }
  1413. public function testEditUserRegularUserSelfEditChangeEmailValid() {
  1414. $loggedInUser = $this->getMockBuilder(IUser::class)
  1415. ->disableOriginalConstructor()
  1416. ->getMock();
  1417. $loggedInUser
  1418. ->expects($this->any())
  1419. ->method('getUID')
  1420. ->willReturn('UID');
  1421. $targetUser = $this->getMockBuilder(IUser::class)
  1422. ->disableOriginalConstructor()
  1423. ->getMock();
  1424. $this->userSession
  1425. ->expects($this->once())
  1426. ->method('getUser')
  1427. ->willReturn($loggedInUser);
  1428. $this->userManager
  1429. ->expects($this->once())
  1430. ->method('get')
  1431. ->with('UserToEdit')
  1432. ->willReturn($targetUser);
  1433. $targetUser
  1434. ->expects($this->once())
  1435. ->method('setEMailAddress')
  1436. ->with('demo@nextcloud.com');
  1437. $targetUser
  1438. ->expects($this->any())
  1439. ->method('getUID')
  1440. ->willReturn('UID');
  1441. $backend = $this->createMock(UserInterface::class);
  1442. $targetUser
  1443. ->expects($this->any())
  1444. ->method('getBackend')
  1445. ->willReturn($backend);
  1446. $this->assertEquals([], $this->api->editUser('UserToEdit', 'email', 'demo@nextcloud.com')->getData());
  1447. }
  1448. public function testEditUserRegularUserSelfEditChangeEmailInvalid() {
  1449. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  1450. $this->expectExceptionCode(102);
  1451. $loggedInUser = $this->getMockBuilder(IUser::class)
  1452. ->disableOriginalConstructor()
  1453. ->getMock();
  1454. $loggedInUser
  1455. ->expects($this->any())
  1456. ->method('getUID')
  1457. ->willReturn('UID');
  1458. $targetUser = $this->getMockBuilder(IUser::class)
  1459. ->disableOriginalConstructor()
  1460. ->getMock();
  1461. $this->userSession
  1462. ->expects($this->once())
  1463. ->method('getUser')
  1464. ->willReturn($loggedInUser);
  1465. $this->userManager
  1466. ->expects($this->once())
  1467. ->method('get')
  1468. ->with('UserToEdit')
  1469. ->willReturn($targetUser);
  1470. $targetUser
  1471. ->expects($this->any())
  1472. ->method('getUID')
  1473. ->willReturn('UID');
  1474. $backend = $this->createMock(UserInterface::class);
  1475. $targetUser
  1476. ->expects($this->any())
  1477. ->method('getBackend')
  1478. ->willReturn($backend);
  1479. $this->api->editUser('UserToEdit', 'email', 'demo.org');
  1480. }
  1481. public function selfEditChangePropertyProvider() {
  1482. return [
  1483. [IAccountManager::PROPERTY_TWITTER, '@oldtwitter', '@newtwitter'],
  1484. [IAccountManager::PROPERTY_FEDIVERSE, '@oldFediverse@floss.social', '@newFediverse@floss.social'],
  1485. [IAccountManager::PROPERTY_PHONE, '1234', '12345'],
  1486. [IAccountManager::PROPERTY_ADDRESS, 'Something street 2', 'Another street 3'],
  1487. [IAccountManager::PROPERTY_WEBSITE, 'https://examplesite1', 'https://examplesite2'],
  1488. [IAccountManager::PROPERTY_ORGANISATION, 'Organisation A', 'Organisation B'],
  1489. [IAccountManager::PROPERTY_ROLE, 'Human', 'Alien'],
  1490. [IAccountManager::PROPERTY_HEADLINE, 'Hi', 'Hello'],
  1491. [IAccountManager::PROPERTY_BIOGRAPHY, 'A biography', 'Another biography'],
  1492. [IAccountManager::PROPERTY_PROFILE_ENABLED, '1', '0'],
  1493. ];
  1494. }
  1495. /**
  1496. * @dataProvider selfEditChangePropertyProvider
  1497. */
  1498. public function testEditUserRegularUserSelfEditChangeProperty($propertyName, $oldValue, $newValue) {
  1499. $loggedInUser = $this->getMockBuilder(IUser::class)
  1500. ->disableOriginalConstructor()
  1501. ->getMock();
  1502. $loggedInUser
  1503. ->expects($this->any())
  1504. ->method('getUID')
  1505. ->willReturn('UID');
  1506. $this->userSession
  1507. ->expects($this->once())
  1508. ->method('getUser')
  1509. ->willReturn($loggedInUser);
  1510. $this->userManager
  1511. ->expects($this->once())
  1512. ->method('get')
  1513. ->with('UserToEdit')
  1514. ->willReturn($loggedInUser);
  1515. $backend = $this->createMock(UserInterface::class);
  1516. $loggedInUser
  1517. ->expects($this->any())
  1518. ->method('getBackend')
  1519. ->willReturn($backend);
  1520. $propertyMock = $this->createMock(IAccountProperty::class);
  1521. $propertyMock->expects($this->any())
  1522. ->method('getName')
  1523. ->willReturn($propertyName);
  1524. $propertyMock->expects($this->any())
  1525. ->method('getValue')
  1526. ->willReturn($oldValue);
  1527. $propertyMock->expects($this->once())
  1528. ->method('setValue')
  1529. ->with($newValue)
  1530. ->willReturnSelf();
  1531. $propertyMock->expects($this->any())
  1532. ->method('getScope')
  1533. ->willReturn(IAccountManager::SCOPE_LOCAL);
  1534. $accountMock = $this->createMock(IAccount::class);
  1535. $accountMock->expects($this->any())
  1536. ->method('getProperty')
  1537. ->with($propertyName)
  1538. ->willReturn($propertyMock);
  1539. $this->accountManager->expects($this->atLeastOnce())
  1540. ->method('getAccount')
  1541. ->with($loggedInUser)
  1542. ->willReturn($accountMock);
  1543. $this->accountManager->expects($this->once())
  1544. ->method('updateAccount')
  1545. ->with($accountMock);
  1546. $this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName, $newValue)->getData());
  1547. }
  1548. public function selfEditChangePropertyScopeProvider() {
  1549. return [
  1550. [IAccountManager::PROPERTY_AVATAR, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1551. [IAccountManager::PROPERTY_DISPLAYNAME, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1552. [IAccountManager::PROPERTY_EMAIL, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1553. [IAccountManager::PROPERTY_TWITTER, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1554. [IAccountManager::PROPERTY_FEDIVERSE, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1555. [IAccountManager::PROPERTY_PHONE, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1556. [IAccountManager::PROPERTY_ADDRESS, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1557. [IAccountManager::PROPERTY_WEBSITE, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1558. [IAccountManager::PROPERTY_ORGANISATION, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1559. [IAccountManager::PROPERTY_ROLE, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1560. [IAccountManager::PROPERTY_HEADLINE, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1561. [IAccountManager::PROPERTY_BIOGRAPHY, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1562. [IAccountManager::PROPERTY_PROFILE_ENABLED, IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_FEDERATED],
  1563. ];
  1564. }
  1565. /**
  1566. * @dataProvider selfEditChangePropertyProvider
  1567. */
  1568. public function testEditUserRegularUserSelfEditChangePropertyScope($propertyName, $oldScope, $newScope) {
  1569. $loggedInUser = $this->getMockBuilder(IUser::class)
  1570. ->disableOriginalConstructor()
  1571. ->getMock();
  1572. $loggedInUser
  1573. ->expects($this->any())
  1574. ->method('getUID')
  1575. ->willReturn('UID');
  1576. $this->userSession
  1577. ->expects($this->once())
  1578. ->method('getUser')
  1579. ->willReturn($loggedInUser);
  1580. $this->userManager
  1581. ->expects($this->once())
  1582. ->method('get')
  1583. ->with('UserToEdit')
  1584. ->willReturn($loggedInUser);
  1585. $backend = $this->createMock(UserInterface::class);
  1586. $loggedInUser
  1587. ->expects($this->any())
  1588. ->method('getBackend')
  1589. ->willReturn($backend);
  1590. $propertyMock = $this->createMock(IAccountProperty::class);
  1591. $propertyMock->expects($this->any())
  1592. ->method('getName')
  1593. ->willReturn($propertyName);
  1594. $propertyMock->expects($this->any())
  1595. ->method('getValue')
  1596. ->willReturn('somevalue');
  1597. $propertyMock->expects($this->any())
  1598. ->method('getScope')
  1599. ->willReturn($oldScope);
  1600. $propertyMock->expects($this->atLeastOnce())
  1601. ->method('setScope')
  1602. ->with($newScope)
  1603. ->willReturnSelf();
  1604. $accountMock = $this->createMock(IAccount::class);
  1605. $accountMock->expects($this->any())
  1606. ->method('getProperty')
  1607. ->with($propertyName)
  1608. ->willReturn($propertyMock);
  1609. $this->accountManager->expects($this->atLeastOnce())
  1610. ->method('getAccount')
  1611. ->with($loggedInUser)
  1612. ->willReturn($accountMock);
  1613. $this->accountManager->expects($this->once())
  1614. ->method('updateAccount')
  1615. ->with($accountMock);
  1616. $this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName . 'Scope', $newScope)->getData());
  1617. }
  1618. public function testEditUserRegularUserSelfEditChangePassword() {
  1619. $loggedInUser = $this->getMockBuilder(IUser::class)
  1620. ->disableOriginalConstructor()
  1621. ->getMock();
  1622. $loggedInUser
  1623. ->expects($this->any())
  1624. ->method('getUID')
  1625. ->willReturn('UID');
  1626. $targetUser = $this->getMockBuilder(IUser::class)
  1627. ->disableOriginalConstructor()
  1628. ->getMock();
  1629. $this->userSession
  1630. ->expects($this->once())
  1631. ->method('getUser')
  1632. ->willReturn($loggedInUser);
  1633. $this->userManager
  1634. ->expects($this->once())
  1635. ->method('get')
  1636. ->with('UserToEdit')
  1637. ->willReturn($targetUser);
  1638. $targetUser
  1639. ->expects($this->once())
  1640. ->method('canChangePassword')
  1641. ->willReturn(true);
  1642. $targetUser
  1643. ->expects($this->once())
  1644. ->method('setPassword')
  1645. ->with('NewPassword');
  1646. $targetUser
  1647. ->expects($this->any())
  1648. ->method('getUID')
  1649. ->willReturn('UID');
  1650. $backend = $this->createMock(UserInterface::class);
  1651. $targetUser
  1652. ->expects($this->any())
  1653. ->method('getBackend')
  1654. ->willReturn($backend);
  1655. $this->assertEquals([], $this->api->editUser('UserToEdit', 'password', 'NewPassword')->getData());
  1656. }
  1657. public function testEditUserRegularUserSelfEditChangeQuota() {
  1658. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  1659. $this->expectExceptionCode(103);
  1660. $loggedInUser = $this->getMockBuilder(IUser::class)
  1661. ->disableOriginalConstructor()
  1662. ->getMock();
  1663. $loggedInUser
  1664. ->expects($this->any())
  1665. ->method('getUID')
  1666. ->willReturn('UID');
  1667. $targetUser = $this->getMockBuilder(IUser::class)
  1668. ->disableOriginalConstructor()
  1669. ->getMock();
  1670. $this->userSession
  1671. ->expects($this->once())
  1672. ->method('getUser')
  1673. ->willReturn($loggedInUser);
  1674. $this->userManager
  1675. ->expects($this->once())
  1676. ->method('get')
  1677. ->with('UserToEdit')
  1678. ->willReturn($targetUser);
  1679. $targetUser
  1680. ->expects($this->any())
  1681. ->method('getUID')
  1682. ->willReturn('UID');
  1683. $backend = $this->createMock(UserInterface::class);
  1684. $targetUser
  1685. ->expects($this->any())
  1686. ->method('getBackend')
  1687. ->willReturn($backend);
  1688. $this->api->editUser('UserToEdit', 'quota', 'NewQuota');
  1689. }
  1690. public function testEditUserAdminUserSelfEditChangeValidQuota() {
  1691. $this->config
  1692. ->expects($this->once())
  1693. ->method('getAppValue')
  1694. ->willReturnCallback(function ($appid, $key, $default) {
  1695. if ($key === 'max_quota') {
  1696. return '-1';
  1697. }
  1698. return null;
  1699. });
  1700. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1701. $loggedInUser
  1702. ->expects($this->any())
  1703. ->method('getUID')
  1704. ->willReturn('UID');
  1705. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1706. $targetUser->expects($this->once())
  1707. ->method('setQuota')
  1708. ->with('2.9 MB');
  1709. $this->userSession
  1710. ->expects($this->once())
  1711. ->method('getUser')
  1712. ->willReturn($loggedInUser);
  1713. $this->userManager
  1714. ->expects($this->once())
  1715. ->method('get')
  1716. ->with('UserToEdit')
  1717. ->willReturn($targetUser);
  1718. $this->groupManager
  1719. ->expects($this->exactly(3))
  1720. ->method('isAdmin')
  1721. ->with('UID')
  1722. ->willReturn(true);
  1723. $targetUser
  1724. ->expects($this->any())
  1725. ->method('getUID')
  1726. ->willReturn('UID');
  1727. $backend = $this->createMock(UserInterface::class);
  1728. $targetUser
  1729. ->expects($this->any())
  1730. ->method('getBackend')
  1731. ->willReturn($backend);
  1732. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  1733. }
  1734. public function testEditUserAdminUserSelfEditChangeInvalidQuota() {
  1735. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  1736. $this->expectExceptionMessage('Invalid quota value ABC');
  1737. $this->expectExceptionCode(102);
  1738. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1739. $loggedInUser
  1740. ->expects($this->any())
  1741. ->method('getUID')
  1742. ->willReturn('UID');
  1743. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1744. $this->userSession
  1745. ->expects($this->once())
  1746. ->method('getUser')
  1747. ->willReturn($loggedInUser);
  1748. $this->userManager
  1749. ->expects($this->once())
  1750. ->method('get')
  1751. ->with('UserToEdit')
  1752. ->willReturn($targetUser);
  1753. $this->groupManager
  1754. ->expects($this->exactly(3))
  1755. ->method('isAdmin')
  1756. ->with('UID')
  1757. ->willReturn(true);
  1758. $targetUser
  1759. ->expects($this->any())
  1760. ->method('getUID')
  1761. ->willReturn('UID');
  1762. $backend = $this->createMock(UserInterface::class);
  1763. $targetUser
  1764. ->expects($this->any())
  1765. ->method('getBackend')
  1766. ->willReturn($backend);
  1767. $this->api->editUser('UserToEdit', 'quota', 'ABC');
  1768. }
  1769. public function testEditUserAdminUserEditChangeValidQuota() {
  1770. $this->config
  1771. ->expects($this->once())
  1772. ->method('getAppValue')
  1773. ->willReturnCallback(function ($appid, $key, $default) {
  1774. if ($key === 'max_quota') {
  1775. return '-1';
  1776. }
  1777. return null;
  1778. });
  1779. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1780. $loggedInUser
  1781. ->expects($this->any())
  1782. ->method('getUID')
  1783. ->willReturn('admin');
  1784. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1785. $targetUser->expects($this->once())
  1786. ->method('setQuota')
  1787. ->with('2.9 MB');
  1788. $this->userSession
  1789. ->expects($this->once())
  1790. ->method('getUser')
  1791. ->willReturn($loggedInUser);
  1792. $this->userManager
  1793. ->expects($this->once())
  1794. ->method('get')
  1795. ->with('UserToEdit')
  1796. ->willReturn($targetUser);
  1797. $this->groupManager
  1798. ->expects($this->once())
  1799. ->method('isAdmin')
  1800. ->with('admin')
  1801. ->willReturn(true);
  1802. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1803. ->disableOriginalConstructor()
  1804. ->getMock();
  1805. $this->groupManager
  1806. ->expects($this->once())
  1807. ->method('getSubAdmin')
  1808. ->willReturn($subAdminManager);
  1809. $targetUser
  1810. ->expects($this->any())
  1811. ->method('getUID')
  1812. ->willReturn('UID');
  1813. $backend = $this->createMock(UserInterface::class);
  1814. $targetUser
  1815. ->expects($this->any())
  1816. ->method('getBackend')
  1817. ->willReturn($backend);
  1818. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  1819. }
  1820. public function testEditUserSelfEditChangeLanguage() {
  1821. $this->l10nFactory->expects($this->once())
  1822. ->method('findAvailableLanguages')
  1823. ->willReturn(['en', 'de', 'sv']);
  1824. $this->config->expects($this->any())
  1825. ->method('getSystemValue')
  1826. ->willReturnMap([
  1827. ['allow_user_to_change_display_name', true, true],
  1828. ['force_language', false, false],
  1829. ]);
  1830. $loggedInUser = $this->createMock(IUser::class);
  1831. $loggedInUser
  1832. ->expects($this->any())
  1833. ->method('getUID')
  1834. ->willReturn('UserToEdit');
  1835. $targetUser = $this->createMock(IUser::class);
  1836. $this->config->expects($this->once())
  1837. ->method('setUserValue')
  1838. ->with('UserToEdit', 'core', 'lang', 'de');
  1839. $this->userSession
  1840. ->expects($this->once())
  1841. ->method('getUser')
  1842. ->willReturn($loggedInUser);
  1843. $this->userManager
  1844. ->expects($this->once())
  1845. ->method('get')
  1846. ->with('UserToEdit')
  1847. ->willReturn($targetUser);
  1848. $this->groupManager
  1849. ->expects($this->atLeastOnce())
  1850. ->method('isAdmin')
  1851. ->with('UserToEdit')
  1852. ->willReturn(false);
  1853. $targetUser
  1854. ->expects($this->any())
  1855. ->method('getUID')
  1856. ->willReturn('UserToEdit');
  1857. $backend = $this->createMock(UserInterface::class);
  1858. $targetUser
  1859. ->expects($this->any())
  1860. ->method('getBackend')
  1861. ->willReturn($backend);
  1862. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData());
  1863. }
  1864. public function dataEditUserSelfEditChangeLanguageButForced() {
  1865. return [
  1866. ['de'],
  1867. [true],
  1868. ];
  1869. }
  1870. /**
  1871. * @dataProvider dataEditUserSelfEditChangeLanguageButForced
  1872. */
  1873. public function testEditUserSelfEditChangeLanguageButForced($forced) {
  1874. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  1875. $this->config->expects($this->any())
  1876. ->method('getSystemValue')
  1877. ->willReturnMap([
  1878. ['allow_user_to_change_display_name', true, true],
  1879. ['force_language', false, $forced],
  1880. ]);
  1881. $loggedInUser = $this->createMock(IUser::class);
  1882. $loggedInUser
  1883. ->expects($this->any())
  1884. ->method('getUID')
  1885. ->willReturn('UserToEdit');
  1886. $targetUser = $this->createMock(IUser::class);
  1887. $this->config->expects($this->never())
  1888. ->method('setUserValue');
  1889. $this->userSession
  1890. ->expects($this->once())
  1891. ->method('getUser')
  1892. ->willReturn($loggedInUser);
  1893. $this->userManager
  1894. ->expects($this->once())
  1895. ->method('get')
  1896. ->with('UserToEdit')
  1897. ->willReturn($targetUser);
  1898. $this->groupManager
  1899. ->expects($this->atLeastOnce())
  1900. ->method('isAdmin')
  1901. ->with('UserToEdit')
  1902. ->willReturn(false);
  1903. $targetUser
  1904. ->expects($this->any())
  1905. ->method('getUID')
  1906. ->willReturn('UserToEdit');
  1907. $backend = $this->createMock(UserInterface::class);
  1908. $targetUser
  1909. ->expects($this->any())
  1910. ->method('getBackend')
  1911. ->willReturn($backend);
  1912. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData());
  1913. }
  1914. public function testEditUserAdminEditChangeLanguage() {
  1915. $this->l10nFactory->expects($this->once())
  1916. ->method('findAvailableLanguages')
  1917. ->willReturn(['en', 'de', 'sv']);
  1918. $loggedInUser = $this->createMock(IUser::class);
  1919. $loggedInUser
  1920. ->expects($this->any())
  1921. ->method('getUID')
  1922. ->willReturn('admin');
  1923. $targetUser = $this->createMock(IUser::class);
  1924. $this->config->expects($this->once())
  1925. ->method('setUserValue')
  1926. ->with('UserToEdit', 'core', 'lang', 'de');
  1927. $this->userSession
  1928. ->expects($this->once())
  1929. ->method('getUser')
  1930. ->willReturn($loggedInUser);
  1931. $this->userManager
  1932. ->expects($this->once())
  1933. ->method('get')
  1934. ->with('UserToEdit')
  1935. ->willReturn($targetUser);
  1936. $this->groupManager
  1937. ->expects($this->once())
  1938. ->method('isAdmin')
  1939. ->with('admin')
  1940. ->willReturn(true);
  1941. $subAdminManager = $this->createMock(SubAdmin::class);
  1942. $this->groupManager
  1943. ->expects($this->once())
  1944. ->method('getSubAdmin')
  1945. ->willReturn($subAdminManager);
  1946. $targetUser
  1947. ->expects($this->any())
  1948. ->method('getUID')
  1949. ->willReturn('UserToEdit');
  1950. $backend = $this->createMock(UserInterface::class);
  1951. $targetUser
  1952. ->expects($this->any())
  1953. ->method('getBackend')
  1954. ->willReturn($backend);
  1955. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData());
  1956. }
  1957. /**
  1958. * @dataProvider dataEditUserSelfEditChangeLanguageButForced
  1959. */
  1960. public function testEditUserAdminEditChangeLanguageInvalidLanguage() {
  1961. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  1962. $this->l10nFactory->expects($this->once())
  1963. ->method('findAvailableLanguages')
  1964. ->willReturn(['en', 'de', 'sv']);
  1965. $loggedInUser = $this->createMock(IUser::class);
  1966. $loggedInUser
  1967. ->expects($this->any())
  1968. ->method('getUID')
  1969. ->willReturn('admin');
  1970. $targetUser = $this->createMock(IUser::class);
  1971. $this->config->expects($this->never())
  1972. ->method('setUserValue');
  1973. $this->userSession
  1974. ->expects($this->once())
  1975. ->method('getUser')
  1976. ->willReturn($loggedInUser);
  1977. $this->userManager
  1978. ->expects($this->once())
  1979. ->method('get')
  1980. ->with('UserToEdit')
  1981. ->willReturn($targetUser);
  1982. $this->groupManager
  1983. ->expects($this->once())
  1984. ->method('isAdmin')
  1985. ->with('admin')
  1986. ->willReturn(true);
  1987. $subAdminManager = $this->createMock(SubAdmin::class);
  1988. $this->groupManager
  1989. ->expects($this->once())
  1990. ->method('getSubAdmin')
  1991. ->willReturn($subAdminManager);
  1992. $targetUser
  1993. ->expects($this->any())
  1994. ->method('getUID')
  1995. ->willReturn('UserToEdit');
  1996. $backend = $this->createMock(UserInterface::class);
  1997. $targetUser
  1998. ->expects($this->any())
  1999. ->method('getBackend')
  2000. ->willReturn($backend);
  2001. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'ru')->getData());
  2002. }
  2003. public function testEditUserSubadminUserAccessible() {
  2004. $this->config
  2005. ->expects($this->once())
  2006. ->method('getAppValue')
  2007. ->willReturnCallback(function ($appid, $key, $default) {
  2008. if ($key === 'max_quota') {
  2009. return '-1';
  2010. }
  2011. return null;
  2012. });
  2013. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2014. $loggedInUser
  2015. ->expects($this->any())
  2016. ->method('getUID')
  2017. ->willReturn('subadmin');
  2018. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2019. $targetUser->expects($this->once())
  2020. ->method('setQuota')
  2021. ->with('2.9 MB');
  2022. $this->userSession
  2023. ->expects($this->once())
  2024. ->method('getUser')
  2025. ->willReturn($loggedInUser);
  2026. $this->userManager
  2027. ->expects($this->once())
  2028. ->method('get')
  2029. ->with('UserToEdit')
  2030. ->willReturn($targetUser);
  2031. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2032. ->disableOriginalConstructor()
  2033. ->getMock();
  2034. $subAdminManager
  2035. ->expects($this->once())
  2036. ->method('isUserAccessible')
  2037. ->with($loggedInUser, $targetUser)
  2038. ->willReturn(true);
  2039. $this->groupManager
  2040. ->expects($this->once())
  2041. ->method('getSubAdmin')
  2042. ->willReturn($subAdminManager);
  2043. $targetUser
  2044. ->expects($this->any())
  2045. ->method('getUID')
  2046. ->willReturn('UID');
  2047. $backend = $this->createMock(UserInterface::class);
  2048. $targetUser
  2049. ->expects($this->any())
  2050. ->method('getBackend')
  2051. ->willReturn($backend);
  2052. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  2053. }
  2054. public function testEditUserSubadminUserInaccessible() {
  2055. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2056. $this->expectExceptionCode(998);
  2057. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2058. $loggedInUser
  2059. ->expects($this->any())
  2060. ->method('getUID')
  2061. ->willReturn('subadmin');
  2062. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2063. $this->userSession
  2064. ->expects($this->once())
  2065. ->method('getUser')
  2066. ->willReturn($loggedInUser);
  2067. $this->userManager
  2068. ->expects($this->once())
  2069. ->method('get')
  2070. ->with('UserToEdit')
  2071. ->willReturn($targetUser);
  2072. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2073. ->disableOriginalConstructor()
  2074. ->getMock();
  2075. $subAdminManager
  2076. ->expects($this->once())
  2077. ->method('isUserAccessible')
  2078. ->with($loggedInUser, $targetUser)
  2079. ->willReturn(false);
  2080. $this->groupManager
  2081. ->expects($this->once())
  2082. ->method('getSubAdmin')
  2083. ->willReturn($subAdminManager);
  2084. $targetUser
  2085. ->expects($this->any())
  2086. ->method('getUID')
  2087. ->willReturn('UID');
  2088. $this->api->editUser('UserToEdit', 'quota', 'value');
  2089. }
  2090. public function testDeleteUserNotExistingUser() {
  2091. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2092. $this->expectExceptionCode(998);
  2093. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2094. $loggedInUser
  2095. ->expects($this->any())
  2096. ->method('getUID')
  2097. ->willReturn('UserToEdit');
  2098. $this->userSession
  2099. ->expects($this->once())
  2100. ->method('getUser')
  2101. ->willReturn($loggedInUser);
  2102. $this->userManager
  2103. ->expects($this->once())
  2104. ->method('get')
  2105. ->with('UserToDelete')
  2106. ->willReturn(null);
  2107. $this->api->deleteUser('UserToDelete');
  2108. }
  2109. public function testDeleteUserSelf() {
  2110. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2111. $this->expectExceptionCode(101);
  2112. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2113. $loggedInUser
  2114. ->expects($this->any())
  2115. ->method('getUID')
  2116. ->willReturn('UID');
  2117. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2118. $targetUser
  2119. ->expects($this->once())
  2120. ->method('getUID')
  2121. ->willReturn('UID');
  2122. $this->userSession
  2123. ->expects($this->once())
  2124. ->method('getUser')
  2125. ->willReturn($loggedInUser);
  2126. $this->userManager
  2127. ->expects($this->once())
  2128. ->method('get')
  2129. ->with('UserToDelete')
  2130. ->willReturn($targetUser);
  2131. $this->api->deleteUser('UserToDelete');
  2132. }
  2133. public function testDeleteSuccessfulUserAsAdmin() {
  2134. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2135. $loggedInUser
  2136. ->expects($this->any())
  2137. ->method('getUID')
  2138. ->willReturn('admin');
  2139. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2140. $targetUser
  2141. ->expects($this->once())
  2142. ->method('getUID')
  2143. ->willReturn('UID');
  2144. $this->userSession
  2145. ->expects($this->once())
  2146. ->method('getUser')
  2147. ->willReturn($loggedInUser);
  2148. $this->userManager
  2149. ->expects($this->once())
  2150. ->method('get')
  2151. ->with('UserToDelete')
  2152. ->willReturn($targetUser);
  2153. $this->groupManager
  2154. ->expects($this->once())
  2155. ->method('isAdmin')
  2156. ->with('admin')
  2157. ->willReturn(true);
  2158. $targetUser
  2159. ->expects($this->once())
  2160. ->method('delete')
  2161. ->willReturn(true);
  2162. $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData());
  2163. }
  2164. public function testDeleteUnsuccessfulUserAsAdmin() {
  2165. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2166. $this->expectExceptionCode(101);
  2167. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2168. $loggedInUser
  2169. ->expects($this->any())
  2170. ->method('getUID')
  2171. ->willReturn('admin');
  2172. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2173. $targetUser
  2174. ->expects($this->once())
  2175. ->method('getUID')
  2176. ->willReturn('UID');
  2177. $this->userSession
  2178. ->expects($this->once())
  2179. ->method('getUser')
  2180. ->willReturn($loggedInUser);
  2181. $this->userManager
  2182. ->expects($this->once())
  2183. ->method('get')
  2184. ->with('UserToDelete')
  2185. ->willReturn($targetUser);
  2186. $this->groupManager
  2187. ->expects($this->once())
  2188. ->method('isAdmin')
  2189. ->with('admin')
  2190. ->willReturn(true);
  2191. $targetUser
  2192. ->expects($this->once())
  2193. ->method('delete')
  2194. ->willReturn(false);
  2195. $this->api->deleteUser('UserToDelete');
  2196. }
  2197. public function testDeleteSuccessfulUserAsSubadmin() {
  2198. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2199. $loggedInUser
  2200. ->expects($this->any())
  2201. ->method('getUID')
  2202. ->willReturn('subadmin');
  2203. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2204. $targetUser
  2205. ->expects($this->once())
  2206. ->method('getUID')
  2207. ->willReturn('UID');
  2208. $this->userSession
  2209. ->expects($this->once())
  2210. ->method('getUser')
  2211. ->willReturn($loggedInUser);
  2212. $this->userManager
  2213. ->expects($this->once())
  2214. ->method('get')
  2215. ->with('UserToDelete')
  2216. ->willReturn($targetUser);
  2217. $this->groupManager
  2218. ->expects($this->once())
  2219. ->method('isAdmin')
  2220. ->with('subadmin')
  2221. ->willReturn(false);
  2222. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2223. ->disableOriginalConstructor()->getMock();
  2224. $subAdminManager
  2225. ->expects($this->once())
  2226. ->method('isUserAccessible')
  2227. ->with($loggedInUser, $targetUser)
  2228. ->willReturn(true);
  2229. $this->groupManager
  2230. ->expects($this->once())
  2231. ->method('getSubAdmin')
  2232. ->willReturn($subAdminManager);
  2233. $targetUser
  2234. ->expects($this->once())
  2235. ->method('delete')
  2236. ->willReturn(true);
  2237. $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData());
  2238. }
  2239. public function testDeleteUnsuccessfulUserAsSubadmin() {
  2240. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2241. $this->expectExceptionCode(101);
  2242. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2243. $loggedInUser
  2244. ->expects($this->any())
  2245. ->method('getUID')
  2246. ->willReturn('subadmin');
  2247. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2248. $targetUser
  2249. ->expects($this->once())
  2250. ->method('getUID')
  2251. ->willReturn('UID');
  2252. $this->userSession
  2253. ->expects($this->once())
  2254. ->method('getUser')
  2255. ->willReturn($loggedInUser);
  2256. $this->userManager
  2257. ->expects($this->once())
  2258. ->method('get')
  2259. ->with('UserToDelete')
  2260. ->willReturn($targetUser);
  2261. $this->groupManager
  2262. ->expects($this->once())
  2263. ->method('isAdmin')
  2264. ->with('subadmin')
  2265. ->willReturn(false);
  2266. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2267. ->disableOriginalConstructor()->getMock();
  2268. $subAdminManager
  2269. ->expects($this->once())
  2270. ->method('isUserAccessible')
  2271. ->with($loggedInUser, $targetUser)
  2272. ->willReturn(true);
  2273. $this->groupManager
  2274. ->expects($this->once())
  2275. ->method('getSubAdmin')
  2276. ->willReturn($subAdminManager);
  2277. $targetUser
  2278. ->expects($this->once())
  2279. ->method('delete')
  2280. ->willReturn(false);
  2281. $this->api->deleteUser('UserToDelete');
  2282. }
  2283. public function testDeleteUserAsSubAdminAndUserIsNotAccessible() {
  2284. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2285. $this->expectExceptionCode(998);
  2286. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2287. $loggedInUser
  2288. ->expects($this->any())
  2289. ->method('getUID')
  2290. ->willReturn('subadmin');
  2291. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2292. $targetUser
  2293. ->expects($this->once())
  2294. ->method('getUID')
  2295. ->willReturn('UID');
  2296. $this->userSession
  2297. ->expects($this->once())
  2298. ->method('getUser')
  2299. ->willReturn($loggedInUser);
  2300. $this->userManager
  2301. ->expects($this->once())
  2302. ->method('get')
  2303. ->with('UserToDelete')
  2304. ->willReturn($targetUser);
  2305. $this->groupManager
  2306. ->expects($this->once())
  2307. ->method('isAdmin')
  2308. ->with('subadmin')
  2309. ->willReturn(false);
  2310. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2311. ->disableOriginalConstructor()->getMock();
  2312. $subAdminManager
  2313. ->expects($this->once())
  2314. ->method('isUserAccessible')
  2315. ->with($loggedInUser, $targetUser)
  2316. ->willReturn(false);
  2317. $this->groupManager
  2318. ->expects($this->once())
  2319. ->method('getSubAdmin')
  2320. ->willReturn($subAdminManager);
  2321. $this->api->deleteUser('UserToDelete');
  2322. }
  2323. public function testGetUsersGroupsTargetUserNotExisting() {
  2324. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2325. $this->expectExceptionCode(998);
  2326. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2327. $this->userSession
  2328. ->expects($this->once())
  2329. ->method('getUser')
  2330. ->willReturn($loggedInUser);
  2331. $this->api->getUsersGroups('UserToLookup');
  2332. }
  2333. public function testGetUsersGroupsSelfTargetted() {
  2334. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2335. $loggedInUser
  2336. ->expects($this->once())
  2337. ->method('getUID')
  2338. ->willReturn('UserToLookup');
  2339. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2340. $targetUser
  2341. ->expects($this->once())
  2342. ->method('getUID')
  2343. ->willReturn('UserToLookup');
  2344. $this->userSession
  2345. ->expects($this->once())
  2346. ->method('getUser')
  2347. ->willReturn($loggedInUser);
  2348. $this->userManager
  2349. ->expects($this->once())
  2350. ->method('get')
  2351. ->with('UserToLookup')
  2352. ->willReturn($targetUser);
  2353. $this->groupManager
  2354. ->expects($this->once())
  2355. ->method('getUserGroupIds')
  2356. ->with($targetUser)
  2357. ->willReturn(['DummyValue']);
  2358. $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData());
  2359. }
  2360. public function testGetUsersGroupsForAdminUser() {
  2361. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2362. $loggedInUser
  2363. ->expects($this->exactly(2))
  2364. ->method('getUID')
  2365. ->willReturn('admin');
  2366. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2367. $targetUser
  2368. ->expects($this->once())
  2369. ->method('getUID')
  2370. ->willReturn('UserToLookup');
  2371. $this->userSession
  2372. ->expects($this->once())
  2373. ->method('getUser')
  2374. ->willReturn($loggedInUser);
  2375. $this->userManager
  2376. ->expects($this->once())
  2377. ->method('get')
  2378. ->with('UserToLookup')
  2379. ->willReturn($targetUser);
  2380. $this->groupManager
  2381. ->expects($this->once())
  2382. ->method('getUserGroupIds')
  2383. ->with($targetUser)
  2384. ->willReturn(['DummyValue']);
  2385. $this->groupManager
  2386. ->expects($this->once())
  2387. ->method('isAdmin')
  2388. ->with('admin')
  2389. ->willReturn(true);
  2390. $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData());
  2391. }
  2392. public function testGetUsersGroupsForSubAdminUserAndUserIsAccessible() {
  2393. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2394. $loggedInUser
  2395. ->expects($this->exactly(2))
  2396. ->method('getUID')
  2397. ->willReturn('subadmin');
  2398. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2399. $targetUser
  2400. ->expects($this->once())
  2401. ->method('getUID')
  2402. ->willReturn('UserToLookup');
  2403. $this->userSession
  2404. ->expects($this->once())
  2405. ->method('getUser')
  2406. ->willReturn($loggedInUser);
  2407. $this->userManager
  2408. ->expects($this->once())
  2409. ->method('get')
  2410. ->with('UserToLookup')
  2411. ->willReturn($targetUser);
  2412. $this->groupManager
  2413. ->expects($this->once())
  2414. ->method('isAdmin')
  2415. ->with('subadmin')
  2416. ->willReturn(false);
  2417. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2418. ->disableOriginalConstructor()->getMock();
  2419. $subAdminManager
  2420. ->expects($this->once())
  2421. ->method('isUserAccessible')
  2422. ->with($loggedInUser, $targetUser)
  2423. ->willReturn(true);
  2424. $this->groupManager
  2425. ->expects($this->once())
  2426. ->method('getSubAdmin')
  2427. ->willReturn($subAdminManager);
  2428. $group1 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2429. $group1
  2430. ->expects($this->any())
  2431. ->method('getGID')
  2432. ->willReturn('Group1');
  2433. $group2 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2434. $group2
  2435. ->expects($this->any())
  2436. ->method('getGID')
  2437. ->willReturn('Group2');
  2438. $subAdminManager
  2439. ->expects($this->once())
  2440. ->method('getSubAdminsGroups')
  2441. ->with($loggedInUser)
  2442. ->willReturn([$group1, $group2]);
  2443. $this->groupManager
  2444. ->expects($this->any())
  2445. ->method('getUserGroupIds')
  2446. ->with($targetUser)
  2447. ->willReturn(['Group1']);
  2448. $this->assertEquals(['groups' => ['Group1']], $this->api->getUsersGroups('UserToLookup')->getData());
  2449. }
  2450. public function testGetUsersGroupsForSubAdminUserAndUserIsInaccessible() {
  2451. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2452. $this->expectExceptionCode(998);
  2453. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2454. $loggedInUser
  2455. ->expects($this->exactly(2))
  2456. ->method('getUID')
  2457. ->willReturn('subadmin');
  2458. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2459. $targetUser
  2460. ->expects($this->once())
  2461. ->method('getUID')
  2462. ->willReturn('UserToLookup');
  2463. $this->userSession
  2464. ->expects($this->once())
  2465. ->method('getUser')
  2466. ->willReturn($loggedInUser);
  2467. $this->userManager
  2468. ->expects($this->once())
  2469. ->method('get')
  2470. ->with('UserToLookup')
  2471. ->willReturn($targetUser);
  2472. $this->groupManager
  2473. ->expects($this->once())
  2474. ->method('isAdmin')
  2475. ->with('subadmin')
  2476. ->willReturn(false);
  2477. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2478. ->disableOriginalConstructor()->getMock();
  2479. $subAdminManager
  2480. ->expects($this->once())
  2481. ->method('isUserAccessible')
  2482. ->with($loggedInUser, $targetUser)
  2483. ->willReturn(false);
  2484. $this->groupManager
  2485. ->expects($this->once())
  2486. ->method('getSubAdmin')
  2487. ->willReturn($subAdminManager);
  2488. $this->groupManager
  2489. ->expects($this->any())
  2490. ->method('getUserGroupIds')
  2491. ->with($targetUser)
  2492. ->willReturn(['Group1']);
  2493. $this->api->getUsersGroups('UserToLookup');
  2494. }
  2495. public function testAddToGroupWithTargetGroupNotExisting() {
  2496. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2497. $this->expectExceptionCode(102);
  2498. $this->groupManager->expects($this->once())
  2499. ->method('get')
  2500. ->with('GroupToAddTo')
  2501. ->willReturn(null);
  2502. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  2503. }
  2504. public function testAddToGroupWithNoGroupSpecified() {
  2505. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2506. $this->expectExceptionCode(101);
  2507. $this->api->addToGroup('TargetUser');
  2508. }
  2509. public function testAddToGroupWithTargetUserNotExisting() {
  2510. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2511. $this->expectExceptionCode(103);
  2512. $targetGroup = $this->createMock(IGroup::class);
  2513. $this->groupManager->expects($this->once())
  2514. ->method('get')
  2515. ->with('GroupToAddTo')
  2516. ->willReturn($targetGroup);
  2517. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  2518. }
  2519. public function testAddToGroupNoSubadmin() {
  2520. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2521. $this->expectExceptionCode(104);
  2522. $targetUser = $this->createMock(IUser::class);
  2523. $loggedInUser = $this->createMock(IUser::class);
  2524. $loggedInUser->expects($this->once())
  2525. ->method('getUID')
  2526. ->willReturn('subadmin');
  2527. $targetGroup = $this->createMock(IGroup::class);
  2528. $targetGroup->expects($this->never())
  2529. ->method('addUser')
  2530. ->with($targetUser);
  2531. $this->groupManager->expects($this->once())
  2532. ->method('get')
  2533. ->with('GroupToAddTo')
  2534. ->willReturn($targetGroup);
  2535. $subAdminManager = $this->createMock(SubAdmin::class);
  2536. $subAdminManager->expects($this->once())
  2537. ->method('isSubAdminOfGroup')
  2538. ->with($loggedInUser, $targetGroup)
  2539. ->willReturn(false);
  2540. $this->groupManager->expects($this->once())
  2541. ->method('getSubAdmin')
  2542. ->willReturn($subAdminManager);
  2543. $this->groupManager->expects($this->once())
  2544. ->method('isAdmin')
  2545. ->with('subadmin')
  2546. ->willReturn(false);
  2547. $this->userManager->expects($this->once())
  2548. ->method('get')
  2549. ->with('TargetUser')
  2550. ->willReturn($targetUser);
  2551. $this->userSession->expects($this->once())
  2552. ->method('getUser')
  2553. ->willReturn($loggedInUser);
  2554. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  2555. }
  2556. public function testAddToGroupSuccessAsSubadmin() {
  2557. $targetUser = $this->createMock(IUser::class);
  2558. $loggedInUser = $this->createMock(IUser::class);
  2559. $loggedInUser->expects($this->once())
  2560. ->method('getUID')
  2561. ->willReturn('subadmin');
  2562. $targetGroup = $this->createMock(IGroup::class);
  2563. $targetGroup->expects($this->once())
  2564. ->method('addUser')
  2565. ->with($targetUser);
  2566. $this->groupManager->expects($this->once())
  2567. ->method('get')
  2568. ->with('GroupToAddTo')
  2569. ->willReturn($targetGroup);
  2570. $subAdminManager = $this->createMock(SubAdmin::class);
  2571. $subAdminManager->expects($this->once())
  2572. ->method('isSubAdminOfGroup')
  2573. ->with($loggedInUser, $targetGroup)
  2574. ->willReturn(true);
  2575. $this->groupManager->expects($this->once())
  2576. ->method('getSubAdmin')
  2577. ->willReturn($subAdminManager);
  2578. $this->groupManager->expects($this->once())
  2579. ->method('isAdmin')
  2580. ->with('subadmin')
  2581. ->willReturn(false);
  2582. $this->userManager->expects($this->once())
  2583. ->method('get')
  2584. ->with('TargetUser')
  2585. ->willReturn($targetUser);
  2586. $this->userSession->expects($this->once())
  2587. ->method('getUser')
  2588. ->willReturn($loggedInUser);
  2589. $this->assertEquals(new DataResponse(), $this->api->addToGroup('TargetUser', 'GroupToAddTo'));
  2590. }
  2591. public function testAddToGroupSuccessAsAdmin() {
  2592. $targetUser = $this->createMock(IUser::class);
  2593. $loggedInUser = $this->createMock(IUser::class);
  2594. $loggedInUser->expects($this->once())
  2595. ->method('getUID')
  2596. ->willReturn('admin');
  2597. $targetGroup = $this->createMock(IGroup::class);
  2598. $targetGroup->expects($this->once())
  2599. ->method('addUser')
  2600. ->with($targetUser);
  2601. $this->groupManager->expects($this->once())
  2602. ->method('get')
  2603. ->with('GroupToAddTo')
  2604. ->willReturn($targetGroup);
  2605. $subAdminManager = $this->createMock(SubAdmin::class);
  2606. $subAdminManager->expects($this->never())
  2607. ->method('isSubAdminOfGroup');
  2608. $this->groupManager->expects($this->once())
  2609. ->method('getSubAdmin')
  2610. ->willReturn($subAdminManager);
  2611. $this->groupManager->expects($this->once())
  2612. ->method('isAdmin')
  2613. ->with('admin')
  2614. ->willReturn(true);
  2615. $this->userManager->expects($this->once())
  2616. ->method('get')
  2617. ->with('TargetUser')
  2618. ->willReturn($targetUser);
  2619. $this->userSession->expects($this->once())
  2620. ->method('getUser')
  2621. ->willReturn($loggedInUser);
  2622. $this->assertEquals(new DataResponse(), $this->api->addToGroup('TargetUser', 'GroupToAddTo'));
  2623. }
  2624. public function testRemoveFromGroupWithNoTargetGroup() {
  2625. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2626. $this->expectExceptionCode(101);
  2627. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2628. $this->userSession
  2629. ->expects($this->once())
  2630. ->method('getUser')
  2631. ->willReturn($loggedInUser);
  2632. $this->api->removeFromGroup('TargetUser', '');
  2633. }
  2634. public function testRemoveFromGroupWithEmptyTargetGroup() {
  2635. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2636. $this->expectExceptionCode(101);
  2637. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2638. $this->userSession
  2639. ->expects($this->once())
  2640. ->method('getUser')
  2641. ->willReturn($loggedInUser);
  2642. $this->api->removeFromGroup('TargetUser', '');
  2643. }
  2644. public function testRemoveFromGroupWithNotExistingTargetGroup() {
  2645. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2646. $this->expectExceptionCode(102);
  2647. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2648. $this->userSession
  2649. ->expects($this->once())
  2650. ->method('getUser')
  2651. ->willReturn($loggedInUser);
  2652. $this->groupManager
  2653. ->expects($this->once())
  2654. ->method('get')
  2655. ->with('TargetGroup')
  2656. ->willReturn(null);
  2657. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  2658. }
  2659. public function testRemoveFromGroupWithNotExistingTargetUser() {
  2660. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2661. $this->expectExceptionCode(103);
  2662. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2663. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2664. $this->userSession
  2665. ->expects($this->once())
  2666. ->method('getUser')
  2667. ->willReturn($loggedInUser);
  2668. $this->groupManager
  2669. ->expects($this->once())
  2670. ->method('get')
  2671. ->with('TargetGroup')
  2672. ->willReturn($targetGroup);
  2673. $this->userManager
  2674. ->expects($this->once())
  2675. ->method('get')
  2676. ->with('TargetUser')
  2677. ->willReturn(null);
  2678. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  2679. }
  2680. public function testRemoveFromGroupWithoutPermission() {
  2681. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2682. $this->expectExceptionCode(104);
  2683. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2684. $loggedInUser
  2685. ->expects($this->once())
  2686. ->method('getUID')
  2687. ->willReturn('unauthorizedUser');
  2688. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2689. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2690. $this->userSession
  2691. ->expects($this->once())
  2692. ->method('getUser')
  2693. ->willReturn($loggedInUser);
  2694. $this->groupManager
  2695. ->expects($this->once())
  2696. ->method('get')
  2697. ->with('TargetGroup')
  2698. ->willReturn($targetGroup);
  2699. $this->userManager
  2700. ->expects($this->once())
  2701. ->method('get')
  2702. ->with('TargetUser')
  2703. ->willReturn($targetUser);
  2704. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2705. ->disableOriginalConstructor()->getMock();
  2706. $this->groupManager
  2707. ->expects($this->once())
  2708. ->method('getSubAdmin')
  2709. ->willReturn($subAdminManager);
  2710. $this->groupManager
  2711. ->expects($this->once())
  2712. ->method('isAdmin')
  2713. ->with('unauthorizedUser')
  2714. ->willReturn(false);
  2715. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  2716. }
  2717. public function testRemoveFromGroupAsAdminFromAdmin() {
  2718. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2719. $this->expectExceptionMessage('Cannot remove yourself from the admin group');
  2720. $this->expectExceptionCode(105);
  2721. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2722. $loggedInUser
  2723. ->expects($this->any())
  2724. ->method('getUID')
  2725. ->willReturn('admin');
  2726. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2727. $targetUser
  2728. ->expects($this->once())
  2729. ->method('getUID')
  2730. ->willReturn('admin');
  2731. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2732. $targetGroup
  2733. ->expects($this->once())
  2734. ->method('getGID')
  2735. ->willReturn('admin');
  2736. $this->userSession
  2737. ->expects($this->once())
  2738. ->method('getUser')
  2739. ->willReturn($loggedInUser);
  2740. $this->groupManager
  2741. ->expects($this->once())
  2742. ->method('get')
  2743. ->with('admin')
  2744. ->willReturn($targetGroup);
  2745. $this->userManager
  2746. ->expects($this->once())
  2747. ->method('get')
  2748. ->with('Admin')
  2749. ->willReturn($targetUser);
  2750. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2751. ->disableOriginalConstructor()->getMock();
  2752. $this->groupManager
  2753. ->expects($this->once())
  2754. ->method('getSubAdmin')
  2755. ->willReturn($subAdminManager);
  2756. $this->groupManager
  2757. ->expects($this->any())
  2758. ->method('isAdmin')
  2759. ->with('admin')
  2760. ->willReturn(true);
  2761. $this->api->removeFromGroup('Admin', 'admin');
  2762. }
  2763. public function testRemoveFromGroupAsSubAdminFromSubAdmin() {
  2764. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2765. $this->expectExceptionMessage('Cannot remove yourself from this group as you are a SubAdmin');
  2766. $this->expectExceptionCode(105);
  2767. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2768. $loggedInUser
  2769. ->expects($this->any())
  2770. ->method('getUID')
  2771. ->willReturn('subadmin');
  2772. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2773. $targetUser
  2774. ->expects($this->once())
  2775. ->method('getUID')
  2776. ->willReturn('subadmin');
  2777. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2778. $targetGroup
  2779. ->expects($this->any())
  2780. ->method('getGID')
  2781. ->willReturn('subadmin');
  2782. $this->userSession
  2783. ->expects($this->once())
  2784. ->method('getUser')
  2785. ->willReturn($loggedInUser);
  2786. $this->groupManager
  2787. ->expects($this->once())
  2788. ->method('get')
  2789. ->with('subadmin')
  2790. ->willReturn($targetGroup);
  2791. $this->userManager
  2792. ->expects($this->once())
  2793. ->method('get')
  2794. ->with('SubAdmin')
  2795. ->willReturn($targetUser);
  2796. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2797. ->disableOriginalConstructor()->getMock();
  2798. $subAdminManager
  2799. ->expects($this->once())
  2800. ->method('isSubAdminOfGroup')
  2801. ->with($loggedInUser, $targetGroup)
  2802. ->willReturn(true);
  2803. $this->groupManager
  2804. ->expects($this->once())
  2805. ->method('getSubAdmin')
  2806. ->willReturn($subAdminManager);
  2807. $this->groupManager
  2808. ->expects($this->any())
  2809. ->method('isAdmin')
  2810. ->with('subadmin')
  2811. ->willReturn(false);
  2812. $this->api->removeFromGroup('SubAdmin', 'subadmin');
  2813. }
  2814. public function testRemoveFromGroupAsSubAdminFromLastSubAdminGroup() {
  2815. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2816. $this->expectExceptionMessage('Not viable to remove user from the last group you are SubAdmin of');
  2817. $this->expectExceptionCode(105);
  2818. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2819. $loggedInUser
  2820. ->expects($this->any())
  2821. ->method('getUID')
  2822. ->willReturn('subadmin');
  2823. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2824. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2825. $targetGroup
  2826. ->expects($this->any())
  2827. ->method('getGID')
  2828. ->willReturn('subadmin');
  2829. $this->userSession
  2830. ->expects($this->once())
  2831. ->method('getUser')
  2832. ->willReturn($loggedInUser);
  2833. $this->groupManager
  2834. ->expects($this->once())
  2835. ->method('get')
  2836. ->with('subadmin')
  2837. ->willReturn($targetGroup);
  2838. $this->userManager
  2839. ->expects($this->once())
  2840. ->method('get')
  2841. ->with('AnotherUser')
  2842. ->willReturn($targetUser);
  2843. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2844. ->disableOriginalConstructor()->getMock();
  2845. $subAdminManager
  2846. ->expects($this->once())
  2847. ->method('isSubAdminOfGroup')
  2848. ->with($loggedInUser, $targetGroup)
  2849. ->willReturn(true);
  2850. $this->groupManager
  2851. ->expects($this->once())
  2852. ->method('getSubAdmin')
  2853. ->willReturn($subAdminManager);
  2854. $subAdminManager
  2855. ->expects($this->once())
  2856. ->method('getSubAdminsGroups')
  2857. ->with($loggedInUser)
  2858. ->willReturn([$targetGroup]);
  2859. $this->groupManager
  2860. ->expects($this->any())
  2861. ->method('isAdmin')
  2862. ->with('subadmin')
  2863. ->willReturn(false);
  2864. $this->groupManager
  2865. ->expects($this->once())
  2866. ->method('getUserGroupIds')
  2867. ->with($targetUser)
  2868. ->willReturn(['subadmin', 'other group']);
  2869. $this->api->removeFromGroup('AnotherUser', 'subadmin');
  2870. }
  2871. public function testRemoveFromGroupSuccessful() {
  2872. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2873. $loggedInUser
  2874. ->expects($this->any())
  2875. ->method('getUID')
  2876. ->willReturn('admin');
  2877. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2878. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2879. $this->userSession
  2880. ->expects($this->once())
  2881. ->method('getUser')
  2882. ->willReturn($loggedInUser);
  2883. $this->groupManager
  2884. ->expects($this->once())
  2885. ->method('get')
  2886. ->with('admin')
  2887. ->willReturn($targetGroup);
  2888. $this->userManager
  2889. ->expects($this->once())
  2890. ->method('get')
  2891. ->with('AnotherUser')
  2892. ->willReturn($targetUser);
  2893. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2894. ->disableOriginalConstructor()->getMock();
  2895. $this->groupManager
  2896. ->expects($this->once())
  2897. ->method('getSubAdmin')
  2898. ->willReturn($subAdminManager);
  2899. $this->groupManager
  2900. ->expects($this->any())
  2901. ->method('isAdmin')
  2902. ->with('admin')
  2903. ->willReturn(true);
  2904. $targetGroup
  2905. ->expects($this->once())
  2906. ->method('removeUser')
  2907. ->with($targetUser);
  2908. $this->assertEquals([], $this->api->removeFromGroup('AnotherUser', 'admin')->getData());
  2909. }
  2910. public function testAddSubAdminWithNotExistingTargetUser() {
  2911. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2912. $this->expectExceptionMessage('User does not exist');
  2913. $this->expectExceptionCode(101);
  2914. $this->userManager
  2915. ->expects($this->once())
  2916. ->method('get')
  2917. ->with('NotExistingUser')
  2918. ->willReturn(null);
  2919. $this->api->addSubAdmin('NotExistingUser', '');
  2920. }
  2921. public function testAddSubAdminWithNotExistingTargetGroup() {
  2922. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2923. $this->expectExceptionMessage('Group does not exist');
  2924. $this->expectExceptionCode(102);
  2925. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2926. $this->userManager
  2927. ->expects($this->once())
  2928. ->method('get')
  2929. ->with('ExistingUser')
  2930. ->willReturn($targetUser);
  2931. $this->groupManager
  2932. ->expects($this->once())
  2933. ->method('get')
  2934. ->with('NotExistingGroup')
  2935. ->willReturn(null);
  2936. $this->api->addSubAdmin('ExistingUser', 'NotExistingGroup');
  2937. }
  2938. public function testAddSubAdminToAdminGroup() {
  2939. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  2940. $this->expectExceptionMessage('Cannot create subadmins for admin group');
  2941. $this->expectExceptionCode(103);
  2942. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2943. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2944. $targetGroup
  2945. ->expects($this->once())
  2946. ->method('getGID')
  2947. ->willReturn('admin');
  2948. $this->userManager
  2949. ->expects($this->once())
  2950. ->method('get')
  2951. ->with('ExistingUser')
  2952. ->willReturn($targetUser);
  2953. $this->groupManager
  2954. ->expects($this->once())
  2955. ->method('get')
  2956. ->with('ADmiN')
  2957. ->willReturn($targetGroup);
  2958. $this->api->addSubAdmin('ExistingUser', 'ADmiN');
  2959. }
  2960. public function testAddSubAdminTwice() {
  2961. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2962. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2963. $this->userManager
  2964. ->expects($this->once())
  2965. ->method('get')
  2966. ->with('ExistingUser')
  2967. ->willReturn($targetUser);
  2968. $this->groupManager
  2969. ->expects($this->once())
  2970. ->method('get')
  2971. ->with('TargetGroup')
  2972. ->willReturn($targetGroup);
  2973. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2974. ->disableOriginalConstructor()->getMock();
  2975. $subAdminManager
  2976. ->expects($this->once())
  2977. ->method('isSubAdminOfGroup')
  2978. ->with($targetUser, $targetGroup)
  2979. ->willReturn(true);
  2980. $this->groupManager
  2981. ->expects($this->once())
  2982. ->method('getSubAdmin')
  2983. ->willReturn($subAdminManager);
  2984. $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData());
  2985. }
  2986. public function testAddSubAdminSuccessful() {
  2987. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2988. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2989. $this->userManager
  2990. ->expects($this->once())
  2991. ->method('get')
  2992. ->with('ExistingUser')
  2993. ->willReturn($targetUser);
  2994. $this->groupManager
  2995. ->expects($this->once())
  2996. ->method('get')
  2997. ->with('TargetGroup')
  2998. ->willReturn($targetGroup);
  2999. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3000. ->disableOriginalConstructor()->getMock();
  3001. $subAdminManager
  3002. ->expects($this->once())
  3003. ->method('isSubAdminOfGroup')
  3004. ->with($targetUser, $targetGroup)
  3005. ->willReturn(false);
  3006. $subAdminManager
  3007. ->expects($this->once())
  3008. ->method('createSubAdmin')
  3009. ->with($targetUser, $targetGroup);
  3010. $this->groupManager
  3011. ->expects($this->once())
  3012. ->method('getSubAdmin')
  3013. ->willReturn($subAdminManager);
  3014. $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData());
  3015. }
  3016. public function testRemoveSubAdminNotExistingTargetUser() {
  3017. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3018. $this->expectExceptionMessage('User does not exist');
  3019. $this->expectExceptionCode(101);
  3020. $this->userManager
  3021. ->expects($this->once())
  3022. ->method('get')
  3023. ->with('NotExistingUser')
  3024. ->willReturn(null);
  3025. $this->api->removeSubAdmin('NotExistingUser', 'GroupToDeleteFrom');
  3026. }
  3027. public function testRemoveSubAdminNotExistingTargetGroup() {
  3028. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3029. $this->expectExceptionMessage('Group does not exist');
  3030. $this->expectExceptionCode(101);
  3031. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3032. $this->userManager
  3033. ->expects($this->once())
  3034. ->method('get')
  3035. ->with('ExistingUser')
  3036. ->willReturn($targetUser);
  3037. $this->groupManager
  3038. ->expects($this->once())
  3039. ->method('get')
  3040. ->with('GroupToDeleteFrom')
  3041. ->willReturn(null);
  3042. $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
  3043. }
  3044. public function testRemoveSubAdminFromNotASubadmin() {
  3045. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3046. $this->expectExceptionMessage('User is not a subadmin of this group');
  3047. $this->expectExceptionCode(102);
  3048. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3049. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  3050. $this->userManager
  3051. ->expects($this->once())
  3052. ->method('get')
  3053. ->with('ExistingUser')
  3054. ->willReturn($targetUser);
  3055. $this->groupManager
  3056. ->expects($this->once())
  3057. ->method('get')
  3058. ->with('GroupToDeleteFrom')
  3059. ->willReturn($targetGroup);
  3060. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3061. ->disableOriginalConstructor()->getMock();
  3062. $subAdminManager
  3063. ->expects($this->once())
  3064. ->method('isSubAdminOfGroup')
  3065. ->with($targetUser, $targetGroup)
  3066. ->willReturn(false);
  3067. $this->groupManager
  3068. ->expects($this->once())
  3069. ->method('getSubAdmin')
  3070. ->willReturn($subAdminManager);
  3071. $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
  3072. }
  3073. public function testRemoveSubAdminSuccessful() {
  3074. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3075. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  3076. $this->userManager
  3077. ->expects($this->once())
  3078. ->method('get')
  3079. ->with('ExistingUser')
  3080. ->willReturn($targetUser);
  3081. $this->groupManager
  3082. ->expects($this->once())
  3083. ->method('get')
  3084. ->with('GroupToDeleteFrom')
  3085. ->willReturn($targetGroup);
  3086. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3087. ->disableOriginalConstructor()->getMock();
  3088. $subAdminManager
  3089. ->expects($this->once())
  3090. ->method('isSubAdminOfGroup')
  3091. ->with($targetUser, $targetGroup)
  3092. ->willReturn(true);
  3093. $subAdminManager
  3094. ->expects($this->once())
  3095. ->method('deleteSubAdmin')
  3096. ->with($targetUser, $targetGroup);
  3097. $this->groupManager
  3098. ->expects($this->once())
  3099. ->method('getSubAdmin')
  3100. ->willReturn($subAdminManager);
  3101. $this->assertEquals([], $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom')->getData());
  3102. }
  3103. public function testGetUserSubAdminGroupsNotExistingTargetUser() {
  3104. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3105. $this->expectExceptionMessage('User does not exist');
  3106. $this->expectExceptionCode(404);
  3107. $this->userManager
  3108. ->expects($this->once())
  3109. ->method('get')
  3110. ->with('RequestedUser')
  3111. ->willReturn(null);
  3112. $this->api->getUserSubAdminGroups('RequestedUser');
  3113. }
  3114. public function testGetUserSubAdminGroupsWithGroups() {
  3115. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3116. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  3117. $targetGroup
  3118. ->expects($this->once())
  3119. ->method('getGID')
  3120. ->willReturn('TargetGroup');
  3121. $this->userManager
  3122. ->expects($this->once())
  3123. ->method('get')
  3124. ->with('RequestedUser')
  3125. ->willReturn($targetUser);
  3126. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3127. ->disableOriginalConstructor()->getMock();
  3128. $subAdminManager
  3129. ->expects($this->once())
  3130. ->method('getSubAdminsGroups')
  3131. ->with($targetUser)
  3132. ->willReturn([$targetGroup]);
  3133. $this->groupManager
  3134. ->expects($this->once())
  3135. ->method('getSubAdmin')
  3136. ->willReturn($subAdminManager);
  3137. $this->assertEquals(['TargetGroup'], $this->api->getUserSubAdminGroups('RequestedUser')->getData());
  3138. }
  3139. public function testEnableUser() {
  3140. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3141. $targetUser->expects($this->once())
  3142. ->method('setEnabled')
  3143. ->with(true);
  3144. $this->userManager
  3145. ->expects($this->once())
  3146. ->method('get')
  3147. ->with('RequestedUser')
  3148. ->willReturn($targetUser);
  3149. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3150. $loggedInUser
  3151. ->expects($this->exactly(2))
  3152. ->method('getUID')
  3153. ->willReturn('admin');
  3154. $this->userSession
  3155. ->expects($this->once())
  3156. ->method('getUser')
  3157. ->willReturn($loggedInUser);
  3158. $this->groupManager
  3159. ->expects($this->once())
  3160. ->method('isAdmin')
  3161. ->willReturn(true);
  3162. $this->assertEquals([], $this->api->enableUser('RequestedUser')->getData());
  3163. }
  3164. public function testDisableUser() {
  3165. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3166. $targetUser->expects($this->once())
  3167. ->method('setEnabled')
  3168. ->with(false);
  3169. $this->userManager
  3170. ->expects($this->once())
  3171. ->method('get')
  3172. ->with('RequestedUser')
  3173. ->willReturn($targetUser);
  3174. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  3175. $loggedInUser
  3176. ->expects($this->exactly(2))
  3177. ->method('getUID')
  3178. ->willReturn('admin');
  3179. $this->userSession
  3180. ->expects($this->once())
  3181. ->method('getUser')
  3182. ->willReturn($loggedInUser);
  3183. $this->groupManager
  3184. ->expects($this->once())
  3185. ->method('isAdmin')
  3186. ->willReturn(true);
  3187. $this->assertEquals([], $this->api->disableUser('RequestedUser')->getData());
  3188. }
  3189. public function testGetCurrentUserLoggedIn() {
  3190. $user = $this->createMock(IUser::class);
  3191. $user->expects($this->once())->method('getUID')->willReturn('UID');
  3192. $this->userSession->expects($this->once())->method('getUser')
  3193. ->willReturn($user);
  3194. /** @var UsersController | MockObject $api */
  3195. $api = $this->getMockBuilder(UsersController::class)
  3196. ->setConstructorArgs([
  3197. 'provisioning_api',
  3198. $this->request,
  3199. $this->userManager,
  3200. $this->config,
  3201. $this->groupManager,
  3202. $this->userSession,
  3203. $this->accountManager,
  3204. $this->urlGenerator,
  3205. $this->logger,
  3206. $this->l10nFactory,
  3207. $this->newUserMailHelper,
  3208. $this->secureRandom,
  3209. $this->remoteWipe,
  3210. $this->knownUserService,
  3211. $this->eventDispatcher,
  3212. ])
  3213. ->setMethods(['getUserData'])
  3214. ->getMock();
  3215. $api->expects($this->once())->method('getUserData')->with('UID', true)
  3216. ->willReturn(
  3217. [
  3218. 'id' => 'UID',
  3219. 'enabled' => 'true',
  3220. 'quota' => ['DummyValue'],
  3221. 'email' => 'demo@nextcloud.com',
  3222. 'displayname' => 'Demo User',
  3223. 'phone' => 'phone',
  3224. 'address' => 'address',
  3225. 'website' => 'website',
  3226. 'twitter' => 'twitter',
  3227. 'fediverse' => 'fediverse',
  3228. 'organisation' => 'organisation',
  3229. 'role' => 'role',
  3230. 'headline' => 'headline',
  3231. 'biography' => 'biography',
  3232. 'profile_enabled' => '1'
  3233. ]
  3234. );
  3235. $expected = [
  3236. 'id' => 'UID',
  3237. 'enabled' => 'true',
  3238. 'quota' => ['DummyValue'],
  3239. 'email' => 'demo@nextcloud.com',
  3240. 'phone' => 'phone',
  3241. 'address' => 'address',
  3242. 'website' => 'website',
  3243. 'twitter' => 'twitter',
  3244. 'fediverse' => 'fediverse',
  3245. 'organisation' => 'organisation',
  3246. 'role' => 'role',
  3247. 'headline' => 'headline',
  3248. 'biography' => 'biography',
  3249. 'profile_enabled' => '1',
  3250. 'display-name' => 'Demo User'
  3251. ];
  3252. $this->assertSame($expected, $api->getCurrentUser()->getData());
  3253. }
  3254. public function testGetCurrentUserNotLoggedIn() {
  3255. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3256. $this->userSession->expects($this->once())->method('getUser')
  3257. ->willReturn(null);
  3258. $this->api->getCurrentUser();
  3259. }
  3260. public function testGetUser() {
  3261. $loggedInUser = $this->createMock(IUser::class);
  3262. $loggedInUser
  3263. ->method('getUID')
  3264. ->willReturn('currentuser');
  3265. $this->userSession
  3266. ->method('getUser')
  3267. ->willReturn($loggedInUser);
  3268. /** @var UsersController | MockObject $api */
  3269. $api = $this->getMockBuilder(UsersController::class)
  3270. ->setConstructorArgs([
  3271. 'provisioning_api',
  3272. $this->request,
  3273. $this->userManager,
  3274. $this->config,
  3275. $this->groupManager,
  3276. $this->userSession,
  3277. $this->accountManager,
  3278. $this->urlGenerator,
  3279. $this->logger,
  3280. $this->l10nFactory,
  3281. $this->newUserMailHelper,
  3282. $this->secureRandom,
  3283. $this->remoteWipe,
  3284. $this->knownUserService,
  3285. $this->eventDispatcher,
  3286. ])
  3287. ->setMethods(['getUserData'])
  3288. ->getMock();
  3289. $expected = [
  3290. 'id' => 'UID',
  3291. 'enabled' => 'true',
  3292. 'quota' => ['DummyValue'],
  3293. 'email' => 'demo@nextcloud.com',
  3294. 'phone' => 'phone',
  3295. 'address' => 'address',
  3296. 'website' => 'website',
  3297. 'twitter' => 'twitter',
  3298. 'fediverse' => 'fediverse',
  3299. 'displayname' => 'Demo User',
  3300. 'organisation' => 'organisation',
  3301. 'role' => 'role',
  3302. 'headline' => 'headline',
  3303. 'biography' => 'biography',
  3304. 'profile_enabled' => '1'
  3305. ];
  3306. $api->expects($this->exactly(2))
  3307. ->method('getUserData')
  3308. ->withConsecutive(
  3309. ['uid', false],
  3310. ['currentuser', true],
  3311. )
  3312. ->willReturn($expected);
  3313. $this->assertSame($expected, $api->getUser('uid')->getData());
  3314. $this->assertSame($expected, $api->getUser('currentuser')->getData());
  3315. }
  3316. public function testResendWelcomeMessageWithNotExistingTargetUser() {
  3317. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3318. $this->expectExceptionCode(998);
  3319. $this->userManager
  3320. ->expects($this->once())
  3321. ->method('get')
  3322. ->with('NotExistingUser')
  3323. ->willReturn(null);
  3324. $this->api->resendWelcomeMessage('NotExistingUser');
  3325. }
  3326. public function testResendWelcomeMessageAsSubAdminAndUserIsNotAccessible() {
  3327. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3328. $this->expectExceptionCode(998);
  3329. $loggedInUser = $this->getMockBuilder(IUser::class)
  3330. ->disableOriginalConstructor()
  3331. ->getMock();
  3332. $loggedInUser
  3333. ->expects($this->exactly(1))
  3334. ->method('getUID')
  3335. ->willReturn('subadmin');
  3336. $targetUser = $this->getMockBuilder(IUser::class)
  3337. ->disableOriginalConstructor()
  3338. ->getMock();
  3339. $this->userSession
  3340. ->expects($this->once())
  3341. ->method('getUser')
  3342. ->willReturn($loggedInUser);
  3343. $this->userManager
  3344. ->expects($this->once())
  3345. ->method('get')
  3346. ->with('UserToGet')
  3347. ->willReturn($targetUser);
  3348. $this->groupManager
  3349. ->expects($this->once())
  3350. ->method('isAdmin')
  3351. ->with('subadmin')
  3352. ->willReturn(false);
  3353. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3354. ->disableOriginalConstructor()
  3355. ->getMock();
  3356. $subAdminManager
  3357. ->expects($this->once())
  3358. ->method('isUserAccessible')
  3359. ->with($loggedInUser, $targetUser)
  3360. ->willReturn(false);
  3361. $this->groupManager
  3362. ->expects($this->once())
  3363. ->method('getSubAdmin')
  3364. ->willReturn($subAdminManager);
  3365. $this->api->resendWelcomeMessage('UserToGet');
  3366. }
  3367. public function testResendWelcomeMessageNoEmail() {
  3368. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3369. $this->expectExceptionMessage('Email address not available');
  3370. $this->expectExceptionCode(101);
  3371. $loggedInUser = $this->getMockBuilder(IUser::class)
  3372. ->disableOriginalConstructor()
  3373. ->getMock();
  3374. $targetUser = $this->getMockBuilder(IUser::class)
  3375. ->disableOriginalConstructor()
  3376. ->getMock();
  3377. $this->userSession
  3378. ->expects($this->once())
  3379. ->method('getUser')
  3380. ->willReturn($loggedInUser);
  3381. $this->userManager
  3382. ->expects($this->once())
  3383. ->method('get')
  3384. ->with('UserToGet')
  3385. ->willReturn($targetUser);
  3386. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3387. ->disableOriginalConstructor()
  3388. ->getMock();
  3389. $subAdminManager
  3390. ->expects($this->once())
  3391. ->method('isUserAccessible')
  3392. ->with($loggedInUser, $targetUser)
  3393. ->willReturn(true);
  3394. $this->groupManager
  3395. ->expects($this->once())
  3396. ->method('getSubAdmin')
  3397. ->willReturn($subAdminManager);
  3398. $targetUser
  3399. ->expects($this->once())
  3400. ->method('getEmailAddress')
  3401. ->willReturn('');
  3402. $this->api->resendWelcomeMessage('UserToGet');
  3403. }
  3404. public function testResendWelcomeMessageNullEmail() {
  3405. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3406. $this->expectExceptionMessage('Email address not available');
  3407. $this->expectExceptionCode(101);
  3408. $loggedInUser = $this->getMockBuilder(IUser::class)
  3409. ->disableOriginalConstructor()
  3410. ->getMock();
  3411. $targetUser = $this->getMockBuilder(IUser::class)
  3412. ->disableOriginalConstructor()
  3413. ->getMock();
  3414. $this->userSession
  3415. ->expects($this->once())
  3416. ->method('getUser')
  3417. ->willReturn($loggedInUser);
  3418. $this->userManager
  3419. ->expects($this->once())
  3420. ->method('get')
  3421. ->with('UserToGet')
  3422. ->willReturn($targetUser);
  3423. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3424. ->disableOriginalConstructor()
  3425. ->getMock();
  3426. $subAdminManager
  3427. ->expects($this->once())
  3428. ->method('isUserAccessible')
  3429. ->with($loggedInUser, $targetUser)
  3430. ->willReturn(true);
  3431. $this->groupManager
  3432. ->expects($this->once())
  3433. ->method('getSubAdmin')
  3434. ->willReturn($subAdminManager);
  3435. $targetUser
  3436. ->expects($this->once())
  3437. ->method('getEmailAddress')
  3438. ->willReturn(null);
  3439. $this->api->resendWelcomeMessage('UserToGet');
  3440. }
  3441. public function testResendWelcomeMessageSuccess() {
  3442. $loggedInUser = $this->getMockBuilder(IUser::class)
  3443. ->disableOriginalConstructor()
  3444. ->getMock();
  3445. $targetUser = $this->getMockBuilder(IUser::class)
  3446. ->disableOriginalConstructor()
  3447. ->getMock();
  3448. $targetUser
  3449. ->method('getUID')
  3450. ->willReturn('user-id');
  3451. $this->userSession
  3452. ->expects($this->once())
  3453. ->method('getUser')
  3454. ->willReturn($loggedInUser);
  3455. $this->userManager
  3456. ->expects($this->once())
  3457. ->method('get')
  3458. ->with('UserToGet')
  3459. ->willReturn($targetUser);
  3460. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3461. ->disableOriginalConstructor()
  3462. ->getMock();
  3463. $subAdminManager
  3464. ->expects($this->once())
  3465. ->method('isUserAccessible')
  3466. ->with($loggedInUser, $targetUser)
  3467. ->willReturn(true);
  3468. $this->groupManager
  3469. ->expects($this->once())
  3470. ->method('getSubAdmin')
  3471. ->willReturn($subAdminManager);
  3472. $targetUser
  3473. ->expects($this->once())
  3474. ->method('getEmailAddress')
  3475. ->willReturn('abc@example.org');
  3476. $emailTemplate = $this->createMock(IEMailTemplate::class);
  3477. $this->newUserMailHelper
  3478. ->expects($this->once())
  3479. ->method('generateTemplate')
  3480. ->willReturn($emailTemplate);
  3481. $this->newUserMailHelper
  3482. ->expects($this->once())
  3483. ->method('sendMail')
  3484. ->with($targetUser, $emailTemplate);
  3485. $this->api->resendWelcomeMessage('UserToGet');
  3486. }
  3487. public function testResendWelcomeMessageSuccessWithFallbackLanguage() {
  3488. $loggedInUser = $this->getMockBuilder(IUser::class)
  3489. ->disableOriginalConstructor()
  3490. ->getMock();
  3491. $targetUser = $this->getMockBuilder(IUser::class)
  3492. ->disableOriginalConstructor()
  3493. ->getMock();
  3494. $targetUser
  3495. ->method('getUID')
  3496. ->willReturn('user-id');
  3497. $this->userSession
  3498. ->expects($this->once())
  3499. ->method('getUser')
  3500. ->willReturn($loggedInUser);
  3501. $this->userManager
  3502. ->expects($this->once())
  3503. ->method('get')
  3504. ->with('UserToGet')
  3505. ->willReturn($targetUser);
  3506. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3507. ->disableOriginalConstructor()
  3508. ->getMock();
  3509. $subAdminManager
  3510. ->expects($this->once())
  3511. ->method('isUserAccessible')
  3512. ->with($loggedInUser, $targetUser)
  3513. ->willReturn(true);
  3514. $this->groupManager
  3515. ->expects($this->once())
  3516. ->method('getSubAdmin')
  3517. ->willReturn($subAdminManager);
  3518. $targetUser
  3519. ->expects($this->once())
  3520. ->method('getEmailAddress')
  3521. ->willReturn('abc@example.org');
  3522. $l10n = $this->getMockBuilder(IL10N::class)
  3523. ->disableOriginalConstructor()
  3524. ->getMock();
  3525. $emailTemplate = $this->createMock(IEMailTemplate::class);
  3526. $this->newUserMailHelper
  3527. ->expects($this->once())
  3528. ->method('generateTemplate')
  3529. ->willReturn($emailTemplate);
  3530. $this->newUserMailHelper
  3531. ->expects($this->once())
  3532. ->method('sendMail')
  3533. ->with($targetUser, $emailTemplate);
  3534. $this->api->resendWelcomeMessage('UserToGet');
  3535. }
  3536. public function testResendWelcomeMessageFailed() {
  3537. $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
  3538. $this->expectExceptionMessage('Sending email failed');
  3539. $this->expectExceptionCode(102);
  3540. $loggedInUser = $this->getMockBuilder(IUser::class)
  3541. ->disableOriginalConstructor()
  3542. ->getMock();
  3543. $targetUser = $this->getMockBuilder(IUser::class)
  3544. ->disableOriginalConstructor()
  3545. ->getMock();
  3546. $targetUser
  3547. ->method('getUID')
  3548. ->willReturn('user-id');
  3549. $this->userSession
  3550. ->expects($this->once())
  3551. ->method('getUser')
  3552. ->willReturn($loggedInUser);
  3553. $this->userManager
  3554. ->expects($this->once())
  3555. ->method('get')
  3556. ->with('UserToGet')
  3557. ->willReturn($targetUser);
  3558. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3559. ->disableOriginalConstructor()
  3560. ->getMock();
  3561. $subAdminManager
  3562. ->expects($this->once())
  3563. ->method('isUserAccessible')
  3564. ->with($loggedInUser, $targetUser)
  3565. ->willReturn(true);
  3566. $this->groupManager
  3567. ->expects($this->once())
  3568. ->method('getSubAdmin')
  3569. ->willReturn($subAdminManager);
  3570. $targetUser
  3571. ->expects($this->once())
  3572. ->method('getEmailAddress')
  3573. ->willReturn('abc@example.org');
  3574. $emailTemplate = $this->createMock(IEMailTemplate::class);
  3575. $this->newUserMailHelper
  3576. ->expects($this->once())
  3577. ->method('generateTemplate')
  3578. ->willReturn($emailTemplate);
  3579. $this->newUserMailHelper
  3580. ->expects($this->once())
  3581. ->method('sendMail')
  3582. ->with($targetUser, $emailTemplate)
  3583. ->willThrowException(new \Exception());
  3584. $this->api->resendWelcomeMessage('UserToGet');
  3585. }
  3586. public function dataGetEditableFields() {
  3587. return [
  3588. [false, ISetDisplayNameBackend::class, [
  3589. IAccountManager::COLLECTION_EMAIL,
  3590. IAccountManager::PROPERTY_PHONE,
  3591. IAccountManager::PROPERTY_ADDRESS,
  3592. IAccountManager::PROPERTY_WEBSITE,
  3593. IAccountManager::PROPERTY_TWITTER,
  3594. IAccountManager::PROPERTY_FEDIVERSE,
  3595. IAccountManager::PROPERTY_ORGANISATION,
  3596. IAccountManager::PROPERTY_ROLE,
  3597. IAccountManager::PROPERTY_HEADLINE,
  3598. IAccountManager::PROPERTY_BIOGRAPHY,
  3599. IAccountManager::PROPERTY_PROFILE_ENABLED,
  3600. ]],
  3601. [true, ISetDisplayNameBackend::class, [
  3602. IAccountManager::PROPERTY_DISPLAYNAME,
  3603. IAccountManager::PROPERTY_EMAIL,
  3604. IAccountManager::COLLECTION_EMAIL,
  3605. IAccountManager::PROPERTY_PHONE,
  3606. IAccountManager::PROPERTY_ADDRESS,
  3607. IAccountManager::PROPERTY_WEBSITE,
  3608. IAccountManager::PROPERTY_TWITTER,
  3609. IAccountManager::PROPERTY_FEDIVERSE,
  3610. IAccountManager::PROPERTY_ORGANISATION,
  3611. IAccountManager::PROPERTY_ROLE,
  3612. IAccountManager::PROPERTY_HEADLINE,
  3613. IAccountManager::PROPERTY_BIOGRAPHY,
  3614. IAccountManager::PROPERTY_PROFILE_ENABLED,
  3615. ]],
  3616. [true, UserInterface::class, [
  3617. IAccountManager::PROPERTY_EMAIL,
  3618. IAccountManager::COLLECTION_EMAIL,
  3619. IAccountManager::PROPERTY_PHONE,
  3620. IAccountManager::PROPERTY_ADDRESS,
  3621. IAccountManager::PROPERTY_WEBSITE,
  3622. IAccountManager::PROPERTY_TWITTER,
  3623. IAccountManager::PROPERTY_FEDIVERSE,
  3624. IAccountManager::PROPERTY_ORGANISATION,
  3625. IAccountManager::PROPERTY_ROLE,
  3626. IAccountManager::PROPERTY_HEADLINE,
  3627. IAccountManager::PROPERTY_BIOGRAPHY,
  3628. IAccountManager::PROPERTY_PROFILE_ENABLED,
  3629. ]],
  3630. ];
  3631. }
  3632. /**
  3633. * @dataProvider dataGetEditableFields
  3634. *
  3635. * @param bool $allowedToChangeDisplayName
  3636. * @param string $userBackend
  3637. * @param array $expected
  3638. */
  3639. public function testGetEditableFields(bool $allowedToChangeDisplayName, string $userBackend, array $expected) {
  3640. $this->config
  3641. ->method('getSystemValue')
  3642. ->with(
  3643. $this->equalTo('allow_user_to_change_display_name'),
  3644. $this->anything()
  3645. )->willReturn($allowedToChangeDisplayName);
  3646. $user = $this->createMock(IUser::class);
  3647. $this->userSession->method('getUser')
  3648. ->willReturn($user);
  3649. $backend = $this->createMock($userBackend);
  3650. $user->method('getUID')
  3651. ->willReturn('userId');
  3652. $user->method('getBackend')
  3653. ->willReturn($backend);
  3654. $expectedResp = new DataResponse($expected);
  3655. $this->assertEquals($expectedResp, $this->api->getEditableFields('userId'));
  3656. }
  3657. private function mockAccount($targetUser, $accountProperties) {
  3658. $mockedProperties = [];
  3659. foreach ($accountProperties as $propertyName => $data) {
  3660. $mockedProperty = $this->createMock(IAccountProperty::class);
  3661. $mockedProperty->method('getValue')->willReturn($data['value'] ?? '');
  3662. $mockedProperty->method('getScope')->willReturn($data['scope'] ?? '');
  3663. $mockedProperties[] = [$propertyName, $mockedProperty];
  3664. }
  3665. $account = $this->createMock(IAccount::class);
  3666. $account->method('getProperty')
  3667. ->will($this->returnValueMap($mockedProperties));
  3668. $this->accountManager->expects($this->any())->method('getAccount')
  3669. ->with($targetUser)
  3670. ->willReturn($account);
  3671. }
  3672. }