LDAPProviderTest.php 23 KB

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