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

UsersControllerTest.php 99KB

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