LDAPProviderTest.php 28 KB

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