UsersControllerTest.php 124 KB

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