ManagerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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 Morris Jobke <hey@morrisjobke.de>
  9. * @author Philippe Jung <phil.jung@free.fr>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Roger Szabo <roger.szabo@web.de>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  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, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\User_LDAP\Tests\User;
  30. use OCA\User_LDAP\Access;
  31. use OCA\User_LDAP\Connection;
  32. use OCA\User_LDAP\FilesystemHelper;
  33. use OCA\User_LDAP\ILDAPWrapper;
  34. use OCA\User_LDAP\LogWrapper;
  35. use OCA\User_LDAP\User\Manager;
  36. use OCA\User_LDAP\User\User;
  37. use OCP\IAvatarManager;
  38. use OCP\IConfig;
  39. use OCP\IDBConnection;
  40. use OCP\Image;
  41. use OCP\IUserManager;
  42. use OCP\Notification\IManager as INotificationManager;
  43. /**
  44. * Class Test_User_Manager
  45. *
  46. * @group DB
  47. *
  48. * @package OCA\User_LDAP\Tests\User
  49. */
  50. class ManagerTest extends \Test\TestCase {
  51. /** @var Access|\PHPUnit_Framework_MockObject_MockObject */
  52. protected $access;
  53. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  54. protected $config;
  55. /** @var FilesystemHelper|\PHPUnit_Framework_MockObject_MockObject */
  56. protected $fileSystemHelper;
  57. /** @var LogWrapper|\PHPUnit_Framework_MockObject_MockObject */
  58. protected $log;
  59. /** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */
  60. protected $avatarManager;
  61. /** @var Image|\PHPUnit_Framework_MockObject_MockObject */
  62. protected $image;
  63. /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
  64. protected $dbc;
  65. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  66. protected $ncUserManager;
  67. /** @var INotificationManager|\PHPUnit_Framework_MockObject_MockObject */
  68. protected $notificationManager;
  69. /** @var ILDAPWrapper|\PHPUnit_Framework_MockObject_MockObject */
  70. protected $ldapWrapper;
  71. /** @var Connection */
  72. protected $connection;
  73. /** @var Manager */
  74. protected $manager;
  75. protected function setUp(): void {
  76. parent::setUp();
  77. $this->access = $this->createMock(Access::class);
  78. $this->config = $this->createMock(IConfig::class);
  79. $this->fileSystemHelper = $this->createMock(FilesystemHelper::class);
  80. $this->log = $this->createMock(LogWrapper::class);
  81. $this->avatarManager = $this->createMock(IAvatarManager::class);
  82. $this->image = $this->createMock(Image::class);
  83. $this->dbc = $this->createMock(IDBConnection::class);
  84. $this->ncUserManager = $this->createMock(IUserManager::class);
  85. $this->notificationManager = $this->createMock(INotificationManager::class);
  86. $this->ldapWrapper = $this->createMock(ILDAPWrapper::class);
  87. $this->connection = new Connection($this->ldapWrapper, '', null);
  88. $this->access->expects($this->any())
  89. ->method('getConnection')
  90. ->willReturn($this->connection);
  91. /** @noinspection PhpUnhandledExceptionInspection */
  92. $this->manager = new Manager(
  93. $this->config,
  94. $this->fileSystemHelper,
  95. $this->log,
  96. $this->avatarManager,
  97. $this->image,
  98. $this->dbc,
  99. $this->ncUserManager,
  100. $this->notificationManager
  101. );
  102. $this->manager->setLdapAccess($this->access);
  103. }
  104. public function dnProvider() {
  105. return [
  106. ['cn=foo,dc=foobar,dc=bar'],
  107. ['uid=foo,o=foobar,c=bar'],
  108. ['ab=cde,f=ghei,mno=pq'],
  109. ];
  110. }
  111. /**
  112. * @dataProvider dnProvider
  113. */
  114. public function testGetByDNExisting(string $inputDN) {
  115. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  116. $this->access->expects($this->once())
  117. ->method('stringResemblesDN')
  118. ->with($this->equalTo($inputDN))
  119. ->willReturn(true);
  120. $this->access->expects($this->once())
  121. ->method('dn2username')
  122. ->with($this->equalTo($inputDN))
  123. ->willReturn($uid);
  124. $this->access->expects($this->never())
  125. ->method('username2dn');
  126. /** @noinspection PhpUnhandledExceptionInspection */
  127. $this->manager->get($inputDN);
  128. // Now we fetch the user again. If this leads to a failing test,
  129. // runtime caching the manager is broken.
  130. /** @noinspection PhpUnhandledExceptionInspection */
  131. $user = $this->manager->get($inputDN);
  132. $this->assertInstanceOf(User::class, $user);
  133. }
  134. public function testGetByDNNotExisting() {
  135. $inputDN = 'cn=gone,dc=foobar,dc=bar';
  136. $this->access->expects($this->once())
  137. ->method('stringResemblesDN')
  138. ->with($this->equalTo($inputDN))
  139. ->willReturn(true);
  140. $this->access->expects($this->once())
  141. ->method('dn2username')
  142. ->with($this->equalTo($inputDN))
  143. ->willReturn(false);
  144. $this->access->expects($this->once())
  145. ->method('username2dn')
  146. ->with($this->equalTo($inputDN))
  147. ->willReturn(false);
  148. /** @noinspection PhpUnhandledExceptionInspection */
  149. $user = $this->manager->get($inputDN);
  150. $this->assertNull($user);
  151. }
  152. public function testGetByUidExisting() {
  153. $dn = 'cn=foo,dc=foobar,dc=bar';
  154. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  155. $this->access->expects($this->never())
  156. ->method('dn2username');
  157. $this->access->expects($this->once())
  158. ->method('username2dn')
  159. ->with($this->equalTo($uid))
  160. ->willReturn($dn);
  161. $this->access->expects($this->once())
  162. ->method('stringResemblesDN')
  163. ->with($this->equalTo($uid))
  164. ->willReturn(false);
  165. /** @noinspection PhpUnhandledExceptionInspection */
  166. $this->manager->get($uid);
  167. // Now we fetch the user again. If this leads to a failing test,
  168. // runtime caching the manager is broken.
  169. /** @noinspection PhpUnhandledExceptionInspection */
  170. $user = $this->manager->get($uid);
  171. $this->assertInstanceOf(User::class, $user);
  172. }
  173. public function testGetByUidNotExisting() {
  174. $uid = 'gone';
  175. $this->access->expects($this->never())
  176. ->method('dn2username');
  177. $this->access->expects($this->exactly(1))
  178. ->method('username2dn')
  179. ->with($this->equalTo($uid))
  180. ->willReturn(false);
  181. /** @noinspection PhpUnhandledExceptionInspection */
  182. $user = $this->manager->get($uid);
  183. $this->assertNull($user);
  184. }
  185. public function attributeRequestProvider() {
  186. return [
  187. [false],
  188. [true],
  189. ];
  190. }
  191. /**
  192. * @dataProvider attributeRequestProvider
  193. */
  194. public function testGetAttributes($minimal) {
  195. $this->connection->setConfiguration([
  196. 'ldapEmailAttribute' => 'MAIL',
  197. 'ldapUserAvatarRule' => 'default',
  198. 'ldapQuotaAttribute' => '',
  199. 'ldapUserDisplayName2' => 'Mail',
  200. ]);
  201. $attributes = $this->manager->getAttributes($minimal);
  202. $this->assertTrue(in_array('dn', $attributes));
  203. $this->assertTrue(in_array(strtolower($this->access->getConnection()->ldapEmailAttribute), $attributes));
  204. $this->assertTrue(!in_array($this->access->getConnection()->ldapEmailAttribute, $attributes)); #cases check
  205. $this->assertFalse(in_array('', $attributes));
  206. $this->assertSame(!$minimal, in_array('jpegphoto', $attributes));
  207. $this->assertSame(!$minimal, in_array('thumbnailphoto', $attributes));
  208. $valueCounts = array_count_values($attributes);
  209. $this->assertSame(1, $valueCounts['mail']);
  210. }
  211. }