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. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  42. /**
  43. * Class LDAPProviderTest
  44. *
  45. * @group DB
  46. *
  47. * @package OCA\User_LDAP\Tests
  48. */
  49. class LDAPProviderTest extends \Test\TestCase {
  50. protected function setUp(): void {
  51. parent::setUp();
  52. }
  53. private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend) {
  54. $server = $this->getMockBuilder('OC\Server')
  55. ->setMethods(['getUserManager', 'getBackends', 'getGroupManager'])
  56. ->setConstructorArgs(['', new \OC\Config(\OC::$configDir)])
  57. ->getMock();
  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(ICacheFactory::class),
  76. $this->createMock(IEventDispatcher::class),
  77. ])
  78. ->getMock();
  79. $userManager->expects($this->any())
  80. ->method('getBackends')
  81. ->willReturn([$userBackend]);
  82. return $userManager;
  83. }
  84. private function getGroupManagerMock(IGroupLDAP $groupBackend) {
  85. $groupManager = $this->getMockBuilder('OC\Group\Manager')
  86. ->setMethods(['getBackends'])
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $groupManager->expects($this->any())
  90. ->method('getBackends')
  91. ->willReturn([$groupBackend]);
  92. return $groupManager;
  93. }
  94. private function getDefaultGroupBackendMock() {
  95. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. return $groupBackend;
  99. }
  100. private function getLDAPProvider(IServerContainer $serverContainer) {
  101. $factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer);
  102. return $factory->getLDAPProvider();
  103. }
  104. public function testGetUserDNUserIDNotFound() {
  105. $this->expectException(\Exception::class);
  106. $this->expectExceptionMessage('User id not found in LDAP');
  107. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  108. ->setMethods(['userExists'])
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  112. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  113. $ldapProvider = $this->getLDAPProvider($server);
  114. $ldapProvider->getUserDN('nonexisting_user');
  115. }
  116. public function testGetUserDN() {
  117. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  118. ->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $userBackend->expects($this->once())
  122. ->method('userExists')
  123. ->willReturn(true);
  124. $userBackend->expects($this->once())
  125. ->method('username2dn')
  126. ->willReturn('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org');
  127. $userBackend->expects($this->any())
  128. ->method($this->anything())
  129. ->willReturnSelf();
  130. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  131. $ldapProvider = $this->getLDAPProvider($server);
  132. $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
  133. $ldapProvider->getUserDN('existing_user'));
  134. }
  135. public function testGetGroupDNGroupIDNotFound() {
  136. $this->expectException(\Exception::class);
  137. $this->expectExceptionMessage('Group id not found in LDAP');
  138. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  142. ->setMethods(['groupExists'])
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  146. $server = $this->getServerMock($userBackend, $groupBackend);
  147. $ldapProvider = $this->getLDAPProvider($server);
  148. $ldapProvider->getGroupDN('nonexisting_group');
  149. }
  150. public function testGetGroupDN() {
  151. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  152. ->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  156. ->setMethods(['groupExists', 'getLDAPAccess', 'groupname2dn'])
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. $groupBackend->expects($this->once())
  160. ->method('groupExists')
  161. ->willReturn(true);
  162. $groupBackend->expects($this->once())
  163. ->method('groupname2dn')
  164. ->willReturn('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org');
  165. $groupBackend->expects($this->any())
  166. ->method($this->anything())
  167. ->willReturnSelf();
  168. $server = $this->getServerMock($userBackend, $groupBackend);
  169. $ldapProvider = $this->getLDAPProvider($server);
  170. $this->assertEquals('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
  171. $ldapProvider->getGroupDN('existing_group'));
  172. }
  173. public function testGetUserName() {
  174. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  175. ->setMethods(['dn2UserName'])
  176. ->disableOriginalConstructor()
  177. ->getMock();
  178. $userBackend->expects($this->any())
  179. ->method('dn2UserName')
  180. ->willReturn('existing_user');
  181. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  182. $ldapProvider = $this->getLDAPProvider($server);
  183. $this->assertEquals('existing_user',
  184. $ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  185. }
  186. public function testDNasBaseParameter() {
  187. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  188. ->setMethods([])
  189. ->disableOriginalConstructor()
  190. ->getMock();
  191. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  192. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  193. $ldapProvider = $this->getLDAPProvider($server);
  194. $this->assertEquals(
  195. $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  196. $ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  197. }
  198. public function testSanitizeDN() {
  199. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  200. ->setMethods([])
  201. ->disableOriginalConstructor()
  202. ->getMock();
  203. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  204. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  205. $ldapProvider = $this->getLDAPProvider($server);
  206. $this->assertEquals(
  207. $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  208. $ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  209. }
  210. public function testGetLDAPConnectionUserIDNotFound() {
  211. $this->expectException(\Exception::class);
  212. $this->expectExceptionMessage('User id not found in LDAP');
  213. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  214. ->setMethods(['userExists'])
  215. ->disableOriginalConstructor()
  216. ->getMock();
  217. $userBackend->expects($this->any())->method('userExists')->willReturn(false);
  218. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  219. $ldapProvider = $this->getLDAPProvider($server);
  220. $ldapProvider->getLDAPConnection('nonexisting_user');
  221. }
  222. public function testGetLDAPConnection() {
  223. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  224. ->setMethods(['userExists', 'getNewLDAPConnection'])
  225. ->disableOriginalConstructor()
  226. ->getMock();
  227. $userBackend->expects($this->any())
  228. ->method('userExists')
  229. ->willReturn(true);
  230. $userBackend->expects($this->any())
  231. ->method('getNewLDAPConnection')
  232. ->willReturn(true);
  233. $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
  234. $ldapProvider = $this->getLDAPProvider($server);
  235. $this->assertTrue($ldapProvider->getLDAPConnection('existing_user'));
  236. }
  237. public function testGetGroupLDAPConnectionGroupIDNotFound() {
  238. $this->expectException(\Exception::class);
  239. $this->expectExceptionMessage('Group id not found in LDAP');
  240. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  241. ->disableOriginalConstructor()
  242. ->getMock();
  243. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  244. ->setMethods(['groupExists'])
  245. ->disableOriginalConstructor()
  246. ->getMock();
  247. $groupBackend->expects($this->any())->method('groupExists')->willReturn(false);
  248. $server = $this->getServerMock($userBackend, $groupBackend);
  249. $ldapProvider = $this->getLDAPProvider($server);
  250. $ldapProvider->getGroupLDAPConnection('nonexisting_group');
  251. }
  252. public function testGetGroupLDAPConnection() {
  253. $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  254. ->disableOriginalConstructor()
  255. ->getMock();
  256. $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
  257. ->setMethods(['groupExists','getNewLDAPConnection'])
  258. ->disableOriginalConstructor()
  259. ->getMock();
  260. $groupBackend->expects($this->any())
  261. ->method('groupExists')
  262. ->willReturn(true);
  263. $groupBackend->expects($this->any())
  264. ->method('getNewLDAPConnection')
  265. ->willReturn(true);
  266. $server = $this->getServerMock($userBackend, $groupBackend);
  267. $ldapProvider = $this->getLDAPProvider($server);
  268. $this->assertTrue($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. }