User_LDAPTest.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Roger Szabo <roger.szabo@web.de>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  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\User_LDAP\Tests;
  32. use OC\User\Backend;
  33. use OC\User\Session;
  34. use OCA\User_LDAP\Access;
  35. use OCA\User_LDAP\Connection;
  36. use OCA\User_LDAP\Mapping\AbstractMapping;
  37. use OCA\User_LDAP\Mapping\UserMapping;
  38. use OCA\User_LDAP\User\Manager;
  39. use OCA\User_LDAP\User\OfflineUser;
  40. use OC\HintException;
  41. use OCA\User_LDAP\User\User;
  42. use OCA\User_LDAP\User_LDAP as UserLDAP;
  43. use OCA\User_LDAP\User_LDAP;
  44. use OCA\User_LDAP\UserPluginManager;
  45. use OCP\IConfig;
  46. use OCP\IUser;
  47. use Test\TestCase;
  48. use OCP\Notification\IManager as INotificationManager;
  49. /**
  50. * Class Test_User_Ldap_Direct
  51. *
  52. * @group DB
  53. *
  54. * @package OCA\User_LDAP\Tests
  55. */
  56. class User_LDAPTest extends TestCase {
  57. /** @var User_LDAP */
  58. protected $backend;
  59. /** @var Access|\PHPUnit_Framework_MockObject_MockObject */
  60. protected $access;
  61. /** @var OfflineUser|\PHPUnit_Framework_MockObject_MockObject */
  62. protected $offlineUser;
  63. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  64. protected $config;
  65. /** @var INotificationManager|\PHPUnit_Framework_MockObject_MockObject */
  66. protected $notificationManager;
  67. /** @var Session|\PHPUnit_Framework_MockObject_MockObject */
  68. protected $session;
  69. /** @var UserPluginManager|\PHPUnit_Framework_MockObject_MockObject */
  70. protected $pluginManager;
  71. /** @var Connection|\PHPUnit_Framework_MockObject_MockObject */
  72. protected $connection;
  73. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  74. protected $userManager;
  75. protected function setUp() {
  76. parent::setUp();
  77. \OC_User::clearBackends();
  78. \OC::$server->getGroupManager()->clearBackends();
  79. $this->connection = $this->createMock(Connection::class);
  80. $this->userManager = $this->createMock(Manager::class);
  81. $this->access = $this->createMock(Access::class);
  82. $this->access->connection = $this->connection;
  83. $this->access->userManager = $this->userManager;
  84. $this->config = $this->createMock(IConfig::class);
  85. $this->notificationManager = $this->createMock(INotificationManager::class);
  86. // Cannot use IUserSession because of private listen() methods
  87. $this->session = $this->createMock(Session::class);
  88. $this->pluginManager = $this->createMock(UserPluginManager::class);
  89. $this->backend = new User_LDAP(
  90. $this->access,
  91. $this->config,
  92. $this->notificationManager,
  93. $this->session,
  94. $this->pluginManager
  95. );
  96. }
  97. private function prepareMockForUserExists() {
  98. $this->access->expects($this->any())
  99. ->method('username2dn')
  100. ->will($this->returnCallback(function($uid) {
  101. switch ($uid) {
  102. case 'gunslinger':
  103. return 'dnOfRoland,dc=test';
  104. break;
  105. case 'formerUser':
  106. return 'dnOfFormerUser,dc=test';
  107. break;
  108. case 'newyorker':
  109. return 'dnOfNewYorker,dc=test';
  110. break;
  111. case 'ladyofshadows':
  112. return 'dnOfLadyOfShadows,dc=test';
  113. break;
  114. default:
  115. return false;
  116. }
  117. }));
  118. $this->access->method('fetchUsersByLoginName')
  119. ->willReturn([]);
  120. }
  121. /**
  122. * Prepares the Access mock for checkPassword tests
  123. * @param bool $noDisplayName
  124. * @return void
  125. */
  126. private function prepareAccessForCheckPassword($noDisplayName = false) {
  127. $this->connection->expects($this->any())
  128. ->method('__get')
  129. ->will($this->returnCallback(function($name) {
  130. if($name === 'ldapLoginFilter') {
  131. return '%uid';
  132. }
  133. return null;
  134. }));
  135. $this->access->expects($this->any())
  136. ->method('fetchListOfUsers')
  137. ->will($this->returnCallback(function($filter) {
  138. if($filter === 'roland') {
  139. return array(array('dn' => ['dnOfRoland,dc=test']));
  140. }
  141. return array();
  142. }));
  143. $this->access->expects($this->any())
  144. ->method('fetchUsersByLoginName')
  145. ->will($this->returnCallback(function($uid) {
  146. if($uid === 'roland') {
  147. return array(array('dn' => ['dnOfRoland,dc=test']));
  148. }
  149. return array();
  150. }));
  151. $retVal = 'gunslinger';
  152. if($noDisplayName === true) {
  153. $retVal = false;
  154. }
  155. $this->access->expects($this->any())
  156. ->method('dn2username')
  157. ->with($this->equalTo('dnOfRoland,dc=test'))
  158. ->will($this->returnValue($retVal));
  159. $this->access->expects($this->any())
  160. ->method('stringResemblesDN')
  161. ->with($this->equalTo('dnOfRoland,dc=test'))
  162. ->will($this->returnValue(true));
  163. $this->access->expects($this->any())
  164. ->method('areCredentialsValid')
  165. ->will($this->returnCallback(function($dn, $pwd) {
  166. if($pwd === 'dt19') {
  167. return true;
  168. }
  169. return false;
  170. }));
  171. }
  172. public function testCheckPasswordUidReturn() {
  173. $user = $this->createMock(User::class);
  174. $user->expects($this->any())
  175. ->method('getUsername')
  176. ->willReturn('gunslinger');
  177. $this->prepareAccessForCheckPassword();
  178. $this->userManager->expects($this->any())
  179. ->method('get')
  180. ->willReturn($user);
  181. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  182. \OC_User::useBackend($backend);
  183. $result = $backend->checkPassword('roland', 'dt19');
  184. $this->assertEquals('gunslinger', $result);
  185. }
  186. public function testCheckPasswordWrongPassword() {
  187. $this->prepareAccessForCheckPassword();
  188. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  189. \OC_User::useBackend($backend);
  190. $result = $backend->checkPassword('roland', 'wrong');
  191. $this->assertFalse($result);
  192. }
  193. public function testCheckPasswordWrongUser() {
  194. $this->prepareAccessForCheckPassword();
  195. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  196. \OC_User::useBackend($backend);
  197. $result = $backend->checkPassword('mallory', 'evil');
  198. $this->assertFalse($result);
  199. }
  200. public function testCheckPasswordNoDisplayName() {
  201. $this->prepareAccessForCheckPassword(true);
  202. $this->prepareAccessForCheckPassword();
  203. $this->userManager->expects($this->atLeastOnce())
  204. ->method('get')
  205. ->willReturn(null);
  206. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  207. \OC_User::useBackend($backend);
  208. $result = $backend->checkPassword('roland', 'dt19');
  209. $this->assertFalse($result);
  210. }
  211. public function testCheckPasswordPublicAPI() {
  212. $user = $this->createMock(User::class);
  213. $user->expects($this->any())
  214. ->method('getUsername')
  215. ->willReturn('gunslinger');
  216. $this->prepareAccessForCheckPassword();
  217. $this->userManager->expects($this->any())
  218. ->method('get')
  219. ->willReturn($user);
  220. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  221. \OC_User::useBackend($backend);
  222. $user = \OC::$server->getUserManager()->checkPassword('roland', 'dt19');
  223. $result = false;
  224. if ($user !== false) {
  225. $result = $user->getUID();
  226. }
  227. $this->assertEquals('gunslinger', $result);
  228. }
  229. public function testCheckPasswordPublicAPIWrongPassword() {
  230. $this->prepareAccessForCheckPassword();
  231. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  232. \OC_User::useBackend($backend);
  233. $user = \OC::$server->getUserManager()->checkPassword('roland', 'wrong');
  234. $result = false;
  235. if ($user !== false) {
  236. $result = $user->getUID();
  237. }
  238. $this->assertFalse($result);
  239. }
  240. public function testCheckPasswordPublicAPIWrongUser() {
  241. $this->prepareAccessForCheckPassword();
  242. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  243. \OC_User::useBackend($backend);
  244. $user = \OC::$server->getUserManager()->checkPassword('mallory', 'evil');
  245. $result = false;
  246. if ($user !== false) {
  247. $result = $user->getUID();
  248. }
  249. $this->assertFalse($result);
  250. }
  251. public function testDeleteUserCancel() {
  252. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  253. $result = $backend->deleteUser('notme');
  254. $this->assertFalse($result);
  255. }
  256. public function testDeleteUserSuccess() {
  257. $uid = 'jeremy';
  258. $home = '/var/vhome/jdings/';
  259. $mapping = $this->createMock(UserMapping::class);
  260. $mapping->expects($this->once())
  261. ->method('unmap')
  262. ->will($this->returnValue(true));
  263. $this->access->expects($this->once())
  264. ->method('getUserMapper')
  265. ->will($this->returnValue($mapping));
  266. $this->connection->expects($this->any())
  267. ->method('getConnectionResource')
  268. ->willReturn('this is an ldap link');
  269. $this->config->expects($this->any())
  270. ->method('getUserValue')
  271. ->with($uid, 'user_ldap', 'isDeleted')
  272. ->willReturn('1');
  273. $offlineUser = $this->createMock(OfflineUser::class);
  274. $offlineUser->expects($this->once())
  275. ->method('getHomePath')
  276. ->willReturn($home);
  277. $offlineUser->expects($this->once())
  278. ->method('getOCName')
  279. ->willReturn($uid);
  280. $this->userManager->expects($this->atLeastOnce())
  281. ->method('get')
  282. ->willReturn($offlineUser);
  283. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  284. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  285. $user = $this->createMock(IUser::class);
  286. $user->expects($this->once())
  287. ->method('getUID')
  288. ->willReturn($uid);
  289. $backend->preDeleteUser($user);
  290. $result = $backend->deleteUser($uid);
  291. $this->assertTrue($result);
  292. /** @noinspection PhpUnhandledExceptionInspection */
  293. $this->assertSame($backend->getHome($uid), $home);
  294. }
  295. public function testDeleteUserWithPlugin() {
  296. $this->pluginManager->expects($this->once())
  297. ->method('canDeleteUser')
  298. ->willReturn(true);
  299. $this->pluginManager->expects($this->once())
  300. ->method('deleteUser')
  301. ->with('uid')
  302. ->willReturn(true);
  303. $this->config->expects($this->once())
  304. ->method('getUserValue')
  305. ->with('uid', 'user_ldap', 'isDeleted', 0)
  306. ->willReturn(1);
  307. $mapper = $this->createMock(UserMapping::class);
  308. $mapper->expects($this->once())
  309. ->method('unmap')
  310. ->with('uid');
  311. $this->access->expects($this->atLeastOnce())
  312. ->method('getUserMapper')
  313. ->willReturn($mapper);
  314. $this->userManager->expects($this->once())
  315. ->method('invalidate')
  316. ->with('uid');
  317. $this->assertEquals(true, $this->backend->deleteUser('uid'));
  318. }
  319. /**
  320. * Prepares the Access mock for getUsers tests
  321. */
  322. private function prepareAccessForGetUsers() {
  323. $this->access->expects($this->once())
  324. ->method('escapeFilterPart')
  325. ->will($this->returnCallback(function($search) {
  326. return $search;
  327. }));
  328. $this->access->expects($this->any())
  329. ->method('getFilterPartForUserSearch')
  330. ->will($this->returnCallback(function($search) {
  331. return $search;
  332. }));
  333. $this->access->expects($this->any())
  334. ->method('combineFilterWithAnd')
  335. ->will($this->returnCallback(function($param) {
  336. return $param[2];
  337. }));
  338. $this->access->expects($this->any())
  339. ->method('fetchListOfUsers')
  340. ->will($this->returnCallback(function($search, $a, $l, $o) {
  341. $users = array('gunslinger', 'newyorker', 'ladyofshadows');
  342. if(empty($search)) {
  343. $result = $users;
  344. } else {
  345. $result = array();
  346. foreach($users as $user) {
  347. if(stripos($user, $search) !== false) {
  348. $result[] = $user;
  349. }
  350. }
  351. }
  352. if(!is_null($l) || !is_null($o)) {
  353. $result = array_slice($result, $o, $l);
  354. }
  355. return $result;
  356. }));
  357. $this->access->expects($this->any())
  358. ->method('nextcloudUserNames')
  359. ->will($this->returnArgument(0));
  360. $this->access->method('fetchUsersByLoginName')
  361. ->willReturn([]);
  362. }
  363. public function testGetUsersNoParam() {
  364. $this->prepareAccessForGetUsers();
  365. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  366. $result = $backend->getUsers();
  367. $this->assertEquals(3, count($result));
  368. }
  369. public function testGetUsersLimitOffset() {
  370. $this->prepareAccessForGetUsers();
  371. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  372. $result = $backend->getUsers('', 1, 2);
  373. $this->assertEquals(1, count($result));
  374. }
  375. public function testGetUsersLimitOffset2() {
  376. $this->prepareAccessForGetUsers();
  377. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  378. $result = $backend->getUsers('', 2, 1);
  379. $this->assertEquals(2, count($result));
  380. }
  381. public function testGetUsersSearchWithResult() {
  382. $this->prepareAccessForGetUsers();
  383. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  384. $result = $backend->getUsers('yo');
  385. $this->assertEquals(2, count($result));
  386. }
  387. public function testGetUsersSearchEmptyResult() {
  388. $this->prepareAccessForGetUsers();
  389. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  390. $result = $backend->getUsers('nix');
  391. $this->assertEquals(0, count($result));
  392. }
  393. private function getUsers($search = '', $limit = null, $offset = null) {
  394. $users = \OC::$server->getUserManager()->search($search, $limit, $offset);
  395. $uids = array_map(function(IUser $user) {
  396. return $user->getUID();
  397. }, $users);
  398. return $uids;
  399. }
  400. public function testGetUsersViaAPINoParam() {
  401. $this->prepareAccessForGetUsers();
  402. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  403. \OC_User::useBackend($backend);
  404. $result = $this->getUsers();
  405. $this->assertEquals(3, count($result));
  406. }
  407. public function testGetUsersViaAPILimitOffset() {
  408. $this->prepareAccessForGetUsers();
  409. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  410. \OC_User::useBackend($backend);
  411. $result = $this->getUsers('', 1, 2);
  412. $this->assertEquals(1, count($result));
  413. }
  414. public function testGetUsersViaAPILimitOffset2() {
  415. $this->prepareAccessForGetUsers();
  416. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  417. \OC_User::useBackend($backend);
  418. $result = $this->getUsers('', 2, 1);
  419. $this->assertEquals(2, count($result));
  420. }
  421. public function testGetUsersViaAPISearchWithResult() {
  422. $this->prepareAccessForGetUsers();
  423. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  424. \OC_User::useBackend($backend);
  425. $result = $this->getUsers('yo');
  426. $this->assertEquals(2, count($result));
  427. }
  428. public function testGetUsersViaAPISearchEmptyResult() {
  429. $this->prepareAccessForGetUsers();
  430. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  431. \OC_User::useBackend($backend);
  432. $result = $this->getUsers('nix');
  433. $this->assertEquals(0, count($result));
  434. }
  435. public function testUserExists() {
  436. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  437. $this->prepareMockForUserExists();
  438. $user = $this->createMock(User::class);
  439. $user->expects($this->any())
  440. ->method('getDN')
  441. ->willReturn('dnOfRoland,dc=test');
  442. $this->access->expects($this->any())
  443. ->method('readAttribute')
  444. ->will($this->returnCallback(function($dn) {
  445. if($dn === 'dnOfRoland,dc=test') {
  446. return array();
  447. }
  448. return false;
  449. }));
  450. $this->userManager->expects($this->atLeastOnce())
  451. ->method('get')
  452. ->willReturn($user);
  453. $this->access->expects($this->any())
  454. ->method('getUserMapper')
  455. ->willReturn($this->createMock(UserMapping::class));
  456. //test for existing user
  457. /** @noinspection PhpUnhandledExceptionInspection */
  458. $result = $backend->userExists('gunslinger');
  459. $this->assertTrue($result);
  460. }
  461. public function testUserExistsForDeleted() {
  462. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  463. $this->prepareMockForUserExists();
  464. $mapper = $this->createMock(UserMapping::class);
  465. $mapper->expects($this->any())
  466. ->method('getUUIDByDN')
  467. ->with('dnOfFormerUser,dc=test')
  468. ->willReturn('45673458748');
  469. $this->access->expects($this->any())
  470. ->method('readAttribute')
  471. ->will($this->returnCallback(function($dn) {
  472. if($dn === 'dnOfRoland,dc=test') {
  473. return array();
  474. }
  475. return false;
  476. }));
  477. $this->access->expects($this->any())
  478. ->method('getUserMapper')
  479. ->willReturn($mapper);
  480. $this->access->expects($this->once())
  481. ->method('getUserDnByUuid')
  482. ->willThrowException(new \Exception());
  483. $user = $this->createMock(User::class);
  484. $user->expects($this->any())
  485. ->method('getDN')
  486. ->willReturn('dnOfFormerUser,dc=test');
  487. $this->userManager->expects($this->atLeastOnce())
  488. ->method('get')
  489. ->willReturn($user);
  490. //test for deleted user
  491. $this->assertFalse($backend->userExists('formerUser'));
  492. }
  493. public function testUserExistsForNeverExisting() {
  494. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  495. $this->prepareMockForUserExists();
  496. $this->access->expects($this->any())
  497. ->method('readAttribute')
  498. ->will($this->returnCallback(function($dn) {
  499. if($dn === 'dnOfRoland,dc=test') {
  500. return array();
  501. }
  502. return false;
  503. }));
  504. //test for never-existing user
  505. /** @noinspection PhpUnhandledExceptionInspection */
  506. $result = $backend->userExists('mallory');
  507. $this->assertFalse($result);
  508. }
  509. public function testUserExistsPublicAPI() {
  510. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  511. $this->prepareMockForUserExists();
  512. \OC_User::useBackend($backend);
  513. $user = $this->createMock(User::class);
  514. $user->expects($this->any())
  515. ->method('getDN')
  516. ->willReturn('dnOfRoland,dc=test');
  517. $this->access->expects($this->any())
  518. ->method('readAttribute')
  519. ->will($this->returnCallback(function($dn) {
  520. if($dn === 'dnOfRoland,dc=test') {
  521. return array();
  522. }
  523. return false;
  524. }));
  525. $this->userManager->expects($this->atLeastOnce())
  526. ->method('get')
  527. ->willReturn($user);
  528. $this->access->expects($this->any())
  529. ->method('getUserMapper')
  530. ->willReturn($this->createMock(UserMapping::class));
  531. //test for existing user
  532. $result = \OC::$server->getUserManager()->userExists('gunslinger');
  533. $this->assertTrue($result);
  534. }
  535. public function testUserExistsPublicAPIForDeleted() {
  536. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  537. $this->prepareMockForUserExists();
  538. \OC_User::useBackend($backend);
  539. $mapper = $this->createMock(UserMapping::class);
  540. $mapper->expects($this->any())
  541. ->method('getUUIDByDN')
  542. ->with('dnOfFormerUser,dc=test')
  543. ->willReturn('45673458748');
  544. $this->access->expects($this->any())
  545. ->method('readAttribute')
  546. ->will($this->returnCallback(function($dn) {
  547. if($dn === 'dnOfRoland,dc=test') {
  548. return array();
  549. }
  550. return false;
  551. }));
  552. $this->access->expects($this->any())
  553. ->method('getUserMapper')
  554. ->willReturn($mapper);
  555. $this->access->expects($this->once())
  556. ->method('getUserDnByUuid')
  557. ->willThrowException(new \Exception());
  558. $user = $this->createMock(User::class);
  559. $user->expects($this->any())
  560. ->method('getDN')
  561. ->willReturn('dnOfFormerUser,dc=test');
  562. $this->userManager->expects($this->atLeastOnce())
  563. ->method('get')
  564. ->willReturn($user);
  565. //test for deleted user
  566. $this->assertFalse(\OC::$server->getUserManager()->userExists('formerUser'));
  567. }
  568. public function testUserExistsPublicAPIForNeverExisting() {
  569. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  570. $this->prepareMockForUserExists();
  571. \OC_User::useBackend($backend);
  572. $this->access->expects($this->any())
  573. ->method('readAttribute')
  574. ->will($this->returnCallback(function($dn) {
  575. if($dn === 'dnOfRoland,dc=test') {
  576. return array();
  577. }
  578. return false;
  579. }));
  580. //test for never-existing user
  581. $result = \OC::$server->getUserManager()->userExists('mallory');
  582. $this->assertFalse($result);
  583. }
  584. public function testDeleteUserExisting() {
  585. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  586. //we do not support deleting existing users at all
  587. $result = $backend->deleteUser('gunslinger');
  588. $this->assertFalse($result);
  589. }
  590. public function testGetHomeAbsolutePath() {
  591. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  592. $this->prepareMockForUserExists();
  593. $this->connection->expects($this->any())
  594. ->method('__get')
  595. ->will($this->returnCallback(function($name) {
  596. if($name === 'homeFolderNamingRule') {
  597. return 'attr:testAttribute';
  598. }
  599. return null;
  600. }));
  601. $this->access->expects($this->any())
  602. ->method('readAttribute')
  603. ->will($this->returnCallback(function($dn, $attr) {
  604. switch ($dn) {
  605. case 'dnOfRoland,dc=test':
  606. if($attr === 'testAttribute') {
  607. return array('/tmp/rolandshome/');
  608. }
  609. return array();
  610. break;
  611. default:
  612. return false;
  613. }
  614. }));
  615. $user = $this->createMock(User::class);
  616. $user->expects($this->any())
  617. ->method('getUsername')
  618. ->willReturn('gunslinger');
  619. $user->expects($this->any())
  620. ->method('getDN')
  621. ->willReturn('dnOfRoland,dc=test');
  622. $user->expects($this->any())
  623. ->method('getHomePath')
  624. ->willReturn('/tmp/rolandshome/');
  625. $this->userManager->expects($this->atLeastOnce())
  626. ->method('get')
  627. ->willReturn($user);
  628. //absolute path
  629. /** @noinspection PhpUnhandledExceptionInspection */
  630. $result = $backend->getHome('gunslinger');
  631. $this->assertEquals('/tmp/rolandshome/', $result);
  632. }
  633. public function testGetHomeRelative() {
  634. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  635. $this->prepareMockForUserExists();
  636. $dataDir = \OC::$server->getConfig()->getSystemValue(
  637. 'datadirectory', \OC::$SERVERROOT.'/data');
  638. $this->connection->expects($this->any())
  639. ->method('__get')
  640. ->will($this->returnCallback(function($name) {
  641. if($name === 'homeFolderNamingRule') {
  642. return 'attr:testAttribute';
  643. }
  644. return null;
  645. }));
  646. $this->access->expects($this->any())
  647. ->method('readAttribute')
  648. ->will($this->returnCallback(function($dn, $attr) {
  649. switch ($dn) {
  650. case 'dnOfLadyOfShadows,dc=test':
  651. if($attr === 'testAttribute') {
  652. return array('susannah/');
  653. }
  654. return array();
  655. break;
  656. default:
  657. return false;
  658. }
  659. }));
  660. $user = $this->createMock(User::class);
  661. $user->expects($this->any())
  662. ->method('getUsername')
  663. ->willReturn('ladyofshadows');
  664. $user->expects($this->any())
  665. ->method('getDN')
  666. ->willReturn('dnOfLadyOfShadows,dc=test');
  667. $user->expects($this->any())
  668. ->method('getHomePath')
  669. ->willReturn($dataDir.'/susannah/');
  670. $this->userManager->expects($this->atLeastOnce())
  671. ->method('get')
  672. ->willReturn($user);
  673. /** @noinspection PhpUnhandledExceptionInspection */
  674. $result = $backend->getHome('ladyofshadows');
  675. $this->assertEquals($dataDir.'/susannah/', $result);
  676. }
  677. /**
  678. * @expectedException \Exception
  679. */
  680. public function testGetHomeNoPath() {
  681. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  682. $this->prepareMockForUserExists();
  683. $this->connection->expects($this->any())
  684. ->method('__get')
  685. ->will($this->returnCallback(function($name) {
  686. if($name === 'homeFolderNamingRule') {
  687. return 'attr:testAttribute';
  688. }
  689. return null;
  690. }));
  691. $this->access->expects($this->any())
  692. ->method('readAttribute')
  693. ->will($this->returnCallback(function($dn, $attr) {
  694. switch ($dn) {
  695. default:
  696. return false;
  697. }
  698. }));
  699. $this->access->connection->expects($this->any())
  700. ->method('getFromCache')
  701. ->willReturnCallback(function($key) {
  702. if($key === 'userExistsnewyorker') {
  703. return true;
  704. }
  705. return null;
  706. });
  707. $user = $this->createMock(User::class);
  708. $user->expects($this->any())
  709. ->method('getUsername')
  710. ->willReturn('newyorker');
  711. $user->expects($this->any())
  712. ->method('getHomePath')
  713. ->willThrowException(new \Exception());
  714. $this->userManager->expects($this->atLeastOnce())
  715. ->method('get')
  716. ->willReturn($user);
  717. //no path at all – triggers OC default behaviour
  718. $result = $backend->getHome('newyorker');
  719. $this->assertFalse($result);
  720. }
  721. /**
  722. * @expectedException \OC\User\NoUserException
  723. */
  724. public function testGetHomeDeletedUser() {
  725. $uid = 'newyorker';
  726. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  727. $this->prepareMockForUserExists();
  728. $this->connection->expects($this->any())
  729. ->method('__get')
  730. ->will($this->returnCallback(function($name) {
  731. if($name === 'homeFolderNamingRule') {
  732. return 'attr:testAttribute';
  733. }
  734. return null;
  735. }));
  736. $this->access->expects($this->any())
  737. ->method('readAttribute')
  738. ->will($this->returnValue([]));
  739. $userMapper = $this->createMock(UserMapping::class);
  740. $this->access->expects($this->any())
  741. ->method('getUserMapper')
  742. ->will($this->returnValue($userMapper));
  743. $this->config->expects($this->any())
  744. ->method('getUserValue')
  745. ->will($this->returnValue(true));
  746. $offlineUser = $this->createMock(OfflineUser::class);
  747. $offlineUser->expects($this->never())
  748. ->method('getHomePath');
  749. $this->userManager->expects($this->atLeastOnce())
  750. ->method('get')
  751. ->willReturn($offlineUser);
  752. $backend->getHome($uid);
  753. }
  754. public function testGetHomeWithPlugin() {
  755. $this->pluginManager->expects($this->once())
  756. ->method('implementsActions')
  757. ->with(Backend::GET_HOME)
  758. ->willReturn(true);
  759. $this->pluginManager->expects($this->once())
  760. ->method('getHome')
  761. ->with('uid')
  762. ->willReturn('result');
  763. $this->connection->expects($this->any())
  764. ->method('getFromCache')
  765. ->will($this->returnCallback(function($uid) {
  766. return true;
  767. }));
  768. /** @noinspection PhpUnhandledExceptionInspection */
  769. $this->assertEquals($this->backend->getHome('uid'),'result');
  770. }
  771. private function prepareAccessForGetDisplayName() {
  772. $this->connection->expects($this->any())
  773. ->method('__get')
  774. ->will($this->returnCallback(function($name) {
  775. if($name === 'ldapUserDisplayName') {
  776. return 'displayname';
  777. }
  778. return null;
  779. }));
  780. $this->access->expects($this->any())
  781. ->method('readAttribute')
  782. ->will($this->returnCallback(function($dn, $attr) {
  783. switch ($dn) {
  784. case 'dnOfRoland,dc=test':
  785. if($attr === 'displayname') {
  786. return array('Roland Deschain');
  787. }
  788. return array();
  789. break;
  790. default:
  791. return false;
  792. }
  793. }));
  794. $this->access->method('fetchUsersByLoginName')
  795. ->willReturn([]);
  796. }
  797. public function testGetDisplayName() {
  798. $this->prepareAccessForGetDisplayName();
  799. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  800. $this->prepareMockForUserExists();
  801. $this->connection->expects($this->any())
  802. ->method('getConnectionResource')
  803. ->will($this->returnCallback(function() {
  804. return true;
  805. }));
  806. $user1 = $this->createMock(User::class);
  807. $user1->expects($this->once())
  808. ->method('composeAndStoreDisplayName')
  809. ->willReturn('Roland Deschain');
  810. $user1->expects($this->any())
  811. ->method('getDN')
  812. ->willReturn('dnOfRoland,dc=test');
  813. $user2 = $this->createMock(User::class);
  814. $user2->expects($this->never())
  815. ->method('composeAndStoreDisplayName');
  816. $user2->expects($this->any())
  817. ->method('getDN')
  818. ->willReturn('another DN');
  819. $mapper = $this->createMock(UserMapping::class);
  820. $mapper->expects($this->any())
  821. ->method('getUUIDByDN')
  822. ->willReturnCallback(function($dn) { return $dn; });
  823. $this->userManager->expects($this->any())
  824. ->method('get')
  825. ->willReturnCallback(function($uid) use ($user1, $user2) {
  826. if($uid === 'gunslinger') {
  827. return $user1;
  828. } else if($uid === 'newyorker') {
  829. return $user2;
  830. }
  831. return null;
  832. });
  833. $this->access->expects($this->any())
  834. ->method('getUserMapper')
  835. ->willReturn($mapper);
  836. $this->access->expects($this->any())
  837. ->method('getUserDnByUuid')
  838. ->willReturnCallback(function($uuid) { return $uuid . '1'; });
  839. //with displayName
  840. $result = $backend->getDisplayName('gunslinger');
  841. $this->assertEquals('Roland Deschain', $result);
  842. //empty displayname retrieved
  843. $result = $backend->getDisplayName('newyorker');
  844. $this->assertEquals(null, $result);
  845. }
  846. public function testGetDisplayNamePublicAPI() {
  847. $this->access->expects($this->any())
  848. ->method('username2dn')
  849. ->will($this->returnCallback(function($uid) {
  850. switch ($uid) {
  851. case 'gunslinger':
  852. return 'dnOfRoland,dc=test';
  853. break;
  854. case 'formerUser':
  855. return 'dnOfFormerUser,dc=test';
  856. break;
  857. case 'newyorker':
  858. return 'dnOfNewYorker,dc=test';
  859. break;
  860. case 'ladyofshadows':
  861. return 'dnOfLadyOfShadows,dc=test';
  862. break;
  863. default:
  864. return false;
  865. }
  866. }));
  867. $this->prepareAccessForGetDisplayName();
  868. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  869. $this->prepareMockForUserExists();
  870. $this->connection->expects($this->any())
  871. ->method('getConnectionResource')
  872. ->will($this->returnCallback(function() {
  873. return true;
  874. }));
  875. \OC_User::useBackend($backend);
  876. $user1 = $this->createMock(User::class);
  877. $user1->expects($this->once())
  878. ->method('composeAndStoreDisplayName')
  879. ->willReturn('Roland Deschain');
  880. $user1->expects($this->any())
  881. ->method('getDN')
  882. ->willReturn('dnOfRoland,dc=test');
  883. $user2 = $this->createMock(User::class);
  884. $user2->expects($this->never())
  885. ->method('composeAndStoreDisplayName');
  886. $user2->expects($this->any())
  887. ->method('getDN')
  888. ->willReturn('another DN');
  889. $mapper = $this->createMock(UserMapping::class);
  890. $mapper->expects($this->any())
  891. ->method('getUUIDByDN')
  892. ->willReturnCallback(function($dn) { return $dn; });
  893. $this->userManager->expects($this->any())
  894. ->method('get')
  895. ->willReturnCallback(function($uid) use ($user1, $user2) {
  896. if($uid === 'gunslinger') {
  897. return $user1;
  898. } else if($uid === 'newyorker') {
  899. return $user2;
  900. }
  901. return null;
  902. });
  903. $this->access->expects($this->any())
  904. ->method('getUserMapper')
  905. ->willReturn($mapper);
  906. $this->access->expects($this->any())
  907. ->method('getUserDnByUuid')
  908. ->willReturnCallback(function($uuid) { return $uuid . '1'; });
  909. //with displayName
  910. $result = \OC::$server->getUserManager()->get('gunslinger')->getDisplayName();
  911. $this->assertEquals('Roland Deschain', $result);
  912. //empty displayname retrieved
  913. $result = \OC::$server->getUserManager()->get('newyorker') === null ? 'newyorker' : \OC::$server->getUserManager()->get('newyorker')->getDisplayName();
  914. $this->assertEquals('newyorker', $result);
  915. }
  916. public function testGetDisplayNameWithPlugin() {
  917. $this->pluginManager->expects($this->once())
  918. ->method('implementsActions')
  919. ->with(Backend::GET_DISPLAYNAME)
  920. ->willReturn(true);
  921. $this->pluginManager->expects($this->once())
  922. ->method('getDisplayName')
  923. ->with('uid')
  924. ->willReturn('result');
  925. $this->assertEquals($this->backend->getDisplayName('uid'),'result');
  926. }
  927. //no test for getDisplayNames, because it just invokes getUsers and
  928. //getDisplayName
  929. public function testCountUsers() {
  930. $this->access->expects($this->once())
  931. ->method('countUsers')
  932. ->will($this->returnValue(5));
  933. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  934. $result = $backend->countUsers();
  935. $this->assertEquals(5, $result);
  936. }
  937. public function testCountUsersFailing() {
  938. $this->access->expects($this->once())
  939. ->method('countUsers')
  940. ->will($this->returnValue(false));
  941. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  942. $result = $backend->countUsers();
  943. $this->assertFalse($result);
  944. }
  945. public function testCountUsersWithPlugin() {
  946. $this->pluginManager->expects($this->once())
  947. ->method('implementsActions')
  948. ->with(Backend::COUNT_USERS)
  949. ->willReturn(true);
  950. $this->pluginManager->expects($this->once())
  951. ->method('countUsers')
  952. ->willReturn(42);
  953. $this->assertEquals($this->backend->countUsers(),42);
  954. }
  955. public function testLoginName2UserNameSuccess() {
  956. $loginName = 'Alice';
  957. $username = 'alice';
  958. $dn = 'uid=alice,dc=what,dc=ever';
  959. $this->access->expects($this->once())
  960. ->method('fetchUsersByLoginName')
  961. ->with($this->equalTo($loginName))
  962. ->willReturn([['dn' => [$dn]]]);
  963. $this->access->expects($this->any())
  964. ->method('stringResemblesDN')
  965. ->with($this->equalTo($dn))
  966. ->willReturn(true);
  967. $this->access->expects($this->any())
  968. ->method('dn2username')
  969. ->with($this->equalTo($dn))
  970. ->willReturn($username);
  971. $this->connection->expects($this->exactly(2))
  972. ->method('getFromCache')
  973. ->with($this->equalTo('loginName2UserName-'.$loginName))
  974. ->willReturnOnConsecutiveCalls(null, $username);
  975. $this->connection->expects($this->once())
  976. ->method('writeToCache')
  977. ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username));
  978. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  979. $user = $this->createMock(User::class);
  980. $user->expects($this->any())
  981. ->method('getUsername')
  982. ->willReturn('alice');
  983. $this->userManager->expects($this->atLeastOnce())
  984. ->method('get')
  985. ->with($dn)
  986. ->willReturn($user);
  987. $name = $backend->loginName2UserName($loginName);
  988. $this->assertSame($username, $name);
  989. // and once again to verify that caching works
  990. $backend->loginName2UserName($loginName);
  991. }
  992. public function testLoginName2UserNameNoUsersOnLDAP() {
  993. $loginName = 'Loki';
  994. $this->access->expects($this->once())
  995. ->method('fetchUsersByLoginName')
  996. ->with($this->equalTo($loginName))
  997. ->willReturn([]);
  998. $this->access->expects($this->never())
  999. ->method('stringResemblesDN');
  1000. $this->access->expects($this->never())
  1001. ->method('dn2username');
  1002. $this->connection->expects($this->exactly(2))
  1003. ->method('getFromCache')
  1004. ->with($this->equalTo('loginName2UserName-'.$loginName))
  1005. ->willReturnOnConsecutiveCalls(null, false);
  1006. $this->connection->expects($this->once())
  1007. ->method('writeToCache')
  1008. ->with($this->equalTo('loginName2UserName-'.$loginName), false);
  1009. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1010. $name = $backend->loginName2UserName($loginName);
  1011. $this->assertSame(false, $name);
  1012. // and once again to verify that caching works
  1013. $backend->loginName2UserName($loginName);
  1014. }
  1015. public function testLoginName2UserNameOfflineUser() {
  1016. $loginName = 'Alice';
  1017. $dn = 'uid=alice,dc=what,dc=ever';
  1018. $offlineUser = $this->getMockBuilder(OfflineUser::class)
  1019. ->disableOriginalConstructor()
  1020. ->getMock();
  1021. $this->access->expects($this->once())
  1022. ->method('fetchUsersByLoginName')
  1023. ->with($this->equalTo($loginName))
  1024. ->willReturn([['dn' => [$dn]]]);
  1025. $this->connection->expects($this->exactly(2))
  1026. ->method('getFromCache')
  1027. ->with($this->equalTo('loginName2UserName-'.$loginName))
  1028. ->willReturnOnConsecutiveCalls(null, false);
  1029. $this->connection->expects($this->once())
  1030. ->method('writeToCache')
  1031. ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo(false));
  1032. $this->userManager->expects($this->any())
  1033. ->method('get')
  1034. ->with($dn)
  1035. ->willReturn($offlineUser);
  1036. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1037. $name = $backend->loginName2UserName($loginName);
  1038. $this->assertSame(false, $name);
  1039. // and once again to verify that caching works
  1040. $backend->loginName2UserName($loginName);
  1041. }
  1042. /**
  1043. * Prepares the Access mock for setPassword tests
  1044. *
  1045. * @param bool $enablePasswordChange
  1046. */
  1047. private function prepareAccessForSetPassword($enablePasswordChange = true) {
  1048. $this->connection->expects($this->any())
  1049. ->method('__get')
  1050. ->will($this->returnCallback(function($name) use (&$enablePasswordChange) {
  1051. if($name === 'ldapLoginFilter') {
  1052. return '%uid';
  1053. }
  1054. if($name === 'turnOnPasswordChange') {
  1055. return $enablePasswordChange?1:0;
  1056. }
  1057. return null;
  1058. }));
  1059. $this->connection->expects($this->any())
  1060. ->method('getFromCache')
  1061. ->will($this->returnCallback(function($uid) {
  1062. if($uid === 'userExists'.'roland') {
  1063. return true;
  1064. }
  1065. return null;
  1066. }));
  1067. $this->access->expects($this->any())
  1068. ->method('fetchListOfUsers')
  1069. ->will($this->returnCallback(function($filter) {
  1070. if($filter === 'roland') {
  1071. return array(array('dn' => ['dnOfRoland,dc=test']));
  1072. }
  1073. return array();
  1074. }));
  1075. $this->access->expects($this->any())
  1076. ->method('fetchUsersByLoginName')
  1077. ->will($this->returnCallback(function($uid) {
  1078. if($uid === 'roland') {
  1079. return array(array('dn' => ['dnOfRoland,dc=test']));
  1080. }
  1081. return array();
  1082. }));
  1083. $this->access->expects($this->any())
  1084. ->method('dn2username')
  1085. ->with($this->equalTo('dnOfRoland,dc=test'))
  1086. ->will($this->returnValue('roland'));
  1087. $this->access->expects($this->any())
  1088. ->method('stringResemblesDN')
  1089. ->with($this->equalTo('dnOfRoland,dc=test'))
  1090. ->will($this->returnValue(true));
  1091. $this->access->expects($this->any())
  1092. ->method('setPassword')
  1093. ->will($this->returnCallback(function($uid, $password) {
  1094. if(strlen($password) <= 5) {
  1095. throw new HintException('Password fails quality checking policy', '', 19);
  1096. }
  1097. return true;
  1098. }));
  1099. }
  1100. /**
  1101. * @expectedException \OC\HintException
  1102. * @expectedExceptionMessage Password fails quality checking policy
  1103. */
  1104. public function testSetPasswordInvalid() {
  1105. $this->prepareAccessForSetPassword($this->access);
  1106. $this->userManager->expects($this->atLeastOnce())
  1107. ->method('get')
  1108. ->willReturn($this->createMock(User::class));
  1109. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1110. \OC_User::useBackend($backend);
  1111. $this->assertTrue(\OC_User::setPassword('roland', 'dt'));
  1112. }
  1113. public function testSetPasswordValid() {
  1114. $this->prepareAccessForSetPassword($this->access);
  1115. $this->userManager->expects($this->any())
  1116. ->method('get')
  1117. ->willReturn($this->createMock(User::class));
  1118. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1119. $this->userManager->expects($this->any())
  1120. ->method('get')
  1121. ->willReturn($this->createMock(User::class));
  1122. \OC_User::useBackend($backend);
  1123. $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$'));
  1124. }
  1125. public function testSetPasswordValidDisabled() {
  1126. $this->userManager->expects($this->any())
  1127. ->method('get')
  1128. ->willReturn($this->createMock(User::class));
  1129. $this->prepareAccessForSetPassword(false);
  1130. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1131. \OC_User::useBackend($backend);
  1132. $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$'));
  1133. }
  1134. /**
  1135. * @expectedException \Exception
  1136. * @expectedExceptionMessage LDAP setPassword: Could not get user object for uid NotExistingUser. Maybe the LDAP entry has no set display name attribute?
  1137. */
  1138. public function testSetPasswordWithInvalidUser() {
  1139. $this->userManager
  1140. ->expects($this->once())
  1141. ->method('get')
  1142. ->with('NotExistingUser')
  1143. ->willReturn(null);
  1144. $this->backend->setPassword('NotExistingUser', 'Password');
  1145. }
  1146. public function testSetPasswordWithUsernameFalse() {
  1147. $user = $this->createMock(User::class);
  1148. $user
  1149. ->expects($this->once())
  1150. ->method('getUsername')
  1151. ->willReturn(false);
  1152. $this->userManager
  1153. ->expects($this->once())
  1154. ->method('get')
  1155. ->with('NotExistingUser')
  1156. ->willReturn($user);
  1157. /** @noinspection PhpUnhandledExceptionInspection */
  1158. $this->assertFalse($this->backend->setPassword('NotExistingUser', 'Password'));
  1159. }
  1160. public function testSetPasswordWithPlugin() {
  1161. $this->pluginManager->expects($this->once())
  1162. ->method('implementsActions')
  1163. ->with(Backend::SET_PASSWORD)
  1164. ->willReturn(true);
  1165. $this->pluginManager->expects($this->once())
  1166. ->method('setPassword')
  1167. ->with('uid','password')
  1168. ->willReturn('result');
  1169. /** @noinspection PhpUnhandledExceptionInspection */
  1170. $this->assertEquals($this->backend->setPassword('uid', 'password'),'result');
  1171. }
  1172. public function avatarDataProvider() {
  1173. return [
  1174. [ 'validImageData', false ],
  1175. [ 'corruptImageData', true ],
  1176. [ false, true]
  1177. ];
  1178. }
  1179. /** @dataProvider avatarDataProvider */
  1180. public function testCanChangeAvatar($imageData, $expected) {
  1181. $isValidImage = strpos((string)$imageData, 'valid') === 0;
  1182. $user = $this->createMock(User::class);
  1183. $user->expects($this->once())
  1184. ->method('getAvatarImage')
  1185. ->willReturn($imageData);
  1186. $user->expects($this->atMost(1))
  1187. ->method('updateAvatar')
  1188. ->willReturn($isValidImage);
  1189. $this->userManager->expects($this->atLeastOnce())
  1190. ->method('get')
  1191. ->willReturn($user);
  1192. /** @noinspection PhpUnhandledExceptionInspection */
  1193. $this->assertSame($expected, $this->backend->canChangeAvatar('uid'));
  1194. }
  1195. public function testCanChangeAvatarWithPlugin() {
  1196. $this->pluginManager->expects($this->once())
  1197. ->method('implementsActions')
  1198. ->with(Backend::PROVIDE_AVATAR)
  1199. ->willReturn(true);
  1200. $this->pluginManager->expects($this->once())
  1201. ->method('canChangeAvatar')
  1202. ->with('uid')
  1203. ->willReturn('result');
  1204. $this->assertEquals($this->backend->canChangeAvatar('uid'),'result');
  1205. }
  1206. public function testSetDisplayNameWithPlugin() {
  1207. $newDisplayName = 'J. Baker';
  1208. $this->pluginManager->expects($this->once())
  1209. ->method('implementsActions')
  1210. ->with(Backend::SET_DISPLAYNAME)
  1211. ->willReturn(true);
  1212. $this->pluginManager->expects($this->once())
  1213. ->method('setDisplayName')
  1214. ->with('uid', $newDisplayName)
  1215. ->willReturn($newDisplayName);
  1216. $this->access->expects($this->once())
  1217. ->method('cacheUserDisplayName');
  1218. $this->assertEquals($newDisplayName, $this->backend->setDisplayName('uid', $newDisplayName));
  1219. }
  1220. /**
  1221. * @expectedException \OC\HintException
  1222. */
  1223. public function testSetDisplayNameErrorWithPlugin() {
  1224. $newDisplayName = 'J. Baker';
  1225. $this->pluginManager->expects($this->once())
  1226. ->method('implementsActions')
  1227. ->with(Backend::SET_DISPLAYNAME)
  1228. ->willReturn(true);
  1229. $this->pluginManager->expects($this->once())
  1230. ->method('setDisplayName')
  1231. ->with('uid', $newDisplayName)
  1232. ->willThrowException(new HintException('something happned'));
  1233. $this->access->expects($this->never())
  1234. ->method('cacheUserDisplayName');
  1235. $this->backend->setDisplayName('uid', $newDisplayName);
  1236. }
  1237. public function testSetDisplayNameFailing() {
  1238. $this->pluginManager->expects($this->once())
  1239. ->method('implementsActions')
  1240. ->with(Backend::SET_DISPLAYNAME)
  1241. ->willReturn(false);
  1242. $this->access->expects($this->never())
  1243. ->method('cacheUserDisplayName');
  1244. $this->assertFalse($this->backend->setDisplayName('uid', 'displayName'));
  1245. }
  1246. public function testCreateUserWithPlugin() {
  1247. $uid = 'alien6372';
  1248. $uuid = '123-2345-36756-123-2345234-4431';
  1249. $pwd = 'passwørd';
  1250. $this->pluginManager->expects($this->once())
  1251. ->method('implementsActions')
  1252. ->with(Backend::CREATE_USER)
  1253. ->willReturn(true);
  1254. $this->pluginManager->expects($this->once())
  1255. ->method('createUser')
  1256. ->with($uid, $pwd)
  1257. ->willReturn('result');
  1258. $this->access->expects($this->atLeastOnce())
  1259. ->method('getUUID')
  1260. ->willReturn($uuid);
  1261. $this->access->expects($this->once())
  1262. ->method('mapAndAnnounceIfApplicable')
  1263. ->with($this->isInstanceOf(AbstractMapping::class), $this->anything(), $uid, $uuid, true);
  1264. $this->access->expects($this->any())
  1265. ->method('getUserMapper')
  1266. ->willReturn($this->createMock(AbstractMapping::class));
  1267. $this->assertEquals($this->backend->createUser($uid, $pwd),true);
  1268. }
  1269. public function testCreateUserFailing() {
  1270. $this->pluginManager->expects($this->once())
  1271. ->method('implementsActions')
  1272. ->with(Backend::CREATE_USER)
  1273. ->willReturn(false);
  1274. $this->assertFalse($this->backend->createUser('uid', 'password'));
  1275. }
  1276. public function actionProvider() {
  1277. return [
  1278. [ 'ldapUserAvatarRule', 'default', Backend::PROVIDE_AVATAR, true] ,
  1279. [ 'ldapUserAvatarRule', 'data:selfiePhoto', Backend::PROVIDE_AVATAR, true],
  1280. [ 'ldapUserAvatarRule', 'none', Backend::PROVIDE_AVATAR, false],
  1281. [ 'turnOnPasswordChange', 0, Backend::SET_PASSWORD, false],
  1282. [ 'turnOnPasswordChange', 1, Backend::SET_PASSWORD, true],
  1283. ];
  1284. }
  1285. /**
  1286. * @dataProvider actionProvider
  1287. */
  1288. public function testImplementsAction($configurable, $value, $actionCode, $expected) {
  1289. $this->pluginManager->expects($this->once())
  1290. ->method('getImplementedActions')
  1291. ->willReturn(0);
  1292. $this->connection->expects($this->any())
  1293. ->method('__get')
  1294. ->willReturnMap([
  1295. [$configurable, $value],
  1296. ]);
  1297. $this->assertSame($expected, $this->backend->implementsActions($actionCode));
  1298. }
  1299. }