1
0

UsersControllerTest.php 103 KB

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