LDAPProviderTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 OCA\User_LDAP\IGroupLDAP;
  27. use OCP\IConfig;
  28. use OCP\IServerContainer;
  29. use OCA\User_LDAP\IUserLDAP;
  30. /**
  31. * Class LDAPProviderTest
  32. *
  33. * @group DB
  34. *
  35. * @package OCA\User_LDAP\Tests
  36. */
  37. class LDAPProviderTest extends \Test\TestCase {
  38. protected function setUp() {
  39. parent::setUp();
  40. }
  41. private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend) {
  42. $server = $this->getMockBuilder('OC\Server')
  43. ->setMethods(['getUserManager', 'getBackends', 'getGroupManager'])
  44. ->setConstructorArgs(['', new \OC\Config(\OC::$configDir)])
  45. ->getMock();
  46. $server->expects($this->at(1))
  47. ->method('getBackends')
  48. ->willReturn([$userBackend]);
  49. $server->expects($this->any())
  50. ->method('getUserManager')
  51. ->willReturn($this->getUserManagerMock($userBackend));
  52. $server->expects($this->any())
  53. ->method('getGroupManager')
  54. ->willReturn($this->getGroupManagerMock($groupBackend));
  55. $server->expects($this->any())
  56. ->method($this->anything())
  57. ->willReturnSelf();
  58. return $server;
  59. }
  60. private function getUserManagerMock(IUserLDAP $userBackend) {
  61. $userManager = $this->getMockBuilder('OC\User\Manager')
  62. ->setMethods(['getBackends'])
  63. ->setConstructorArgs([$this->createMock(IConfig::class)])
  64. ->getMock();
  65. $userManager->expects($this->any())
  66. ->method('getBackends')
  67. ->willReturn([$userBackend]);
  68. return $userManager;
  69. }
  70. private function getGroupManagerMock(IGroupLDAP $groupBackend) {
  71. $groupManager = $this->getMockBuilder('OC\Group\Manager')
  72. ->setMethods(['getBackends'])
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $groupManager->expects($this->any())
  76. ->method('getBackends')
  77. ->willReturn([$groupBackend]);
  78. return $groupManager;
  79. }
  80. private function getDefaultGroupBackendMock() {
  81. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. return $groupBackend;
  85. }
  86. private function getLDAPProvider(IServerContainer $serverContainer) {
  87. $factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer);
  88. return $factory->getLDAPProvider();
  89. }
  90. /**
  91. * @expectedException \Exception
  92. * @expectedExceptionMessage User id not found in LDAP
  93. */
  94. public function testGetUserDNUserIDNotFound() {
  95. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  96. ->setMethods(['userExists'])
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  100. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  101. $ldapProvider = $this->getLDAPProvider($server);
  102. $ldapProvider->getUserDN('nonexisting_user');
  103. }
  104. public function testGetUserDN() {
  105. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  106. ->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $userBackend->expects($this->at(0))
  110. ->method('userExists')
  111. ->willReturn(true);
  112. $userBackend->expects($this->at(2))
  113. ->method('username2dn')
  114. ->willReturn('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org');
  115. $userBackend->expects($this->any())
  116. ->method($this->anything())
  117. ->willReturnSelf();
  118. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  119. $ldapProvider = $this->getLDAPProvider($server);
  120. $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
  121. $ldapProvider->getUserDN('existing_user'));
  122. }
  123. /**
  124. * @expectedException \Exception
  125. * @expectedExceptionMessage Group id not found in LDAP
  126. */
  127. public function testGetGroupDNGroupIDNotFound() {
  128. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  132. ->setMethods(['groupExists'])
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  136. $server = $this->getServerMock($userBackend, $groupBackend);
  137. $ldapProvider = $this->getLDAPProvider($server);
  138. $ldapProvider->getGroupDN('nonexisting_group');
  139. }
  140. public function testGetGroupDN() {
  141. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  142. ->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  146. ->setMethods(['groupExists', 'getLDAPAccess', 'groupname2dn'])
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. $groupBackend->expects($this->at(0))
  150. ->method('groupExists')
  151. ->willReturn(true);
  152. $groupBackend->expects($this->at(2))
  153. ->method('groupname2dn')
  154. ->willReturn('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org');
  155. $groupBackend->expects($this->any())
  156. ->method($this->anything())
  157. ->willReturnSelf();
  158. $server = $this->getServerMock($userBackend, $groupBackend);
  159. $ldapProvider = $this->getLDAPProvider($server);
  160. $this->assertEquals('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
  161. $ldapProvider->getGroupDN('existing_group'));
  162. }
  163. public function testGetUserName() {
  164. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  165. ->setMethods(['dn2UserName'])
  166. ->disableOriginalConstructor()
  167. ->getMock();
  168. $userBackend->expects($this->any())
  169. ->method('dn2UserName')
  170. ->willReturn('existing_user');
  171. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  172. $ldapProvider = $this->getLDAPProvider($server);
  173. $this->assertEquals('existing_user',
  174. $ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  175. }
  176. public function testDNasBaseParameter() {
  177. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  178. ->setMethods([])
  179. ->disableOriginalConstructor()
  180. ->getMock();
  181. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  182. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
  183. $ldapProvider = $this->getLDAPProvider($server);
  184. $this->assertEquals(
  185. $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  186. $ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  187. }
  188. public function testSanitizeDN() {
  189. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  190. ->setMethods([])
  191. ->disableOriginalConstructor()
  192. ->getMock();
  193. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  194. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
  195. $ldapProvider = $this->getLDAPProvider($server);
  196. $this->assertEquals(
  197. $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  198. $ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  199. }
  200. /**
  201. * @expectedException \Exception
  202. * @expectedExceptionMessage User id not found in LDAP
  203. */
  204. public function testGetLDAPConnectionUserIDNotFound() {
  205. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  206. ->setMethods(['userExists'])
  207. ->disableOriginalConstructor()
  208. ->getMock();
  209. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  210. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  211. $ldapProvider = $this->getLDAPProvider($server);
  212. $ldapProvider->getLDAPConnection('nonexisting_user');
  213. }
  214. public function testGetLDAPConnection() {
  215. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  216. ->setMethods(['userExists', 'getNewLDAPConnection'])
  217. ->disableOriginalConstructor()
  218. ->getMock();
  219. $userBackend->expects($this->any())
  220. ->method('userExists')
  221. ->willReturn(true);
  222. $userBackend->expects($this->any())
  223. ->method('getNewLDAPConnection')
  224. ->willReturn(true);
  225. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  226. $ldapProvider = $this->getLDAPProvider($server);
  227. $this->assertTrue($ldapProvider->getLDAPConnection('existing_user'));
  228. }
  229. /**
  230. * @expectedException \Exception
  231. * @expectedExceptionMessage Group id not found in LDAP
  232. */
  233. public function testGetGroupLDAPConnectionGroupIDNotFound() {
  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'])
  239. ->disableOriginalConstructor()
  240. ->getMock();
  241. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  242. $server = $this->getServerMock($userBackend, $groupBackend);
  243. $ldapProvider = $this->getLDAPProvider($server);
  244. $ldapProvider->getGroupLDAPConnection('nonexisting_group');
  245. }
  246. public function testGetGroupLDAPConnection() {
  247. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  248. ->disableOriginalConstructor()
  249. ->getMock();
  250. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  251. ->setMethods(['groupExists','getNewLDAPConnection'])
  252. ->disableOriginalConstructor()
  253. ->getMock();
  254. $groupBackend->expects($this->any())
  255. ->method('groupExists')
  256. ->willReturn(true);
  257. $groupBackend->expects($this->any())
  258. ->method('getNewLDAPConnection')
  259. ->willReturn(true);
  260. $server = $this->getServerMock($userBackend, $groupBackend);
  261. $ldapProvider = $this->getLDAPProvider($server);
  262. $this->assertTrue($ldapProvider->getGroupLDAPConnection('existing_group'));
  263. }
  264. /**
  265. * @expectedException \Exception
  266. * @expectedExceptionMessage User id not found in LDAP
  267. */
  268. public function testGetLDAPBaseUsersUserIDNotFound() {
  269. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  270. ->setMethods(['userExists'])
  271. ->disableOriginalConstructor()
  272. ->getMock();
  273. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  274. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  275. $ldapProvider = $this->getLDAPProvider($server);
  276. $ldapProvider->getLDAPBaseUsers('nonexisting_user');
  277. }
  278. public function testGetLDAPBaseUsers() {
  279. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  280. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  281. ->disableOriginalConstructor()
  282. ->getMock();
  283. $userBackend->expects($this->at(0))
  284. ->method('userExists')
  285. ->willReturn(true);
  286. $userBackend->expects($this->at(3))
  287. ->method('getConfiguration')
  288. ->willReturn(array('ldap_base_users'=>'ou=users,dc=example,dc=org'));
  289. $userBackend->expects($this->any())
  290. ->method($this->anything())
  291. ->willReturnSelf();
  292. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  293. $ldapProvider = $this->getLDAPProvider($server);
  294. $this->assertEquals('ou=users,dc=example,dc=org', $ldapProvider->getLDAPBaseUsers('existing_user'));
  295. }
  296. /**
  297. * @expectedException \Exception
  298. * @expectedExceptionMessage User id not found in LDAP
  299. */
  300. public function testGetLDAPBaseGroupsUserIDNotFound() {
  301. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  302. ->setMethods(['userExists'])
  303. ->disableOriginalConstructor()
  304. ->getMock();
  305. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  306. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  307. $ldapProvider = $this->getLDAPProvider($server);
  308. $ldapProvider->getLDAPBaseGroups('nonexisting_user');
  309. }
  310. public function testGetLDAPBaseGroups() {
  311. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  312. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  313. ->disableOriginalConstructor()
  314. ->getMock();
  315. $userBackend->expects($this->at(0))
  316. ->method('userExists')
  317. ->willReturn(true);
  318. $userBackend->expects($this->at(3))
  319. ->method('getConfiguration')
  320. ->willReturn(array('ldap_base_groups'=>'ou=groups,dc=example,dc=org'));
  321. $userBackend->expects($this->any())
  322. ->method($this->anything())
  323. ->willReturnSelf();
  324. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  325. $ldapProvider = $this->getLDAPProvider($server);
  326. $this->assertEquals('ou=groups,dc=example,dc=org', $ldapProvider->getLDAPBaseGroups('existing_user'));
  327. }
  328. /**
  329. * @expectedException \Exception
  330. * @expectedExceptionMessage User id not found in LDAP
  331. */
  332. public function testClearCacheUserIDNotFound() {
  333. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  334. ->setMethods(['userExists'])
  335. ->disableOriginalConstructor()
  336. ->getMock();
  337. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  338. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  339. $ldapProvider = $this->getLDAPProvider($server);
  340. $ldapProvider->clearCache('nonexisting_user');
  341. }
  342. public function testClearCache() {
  343. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  344. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
  345. ->disableOriginalConstructor()
  346. ->getMock();
  347. $userBackend->expects($this->at(0))
  348. ->method('userExists')
  349. ->willReturn(true);
  350. $userBackend->expects($this->at(3))
  351. ->method('clearCache')
  352. ->willReturn(true);
  353. $userBackend->expects($this->any())
  354. ->method($this->anything())
  355. ->willReturnSelf();
  356. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  357. $ldapProvider = $this->getLDAPProvider($server);
  358. $ldapProvider->clearCache('existing_user');
  359. $this->assertTrue(TRUE);
  360. }
  361. /**
  362. * @expectedException \Exception
  363. * @expectedExceptionMessage Group id not found in LDAP
  364. */
  365. public function testClearGroupCacheGroupIDNotFound() {
  366. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  367. ->disableOriginalConstructor()
  368. ->getMock();
  369. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  370. ->setMethods(['groupExists'])
  371. ->disableOriginalConstructor()
  372. ->getMock();
  373. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  374. $server = $this->getServerMock($userBackend, $groupBackend);
  375. $ldapProvider = $this->getLDAPProvider($server);
  376. $ldapProvider->clearGroupCache('nonexisting_group');
  377. }
  378. public function testClearGroupCache() {
  379. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  380. ->disableOriginalConstructor()
  381. ->getMock();
  382. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  383. ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
  384. ->disableOriginalConstructor()
  385. ->getMock();
  386. $groupBackend->expects($this->at(0))
  387. ->method('groupExists')
  388. ->willReturn(true);
  389. $groupBackend->expects($this->at(3))
  390. ->method('clearCache')
  391. ->willReturn(true);
  392. $groupBackend->expects($this->any())
  393. ->method($this->anything())
  394. ->willReturnSelf();
  395. $server = $this->getServerMock($userBackend, $groupBackend);
  396. $ldapProvider = $this->getLDAPProvider($server);
  397. $ldapProvider->clearGroupCache('existing_group');
  398. $this->assertTrue(TRUE);
  399. }
  400. public function testDnExists() {
  401. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  402. ->setMethods(['dn2UserName'])
  403. ->disableOriginalConstructor()
  404. ->getMock();
  405. $userBackend->expects($this->any())
  406. ->method('dn2UserName')
  407. ->willReturn('existing_user');
  408. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  409. $ldapProvider = $this->getLDAPProvider($server);
  410. $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  411. }
  412. public function testFlagRecord() {
  413. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  414. ->setMethods([])
  415. ->disableOriginalConstructor()
  416. ->getMock();
  417. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  418. $ldapProvider = $this->getLDAPProvider($server);
  419. $ldapProvider->flagRecord('existing_user');
  420. $this->assertTrue(TRUE);
  421. }
  422. public function testUnflagRecord() {
  423. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  424. ->setMethods([])
  425. ->disableOriginalConstructor()
  426. ->getMock();
  427. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  428. $ldapProvider = $this->getLDAPProvider($server);
  429. $ldapProvider->unflagRecord('existing_user');
  430. $this->assertTrue(TRUE);
  431. }
  432. /**
  433. * @expectedException \Exception
  434. * @expectedExceptionMessage User id not found in LDAP
  435. */
  436. public function testGetLDAPDisplayNameFieldUserIDNotFound() {
  437. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  438. ->setMethods(['userExists'])
  439. ->disableOriginalConstructor()
  440. ->getMock();
  441. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  442. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  443. $ldapProvider = $this->getLDAPProvider($server);
  444. $ldapProvider->getLDAPDisplayNameField('nonexisting_user');
  445. }
  446. public function testGetLDAPDisplayNameField() {
  447. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  448. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  449. ->disableOriginalConstructor()
  450. ->getMock();
  451. $userBackend->expects($this->at(0))
  452. ->method('userExists')
  453. ->willReturn(true);
  454. $userBackend->expects($this->at(3))
  455. ->method('getConfiguration')
  456. ->willReturn(array('ldap_display_name'=>'displayName'));
  457. $userBackend->expects($this->any())
  458. ->method($this->anything())
  459. ->willReturnSelf();
  460. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  461. $ldapProvider = $this->getLDAPProvider($server);
  462. $this->assertEquals('displayName', $ldapProvider->getLDAPDisplayNameField('existing_user'));
  463. }
  464. /**
  465. * @expectedException \Exception
  466. * @expectedExceptionMessage User id not found in LDAP
  467. */
  468. public function testGetLDAPEmailFieldUserIDNotFound() {
  469. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  470. ->setMethods(['userExists'])
  471. ->disableOriginalConstructor()
  472. ->getMock();
  473. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  474. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  475. $ldapProvider = $this->getLDAPProvider($server);
  476. $ldapProvider->getLDAPEmailField('nonexisting_user');
  477. }
  478. public function testGetLDAPEmailField() {
  479. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  480. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  481. ->disableOriginalConstructor()
  482. ->getMock();
  483. $userBackend->expects($this->at(0))
  484. ->method('userExists')
  485. ->willReturn(true);
  486. $userBackend->expects($this->at(3))
  487. ->method('getConfiguration')
  488. ->willReturn(array('ldap_email_attr'=>'mail'));
  489. $userBackend->expects($this->any())
  490. ->method($this->anything())
  491. ->willReturnSelf();
  492. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  493. $ldapProvider = $this->getLDAPProvider($server);
  494. $this->assertEquals('mail', $ldapProvider->getLDAPEmailField('existing_user'));
  495. }
  496. /**
  497. * @expectedException \Exception
  498. * @expectedExceptionMessage Group id not found in LDAP
  499. */
  500. public function testGetLDAPGroupMemberAssocUserIDNotFound() {
  501. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  502. ->disableOriginalConstructor()
  503. ->getMock();
  504. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  505. ->setMethods(['groupExists'])
  506. ->disableOriginalConstructor()
  507. ->getMock();
  508. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  509. $server = $this->getServerMock($userBackend, $groupBackend);
  510. $ldapProvider = $this->getLDAPProvider($server);
  511. $ldapProvider->getLDAPGroupMemberAssoc('nonexisting_group');
  512. }
  513. public function testgetLDAPGroupMemberAssoc() {
  514. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  515. ->disableOriginalConstructor()
  516. ->getMock();
  517. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  518. ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  519. ->disableOriginalConstructor()
  520. ->getMock();
  521. $groupBackend->expects($this->at(0))
  522. ->method('groupExists')
  523. ->willReturn(true);
  524. $groupBackend->expects($this->any())
  525. ->method('getConfiguration')
  526. ->willReturn(array('ldap_group_member_assoc_attribute'=>'assoc_type'));
  527. $groupBackend->expects($this->any())
  528. ->method($this->anything())
  529. ->willReturnSelf();
  530. $server = $this->getServerMock($userBackend, $groupBackend);
  531. $ldapProvider = $this->getLDAPProvider($server);
  532. $this->assertEquals('assoc_type', $ldapProvider->getLDAPGroupMemberAssoc('existing_group'));
  533. }
  534. }