1
0

LDAPProviderTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. $userBackend->expects($this->any())
  229. ->method('getNewLDAPConnection')
  230. ->willReturn(true);
  231. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  232. $ldapProvider = $this->getLDAPProvider($server);
  233. $this->assertTrue($ldapProvider->getLDAPConnection('existing_user'));
  234. }
  235. public function testGetGroupLDAPConnectionGroupIDNotFound() {
  236. $this->expectException(\Exception::class);
  237. $this->expectExceptionMessage('Group id not found in LDAP');
  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. public function testGetLDAPBaseUsersUserIDNotFound() {
  269. $this->expectException(\Exception::class);
  270. $this->expectExceptionMessage('User id not found in LDAP');
  271. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  272. ->setMethods(['userExists'])
  273. ->disableOriginalConstructor()
  274. ->getMock();
  275. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  276. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  277. $ldapProvider = $this->getLDAPProvider($server);
  278. $ldapProvider->getLDAPBaseUsers('nonexisting_user');
  279. }
  280. public function testGetLDAPBaseUsers() {
  281. $bases = [
  282. 'ou=users,ou=foobar,dc=example,dc=org',
  283. 'ou=users,ou=barfoo,dc=example,dc=org',
  284. ];
  285. $dn = 'uid=malik,' . $bases[1];
  286. $connection = $this->createMock(Connection::class);
  287. $connection->expects($this->any())
  288. ->method('__get')
  289. ->willReturnCallback(function ($key) use ($bases) {
  290. switch ($key) {
  291. case 'ldapBaseUsers':
  292. return $bases;
  293. }
  294. return null;
  295. });
  296. $access = $this->createMock(Access::class);
  297. $access->expects($this->any())
  298. ->method('getConnection')
  299. ->willReturn($connection);
  300. $access->expects($this->exactly(2))
  301. ->method('isDNPartOfBase')
  302. ->willReturnOnConsecutiveCalls(false, true);
  303. $access->expects($this->atLeastOnce())
  304. ->method('username2dn')
  305. ->willReturn($dn);
  306. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  307. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  308. ->disableOriginalConstructor()
  309. ->getMock();
  310. $userBackend->expects($this->atLeastOnce())
  311. ->method('userExists')
  312. ->willReturn(true);
  313. $userBackend->expects($this->any())
  314. ->method('getLDAPAccess')
  315. ->willReturn($access);
  316. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  317. $ldapProvider = $this->getLDAPProvider($server);
  318. $this->assertEquals($bases[1], $ldapProvider->getLDAPBaseUsers('existing_user'));
  319. }
  320. public function testGetLDAPBaseGroupsUserIDNotFound() {
  321. $this->expectException(\Exception::class);
  322. $this->expectExceptionMessage('User id not found in LDAP');
  323. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  324. ->setMethods(['userExists'])
  325. ->disableOriginalConstructor()
  326. ->getMock();
  327. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  328. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  329. $ldapProvider = $this->getLDAPProvider($server);
  330. $ldapProvider->getLDAPBaseGroups('nonexisting_user');
  331. }
  332. public function testGetLDAPBaseGroups() {
  333. $bases = [
  334. 'ou=groupd,ou=foobar,dc=example,dc=org',
  335. 'ou=groups,ou=barfoo,dc=example,dc=org',
  336. ];
  337. $connection = $this->createMock(Connection::class);
  338. $connection->expects($this->any())
  339. ->method('__get')
  340. ->willReturnCallback(function ($key) use ($bases) {
  341. switch ($key) {
  342. case 'ldapBaseGroups':
  343. return $bases;
  344. }
  345. return null;
  346. });
  347. $access = $this->createMock(Access::class);
  348. $access->expects($this->any())
  349. ->method('getConnection')
  350. ->willReturn($connection);
  351. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  352. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  353. ->disableOriginalConstructor()
  354. ->getMock();
  355. $userBackend->expects($this->any())
  356. ->method('userExists')
  357. ->willReturn(true);
  358. $userBackend->expects($this->any())
  359. ->method('getLDAPAccess')
  360. ->willReturn($access);
  361. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  362. $ldapProvider = $this->getLDAPProvider($server);
  363. $this->assertEquals($bases[0], $ldapProvider->getLDAPBaseGroups('existing_user'));
  364. }
  365. public function testClearCacheUserIDNotFound() {
  366. $this->expectException(\Exception::class);
  367. $this->expectExceptionMessage('User id not found in LDAP');
  368. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  369. ->setMethods(['userExists'])
  370. ->disableOriginalConstructor()
  371. ->getMock();
  372. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  373. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  374. $ldapProvider = $this->getLDAPProvider($server);
  375. $ldapProvider->clearCache('nonexisting_user');
  376. }
  377. public function testClearCache() {
  378. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  379. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
  380. ->disableOriginalConstructor()
  381. ->getMock();
  382. $userBackend->expects($this->once())
  383. ->method('userExists')
  384. ->willReturn(true);
  385. $userBackend->expects($this->once())
  386. ->method('clearCache')
  387. ->willReturn(true);
  388. $userBackend->expects($this->any())
  389. ->method($this->anything())
  390. ->willReturnSelf();
  391. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  392. $ldapProvider = $this->getLDAPProvider($server);
  393. $ldapProvider->clearCache('existing_user');
  394. $this->addToAssertionCount(1);
  395. }
  396. public function testClearGroupCacheGroupIDNotFound() {
  397. $this->expectException(\Exception::class);
  398. $this->expectExceptionMessage('Group id not found in LDAP');
  399. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  400. ->disableOriginalConstructor()
  401. ->getMock();
  402. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  403. ->setMethods(['groupExists'])
  404. ->disableOriginalConstructor()
  405. ->getMock();
  406. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  407. $server = $this->getServerMock($userBackend, $groupBackend);
  408. $ldapProvider = $this->getLDAPProvider($server);
  409. $ldapProvider->clearGroupCache('nonexisting_group');
  410. }
  411. public function testClearGroupCache() {
  412. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  413. ->disableOriginalConstructor()
  414. ->getMock();
  415. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  416. ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
  417. ->disableOriginalConstructor()
  418. ->getMock();
  419. $groupBackend->expects($this->once())
  420. ->method('groupExists')
  421. ->willReturn(true);
  422. $groupBackend->expects($this->once())
  423. ->method('clearCache')
  424. ->willReturn(true);
  425. $groupBackend->expects($this->any())
  426. ->method($this->anything())
  427. ->willReturnSelf();
  428. $server = $this->getServerMock($userBackend, $groupBackend);
  429. $ldapProvider = $this->getLDAPProvider($server);
  430. $ldapProvider->clearGroupCache('existing_group');
  431. $this->addToAssertionCount(1);
  432. }
  433. public function testDnExists() {
  434. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  435. ->setMethods(['dn2UserName'])
  436. ->disableOriginalConstructor()
  437. ->getMock();
  438. $userBackend->expects($this->any())
  439. ->method('dn2UserName')
  440. ->willReturn('existing_user');
  441. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  442. $ldapProvider = $this->getLDAPProvider($server);
  443. $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  444. }
  445. public function testFlagRecord() {
  446. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  447. ->setMethods([])
  448. ->disableOriginalConstructor()
  449. ->getMock();
  450. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  451. $ldapProvider = $this->getLDAPProvider($server);
  452. $ldapProvider->flagRecord('existing_user');
  453. $this->addToAssertionCount(1);
  454. }
  455. public function testUnflagRecord() {
  456. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  457. ->setMethods([])
  458. ->disableOriginalConstructor()
  459. ->getMock();
  460. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  461. $ldapProvider = $this->getLDAPProvider($server);
  462. $ldapProvider->unflagRecord('existing_user');
  463. $this->addToAssertionCount(1);
  464. }
  465. public function testGetLDAPDisplayNameFieldUserIDNotFound() {
  466. $this->expectException(\Exception::class);
  467. $this->expectExceptionMessage('User id not found in LDAP');
  468. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  469. ->setMethods(['userExists'])
  470. ->disableOriginalConstructor()
  471. ->getMock();
  472. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  473. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  474. $ldapProvider = $this->getLDAPProvider($server);
  475. $ldapProvider->getLDAPDisplayNameField('nonexisting_user');
  476. }
  477. public function testGetLDAPDisplayNameField() {
  478. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  479. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  480. ->disableOriginalConstructor()
  481. ->getMock();
  482. $userBackend->expects($this->once())
  483. ->method('userExists')
  484. ->willReturn(true);
  485. $userBackend->expects($this->once())
  486. ->method('getConfiguration')
  487. ->willReturn(['ldap_display_name' => 'displayName']);
  488. $userBackend->expects($this->any())
  489. ->method($this->anything())
  490. ->willReturnSelf();
  491. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  492. $ldapProvider = $this->getLDAPProvider($server);
  493. $this->assertEquals('displayName', $ldapProvider->getLDAPDisplayNameField('existing_user'));
  494. }
  495. public function testGetLDAPEmailFieldUserIDNotFound() {
  496. $this->expectException(\Exception::class);
  497. $this->expectExceptionMessage('User id not found in LDAP');
  498. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  499. ->setMethods(['userExists'])
  500. ->disableOriginalConstructor()
  501. ->getMock();
  502. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  503. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  504. $ldapProvider = $this->getLDAPProvider($server);
  505. $ldapProvider->getLDAPEmailField('nonexisting_user');
  506. }
  507. public function testGetLDAPEmailField() {
  508. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  509. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  510. ->disableOriginalConstructor()
  511. ->getMock();
  512. $userBackend->expects($this->once())
  513. ->method('userExists')
  514. ->willReturn(true);
  515. $userBackend->expects($this->once())
  516. ->method('getConfiguration')
  517. ->willReturn(['ldap_email_attr' => 'mail']);
  518. $userBackend->expects($this->any())
  519. ->method($this->anything())
  520. ->willReturnSelf();
  521. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  522. $ldapProvider = $this->getLDAPProvider($server);
  523. $this->assertEquals('mail', $ldapProvider->getLDAPEmailField('existing_user'));
  524. }
  525. public function testGetLDAPGroupMemberAssocUserIDNotFound() {
  526. $this->expectException(\Exception::class);
  527. $this->expectExceptionMessage('Group id not found in LDAP');
  528. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  529. ->disableOriginalConstructor()
  530. ->getMock();
  531. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  532. ->setMethods(['groupExists'])
  533. ->disableOriginalConstructor()
  534. ->getMock();
  535. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  536. $server = $this->getServerMock($userBackend, $groupBackend);
  537. $ldapProvider = $this->getLDAPProvider($server);
  538. $ldapProvider->getLDAPGroupMemberAssoc('nonexisting_group');
  539. }
  540. public function testgetLDAPGroupMemberAssoc() {
  541. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  542. ->disableOriginalConstructor()
  543. ->getMock();
  544. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  545. ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  546. ->disableOriginalConstructor()
  547. ->getMock();
  548. $groupBackend->expects($this->once())
  549. ->method('groupExists')
  550. ->willReturn(true);
  551. $groupBackend->expects($this->any())
  552. ->method('getConfiguration')
  553. ->willReturn(['ldap_group_member_assoc_attribute' => 'assoc_type']);
  554. $groupBackend->expects($this->any())
  555. ->method($this->anything())
  556. ->willReturnSelf();
  557. $server = $this->getServerMock($userBackend, $groupBackend);
  558. $ldapProvider = $this->getLDAPProvider($server);
  559. $this->assertEquals('assoc_type', $ldapProvider->getLDAPGroupMemberAssoc('existing_group'));
  560. }
  561. public function testGetMultiValueUserAttributeUserNotFound() {
  562. $this->expectException(\Exception::class);
  563. $this->expectExceptionMessage('User id not found in LDAP');
  564. $userBackend = $this->createMock(User_LDAP::class);
  565. $userBackend->expects(self::once())
  566. ->method('userExists')
  567. ->with('admin')
  568. ->willReturn(false);
  569. $groupBackend = $this->createMock(Group_LDAP::class);
  570. $server = $this->getServerMock($userBackend, $groupBackend);
  571. $ldapProvider = $this->getLDAPProvider($server);
  572. $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  573. }
  574. public function testGetMultiValueUserAttributeCacheHit() {
  575. $connection = $this->createMock(Connection::class);
  576. $connection->expects(self::once())
  577. ->method('getFromCache')
  578. ->with('admin-mailAlias')
  579. ->willReturn(['aliasA@test.local', 'aliasB@test.local']);
  580. $access = $this->createMock(Access::class);
  581. $access->expects(self::once())
  582. ->method('getConnection')
  583. ->willReturn($connection);
  584. $userBackend = $this->createMock(User_LDAP::class);
  585. $userBackend->expects(self::once())
  586. ->method('userExists')
  587. ->with('admin')
  588. ->willReturn(true);
  589. $userBackend->expects(self::once())
  590. ->method('getLDAPAccess')
  591. ->willReturn($access);
  592. $groupBackend = $this->createMock(Group_LDAP::class);
  593. $server = $this->getServerMock($userBackend, $groupBackend);
  594. $ldapProvider = $this->getLDAPProvider($server);
  595. $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  596. }
  597. public function testGetMultiValueUserAttributeLdapError() {
  598. $connection = $this->createMock(Connection::class);
  599. $connection->expects(self::once())
  600. ->method('getFromCache')
  601. ->with('admin-mailAlias')
  602. ->willReturn(null);
  603. $access = $this->createMock(Access::class);
  604. $access->expects(self::once())
  605. ->method('getConnection')
  606. ->willReturn($connection);
  607. $access->expects(self::once())
  608. ->method('username2dn')
  609. ->with('admin')
  610. ->willReturn('admin');
  611. $access->expects(self::once())
  612. ->method('readAttribute')
  613. ->with('admin', 'mailAlias')
  614. ->willReturn(false);
  615. $userBackend = $this->getMockBuilder(User_LDAP::class)
  616. ->disableOriginalConstructor()
  617. ->getMock();
  618. $userBackend->method('userExists')
  619. ->with('admin')
  620. ->willReturn(true);
  621. $userBackend->method('getLDAPAccess')
  622. ->willReturn($access);
  623. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  624. ->disableOriginalConstructor()
  625. ->getMock();
  626. $server = $this->getServerMock($userBackend, $groupBackend);
  627. $ldapProvider = $this->getLDAPProvider($server);
  628. $values = $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  629. self::assertCount(0, $values);
  630. }
  631. public function testGetMultiValueUserAttribute() {
  632. $connection = $this->createMock(Connection::class);
  633. $connection->expects(self::once())
  634. ->method('getFromCache')
  635. ->with('admin-mailAlias')
  636. ->willReturn(null);
  637. $access = $this->createMock(Access::class);
  638. $access->expects(self::once())
  639. ->method('getConnection')
  640. ->willReturn($connection);
  641. $access->expects(self::once())
  642. ->method('username2dn')
  643. ->with('admin')
  644. ->willReturn('admin');
  645. $access->expects(self::once())
  646. ->method('readAttribute')
  647. ->with('admin', 'mailAlias')
  648. ->willReturn(['aliasA@test.local', 'aliasB@test.local']);
  649. $userBackend = $this->getMockBuilder(User_LDAP::class)
  650. ->disableOriginalConstructor()
  651. ->getMock();
  652. $userBackend->method('userExists')
  653. ->with('admin')
  654. ->willReturn(true);
  655. $userBackend->method('getLDAPAccess')
  656. ->willReturn($access);
  657. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  658. ->disableOriginalConstructor()
  659. ->getMock();
  660. $server = $this->getServerMock($userBackend, $groupBackend);
  661. $ldapProvider = $this->getLDAPProvider($server);
  662. $values = $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias');
  663. self::assertCount(2, $values);
  664. }
  665. public function testGetUserAttributeLdapError() {
  666. $connection = $this->createMock(Connection::class);
  667. $connection->expects(self::once())
  668. ->method('getFromCache')
  669. ->with('admin-mailAlias')
  670. ->willReturn(null);
  671. $access = $this->createMock(Access::class);
  672. $access->expects(self::once())
  673. ->method('getConnection')
  674. ->willReturn($connection);
  675. $access->expects(self::once())
  676. ->method('username2dn')
  677. ->with('admin')
  678. ->willReturn('admin');
  679. $access->expects(self::once())
  680. ->method('readAttribute')
  681. ->with('admin', 'mailAlias')
  682. ->willReturn(false);
  683. $userBackend = $this->getMockBuilder(User_LDAP::class)
  684. ->disableOriginalConstructor()
  685. ->getMock();
  686. $userBackend->method('userExists')
  687. ->with('admin')
  688. ->willReturn(true);
  689. $userBackend->method('getLDAPAccess')
  690. ->willReturn($access);
  691. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  692. ->disableOriginalConstructor()
  693. ->getMock();
  694. $server = $this->getServerMock($userBackend, $groupBackend);
  695. $ldapProvider = $this->getLDAPProvider($server);
  696. $value = $ldapProvider->getUserAttribute('admin', 'mailAlias');
  697. self::assertNull($value);
  698. }
  699. public function testGetUserAttribute() {
  700. $connection = $this->createMock(Connection::class);
  701. $connection->expects(self::once())
  702. ->method('getFromCache')
  703. ->with('admin-mailAlias')
  704. ->willReturn(null);
  705. $access = $this->createMock(Access::class);
  706. $access->expects(self::once())
  707. ->method('getConnection')
  708. ->willReturn($connection);
  709. $access->expects(self::once())
  710. ->method('username2dn')
  711. ->with('admin')
  712. ->willReturn('admin');
  713. $access->expects(self::once())
  714. ->method('readAttribute')
  715. ->with('admin', 'mailAlias')
  716. ->willReturn(['aliasA@test.local', 'aliasB@test.local']);
  717. $userBackend = $this->getMockBuilder(User_LDAP::class)
  718. ->disableOriginalConstructor()
  719. ->getMock();
  720. $userBackend->method('userExists')
  721. ->with('admin')
  722. ->willReturn(true);
  723. $userBackend->method('getLDAPAccess')
  724. ->willReturn($access);
  725. $groupBackend = $this->getMockBuilder(Group_LDAP::class)
  726. ->disableOriginalConstructor()
  727. ->getMock();
  728. $server = $this->getServerMock($userBackend, $groupBackend);
  729. $ldapProvider = $this->getLDAPProvider($server);
  730. $value = $ldapProvider->getUserAttribute('admin', 'mailAlias');
  731. self::assertEquals('aliasA@test.local', $value);
  732. }
  733. }