LDAPProviderTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Tests;
  7. use OC\User\Manager;
  8. use OCA\User_LDAP\Access;
  9. use OCA\User_LDAP\Connection;
  10. use OCA\User_LDAP\Group_LDAP;
  11. use OCA\User_LDAP\IGroupLDAP;
  12. use OCA\User_LDAP\IUserLDAP;
  13. use OCA\User_LDAP\User_LDAP;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\ICacheFactory;
  16. use OCP\IConfig;
  17. use OCP\IServerContainer;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * Class LDAPProviderTest
  21. *
  22. * @group DB
  23. *
  24. * @package OCA\User_LDAP\Tests
  25. */
  26. class LDAPProviderTest extends \Test\TestCase {
  27. protected function setUp(): void {
  28. parent::setUp();
  29. }
  30. private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend) {
  31. $server = $this->getMockBuilder('OC\Server')
  32. ->setMethods(['getUserManager', 'getBackends', 'getGroupManager'])
  33. ->setConstructorArgs(['', new \OC\Config(\OC::$configDir)])
  34. ->getMock();
  35. $server->expects($this->any())
  36. ->method('getUserManager')
  37. ->willReturn($this->getUserManagerMock($userBackend));
  38. $server->expects($this->any())
  39. ->method('getGroupManager')
  40. ->willReturn($this->getGroupManagerMock($groupBackend));
  41. $server->expects($this->any())
  42. ->method($this->anything())
  43. ->willReturnSelf();
  44. return $server;
  45. }
  46. private function getUserManagerMock(IUserLDAP $userBackend) {
  47. $userManager = $this->getMockBuilder(Manager::class)
  48. ->setMethods(['getBackends'])
  49. ->setConstructorArgs([
  50. $this->createMock(IConfig::class),
  51. $this->createMock(ICacheFactory::class),
  52. $this->createMock(IEventDispatcher::class),
  53. $this->createMock(LoggerInterface::class),
  54. ])
  55. ->getMock();
  56. $userManager->expects($this->any())
  57. ->method('getBackends')
  58. ->willReturn([$userBackend]);
  59. return $userManager;
  60. }
  61. private function getGroupManagerMock(IGroupLDAP $groupBackend) {
  62. $groupManager = $this->getMockBuilder('OC\Group\Manager')
  63. ->setMethods(['getBackends'])
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $groupManager->expects($this->any())
  67. ->method('getBackends')
  68. ->willReturn([$groupBackend]);
  69. return $groupManager;
  70. }
  71. private function getDefaultGroupBackendMock() {
  72. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. return $groupBackend;
  76. }
  77. private function getLDAPProvider(IServerContainer $serverContainer) {
  78. $factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer);
  79. return $factory->getLDAPProvider();
  80. }
  81. public function testGetUserDNUserIDNotFound() {
  82. $this->expectException(\Exception::class);
  83. $this->expectExceptionMessage('User id not found in LDAP');
  84. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  85. ->setMethods(['userExists'])
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  89. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  90. $ldapProvider = $this->getLDAPProvider($server);
  91. $ldapProvider->getUserDN('nonexisting_user');
  92. }
  93. public function testGetUserDN() {
  94. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  95. ->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $userBackend->expects($this->once())
  99. ->method('userExists')
  100. ->willReturn(true);
  101. $userBackend->expects($this->once())
  102. ->method('username2dn')
  103. ->willReturn('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org');
  104. $userBackend->expects($this->any())
  105. ->method($this->anything())
  106. ->willReturnSelf();
  107. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  108. $ldapProvider = $this->getLDAPProvider($server);
  109. $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
  110. $ldapProvider->getUserDN('existing_user'));
  111. }
  112. public function testGetGroupDNGroupIDNotFound() {
  113. $this->expectException(\Exception::class);
  114. $this->expectExceptionMessage('Group id not found in LDAP');
  115. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  119. ->setMethods(['groupExists'])
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  123. $server = $this->getServerMock($userBackend, $groupBackend);
  124. $ldapProvider = $this->getLDAPProvider($server);
  125. $ldapProvider->getGroupDN('nonexisting_group');
  126. }
  127. public function testGetGroupDN() {
  128. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  129. ->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  133. ->setMethods(['groupExists', 'getLDAPAccess', 'groupname2dn'])
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $groupBackend->expects($this->once())
  137. ->method('groupExists')
  138. ->willReturn(true);
  139. $groupBackend->expects($this->once())
  140. ->method('groupname2dn')
  141. ->willReturn('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org');
  142. $groupBackend->expects($this->any())
  143. ->method($this->anything())
  144. ->willReturnSelf();
  145. $server = $this->getServerMock($userBackend, $groupBackend);
  146. $ldapProvider = $this->getLDAPProvider($server);
  147. $this->assertEquals('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
  148. $ldapProvider->getGroupDN('existing_group'));
  149. }
  150. public function testGetUserName() {
  151. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  152. ->setMethods(['dn2UserName'])
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $userBackend->expects($this->any())
  156. ->method('dn2UserName')
  157. ->willReturn('existing_user');
  158. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  159. $ldapProvider = $this->getLDAPProvider($server);
  160. $this->assertEquals('existing_user',
  161. $ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  162. }
  163. public function testDNasBaseParameter() {
  164. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  165. ->setMethods([])
  166. ->disableOriginalConstructor()
  167. ->getMock();
  168. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  169. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  170. $ldapProvider = $this->getLDAPProvider($server);
  171. $this->assertEquals(
  172. $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  173. $ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  174. }
  175. public function testSanitizeDN() {
  176. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  177. ->setMethods([])
  178. ->disableOriginalConstructor()
  179. ->getMock();
  180. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  181. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  182. $ldapProvider = $this->getLDAPProvider($server);
  183. $this->assertEquals(
  184. $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  185. $ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  186. }
  187. public function testGetLDAPConnectionUserIDNotFound() {
  188. $this->expectException(\Exception::class);
  189. $this->expectExceptionMessage('User id not found in LDAP');
  190. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  191. ->setMethods(['userExists'])
  192. ->disableOriginalConstructor()
  193. ->getMock();
  194. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  195. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  196. $ldapProvider = $this->getLDAPProvider($server);
  197. $ldapProvider->getLDAPConnection('nonexisting_user');
  198. }
  199. public function testGetLDAPConnection() {
  200. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  201. ->setMethods(['userExists', 'getNewLDAPConnection'])
  202. ->disableOriginalConstructor()
  203. ->getMock();
  204. $userBackend->expects($this->any())
  205. ->method('userExists')
  206. ->willReturn(true);
  207. $ldapConnection = ldap_connect('ldap://example.com');
  208. $userBackend->expects($this->any())
  209. ->method('getNewLDAPConnection')
  210. ->willReturn($ldapConnection);
  211. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  212. $ldapProvider = $this->getLDAPProvider($server);
  213. $this->assertEquals($ldapConnection, $ldapProvider->getLDAPConnection('existing_user'));
  214. }
  215. public function testGetGroupLDAPConnectionGroupIDNotFound() {
  216. $this->expectException(\Exception::class);
  217. $this->expectExceptionMessage('Group id not found in LDAP');
  218. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  219. ->disableOriginalConstructor()
  220. ->getMock();
  221. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  222. ->setMethods(['groupExists'])
  223. ->disableOriginalConstructor()
  224. ->getMock();
  225. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  226. $server = $this->getServerMock($userBackend, $groupBackend);
  227. $ldapProvider = $this->getLDAPProvider($server);
  228. $ldapProvider->getGroupLDAPConnection('nonexisting_group');
  229. }
  230. public function testGetGroupLDAPConnection() {
  231. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  232. ->disableOriginalConstructor()
  233. ->getMock();
  234. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  235. ->setMethods(['groupExists','getNewLDAPConnection'])
  236. ->disableOriginalConstructor()
  237. ->getMock();
  238. $groupBackend->expects($this->any())
  239. ->method('groupExists')
  240. ->willReturn(true);
  241. $ldapConnection = ldap_connect('ldap://example.com');
  242. $groupBackend->expects($this->any())
  243. ->method('getNewLDAPConnection')
  244. ->willReturn($ldapConnection);
  245. $server = $this->getServerMock($userBackend, $groupBackend);
  246. $ldapProvider = $this->getLDAPProvider($server);
  247. $this->assertEquals($ldapConnection, $ldapProvider->getGroupLDAPConnection('existing_group'));
  248. }
  249. public function testGetLDAPBaseUsersUserIDNotFound() {
  250. $this->expectException(\Exception::class);
  251. $this->expectExceptionMessage('User id not found in LDAP');
  252. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  253. ->setMethods(['userExists'])
  254. ->disableOriginalConstructor()
  255. ->getMock();
  256. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  257. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  258. $ldapProvider = $this->getLDAPProvider($server);
  259. $ldapProvider->getLDAPBaseUsers('nonexisting_user');
  260. }
  261. public function testGetLDAPBaseUsers() {
  262. $bases = [
  263. 'ou=users,ou=foobar,dc=example,dc=org',
  264. 'ou=users,ou=barfoo,dc=example,dc=org',
  265. ];
  266. $dn = 'uid=malik,' . $bases[1];
  267. $connection = $this->createMock(Connection::class);
  268. $connection->expects($this->any())
  269. ->method('__get')
  270. ->willReturnCallback(function ($key) use ($bases) {
  271. switch ($key) {
  272. case 'ldapBaseUsers':
  273. return $bases;
  274. }
  275. return null;
  276. });
  277. $access = $this->createMock(Access::class);
  278. $access->expects($this->any())
  279. ->method('getConnection')
  280. ->willReturn($connection);
  281. $access->expects($this->exactly(2))
  282. ->method('isDNPartOfBase')
  283. ->willReturnOnConsecutiveCalls(false, true);
  284. $access->expects($this->atLeastOnce())
  285. ->method('username2dn')
  286. ->willReturn($dn);
  287. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  288. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  289. ->disableOriginalConstructor()
  290. ->getMock();
  291. $userBackend->expects($this->atLeastOnce())
  292. ->method('userExists')
  293. ->willReturn(true);
  294. $userBackend->expects($this->any())
  295. ->method('getLDAPAccess')
  296. ->willReturn($access);
  297. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  298. $ldapProvider = $this->getLDAPProvider($server);
  299. $this->assertEquals($bases[1], $ldapProvider->getLDAPBaseUsers('existing_user'));
  300. }
  301. public function testGetLDAPBaseGroupsUserIDNotFound() {
  302. $this->expectException(\Exception::class);
  303. $this->expectExceptionMessage('User id not found in LDAP');
  304. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  305. ->setMethods(['userExists'])
  306. ->disableOriginalConstructor()
  307. ->getMock();
  308. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  309. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  310. $ldapProvider = $this->getLDAPProvider($server);
  311. $ldapProvider->getLDAPBaseGroups('nonexisting_user');
  312. }
  313. public function testGetLDAPBaseGroups() {
  314. $bases = [
  315. 'ou=groupd,ou=foobar,dc=example,dc=org',
  316. 'ou=groups,ou=barfoo,dc=example,dc=org',
  317. ];
  318. $connection = $this->createMock(Connection::class);
  319. $connection->expects($this->any())
  320. ->method('__get')
  321. ->willReturnCallback(function ($key) use ($bases) {
  322. switch ($key) {
  323. case 'ldapBaseGroups':
  324. return $bases;
  325. }
  326. return null;
  327. });
  328. $access = $this->createMock(Access::class);
  329. $access->expects($this->any())
  330. ->method('getConnection')
  331. ->willReturn($connection);
  332. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  333. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  334. ->disableOriginalConstructor()
  335. ->getMock();
  336. $userBackend->expects($this->any())
  337. ->method('userExists')
  338. ->willReturn(true);
  339. $userBackend->expects($this->any())
  340. ->method('getLDAPAccess')
  341. ->willReturn($access);
  342. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  343. $ldapProvider = $this->getLDAPProvider($server);
  344. $this->assertEquals($bases[0], $ldapProvider->getLDAPBaseGroups('existing_user'));
  345. }
  346. public function testClearCacheUserIDNotFound() {
  347. $this->expectException(\Exception::class);
  348. $this->expectExceptionMessage('User id not found in LDAP');
  349. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  350. ->setMethods(['userExists'])
  351. ->disableOriginalConstructor()
  352. ->getMock();
  353. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  354. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  355. $ldapProvider = $this->getLDAPProvider($server);
  356. $ldapProvider->clearCache('nonexisting_user');
  357. }
  358. public function testClearCache() {
  359. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  360. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
  361. ->disableOriginalConstructor()
  362. ->getMock();
  363. $userBackend->expects($this->once())
  364. ->method('userExists')
  365. ->willReturn(true);
  366. $userBackend->expects($this->once())
  367. ->method('clearCache')
  368. ->willReturn(true);
  369. $userBackend->expects($this->any())
  370. ->method($this->anything())
  371. ->willReturnSelf();
  372. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  373. $ldapProvider = $this->getLDAPProvider($server);
  374. $ldapProvider->clearCache('existing_user');
  375. $this->addToAssertionCount(1);
  376. }
  377. public function testClearGroupCacheGroupIDNotFound() {
  378. $this->expectException(\Exception::class);
  379. $this->expectExceptionMessage('Group id not found in LDAP');
  380. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  381. ->disableOriginalConstructor()
  382. ->getMock();
  383. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  384. ->setMethods(['groupExists'])
  385. ->disableOriginalConstructor()
  386. ->getMock();
  387. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  388. $server = $this->getServerMock($userBackend, $groupBackend);
  389. $ldapProvider = $this->getLDAPProvider($server);
  390. $ldapProvider->clearGroupCache('nonexisting_group');
  391. }
  392. public function testClearGroupCache() {
  393. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  394. ->disableOriginalConstructor()
  395. ->getMock();
  396. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  397. ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
  398. ->disableOriginalConstructor()
  399. ->getMock();
  400. $groupBackend->expects($this->once())
  401. ->method('groupExists')
  402. ->willReturn(true);
  403. $groupBackend->expects($this->once())
  404. ->method('clearCache')
  405. ->willReturn(true);
  406. $groupBackend->expects($this->any())
  407. ->method($this->anything())
  408. ->willReturnSelf();
  409. $server = $this->getServerMock($userBackend, $groupBackend);
  410. $ldapProvider = $this->getLDAPProvider($server);
  411. $ldapProvider->clearGroupCache('existing_group');
  412. $this->addToAssertionCount(1);
  413. }
  414. public function testDnExists() {
  415. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  416. ->setMethods(['dn2UserName'])
  417. ->disableOriginalConstructor()
  418. ->getMock();
  419. $userBackend->expects($this->any())
  420. ->method('dn2UserName')
  421. ->willReturn('existing_user');
  422. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  423. $ldapProvider = $this->getLDAPProvider($server);
  424. $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  425. }
  426. public function testFlagRecord() {
  427. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  428. ->setMethods([])
  429. ->disableOriginalConstructor()
  430. ->getMock();
  431. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  432. $ldapProvider = $this->getLDAPProvider($server);
  433. $ldapProvider->flagRecord('existing_user');
  434. $this->addToAssertionCount(1);
  435. }
  436. public function testUnflagRecord() {
  437. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  438. ->setMethods([])
  439. ->disableOriginalConstructor()
  440. ->getMock();
  441. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  442. $ldapProvider = $this->getLDAPProvider($server);
  443. $ldapProvider->unflagRecord('existing_user');
  444. $this->addToAssertionCount(1);
  445. }
  446. public function testGetLDAPDisplayNameFieldUserIDNotFound() {
  447. $this->expectException(\Exception::class);
  448. $this->expectExceptionMessage('User id not found in LDAP');
  449. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  450. ->setMethods(['userExists'])
  451. ->disableOriginalConstructor()
  452. ->getMock();
  453. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  454. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  455. $ldapProvider = $this->getLDAPProvider($server);
  456. $ldapProvider->getLDAPDisplayNameField('nonexisting_user');
  457. }
  458. public function testGetLDAPDisplayNameField() {
  459. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  460. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  461. ->disableOriginalConstructor()
  462. ->getMock();
  463. $userBackend->expects($this->once())
  464. ->method('userExists')
  465. ->willReturn(true);
  466. $userBackend->expects($this->once())
  467. ->method('getConfiguration')
  468. ->willReturn(['ldap_display_name' => 'displayName']);
  469. $userBackend->expects($this->any())
  470. ->method($this->anything())
  471. ->willReturnSelf();
  472. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  473. $ldapProvider = $this->getLDAPProvider($server);
  474. $this->assertEquals('displayName', $ldapProvider->getLDAPDisplayNameField('existing_user'));
  475. }
  476. public function testGetLDAPEmailFieldUserIDNotFound() {
  477. $this->expectException(\Exception::class);
  478. $this->expectExceptionMessage('User id not found in LDAP');
  479. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  480. ->setMethods(['userExists'])
  481. ->disableOriginalConstructor()
  482. ->getMock();
  483. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  484. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  485. $ldapProvider = $this->getLDAPProvider($server);
  486. $ldapProvider->getLDAPEmailField('nonexisting_user');
  487. }
  488. public function testGetLDAPEmailField() {
  489. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  490. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  491. ->disableOriginalConstructor()
  492. ->getMock();
  493. $userBackend->expects($this->once())
  494. ->method('userExists')
  495. ->willReturn(true);
  496. $userBackend->expects($this->once())
  497. ->method('getConfiguration')
  498. ->willReturn(['ldap_email_attr' => 'mail']);
  499. $userBackend->expects($this->any())
  500. ->method($this->anything())
  501. ->willReturnSelf();
  502. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  503. $ldapProvider = $this->getLDAPProvider($server);
  504. $this->assertEquals('mail', $ldapProvider->getLDAPEmailField('existing_user'));
  505. }
  506. public function testGetLDAPGroupMemberAssocUserIDNotFound() {
  507. $this->expectException(\Exception::class);
  508. $this->expectExceptionMessage('Group id not found in LDAP');
  509. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  510. ->disableOriginalConstructor()
  511. ->getMock();
  512. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  513. ->setMethods(['groupExists'])
  514. ->disableOriginalConstructor()
  515. ->getMock();
  516. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  517. $server = $this->getServerMock($userBackend, $groupBackend);
  518. $ldapProvider = $this->getLDAPProvider($server);
  519. $ldapProvider->getLDAPGroupMemberAssoc('nonexisting_group');
  520. }
  521. public function testgetLDAPGroupMemberAssoc() {
  522. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  523. ->disableOriginalConstructor()
  524. ->getMock();
  525. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  526. ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  527. ->disableOriginalConstructor()
  528. ->getMock();
  529. $groupBackend->expects($this->once())
  530. ->method('groupExists')
  531. ->willReturn(true);
  532. $groupBackend->expects($this->any())
  533. ->method('getConfiguration')
  534. ->willReturn(['ldap_group_member_assoc_attribute' => 'assoc_type']);
  535. $groupBackend->expects($this->any())
  536. ->method($this->anything())
  537. ->willReturnSelf();
  538. $server = $this->getServerMock($userBackend, $groupBackend);
  539. $ldapProvider = $this->getLDAPProvider($server);
  540. $this->assertEquals('assoc_type', $ldapProvider->getLDAPGroupMemberAssoc('existing_group'));
  541. }
  542. public function testGetMultiValueUserAttributeUserNotFound() {
  543. $this->expectException(\Exception::class);
  544. $this->expectExceptionMessage('User id not found in LDAP');
  545. $userBackend = $this->createMock(User_LDAP::class);
  546. $userBackend->expects(self::once())
  547. ->method('userExists')
  548. ->with('admin')
  549. ->willReturn(false);
  550. $groupBackend = $this->createMock(Group_LDAP::class);
  551. $server = $this->getServerMock($userBackend, $groupBackend);
  552. $ldapProvider = $this->getLDAPProvider($server);
  553. $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  554. }
  555. public function testGetMultiValueUserAttributeCacheHit() {
  556. $connection = $this->createMock(Connection::class);
  557. $connection->expects(self::once())
  558. ->method('getFromCache')
  559. ->with('admin-mailAlias')
  560. ->willReturn(['aliasA@test.local', 'aliasB@test.local']);
  561. $access = $this->createMock(Access::class);
  562. $access->expects(self::once())
  563. ->method('getConnection')
  564. ->willReturn($connection);
  565. $userBackend = $this->createMock(User_LDAP::class);
  566. $userBackend->expects(self::once())
  567. ->method('userExists')
  568. ->with('admin')
  569. ->willReturn(true);
  570. $userBackend->expects(self::once())
  571. ->method('getLDAPAccess')
  572. ->willReturn($access);
  573. $groupBackend = $this->createMock(Group_LDAP::class);
  574. $server = $this->getServerMock($userBackend, $groupBackend);
  575. $ldapProvider = $this->getLDAPProvider($server);
  576. $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  577. }
  578. public function testGetMultiValueUserAttributeLdapError() {
  579. $connection = $this->createMock(Connection::class);
  580. $connection->expects(self::once())
  581. ->method('getFromCache')
  582. ->with('admin-mailAlias')
  583. ->willReturn(null);
  584. $access = $this->createMock(Access::class);
  585. $access->expects(self::once())
  586. ->method('getConnection')
  587. ->willReturn($connection);
  588. $access->expects(self::once())
  589. ->method('username2dn')
  590. ->with('admin')
  591. ->willReturn('admin');
  592. $access->expects(self::once())
  593. ->method('readAttribute')
  594. ->with('admin', 'mailAlias')
  595. ->willReturn(false);
  596. $userBackend = $this->getMockBuilder(User_LDAP::class)
  597. ->disableOriginalConstructor()
  598. ->getMock();
  599. $userBackend->method('userExists')
  600. ->with('admin')
  601. ->willReturn(true);
  602. $userBackend->method('getLDAPAccess')
  603. ->willReturn($access);
  604. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  605. ->disableOriginalConstructor()
  606. ->getMock();
  607. $server = $this->getServerMock($userBackend, $groupBackend);
  608. $ldapProvider = $this->getLDAPProvider($server);
  609. $values = $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  610. self::assertCount(0, $values);
  611. }
  612. public function testGetMultiValueUserAttribute() {
  613. $connection = $this->createMock(Connection::class);
  614. $connection->expects(self::once())
  615. ->method('getFromCache')
  616. ->with('admin-mailAlias')
  617. ->willReturn(null);
  618. $access = $this->createMock(Access::class);
  619. $access->expects(self::once())
  620. ->method('getConnection')
  621. ->willReturn($connection);
  622. $access->expects(self::once())
  623. ->method('username2dn')
  624. ->with('admin')
  625. ->willReturn('admin');
  626. $access->expects(self::once())
  627. ->method('readAttribute')
  628. ->with('admin', 'mailAlias')
  629. ->willReturn(['aliasA@test.local', 'aliasB@test.local']);
  630. $userBackend = $this->getMockBuilder(User_LDAP::class)
  631. ->disableOriginalConstructor()
  632. ->getMock();
  633. $userBackend->method('userExists')
  634. ->with('admin')
  635. ->willReturn(true);
  636. $userBackend->method('getLDAPAccess')
  637. ->willReturn($access);
  638. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  639. ->disableOriginalConstructor()
  640. ->getMock();
  641. $server = $this->getServerMock($userBackend, $groupBackend);
  642. $ldapProvider = $this->getLDAPProvider($server);
  643. $values = $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  644. self::assertCount(2, $values);
  645. }
  646. public function testGetUserAttributeLdapError() {
  647. $connection = $this->createMock(Connection::class);
  648. $connection->expects(self::once())
  649. ->method('getFromCache')
  650. ->with('admin-mailAlias')
  651. ->willReturn(null);
  652. $access = $this->createMock(Access::class);
  653. $access->expects(self::once())
  654. ->method('getConnection')
  655. ->willReturn($connection);
  656. $access->expects(self::once())
  657. ->method('username2dn')
  658. ->with('admin')
  659. ->willReturn('admin');
  660. $access->expects(self::once())
  661. ->method('readAttribute')
  662. ->with('admin', 'mailAlias')
  663. ->willReturn(false);
  664. $userBackend = $this->getMockBuilder(User_LDAP::class)
  665. ->disableOriginalConstructor()
  666. ->getMock();
  667. $userBackend->method('userExists')
  668. ->with('admin')
  669. ->willReturn(true);
  670. $userBackend->method('getLDAPAccess')
  671. ->willReturn($access);
  672. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  673. ->disableOriginalConstructor()
  674. ->getMock();
  675. $server = $this->getServerMock($userBackend, $groupBackend);
  676. $ldapProvider = $this->getLDAPProvider($server);
  677. $value = $ldapProvider->getUserAttribute('admin', 'mailAlias');
  678. self::assertNull($value);
  679. }
  680. public function testGetUserAttribute() {
  681. $connection = $this->createMock(Connection::class);
  682. $connection->expects(self::once())
  683. ->method('getFromCache')
  684. ->with('admin-mailAlias')
  685. ->willReturn(null);
  686. $access = $this->createMock(Access::class);
  687. $access->expects(self::once())
  688. ->method('getConnection')
  689. ->willReturn($connection);
  690. $access->expects(self::once())
  691. ->method('username2dn')
  692. ->with('admin')
  693. ->willReturn('admin');
  694. $access->expects(self::once())
  695. ->method('readAttribute')
  696. ->with('admin', 'mailAlias')
  697. ->willReturn(['aliasA@test.local', 'aliasB@test.local']);
  698. $userBackend = $this->getMockBuilder(User_LDAP::class)
  699. ->disableOriginalConstructor()
  700. ->getMock();
  701. $userBackend->method('userExists')
  702. ->with('admin')
  703. ->willReturn(true);
  704. $userBackend->method('getLDAPAccess')
  705. ->willReturn($access);
  706. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  707. ->disableOriginalConstructor()
  708. ->getMock();
  709. $server = $this->getServerMock($userBackend, $groupBackend);
  710. $ldapProvider = $this->getLDAPProvider($server);
  711. $value = $ldapProvider->getUserAttribute('admin', 'mailAlias');
  712. self::assertEquals('aliasA@test.local', $value);
  713. }
  714. }