UserTest.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Roger Szabo <roger.szabo@web.de>
  12. * @author sidey79 <s.butzek@gmx.de>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\User_LDAP\Tests\User;
  31. use OCA\User_LDAP\Access;
  32. use OCA\User_LDAP\Connection;
  33. use OCA\User_LDAP\FilesystemHelper;
  34. use OCA\User_LDAP\LogWrapper;
  35. use OCA\User_LDAP\User\User;
  36. use OCP\IAvatar;
  37. use OCP\IAvatarManager;
  38. use OCP\IConfig;
  39. use OCP\Image;
  40. use OCP\IUser;
  41. use OCP\IUserManager;
  42. use OCP\Notification\IManager as INotificationManager;
  43. use OCP\Notification\INotification;
  44. /**
  45. * Class UserTest
  46. *
  47. * @group DB
  48. *
  49. * @package OCA\User_LDAP\Tests\User
  50. */
  51. class UserTest extends \Test\TestCase {
  52. /** @var Access|\PHPUnit_Framework_MockObject_MockObject */
  53. protected $access;
  54. /** @var Connection|\PHPUnit_Framework_MockObject_MockObject */
  55. protected $connection;
  56. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  57. protected $config;
  58. /** @var FilesystemHelper|\PHPUnit_Framework_MockObject_MockObject */
  59. protected $filesystemhelper;
  60. /** @var INotificationManager|\PHPUnit_Framework_MockObject_MockObject */
  61. protected $notificationManager;
  62. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  63. protected $userManager;
  64. /** @var Image|\PHPUnit_Framework_MockObject_MockObject */
  65. protected $image;
  66. /** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */
  67. protected $avatarManager;
  68. /** @var LogWrapper|\PHPUnit_Framework_MockObject_MockObject */
  69. protected $log;
  70. /** @var string */
  71. protected $uid = 'alice';
  72. /** @var string */
  73. protected $dn = 'uid=alice,dc=foo,dc=bar';
  74. /** @var User */
  75. protected $user;
  76. protected function setUp(): void {
  77. parent::setUp();
  78. $this->connection = $this->createMock(Connection::class);
  79. $this->access = $this->createMock(Access::class);
  80. $this->access->connection = $this->connection;
  81. $this->access->expects($this->any())
  82. ->method('getConnection')
  83. ->willReturn($this->connection);
  84. $this->config = $this->createMock(IConfig::class);
  85. $this->filesystemhelper = $this->createMock(FilesystemHelper::class);
  86. $this->log = $this->createMock(LogWrapper::class);
  87. $this->avatarManager = $this->createMock(IAvatarManager::class);
  88. $this->image = $this->createMock(Image::class);
  89. $this->userManager = $this->createMock(IUserManager::class);
  90. $this->notificationManager = $this->createMock(INotificationManager::class);
  91. $this->user = new User(
  92. $this->uid,
  93. $this->dn,
  94. $this->access,
  95. $this->config,
  96. $this->filesystemhelper,
  97. $this->image,
  98. $this->log,
  99. $this->avatarManager,
  100. $this->userManager,
  101. $this->notificationManager
  102. );
  103. }
  104. public function testGetDNandUsername() {
  105. $this->assertSame($this->dn, $this->user->getDN());
  106. $this->assertSame($this->uid, $this->user->getUsername());
  107. }
  108. public function testUpdateEmailProvided() {
  109. $this->connection->expects($this->once())
  110. ->method('__get')
  111. ->with($this->equalTo('ldapEmailAttribute'))
  112. ->willReturn('email');
  113. $this->access->expects($this->once())
  114. ->method('readAttribute')
  115. ->with($this->equalTo($this->dn),
  116. $this->equalTo('email'))
  117. ->willReturn(['alice@foo.bar']);
  118. $coreUser = $this->getMockBuilder(IUser::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $coreUser->expects($this->once())
  122. ->method('setEMailAddress')
  123. ->with('alice@foo.bar');
  124. $this->userManager->expects($this->any())
  125. ->method('get')
  126. ->willReturn($coreUser);
  127. $this->user->updateEmail();
  128. }
  129. public function testUpdateEmailNotProvided() {
  130. $this->connection->expects($this->once())
  131. ->method('__get')
  132. ->with($this->equalTo('ldapEmailAttribute'))
  133. ->willReturn('email');
  134. $this->access->expects($this->once())
  135. ->method('readAttribute')
  136. ->with($this->equalTo($this->dn),
  137. $this->equalTo('email'))
  138. ->willReturn(false);
  139. $this->config->expects($this->never())
  140. ->method('setUserValue');
  141. $this->user->updateEmail();
  142. }
  143. public function testUpdateEmailNotConfigured() {
  144. $this->connection->expects($this->once())
  145. ->method('__get')
  146. ->with($this->equalTo('ldapEmailAttribute'))
  147. ->willReturn('');
  148. $this->access->expects($this->never())
  149. ->method('readAttribute');
  150. $this->config->expects($this->never())
  151. ->method('setUserValue');
  152. $this->user->updateEmail();
  153. }
  154. public function testUpdateQuotaAllProvided() {
  155. $this->connection->expects($this->exactly(2))
  156. ->method('__get')
  157. ->willReturnMap([
  158. ['ldapQuotaAttribute', 'myquota'],
  159. ['ldapQuotaDefault', '']
  160. ]);
  161. $this->access->expects($this->once())
  162. ->method('readAttribute')
  163. ->with($this->equalTo($this->dn),
  164. $this->equalTo('myquota'))
  165. ->willReturn(['42 GB']);
  166. $coreUser = $this->createMock(IUser::class);
  167. $coreUser->expects($this->once())
  168. ->method('setQuota')
  169. ->with('42 GB');
  170. $this->userManager->expects($this->atLeastOnce())
  171. ->method('get')
  172. ->with($this->uid)
  173. ->willReturn($coreUser);
  174. $this->user->updateQuota();
  175. }
  176. public function testUpdateQuotaToDefaultAllProvided() {
  177. $this->connection->expects($this->exactly(2))
  178. ->method('__get')
  179. ->willReturnMap([
  180. ['ldapQuotaAttribute', 'myquota'],
  181. ['ldapQuotaDefault', '']
  182. ]);
  183. $this->access->expects($this->once())
  184. ->method('readAttribute')
  185. ->with($this->equalTo($this->dn),
  186. $this->equalTo('myquota'))
  187. ->willReturn(['default']);
  188. $coreUser = $this->createMock(IUser::class);
  189. $coreUser->expects($this->once())
  190. ->method('setQuota')
  191. ->with('default');
  192. $this->userManager->expects($this->once())
  193. ->method('get')
  194. ->with($this->uid)
  195. ->willReturn($coreUser);
  196. $this->user->updateQuota();
  197. }
  198. public function testUpdateQuotaToNoneAllProvided() {
  199. $this->connection->expects($this->exactly(2))
  200. ->method('__get')
  201. ->willReturnMap([
  202. ['ldapQuotaAttribute', 'myquota'],
  203. ['ldapQuotaDefault', '']
  204. ]);
  205. $this->access->expects($this->once())
  206. ->method('readAttribute')
  207. ->with($this->equalTo($this->dn),
  208. $this->equalTo('myquota'))
  209. ->willReturn(['none']);
  210. $coreUser = $this->createMock(IUser::class);
  211. $coreUser->expects($this->once())
  212. ->method('setQuota')
  213. ->with('none');
  214. $this->userManager->expects($this->once())
  215. ->method('get')
  216. ->with($this->uid)
  217. ->willReturn($coreUser);
  218. $this->user->updateQuota();
  219. }
  220. public function testUpdateQuotaDefaultProvided() {
  221. $this->connection->expects($this->at(0))
  222. ->method('__get')
  223. ->with($this->equalTo('ldapQuotaAttribute'))
  224. ->willReturn('myquota');
  225. $this->connection->expects($this->at(1))
  226. ->method('__get')
  227. ->with($this->equalTo('ldapQuotaDefault'))
  228. ->willReturn('25 GB');
  229. $this->connection->expects($this->exactly(2))
  230. ->method('__get');
  231. $this->access->expects($this->once())
  232. ->method('readAttribute')
  233. ->with($this->equalTo($this->dn),
  234. $this->equalTo('myquota'))
  235. ->willReturn(false);
  236. $coreUser = $this->createMock(IUser::class);
  237. $coreUser->expects($this->once())
  238. ->method('setQuota')
  239. ->with('25 GB');
  240. $this->userManager->expects($this->once())
  241. ->method('get')
  242. ->with($this->uid)
  243. ->willReturn($coreUser);
  244. $this->user->updateQuota();
  245. }
  246. public function testUpdateQuotaIndividualProvided() {
  247. $this->connection->expects($this->exactly(2))
  248. ->method('__get')
  249. ->willReturnMap([
  250. ['ldapQuotaAttribute', 'myquota'],
  251. ['ldapQuotaDefault', '']
  252. ]);
  253. $this->access->expects($this->once())
  254. ->method('readAttribute')
  255. ->with($this->equalTo($this->dn),
  256. $this->equalTo('myquota'))
  257. ->willReturn(['27 GB']);
  258. $coreUser = $this->createMock(IUser::class);
  259. $coreUser->expects($this->once())
  260. ->method('setQuota')
  261. ->with('27 GB');
  262. $this->userManager->expects($this->once())
  263. ->method('get')
  264. ->with($this->uid)
  265. ->willReturn($coreUser);
  266. $this->user->updateQuota();
  267. }
  268. public function testUpdateQuotaNoneProvided() {
  269. $this->connection->expects($this->exactly(2))
  270. ->method('__get')
  271. ->willReturnMap([
  272. ['ldapQuotaAttribute', 'myquota'],
  273. ['ldapQuotaDefault', '']
  274. ]);
  275. $this->access->expects($this->once())
  276. ->method('readAttribute')
  277. ->with($this->equalTo($this->dn),
  278. $this->equalTo('myquota'))
  279. ->willReturn(false);
  280. $coreUser = $this->createMock(IUser::class);
  281. $coreUser->expects($this->never())
  282. ->method('setQuota');
  283. $this->userManager->expects($this->never())
  284. ->method('get')
  285. ->with($this->uid);
  286. $this->config->expects($this->never())
  287. ->method('setUserValue');
  288. $this->user->updateQuota();
  289. }
  290. public function testUpdateQuotaNoneConfigured() {
  291. $this->connection->expects($this->exactly(2))
  292. ->method('__get')
  293. ->willReturnMap([
  294. ['ldapQuotaAttribute', ''],
  295. ['ldapQuotaDefault', '']
  296. ]);
  297. $coreUser = $this->createMock(IUser::class);
  298. $coreUser->expects($this->never())
  299. ->method('setQuota');
  300. $this->userManager->expects($this->never())
  301. ->method('get');
  302. $this->access->expects($this->never())
  303. ->method('readAttribute');
  304. $this->config->expects($this->never())
  305. ->method('setUserValue');
  306. $this->user->updateQuota();
  307. }
  308. public function testUpdateQuotaFromValue() {
  309. $readQuota = '19 GB';
  310. $this->connection->expects($this->exactly(2))
  311. ->method('__get')
  312. ->willReturnMap([
  313. ['ldapQuotaAttribute', 'myquota'],
  314. ['ldapQuotaDefault', '']
  315. ]);
  316. $this->access->expects($this->never())
  317. ->method('readAttribute');
  318. $user = $this->createMock(IUser::class);
  319. $user->expects($this->once())
  320. ->method('setQuota')
  321. ->with($readQuota);
  322. $this->userManager->expects($this->once())
  323. ->method('get')
  324. ->with($this->uid)
  325. ->willReturn($user);
  326. $this->user->updateQuota($readQuota);
  327. }
  328. /**
  329. * Unparseable quota will fallback to use the LDAP default
  330. */
  331. public function testUpdateWrongQuotaAllProvided() {
  332. $this->connection->expects($this->exactly(2))
  333. ->method('__get')
  334. ->willReturnMap([
  335. ['ldapQuotaAttribute', 'myquota'],
  336. ['ldapQuotaDefault', '23 GB']
  337. ]);
  338. $this->access->expects($this->once())
  339. ->method('readAttribute')
  340. ->with($this->equalTo($this->dn),
  341. $this->equalTo('myquota'))
  342. ->willReturn(['42 GBwos']);
  343. $coreUser = $this->createMock(IUser::class);
  344. $coreUser->expects($this->once())
  345. ->method('setQuota')
  346. ->with('23 GB');
  347. $this->userManager->expects($this->once())
  348. ->method('get')
  349. ->with($this->uid)
  350. ->willReturn($coreUser);
  351. $this->user->updateQuota();
  352. }
  353. /**
  354. * No user quota and wrong default will set 'default' as quota
  355. */
  356. public function testUpdateWrongDefaultQuotaProvided() {
  357. $this->connection->expects($this->exactly(2))
  358. ->method('__get')
  359. ->willReturnMap([
  360. ['ldapQuotaAttribute', 'myquota'],
  361. ['ldapQuotaDefault', '23 GBwowowo']
  362. ]);
  363. $this->access->expects($this->once())
  364. ->method('readAttribute')
  365. ->with($this->equalTo($this->dn),
  366. $this->equalTo('myquota'))
  367. ->willReturn(false);
  368. $coreUser = $this->createMock(IUser::class);
  369. $coreUser->expects($this->never())
  370. ->method('setQuota');
  371. $this->userManager->expects($this->never())
  372. ->method('get');
  373. $this->user->updateQuota();
  374. }
  375. /**
  376. * Wrong user quota and wrong default will set 'default' as quota
  377. */
  378. public function testUpdateWrongQuotaAndDefaultAllProvided() {
  379. $this->connection->expects($this->exactly(2))
  380. ->method('__get')
  381. ->willReturnMap([
  382. ['ldapQuotaAttribute', 'myquota'],
  383. ['ldapQuotaDefault', '23 GBwowowo']
  384. ]);
  385. $this->access->expects($this->once())
  386. ->method('readAttribute')
  387. ->with($this->equalTo($this->dn),
  388. $this->equalTo('myquota'))
  389. ->willReturn(['23 flush']);
  390. $coreUser = $this->createMock(IUser::class);
  391. $coreUser->expects($this->never())
  392. ->method('setQuota');
  393. $this->userManager->expects($this->never())
  394. ->method('get');
  395. $this->user->updateQuota();
  396. }
  397. /**
  398. * No quota attribute set and wrong default will set 'default' as quota
  399. */
  400. public function testUpdateWrongDefaultQuotaNotProvided() {
  401. $this->connection->expects($this->exactly(2))
  402. ->method('__get')
  403. ->willReturnMap([
  404. ['ldapQuotaAttribute', ''],
  405. ['ldapQuotaDefault', '23 GBwowowo']
  406. ]);
  407. $this->access->expects($this->never())
  408. ->method('readAttribute');
  409. $coreUser = $this->createMock(IUser::class);
  410. $coreUser->expects($this->never())
  411. ->method('setQuota');
  412. $this->userManager->expects($this->never())
  413. ->method('get');
  414. $this->user->updateQuota();
  415. }
  416. //the testUpdateAvatar series also implicitely tests getAvatarImage
  417. public function XtestUpdateAvatarJpegPhotoProvided() {
  418. $this->access->expects($this->once())
  419. ->method('readAttribute')
  420. ->with($this->equalTo($this->dn),
  421. $this->equalTo('jpegphoto'))
  422. ->willReturn(['this is a photo']);
  423. $this->image->expects($this->once())
  424. ->method('loadFromBase64')
  425. ->willReturn('imageResource');
  426. $this->image->expects($this->once())
  427. ->method('valid')
  428. ->willReturn(true);
  429. $this->image->expects($this->once())
  430. ->method('width')
  431. ->willReturn(128);
  432. $this->image->expects($this->once())
  433. ->method('height')
  434. ->willReturn(128);
  435. $this->image->expects($this->once())
  436. ->method('centerCrop')
  437. ->willReturn(true);
  438. $this->image->expects($this->once())
  439. ->method('data')
  440. ->willReturn('this is a photo');
  441. $this->config->expects($this->once())
  442. ->method('getUserValue')
  443. ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
  444. ->willReturn('');
  445. $this->config->expects($this->once())
  446. ->method('setUserValue')
  447. ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo'));
  448. $this->filesystemhelper->expects($this->once())
  449. ->method('isLoaded')
  450. ->willReturn(true);
  451. $avatar = $this->createMock(IAvatar::class);
  452. $avatar->expects($this->once())
  453. ->method('set')
  454. ->with($this->isInstanceOf($this->image));
  455. $this->avatarManager->expects($this->once())
  456. ->method('getAvatar')
  457. ->with($this->equalTo($this->uid))
  458. ->willReturn($avatar);
  459. $this->connection->expects($this->any())
  460. ->method('resolveRule')
  461. ->with('avatar')
  462. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  463. $this->user->updateAvatar();
  464. }
  465. public function testUpdateAvatarKnownJpegPhotoProvided() {
  466. $this->access->expects($this->once())
  467. ->method('readAttribute')
  468. ->with($this->equalTo($this->dn),
  469. $this->equalTo('jpegphoto'))
  470. ->willReturn(['this is a photo']);
  471. $this->image->expects($this->once())
  472. ->method('loadFromBase64')
  473. ->willReturn('imageResource');
  474. $this->image->expects($this->never())
  475. ->method('valid');
  476. $this->image->expects($this->never())
  477. ->method('width');
  478. $this->image->expects($this->never())
  479. ->method('height');
  480. $this->image->expects($this->never())
  481. ->method('centerCrop');
  482. $this->image->expects($this->once())
  483. ->method('data')
  484. ->willReturn('this is a photo');
  485. $this->config->expects($this->once())
  486. ->method('getUserValue')
  487. ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
  488. ->willReturn(md5('this is a photo'));
  489. $this->config->expects($this->never())
  490. ->method('setUserValue');
  491. $this->filesystemhelper->expects($this->never())
  492. ->method('isLoaded');
  493. $avatar = $this->createMock(IAvatar::class);
  494. $avatar->expects($this->never())
  495. ->method('set');
  496. $this->avatarManager->expects($this->never())
  497. ->method('getAvatar');
  498. $this->connection->expects($this->any())
  499. ->method('resolveRule')
  500. ->with('avatar')
  501. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  502. $this->assertTrue($this->user->updateAvatar());
  503. }
  504. public function XtestUpdateAvatarThumbnailPhotoProvided() {
  505. $this->access->expects($this->any())
  506. ->method('readAttribute')
  507. ->willReturnCallback(function($dn, $attr) {
  508. if($dn === $this->dn
  509. && $attr === 'jpegphoto')
  510. {
  511. return false;
  512. } elseif($dn === $this->dn
  513. && $attr === 'thumbnailphoto')
  514. {
  515. return ['this is a photo'];
  516. }
  517. return null;
  518. });
  519. $this->image->expects($this->once())
  520. ->method('loadFromBase64')
  521. ->willReturn('imageResource');
  522. $this->image->expects($this->once())
  523. ->method('valid')
  524. ->willReturn(true);
  525. $this->image->expects($this->once())
  526. ->method('width')
  527. ->willReturn(128);
  528. $this->image->expects($this->once())
  529. ->method('height')
  530. ->willReturn(128);
  531. $this->image->expects($this->once())
  532. ->method('centerCrop')
  533. ->willReturn(true);
  534. $this->image->expects($this->once())
  535. ->method('data')
  536. ->willReturn('this is a photo');
  537. $this->config->expects($this->once())
  538. ->method('getUserValue')
  539. ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
  540. ->willReturn('');
  541. $this->config->expects($this->once())
  542. ->method('setUserValue')
  543. ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo'));
  544. $this->filesystemhelper->expects($this->once())
  545. ->method('isLoaded')
  546. ->willReturn(true);
  547. $avatar = $this->createMock(IAvatar::class);
  548. $avatar->expects($this->once())
  549. ->method('set')
  550. ->with($this->isInstanceOf($this->image));
  551. $this->avatarManager->expects($this->once())
  552. ->method('getAvatar')
  553. ->with($this->equalTo($this->uid))
  554. ->willReturn($avatar);
  555. $this->connection->expects($this->any())
  556. ->method('resolveRule')
  557. ->with('avatar')
  558. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  559. $this->user->updateAvatar();
  560. }
  561. public function testUpdateAvatarCorruptPhotoProvided() {
  562. $this->access->expects($this->any())
  563. ->method('readAttribute')
  564. ->willReturnCallback(function($dn, $attr) {
  565. if($dn === $this->dn
  566. && $attr === 'jpegphoto')
  567. {
  568. return false;
  569. } elseif($dn === $this->dn
  570. && $attr === 'thumbnailphoto')
  571. {
  572. return ['this is a photo'];
  573. }
  574. return null;
  575. });
  576. $this->image->expects($this->once())
  577. ->method('loadFromBase64')
  578. ->willReturn(false);
  579. $this->image->expects($this->never())
  580. ->method('valid');
  581. $this->image->expects($this->never())
  582. ->method('width');
  583. $this->image->expects($this->never())
  584. ->method('height');
  585. $this->image->expects($this->never())
  586. ->method('centerCrop');
  587. $this->image->expects($this->never())
  588. ->method('data');
  589. $this->config->expects($this->never())
  590. ->method('getUserValue');
  591. $this->config->expects($this->never())
  592. ->method('setUserValue');
  593. $this->filesystemhelper->expects($this->never())
  594. ->method('isLoaded');
  595. $avatar = $this->createMock(IAvatar::class);
  596. $avatar->expects($this->never())
  597. ->method('set');
  598. $this->avatarManager->expects($this->never())
  599. ->method('getAvatar');
  600. $this->connection->expects($this->any())
  601. ->method('resolveRule')
  602. ->with('avatar')
  603. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  604. $this->user->updateAvatar();
  605. }
  606. public function XtestUpdateAvatarUnsupportedThumbnailPhotoProvided() {
  607. $this->access->expects($this->any())
  608. ->method('readAttribute')
  609. ->willReturnCallback(function($dn, $attr) {
  610. if($dn === $this->dn
  611. && $attr === 'jpegphoto')
  612. {
  613. return false;
  614. } elseif($dn === $this->dn
  615. && $attr === 'thumbnailphoto')
  616. {
  617. return ['this is a photo'];
  618. }
  619. return null;
  620. });
  621. $this->image->expects($this->once())
  622. ->method('loadFromBase64')
  623. ->willReturn('imageResource');
  624. $this->image->expects($this->once())
  625. ->method('valid')
  626. ->willReturn(true);
  627. $this->image->expects($this->once())
  628. ->method('width')
  629. ->willReturn(128);
  630. $this->image->expects($this->once())
  631. ->method('height')
  632. ->willReturn(128);
  633. $this->image->expects($this->once())
  634. ->method('centerCrop')
  635. ->willReturn(true);
  636. $this->image->expects($this->once())
  637. ->method('data')
  638. ->willReturn('this is a photo');
  639. $this->config->expects($this->once())
  640. ->method('getUserValue')
  641. ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
  642. ->willReturn('');
  643. $this->config->expects($this->never())
  644. ->method('setUserValue');
  645. $this->filesystemhelper->expects($this->once())
  646. ->method('isLoaded')
  647. ->willReturn(true);
  648. $avatar = $this->createMock(IAvatar::class);
  649. $avatar->expects($this->once())
  650. ->method('set')
  651. ->with($this->isInstanceOf($this->image))
  652. ->willThrowException(new \Exception());
  653. $this->avatarManager->expects($this->once())
  654. ->method('getAvatar')
  655. ->with($this->equalTo($this->uid))
  656. ->willReturn($avatar);
  657. $this->connection->expects($this->any())
  658. ->method('resolveRule')
  659. ->with('avatar')
  660. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  661. $this->assertFalse($this->user->updateAvatar());
  662. }
  663. public function testUpdateAvatarNotProvided() {
  664. $this->access->expects($this->any())
  665. ->method('readAttribute')
  666. ->willReturnCallback(function($dn, $attr) {
  667. if($dn === $this->dn
  668. && $attr === 'jpegPhoto')
  669. {
  670. return false;
  671. } elseif($dn === $this->dn
  672. && $attr === 'thumbnailPhoto')
  673. {
  674. return false;
  675. }
  676. return null;
  677. });
  678. $this->image->expects($this->never())
  679. ->method('valid');
  680. $this->image->expects($this->never())
  681. ->method('width');
  682. $this->image->expects($this->never())
  683. ->method('height');
  684. $this->image->expects($this->never())
  685. ->method('centerCrop');
  686. $this->image->expects($this->never())
  687. ->method('data');
  688. $this->config->expects($this->never())
  689. ->method('getUserValue');
  690. $this->config->expects($this->never())
  691. ->method('setUserValue');
  692. $this->filesystemhelper->expects($this->never())
  693. ->method('isLoaded');
  694. $this->avatarManager->expects($this->never())
  695. ->method('getAvatar');
  696. $this->connection->expects($this->any())
  697. ->method('resolveRule')
  698. ->with('avatar')
  699. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  700. $this->user->updateAvatar();
  701. }
  702. public function testUpdateBeforeFirstLogin() {
  703. $this->config->expects($this->at(0))
  704. ->method('getUserValue')
  705. ->with($this->equalTo($this->uid), $this->equalTo('user_ldap'),
  706. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  707. $this->equalTo(0))
  708. ->willReturn(0);
  709. $this->config->expects($this->at(1))
  710. ->method('getUserValue')
  711. ->with($this->equalTo($this->uid), $this->equalTo('user_ldap'),
  712. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  713. $this->equalTo(0))
  714. ->willReturn(0);
  715. $this->config->expects($this->exactly(2))
  716. ->method('getUserValue');
  717. $this->config->expects($this->never())
  718. ->method('setUserValue');
  719. $this->user->update();
  720. }
  721. public function testUpdateAfterFirstLogin() {
  722. $this->config->expects($this->at(0))
  723. ->method('getUserValue')
  724. ->with($this->equalTo($this->uid), $this->equalTo('user_ldap'),
  725. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  726. $this->equalTo(0))
  727. ->willReturn(1);
  728. $this->config->expects($this->at(1))
  729. ->method('getUserValue')
  730. ->with($this->equalTo($this->uid), $this->equalTo('user_ldap'),
  731. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  732. $this->equalTo(0))
  733. ->willReturn(0);
  734. $this->config->expects($this->exactly(2))
  735. ->method('getUserValue');
  736. $this->config->expects($this->once())
  737. ->method('setUserValue')
  738. ->with($this->equalTo($this->uid), $this->equalTo('user_ldap'),
  739. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  740. $this->anything())
  741. ->willReturn(true);
  742. $this->connection->expects($this->any())
  743. ->method('resolveRule')
  744. ->with('avatar')
  745. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  746. $this->user->update();
  747. }
  748. public function extStorageHomeDataProvider() {
  749. return [
  750. [ 'myFolder', null ],
  751. [ '', null, false ],
  752. [ 'myFolder', 'myFolder' ],
  753. ];
  754. }
  755. /**
  756. * @dataProvider extStorageHomeDataProvider
  757. */
  758. public function testUpdateExtStorageHome(string $expected, string $valueFromLDAP = null, bool $isSet = true) {
  759. if($valueFromLDAP === null) {
  760. $this->connection->expects($this->once())
  761. ->method('__get')
  762. ->willReturnMap([
  763. ['ldapExtStorageHomeAttribute', 'homeDirectory'],
  764. ]);
  765. $return = [];
  766. if($isSet) {
  767. $return[] = $expected;
  768. }
  769. $this->access->expects($this->once())
  770. ->method('readAttribute')
  771. ->with($this->dn, 'homeDirectory')
  772. ->willReturn($return);
  773. }
  774. if($expected !== '') {
  775. $this->config->expects($this->once())
  776. ->method('setUserValue')
  777. ->with($this->uid, 'user_ldap', 'extStorageHome', $expected);
  778. } else {
  779. $this->config->expects($this->once())
  780. ->method('deleteUserValue')
  781. ->with($this->uid, 'user_ldap', 'extStorageHome');
  782. }
  783. $actual = $this->user->updateExtStorageHome($valueFromLDAP);
  784. $this->assertSame($expected, $actual);
  785. }
  786. public function testUpdateNoRefresh() {
  787. $this->config->expects($this->at(0))
  788. ->method('getUserValue')
  789. ->with($this->equalTo($this->uid), $this->equalTo('user_ldap'),
  790. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  791. $this->equalTo(0))
  792. ->willReturn(1);
  793. $this->config->expects($this->at(1))
  794. ->method('getUserValue')
  795. ->with($this->equalTo($this->uid), $this->equalTo('user_ldap'),
  796. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  797. $this->equalTo(0))
  798. ->willReturn(time() - 10);
  799. $this->config->expects($this->once())
  800. ->method('getAppValue')
  801. ->with($this->equalTo('user_ldap'),
  802. $this->equalTo('updateAttributesInterval'),
  803. $this->anything())
  804. ->willReturn(1800);
  805. $this->config->expects($this->exactly(2))
  806. ->method('getUserValue');
  807. $this->config->expects($this->never())
  808. ->method('setUserValue');
  809. $this->user->update();
  810. }
  811. public function testMarkLogin() {
  812. $this->config->expects($this->once())
  813. ->method('setUserValue')
  814. ->with($this->equalTo($this->uid),
  815. $this->equalTo('user_ldap'),
  816. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  817. $this->equalTo(1))
  818. ->willReturn(true);
  819. $this->user->markLogin();
  820. }
  821. public function testGetAvatarImageProvided() {
  822. $this->access->expects($this->once())
  823. ->method('readAttribute')
  824. ->with($this->equalTo($this->dn),
  825. $this->equalTo('jpegphoto'))
  826. ->willReturn(['this is a photo']);
  827. $this->connection->expects($this->any())
  828. ->method('resolveRule')
  829. ->with('avatar')
  830. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  831. $photo = $this->user->getAvatarImage();
  832. $this->assertSame('this is a photo', $photo);
  833. //make sure readAttribute is not called again but the already fetched
  834. //photo is returned
  835. $this->user->getAvatarImage();
  836. }
  837. public function testGetAvatarImageDisabled() {
  838. $this->access->expects($this->never())
  839. ->method('readAttribute')
  840. ->with($this->equalTo($this->dn), $this->anything());
  841. $this->connection->expects($this->any())
  842. ->method('resolveRule')
  843. ->with('avatar')
  844. ->willReturn([]);
  845. $this->assertFalse($this->user->getAvatarImage());
  846. }
  847. public function imageDataProvider() {
  848. return [
  849. [ false, false ],
  850. [ 'corruptData', false ],
  851. [ 'validData', true ],
  852. ];
  853. }
  854. public function testProcessAttributes() {
  855. $requiredMethods = [
  856. 'markRefreshTime',
  857. 'updateQuota',
  858. 'updateEmail',
  859. 'composeAndStoreDisplayName',
  860. 'storeLDAPUserName',
  861. 'getHomePath',
  862. 'updateAvatar',
  863. 'updateExtStorageHome',
  864. ];
  865. /** @var User|\PHPUnit_Framework_MockObject_MockObject $userMock */
  866. $userMock = $this->getMockBuilder(User::class)
  867. ->setConstructorArgs([
  868. $this->uid,
  869. $this->dn,
  870. $this->access,
  871. $this->config,
  872. $this->filesystemhelper,
  873. $this->image,
  874. $this->log,
  875. $this->avatarManager,
  876. $this->userManager,
  877. $this->notificationManager
  878. ])
  879. ->setMethods($requiredMethods)
  880. ->getMock();
  881. $this->connection->setConfiguration([
  882. 'homeFolderNamingRule' => 'homeDirectory'
  883. ]);
  884. $this->connection->expects($this->any())
  885. ->method('__get')
  886. ->willReturnCallback(function($name) {
  887. if($name === 'homeFolderNamingRule') {
  888. return 'attr:homeDirectory';
  889. }
  890. return $name;
  891. });
  892. $this->connection->expects($this->any())
  893. ->method('resolveRule')
  894. ->with('avatar')
  895. ->willReturn(['jpegphoto', 'thumbnailphoto']);
  896. $record = [
  897. strtolower($this->connection->ldapQuotaAttribute) => ['4096'],
  898. strtolower($this->connection->ldapEmailAttribute) => ['alice@wonderland.org'],
  899. strtolower($this->connection->ldapUserDisplayName) => ['Aaaaalice'],
  900. strtolower($this->connection->ldapExtStorageHomeAttribute) => ['homeDirectory'],
  901. 'uid' => [$this->uid],
  902. 'homedirectory' => ['Alice\'s Folder'],
  903. 'memberof' => ['cn=groupOne', 'cn=groupTwo'],
  904. 'jpegphoto' => ['here be an image']
  905. ];
  906. foreach($requiredMethods as $method) {
  907. $userMock->expects($this->once())
  908. ->method($method);
  909. }
  910. \OC_Hook::clear();//disconnect irrelevant hooks
  911. $userMock->processAttributes($record);
  912. /** @noinspection PhpUnhandledExceptionInspection */
  913. \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]);
  914. }
  915. public function emptyHomeFolderAttributeValueProvider() {
  916. return [
  917. 'empty' => [''],
  918. 'prefixOnly' => ['attr:'],
  919. ];
  920. }
  921. /**
  922. * @dataProvider emptyHomeFolderAttributeValueProvider
  923. */
  924. public function testGetHomePathNotConfigured($attributeValue) {
  925. $this->connection->expects($this->any())
  926. ->method('__get')
  927. ->with($this->equalTo('homeFolderNamingRule'))
  928. ->willReturn($attributeValue);
  929. $this->access->expects($this->never())
  930. ->method('readAttribute');
  931. $this->config->expects($this->never())
  932. ->method('getAppValue');
  933. /** @noinspection PhpUnhandledExceptionInspection */
  934. $this->assertFalse($this->user->getHomePath());
  935. }
  936. public function testGetHomePathConfiguredNotAvailableAllowed() {
  937. $this->connection->expects($this->any())
  938. ->method('__get')
  939. ->with($this->equalTo('homeFolderNamingRule'))
  940. ->willReturn('attr:foobar');
  941. $this->access->expects($this->once())
  942. ->method('readAttribute')
  943. ->willReturn(false);
  944. // asks for "enforce_home_folder_naming_rule"
  945. $this->config->expects($this->once())
  946. ->method('getAppValue')
  947. ->willReturn(false);
  948. /** @noinspection PhpUnhandledExceptionInspection */
  949. $this->assertFalse($this->user->getHomePath());
  950. }
  951. public function testGetHomePathConfiguredNotAvailableNotAllowed() {
  952. $this->expectException(\Exception::class);
  953. $this->connection->expects($this->any())
  954. ->method('__get')
  955. ->with($this->equalTo('homeFolderNamingRule'))
  956. ->willReturn('attr:foobar');
  957. $this->access->expects($this->once())
  958. ->method('readAttribute')
  959. ->willReturn(false);
  960. // asks for "enforce_home_folder_naming_rule"
  961. $this->config->expects($this->once())
  962. ->method('getAppValue')
  963. ->willReturn(true);
  964. $this->user->getHomePath();
  965. }
  966. public function displayNameProvider() {
  967. return [
  968. ['Roland Deschain', '', 'Roland Deschain', false],
  969. ['Roland Deschain', '', 'Roland Deschain', true],
  970. ['Roland Deschain', null, 'Roland Deschain', false],
  971. ['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', false],
  972. ['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', true],
  973. ];
  974. }
  975. /**
  976. * @dataProvider displayNameProvider
  977. */
  978. public function testComposeAndStoreDisplayName($part1, $part2, $expected, $expectTriggerChange) {
  979. $this->config->expects($this->once())
  980. ->method('setUserValue');
  981. $oldName = $expectTriggerChange ? 'xxGunslingerxx' : null;
  982. $this->config->expects($this->once())
  983. ->method('getUserValue')
  984. ->with($this->user->getUsername(), 'user_ldap', 'displayName', null)
  985. ->willReturn($oldName);
  986. $ncUserObj = $this->createMock(\OC\User\User::class);
  987. if ($expectTriggerChange) {
  988. $ncUserObj->expects($this->once())
  989. ->method('triggerChange')
  990. ->with('displayName', $expected);
  991. } else {
  992. $ncUserObj->expects($this->never())
  993. ->method('triggerChange');
  994. }
  995. $this->userManager->expects($this->once())
  996. ->method('get')
  997. ->willReturn($ncUserObj);
  998. $displayName = $this->user->composeAndStoreDisplayName($part1, $part2);
  999. $this->assertSame($expected, $displayName);
  1000. }
  1001. public function testComposeAndStoreDisplayNameNoOverwrite() {
  1002. $displayName = 'Randall Flagg';
  1003. $this->config->expects($this->never())
  1004. ->method('setUserValue');
  1005. $this->config->expects($this->once())
  1006. ->method('getUserValue')
  1007. ->willReturn($displayName);
  1008. $this->userManager->expects($this->never())
  1009. ->method('get'); // Implicit: no triggerChange can be called
  1010. $composedDisplayName = $this->user->composeAndStoreDisplayName($displayName);
  1011. $this->assertSame($composedDisplayName, $displayName);
  1012. }
  1013. public function testHandlePasswordExpiryWarningDefaultPolicy() {
  1014. $this->connection->expects($this->any())
  1015. ->method('__get')
  1016. ->willReturnCallback(function($name) {
  1017. if($name === 'ldapDefaultPPolicyDN') {
  1018. return 'cn=default,ou=policies,dc=foo,dc=bar';
  1019. }
  1020. if($name === 'turnOnPasswordChange') {
  1021. return '1';
  1022. }
  1023. return $name;
  1024. });
  1025. $this->access->expects($this->any())
  1026. ->method('search')
  1027. ->willReturnCallback(function($filter, $base) {
  1028. if($base === [$this->dn]) {
  1029. return [
  1030. [
  1031. 'pwdchangedtime' => [(new \DateTime())->sub(new \DateInterval('P28D'))->format('Ymdhis').'Z'],
  1032. 'pwdgraceusetime' => [],
  1033. ],
  1034. ];
  1035. }
  1036. if($base === ['cn=default,ou=policies,dc=foo,dc=bar']) {
  1037. return [
  1038. [
  1039. 'pwdmaxage' => ['2592000'],
  1040. 'pwdexpirewarning' => ['2591999'],
  1041. ],
  1042. ];
  1043. }
  1044. return [];
  1045. });
  1046. $notification = $this->getMockBuilder(INotification::class)
  1047. ->disableOriginalConstructor()
  1048. ->getMock();
  1049. $notification->expects($this->any())
  1050. ->method('setApp')
  1051. ->willReturn($notification);
  1052. $notification->expects($this->any())
  1053. ->method('setUser')
  1054. ->willReturn($notification);
  1055. $notification->expects($this->any())
  1056. ->method('setObject')
  1057. ->willReturn($notification);
  1058. $notification->expects($this->any())
  1059. ->method('setDateTime')
  1060. ->willReturn($notification);
  1061. $this->notificationManager->expects($this->exactly(2))
  1062. ->method('createNotification')
  1063. ->willReturn($notification);
  1064. $this->notificationManager->expects($this->exactly(1))
  1065. ->method('notify');
  1066. \OC_Hook::clear();//disconnect irrelevant hooks
  1067. \OCP\Util::connectHook('OC_User', 'post_login', $this->user, 'handlePasswordExpiry');
  1068. /** @noinspection PhpUnhandledExceptionInspection */
  1069. \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]);
  1070. }
  1071. public function testHandlePasswordExpiryWarningCustomPolicy() {
  1072. $this->connection->expects($this->any())
  1073. ->method('__get')
  1074. ->willReturnCallback(function($name) {
  1075. if($name === 'ldapDefaultPPolicyDN') {
  1076. return 'cn=default,ou=policies,dc=foo,dc=bar';
  1077. }
  1078. if($name === 'turnOnPasswordChange') {
  1079. return '1';
  1080. }
  1081. return $name;
  1082. });
  1083. $this->access->expects($this->any())
  1084. ->method('search')
  1085. ->willReturnCallback(function($filter, $base) {
  1086. if($base === [$this->dn]) {
  1087. return [
  1088. [
  1089. 'pwdpolicysubentry' => ['cn=custom,ou=policies,dc=foo,dc=bar'],
  1090. 'pwdchangedtime' => [(new \DateTime())->sub(new \DateInterval('P28D'))->format('Ymdhis').'Z'],
  1091. 'pwdgraceusetime' => [],
  1092. ]
  1093. ];
  1094. }
  1095. if($base === ['cn=custom,ou=policies,dc=foo,dc=bar']) {
  1096. return [
  1097. [
  1098. 'pwdmaxage' => ['2592000'],
  1099. 'pwdexpirewarning' => ['2591999'],
  1100. ]
  1101. ];
  1102. }
  1103. return [];
  1104. });
  1105. $notification = $this->getMockBuilder(INotification::class)
  1106. ->disableOriginalConstructor()
  1107. ->getMock();
  1108. $notification->expects($this->any())
  1109. ->method('setApp')
  1110. ->willReturn($notification);
  1111. $notification->expects($this->any())
  1112. ->method('setUser')
  1113. ->willReturn($notification);
  1114. $notification->expects($this->any())
  1115. ->method('setObject')
  1116. ->willReturn($notification);
  1117. $notification->expects($this->any())
  1118. ->method('setDateTime')
  1119. ->willReturn($notification);
  1120. $this->notificationManager->expects($this->exactly(2))
  1121. ->method('createNotification')
  1122. ->willReturn($notification);
  1123. $this->notificationManager->expects($this->exactly(1))
  1124. ->method('notify');
  1125. \OC_Hook::clear();//disconnect irrelevant hooks
  1126. \OCP\Util::connectHook('OC_User', 'post_login', $this->user, 'handlePasswordExpiry');
  1127. /** @noinspection PhpUnhandledExceptionInspection */
  1128. \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]);
  1129. }
  1130. }