ManagerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Philippe Jung <phil.jung@free.fr>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP\Tests\User;
  27. use OCA\User_LDAP\FilesystemHelper;
  28. use OCA\User_LDAP\ILDAPWrapper;
  29. use OCA\User_LDAP\LogWrapper;
  30. use OCA\User_LDAP\User\IUserTools;
  31. use OCA\User_LDAP\User\Manager;
  32. use OCP\IAvatarManager;
  33. use OCP\IConfig;
  34. use OCP\IDBConnection;
  35. use OCP\Image;
  36. use OCP\IUserManager;
  37. /**
  38. * Class Test_User_Manager
  39. *
  40. * @group DB
  41. *
  42. * @package OCA\User_LDAP\Tests\User
  43. */
  44. class ManagerTest extends \Test\TestCase {
  45. private function getTestInstances() {
  46. $access = $this->createMock(IUserTools::class);
  47. $config = $this->createMock(IConfig::class);
  48. $filesys = $this->createMock(FilesystemHelper::class);
  49. $log = $this->createMock(LogWrapper::class);
  50. $avaMgr = $this->createMock(IAvatarManager::class);
  51. $image = $this->createMock(Image::class);
  52. $dbc = $this->createMock(IDBConnection::class);
  53. $userMgr = $this->createMock(IUserManager::class);
  54. $connection = new \OCA\User_LDAP\Connection(
  55. $lw = $this->createMock(ILDAPWrapper::class),
  56. '',
  57. null
  58. );
  59. $access->expects($this->any())
  60. ->method('getConnection')
  61. ->will($this->returnValue($connection));
  62. return array($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr);
  63. }
  64. public function testGetByDNExisting() {
  65. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  66. $this->getTestInstances();
  67. $inputDN = 'cn=foo,dc=foobar,dc=bar';
  68. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  69. $access->expects($this->once())
  70. ->method('stringResemblesDN')
  71. ->with($this->equalTo($inputDN))
  72. ->will($this->returnValue(true));
  73. $access->expects($this->once())
  74. ->method('dn2username')
  75. ->with($this->equalTo($inputDN))
  76. ->will($this->returnValue($uid));
  77. $access->expects($this->never())
  78. ->method('username2dn');
  79. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  80. $manager->setLdapAccess($access);
  81. $user = $manager->get($inputDN);
  82. // Now we fetch the user again. If this leads to a failing test,
  83. // runtime caching the manager is broken.
  84. $user = $manager->get($inputDN);
  85. $this->assertInstanceOf('\OCA\User_LDAP\User\User', $user);
  86. }
  87. public function testGetByEDirectoryDN() {
  88. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  89. $this->getTestInstances();
  90. $inputDN = 'uid=foo,o=foobar,c=bar';
  91. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  92. $access->expects($this->once())
  93. ->method('stringResemblesDN')
  94. ->with($this->equalTo($inputDN))
  95. ->will($this->returnValue(true));
  96. $access->expects($this->once())
  97. ->method('dn2username')
  98. ->with($this->equalTo($inputDN))
  99. ->will($this->returnValue($uid));
  100. $access->expects($this->never())
  101. ->method('username2dn');
  102. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  103. $manager->setLdapAccess($access);
  104. $user = $manager->get($inputDN);
  105. $this->assertInstanceOf('\OCA\User_LDAP\User\User', $user);
  106. }
  107. public function testGetByExoticDN() {
  108. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  109. $this->getTestInstances();
  110. $inputDN = 'ab=cde,f=ghei,mno=pq';
  111. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  112. $access->expects($this->once())
  113. ->method('stringResemblesDN')
  114. ->with($this->equalTo($inputDN))
  115. ->will($this->returnValue(true));
  116. $access->expects($this->once())
  117. ->method('dn2username')
  118. ->with($this->equalTo($inputDN))
  119. ->will($this->returnValue($uid));
  120. $access->expects($this->never())
  121. ->method('username2dn');
  122. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  123. $manager->setLdapAccess($access);
  124. $user = $manager->get($inputDN);
  125. $this->assertInstanceOf('\OCA\User_LDAP\User\User', $user);
  126. }
  127. public function testGetByDNNotExisting() {
  128. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  129. $this->getTestInstances();
  130. $inputDN = 'cn=gone,dc=foobar,dc=bar';
  131. $access->expects($this->once())
  132. ->method('stringResemblesDN')
  133. ->with($this->equalTo($inputDN))
  134. ->will($this->returnValue(true));
  135. $access->expects($this->once())
  136. ->method('dn2username')
  137. ->with($this->equalTo($inputDN))
  138. ->will($this->returnValue(false));
  139. $access->expects($this->once())
  140. ->method('username2dn')
  141. ->with($this->equalTo($inputDN))
  142. ->will($this->returnValue(false));
  143. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  144. $manager->setLdapAccess($access);
  145. $user = $manager->get($inputDN);
  146. $this->assertNull($user);
  147. }
  148. public function testGetByUidExisting() {
  149. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  150. $this->getTestInstances();
  151. $dn = 'cn=foo,dc=foobar,dc=bar';
  152. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  153. $access->expects($this->never())
  154. ->method('dn2username');
  155. $access->expects($this->once())
  156. ->method('username2dn')
  157. ->with($this->equalTo($uid))
  158. ->will($this->returnValue($dn));
  159. $access->expects($this->once())
  160. ->method('stringResemblesDN')
  161. ->with($this->equalTo($uid))
  162. ->will($this->returnValue(false));
  163. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  164. $manager->setLdapAccess($access);
  165. $user = $manager->get($uid);
  166. // Now we fetch the user again. If this leads to a failing test,
  167. // runtime caching the manager is broken.
  168. $user = $manager->get($uid);
  169. $this->assertInstanceOf('\OCA\User_LDAP\User\User', $user);
  170. }
  171. public function testGetByUidNotExisting() {
  172. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  173. $this->getTestInstances();
  174. $uid = 'gone';
  175. $access->expects($this->never())
  176. ->method('dn2username');
  177. $access->expects($this->exactly(1))
  178. ->method('username2dn')
  179. ->with($this->equalTo($uid))
  180. ->will($this->returnValue(false));
  181. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  182. $manager->setLdapAccess($access);
  183. $user = $manager->get($uid);
  184. $this->assertNull($user);
  185. }
  186. public function testGetAttributesAll() {
  187. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  188. $this->getTestInstances();
  189. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  190. $manager->setLdapAccess($access);
  191. $connection = $access->getConnection();
  192. $connection->setConfiguration(array('ldapEmailAttribute' => 'mail'));
  193. $attributes = $manager->getAttributes();
  194. $this->assertTrue(in_array('dn', $attributes));
  195. $this->assertTrue(in_array($access->getConnection()->ldapEmailAttribute, $attributes));
  196. $this->assertTrue(in_array('jpegphoto', $attributes));
  197. $this->assertTrue(in_array('thumbnailphoto', $attributes));
  198. }
  199. public function testGetAttributesMinimal() {
  200. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  201. $this->getTestInstances();
  202. $manager = new Manager($config, $filesys, $log, $avaMgr, $image, $dbc, $userMgr);
  203. $manager->setLdapAccess($access);
  204. $attributes = $manager->getAttributes(true);
  205. $this->assertTrue(in_array('dn', $attributes));
  206. $this->assertTrue(!in_array('jpegphoto', $attributes));
  207. $this->assertTrue(!in_array('thumbnailphoto', $attributes));
  208. }
  209. }