LDAPProviderTest.php 28 KB

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