1
0

User_LDAPTest.php 44 KB

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