You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsersControllerTest.php 104KB

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