ManagerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\Tests\User;
  8. use OCA\User_LDAP\Access;
  9. use OCA\User_LDAP\Connection;
  10. use OCA\User_LDAP\FilesystemHelper;
  11. use OCA\User_LDAP\ILDAPWrapper;
  12. use OCA\User_LDAP\User\Manager;
  13. use OCA\User_LDAP\User\User;
  14. use OCP\IAvatarManager;
  15. use OCP\IConfig;
  16. use OCP\IDBConnection;
  17. use OCP\Image;
  18. use OCP\IUserManager;
  19. use OCP\Notification\IManager as INotificationManager;
  20. use OCP\Share\IManager;
  21. use Psr\Log\LoggerInterface;
  22. /**
  23. * Class Test_User_Manager
  24. *
  25. * @group DB
  26. *
  27. * @package OCA\User_LDAP\Tests\User
  28. */
  29. class ManagerTest extends \Test\TestCase {
  30. /** @var Access|\PHPUnit\Framework\MockObject\MockObject */
  31. protected $access;
  32. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  33. protected $config;
  34. /** @var FilesystemHelper|\PHPUnit\Framework\MockObject\MockObject */
  35. protected $fileSystemHelper;
  36. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $logger;
  38. /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $avatarManager;
  40. /** @var Image|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $image;
  42. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $dbc;
  44. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  45. protected $ncUserManager;
  46. /** @var INotificationManager|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $notificationManager;
  48. /** @var ILDAPWrapper|\PHPUnit\Framework\MockObject\MockObject */
  49. protected $ldapWrapper;
  50. /** @var Connection */
  51. protected $connection;
  52. /** @var Manager */
  53. protected $manager;
  54. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  55. protected $shareManager;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->access = $this->createMock(Access::class);
  59. $this->config = $this->createMock(IConfig::class);
  60. $this->fileSystemHelper = $this->createMock(FilesystemHelper::class);
  61. $this->logger = $this->createMock(LoggerInterface::class);
  62. $this->avatarManager = $this->createMock(IAvatarManager::class);
  63. $this->image = $this->createMock(Image::class);
  64. $this->ncUserManager = $this->createMock(IUserManager::class);
  65. $this->notificationManager = $this->createMock(INotificationManager::class);
  66. $this->ldapWrapper = $this->createMock(ILDAPWrapper::class);
  67. $this->shareManager = $this->createMock(IManager::class);
  68. $this->connection = new Connection($this->ldapWrapper, '', null);
  69. $this->access->expects($this->any())
  70. ->method('getConnection')
  71. ->willReturn($this->connection);
  72. /** @noinspection PhpUnhandledExceptionInspection */
  73. $this->manager = new Manager(
  74. $this->config,
  75. $this->fileSystemHelper,
  76. $this->logger,
  77. $this->avatarManager,
  78. $this->image,
  79. $this->ncUserManager,
  80. $this->notificationManager,
  81. $this->shareManager
  82. );
  83. $this->manager->setLdapAccess($this->access);
  84. }
  85. public function dnProvider() {
  86. return [
  87. ['cn=foo,dc=foobar,dc=bar'],
  88. ['uid=foo,o=foobar,c=bar'],
  89. ['ab=cde,f=ghei,mno=pq'],
  90. ];
  91. }
  92. /**
  93. * @dataProvider dnProvider
  94. */
  95. public function testGetByDNExisting(string $inputDN) {
  96. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  97. $this->access->expects($this->once())
  98. ->method('stringResemblesDN')
  99. ->with($this->equalTo($inputDN))
  100. ->willReturn(true);
  101. $this->access->expects($this->once())
  102. ->method('dn2username')
  103. ->with($this->equalTo($inputDN))
  104. ->willReturn($uid);
  105. $this->access->expects($this->never())
  106. ->method('username2dn');
  107. /** @noinspection PhpUnhandledExceptionInspection */
  108. $this->manager->get($inputDN);
  109. // Now we fetch the user again. If this leads to a failing test,
  110. // runtime caching the manager is broken.
  111. /** @noinspection PhpUnhandledExceptionInspection */
  112. $user = $this->manager->get($inputDN);
  113. $this->assertInstanceOf(User::class, $user);
  114. }
  115. public function testGetByDNNotExisting() {
  116. $inputDN = 'cn=gone,dc=foobar,dc=bar';
  117. $this->access->expects($this->once())
  118. ->method('stringResemblesDN')
  119. ->with($this->equalTo($inputDN))
  120. ->willReturn(true);
  121. $this->access->expects($this->once())
  122. ->method('dn2username')
  123. ->with($this->equalTo($inputDN))
  124. ->willReturn(false);
  125. $this->access->expects($this->once())
  126. ->method('username2dn')
  127. ->with($this->equalTo($inputDN))
  128. ->willReturn(false);
  129. /** @noinspection PhpUnhandledExceptionInspection */
  130. $user = $this->manager->get($inputDN);
  131. $this->assertNull($user);
  132. }
  133. public function testGetByUidExisting() {
  134. $dn = 'cn=foo,dc=foobar,dc=bar';
  135. $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e';
  136. $this->access->expects($this->never())
  137. ->method('dn2username');
  138. $this->access->expects($this->once())
  139. ->method('username2dn')
  140. ->with($this->equalTo($uid))
  141. ->willReturn($dn);
  142. $this->access->expects($this->once())
  143. ->method('stringResemblesDN')
  144. ->with($this->equalTo($uid))
  145. ->willReturn(false);
  146. /** @noinspection PhpUnhandledExceptionInspection */
  147. $this->manager->get($uid);
  148. // Now we fetch the user again. If this leads to a failing test,
  149. // runtime caching the manager is broken.
  150. /** @noinspection PhpUnhandledExceptionInspection */
  151. $user = $this->manager->get($uid);
  152. $this->assertInstanceOf(User::class, $user);
  153. }
  154. public function testGetByUidNotExisting() {
  155. $uid = 'gone';
  156. $this->access->expects($this->never())
  157. ->method('dn2username');
  158. $this->access->expects($this->exactly(1))
  159. ->method('username2dn')
  160. ->with($this->equalTo($uid))
  161. ->willReturn(false);
  162. /** @noinspection PhpUnhandledExceptionInspection */
  163. $user = $this->manager->get($uid);
  164. $this->assertNull($user);
  165. }
  166. public function attributeRequestProvider() {
  167. return [
  168. [false],
  169. [true],
  170. ];
  171. }
  172. /**
  173. * @dataProvider attributeRequestProvider
  174. */
  175. public function testGetAttributes($minimal) {
  176. $this->connection->setConfiguration([
  177. 'ldapEmailAttribute' => 'MAIL',
  178. 'ldapUserAvatarRule' => 'default',
  179. 'ldapQuotaAttribute' => '',
  180. 'ldapUserDisplayName2' => 'Mail',
  181. ]);
  182. $attributes = $this->manager->getAttributes($minimal);
  183. $this->assertTrue(in_array('dn', $attributes));
  184. $this->assertTrue(in_array(strtolower($this->access->getConnection()->ldapEmailAttribute), $attributes));
  185. $this->assertTrue(!in_array($this->access->getConnection()->ldapEmailAttribute, $attributes)); #cases check
  186. $this->assertFalse(in_array('', $attributes));
  187. $this->assertSame(!$minimal, in_array('jpegphoto', $attributes));
  188. $this->assertSame(!$minimal, in_array('thumbnailphoto', $attributes));
  189. $valueCounts = array_count_values($attributes);
  190. $this->assertSame(1, $valueCounts['mail']);
  191. }
  192. }