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