UsersControllerTest.php 101 KB

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