ManagerTest.php 7.5 KB

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