LDAPProviderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2016, Roger Szabo (roger.szabo@web.de)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\User_LDAP\Tests;
  23. use OCP\IServerContainer;
  24. use OCA\User_LDAP\IUserLDAP;
  25. /**
  26. * Class LDAPProviderTest
  27. *
  28. * @group DB
  29. *
  30. * @package OCA\User_LDAP\Tests
  31. */
  32. class LDAPProviderTest extends \Test\TestCase {
  33. protected function setUp() {
  34. parent::setUp();
  35. }
  36. private function getServerMock(IUserLDAP $backend) {
  37. $server = $this->getMockBuilder('OC\Server')
  38. ->setMethods(['getUserManager', 'getBackends'])
  39. ->setConstructorArgs(['', new \OC\Config(\OC::$configDir)])
  40. ->getMock();
  41. $server->expects($this->at(1))
  42. ->method('getBackends')
  43. ->willReturn([$backend]);
  44. $server->expects($this->any())
  45. ->method($this->anything())
  46. ->willReturnSelf();
  47. return $server;
  48. }
  49. private function getLDAPProvider(IServerContainer $serverContainer) {
  50. $factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer);
  51. return $factory->getLDAPProvider();
  52. }
  53. /**
  54. * @expectedException \Exception
  55. * @expectedExceptionMessage User id not found in LDAP
  56. */
  57. public function testGetUserDNUserIDNotFound() {
  58. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  59. ->setMethods(['userExists'])
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $backend->expects($this->any())->method('userExists')->willReturn(false);
  63. $server = $this->getServerMock($backend);
  64. $ldapProvider = $this->getLDAPProvider($server);
  65. $ldapProvider->getUserDN('nonexisting_user');
  66. }
  67. public function testGetUserDN() {
  68. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  69. ->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $backend->expects($this->at(0))
  73. ->method('userExists')
  74. ->willReturn(true);
  75. $backend->expects($this->at(2))
  76. ->method('username2dn')
  77. ->willReturn('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org');
  78. $backend->expects($this->any())
  79. ->method($this->anything())
  80. ->willReturnSelf();
  81. $server = $this->getServerMock($backend);
  82. $ldapProvider = $this->getLDAPProvider($server);
  83. $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
  84. $ldapProvider->getUserDN('existing_user'));
  85. }
  86. public function testGetUserName() {
  87. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  88. ->setMethods(['dn2UserName'])
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $backend->expects($this->any())
  92. ->method('dn2UserName')
  93. ->willReturn('existing_user');
  94. $server = $this->getServerMock($backend);
  95. $ldapProvider = $this->getLDAPProvider($server);
  96. $this->assertEquals('existing_user',
  97. $ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  98. }
  99. public function testDNasBaseParameter() {
  100. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  101. ->setMethods([])
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $server = $this->getServerMock($backend);
  105. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
  106. $ldapProvider = $this->getLDAPProvider($server);
  107. $this->assertEquals(
  108. $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  109. $ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  110. }
  111. public function testSanitizeDN() {
  112. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  113. ->setMethods([])
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $server = $this->getServerMock($backend);
  117. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
  118. $ldapProvider = $this->getLDAPProvider($server);
  119. $this->assertEquals(
  120. $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
  121. $ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  122. }
  123. /**
  124. * @expectedException \Exception
  125. * @expectedExceptionMessage User id not found in LDAP
  126. */
  127. public function testGetLDAPConnectionUserIDNotFound() {
  128. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  129. ->setMethods(['userExists'])
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $backend->expects($this->any())->method('userExists')->willReturn(false);
  133. $server = $this->getServerMock($backend);
  134. $ldapProvider = $this->getLDAPProvider($server);
  135. $ldapProvider->getLDAPConnection('nonexisting_user');
  136. }
  137. public function testGetLDAPConnection() {
  138. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  139. ->setMethods(['userExists', 'getNewLDAPConnection'])
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $backend->expects($this->any())
  143. ->method('userExists')
  144. ->willReturn(true);
  145. $backend->expects($this->any())
  146. ->method('getNewLDAPConnection')
  147. ->willReturn(true);
  148. $server = $this->getServerMock($backend);
  149. $ldapProvider = $this->getLDAPProvider($server);
  150. $this->assertTrue($ldapProvider->getLDAPConnection('existing_user'));
  151. }
  152. /**
  153. * @expectedException \Exception
  154. * @expectedExceptionMessage User id not found in LDAP
  155. */
  156. public function testGetLDAPBaseUsersUserIDNotFound() {
  157. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  158. ->setMethods(['userExists'])
  159. ->disableOriginalConstructor()
  160. ->getMock();
  161. $backend->expects($this->any())->method('userExists')->willReturn(false);
  162. $server = $this->getServerMock($backend);
  163. $ldapProvider = $this->getLDAPProvider($server);
  164. $ldapProvider->getLDAPBaseUsers('nonexisting_user');
  165. }
  166. public function testGetLDAPBaseUsers() {
  167. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  168. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  169. ->disableOriginalConstructor()
  170. ->getMock();
  171. $backend->expects($this->at(0))
  172. ->method('userExists')
  173. ->willReturn(true);
  174. $backend->expects($this->at(3))
  175. ->method('getConfiguration')
  176. ->willReturn(array('ldap_base_users'=>'ou=users,dc=example,dc=org'));
  177. $backend->expects($this->any())
  178. ->method($this->anything())
  179. ->willReturnSelf();
  180. $server = $this->getServerMock($backend);
  181. $ldapProvider = $this->getLDAPProvider($server);
  182. $this->assertEquals('ou=users,dc=example,dc=org', $ldapProvider->getLDAPBaseUsers('existing_user'));
  183. }
  184. /**
  185. * @expectedException \Exception
  186. * @expectedExceptionMessage User id not found in LDAP
  187. */
  188. public function testGetLDAPBaseGroupsUserIDNotFound() {
  189. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  190. ->setMethods(['userExists'])
  191. ->disableOriginalConstructor()
  192. ->getMock();
  193. $backend->expects($this->any())->method('userExists')->willReturn(false);
  194. $server = $this->getServerMock($backend);
  195. $ldapProvider = $this->getLDAPProvider($server);
  196. $ldapProvider->getLDAPBaseGroups('nonexisting_user');
  197. }
  198. public function testGetLDAPBaseGroups() {
  199. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  200. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
  201. ->disableOriginalConstructor()
  202. ->getMock();
  203. $backend->expects($this->at(0))
  204. ->method('userExists')
  205. ->willReturn(true);
  206. $backend->expects($this->at(3))
  207. ->method('getConfiguration')
  208. ->willReturn(array('ldap_base_groups'=>'ou=groups,dc=example,dc=org'));
  209. $backend->expects($this->any())
  210. ->method($this->anything())
  211. ->willReturnSelf();
  212. $server = $this->getServerMock($backend);
  213. $ldapProvider = $this->getLDAPProvider($server);
  214. $this->assertEquals('ou=groups,dc=example,dc=org', $ldapProvider->getLDAPBaseGroups('existing_user'));
  215. }
  216. /**
  217. * @expectedException \Exception
  218. * @expectedExceptionMessage User id not found in LDAP
  219. */
  220. public function testClearCacheUserIDNotFound() {
  221. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  222. ->setMethods(['userExists'])
  223. ->disableOriginalConstructor()
  224. ->getMock();
  225. $backend->expects($this->any())->method('userExists')->willReturn(false);
  226. $server = $this->getServerMock($backend);
  227. $ldapProvider = $this->getLDAPProvider($server);
  228. $ldapProvider->clearCache('nonexisting_user');
  229. }
  230. public function testClearCache() {
  231. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  232. ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $backend->expects($this->at(0))
  236. ->method('userExists')
  237. ->willReturn(true);
  238. $backend->expects($this->at(3))
  239. ->method('clearCache')
  240. ->willReturn(true);
  241. $backend->expects($this->any())
  242. ->method($this->anything())
  243. ->willReturnSelf();
  244. $server = $this->getServerMock($backend);
  245. $ldapProvider = $this->getLDAPProvider($server);
  246. $ldapProvider->clearCache('existing_user');
  247. $this->assertTrue(TRUE);
  248. }
  249. public function testDnExists() {
  250. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  251. ->setMethods(['dn2UserName'])
  252. ->disableOriginalConstructor()
  253. ->getMock();
  254. $backend->expects($this->any())
  255. ->method('dn2UserName')
  256. ->willReturn('existing_user');
  257. $server = $this->getServerMock($backend);
  258. $ldapProvider = $this->getLDAPProvider($server);
  259. $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
  260. }
  261. public function testFlagRecord() {
  262. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  263. ->setMethods([])
  264. ->disableOriginalConstructor()
  265. ->getMock();
  266. $server = $this->getServerMock($backend);
  267. $ldapProvider = $this->getLDAPProvider($server);
  268. $ldapProvider->flagRecord('existing_user');
  269. $this->assertTrue(TRUE);
  270. }
  271. public function testUnflagRecord() {
  272. $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
  273. ->setMethods([])
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $server = $this->getServerMock($backend);
  277. $ldapProvider = $this->getLDAPProvider($server);
  278. $ldapProvider->unflagRecord('existing_user');
  279. $this->assertTrue(TRUE);
  280. }
  281. }