LDAPProviderTest.php 29 KB

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