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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
<?php
namespace Kanboard\Core\Ldap;
require_once __DIR__.'/../../Base.php';
function ldap_connect($hostname, $port)
{
return ClientTest::$functions->ldap_connect($hostname, $port);
}
function ldap_set_option()
{
}
function ldap_get_option($link_identifier, $option, &$error)
{
$error = 'some extended error';
}
function ldap_error($link_identifier)
{
return 'some error';
}
function ldap_errno($link_identifier)
{
return -100;
}
function ldap_bind($link_identifier, $bind_rdn = null, $bind_password = null)
{
return ClientTest::$functions->ldap_bind($link_identifier, $bind_rdn, $bind_password);
}
function ldap_start_tls($link_identifier)
{
return ClientTest::$functions->ldap_start_tls($link_identifier);
}
class ClientTest extends \Base
{
public static $functions;
public function setUp()
{
parent::setup();
self::$functions = $this
->getMockBuilder('stdClass')
->setMethods(array(
'ldap_connect',
'ldap_set_option',
'ldap_get_option',
'ldap_bind',
'ldap_start_tls',
'ldap_error',
'ldap_errno'
))
->getMock();
}
public function tearDown()
{
parent::tearDown();
self::$functions = null;
}
public function testGetLdapServerNotConfigured()
{
$this->setExpectedException('\LogicException');
$ldap = new Client;
$ldap->getLdapServer();
}
public function testConnectSuccess()
{
self::$functions
->expects($this->once())
->method('ldap_connect')
->with(
$this->equalTo('my_ldap_server'),
$this->equalTo(389)
)
->will($this->returnValue('my_ldap_resource'));
$ldap = new Client;
$ldap->open('my_ldap_server');
$this->assertEquals('my_ldap_resource', $ldap->getConnection());
}
public function testConnectFailure()
{
self::$functions
->expects($this->once())
->method('ldap_connect')
->with(
$this->equalTo('my_ldap_server'),
$this->equalTo(389)
)
->will($this->returnValue(false));
$this->setExpectedException('\Kanboard\Core\Ldap\ConnectionException');
$ldap = new Client;
$ldap->open('my_ldap_server');
$this->assertNotEquals('my_ldap_resource', $ldap->getConnection());
}
public function testConnectSuccessWithTLS()
{
self::$functions
->expects($this->once())
->method('ldap_connect')
->with(
$this->equalTo('my_ldap_server'),
$this->equalTo(389)
)
->will($this->returnValue('my_ldap_resource'));
self::$functions
->expects($this->once())
->method('ldap_start_tls')
->with(
$this->equalTo('my_ldap_resource')
)
->will($this->returnValue(true));
$ldap = new Client;
$ldap->open('my_ldap_server', 389, true);
$this->assertEquals('my_ldap_resource', $ldap->getConnection());
}
public function testConnectFailureWithTLS()
{
self::$functions
->expects($this->once())
->method('ldap_connect')
->with(
$this->equalTo('my_ldap_server'),
$this->equalTo(389)
)
->will($this->returnValue('my_ldap_resource'));
self::$functions
->expects($this->once())
->method('ldap_start_tls')
->with(
$this->equalTo('my_ldap_resource')
)
->will($this->returnValue(false));
$this->setExpectedException('\Kanboard\Core\Ldap\ConnectionException');
$ldap = new Client;
$ldap->open('my_ldap_server', 389, true);
$this->assertNotEquals('my_ldap_resource', $ldap->getConnection());
}
public function testAnonymousAuthenticationSuccess()
{
self::$functions
->expects($this->once())
->method('ldap_bind')
->will($this->returnValue(true));
$ldap = new Client;
$this->assertTrue($ldap->useAnonymousAuthentication());
}
public function testAnonymousAuthenticationFailure()
{
self::$functions
->expects($this->once())
->method('ldap_bind')
->will($this->returnValue(false));
$this->setExpectedException('\Kanboard\Core\Ldap\ClientException');
$ldap = new Client;
$ldap->useAnonymousAuthentication();
}
public function testUserAuthenticationSuccess()
{
self::$functions
->expects($this->once())
->method('ldap_connect')
->with(
$this->equalTo('my_ldap_server'),
$this->equalTo(389)
)
->will($this->returnValue('my_ldap_resource'));
self::$functions
->expects($this->once())
->method('ldap_bind')
->with(
$this->equalTo('my_ldap_resource'),
$this->equalTo('my_ldap_user'),
$this->equalTo('my_ldap_password')
)
->will($this->returnValue(true));
$ldap = new Client;
$ldap->open('my_ldap_server');
$this->assertTrue($ldap->authenticate('my_ldap_user', 'my_ldap_password'));
}
public function testUserAuthenticationFailure()
{
self::$functions
->expects($this->once())
->method('ldap_connect')
->with(
$this->equalTo('my_ldap_server'),
$this->equalTo(389)
)
->will($this->returnValue('my_ldap_resource'));
self::$functions
->expects($this->once())
->method('ldap_bind')
->with(
$this->equalTo('my_ldap_resource'),
$this->equalTo('my_ldap_user'),
$this->equalTo('my_ldap_password')
)
->will($this->returnValue(false));
$this->setExpectedException('\Kanboard\Core\Ldap\ClientException');
$ldap = new Client;
$ldap->open('my_ldap_server');
$ldap->authenticate('my_ldap_user', 'my_ldap_password');
}
}
|