LDAPProviderTest.php 23 KB

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