LDAPProviderTest.php 29 KB

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