2
2
3
3
namespace Adldap \Laravel \Tests ;
4
4
5
- use Adldap \Auth \Guard ;
6
- use Adldap \Connections \Manager ;
7
- use Adldap \Connections \Provider ;
5
+ use Adldap \Models \User ;
6
+ use Adldap \Connections \Ldap ;
8
7
use Adldap \Contracts \AdldapInterface ;
9
- use Adldap \Contracts \Connections \ConnectionInterface ;
10
8
use Adldap \Laravel \Facades \Adldap ;
11
9
use Adldap \Laravel \Tests \Models \User as EloquentUser ;
12
- use Adldap \Models \User ;
13
- use Adldap \Query \Builder ;
14
- use Adldap \Schemas \Schema ;
15
- use Adldap \Search \Factory ;
16
10
use Illuminate \Support \Facades \App ;
17
11
use Illuminate \Support \Facades \Auth ;
18
12
use Illuminate \Support \Facades \Hash ;
19
13
20
14
class AdldapTest extends FunctionalTestCase
21
15
{
22
- public function setUp ()
23
- {
24
- parent ::setUp ();
25
-
26
- // Set auth configuration for email use since stock
27
- // laravel only comes with an email field
28
- $ this ->app ['config ' ]->set ('adldap_auth.username_attribute ' , [
29
- 'email ' => 'mail ' ,
30
- ]);
31
-
32
- // Set auto_connect to false.
33
- $ this ->app ['config ' ]->set ('adldap.connections.default.auto_connect ' , false );
34
- }
35
-
36
16
public function test_configuration_not_found_exception ()
37
17
{
38
18
$ this ->app ['config ' ]->set ('adldap ' , null );
@@ -59,7 +39,29 @@ public function test_auth_passes($credentials = null)
59
39
{
60
40
$ credentials =
$ credentials ?: [
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ];
61
41
62
- $ this ->getMockAuth ()->shouldReceive ('attempt ' )->once ()->andReturn (true );
42
+ $ user = $ this ->getMockUser ([
43
+ 'cn ' => '' ,
44
+
45
+ 'samaccountname ' => 'jdoe ' ,
46
+ ]);
47
+
48
+ $ connection = $ this ->getMockConnection ();
49
+
50
+ $ connection ->expects ($ this ->exactly (2 ))->method ('isBound ' )->willReturn (true );
51
+
52
+ $ connection ->expects ($ this ->exactly (1 ))->method ('search ' )->willReturn ('resource ' );
53
+
54
+ $ connection ->expects ($ this ->exactly (1 ))->method ('getEntries ' )->willReturn ([
55
+ 'count ' => 1 ,
56
+ $ user ->getAttributes (),
57
+ ]);
58
+
59
+ $ connection ->expects ($ this ->exactly (2 ))->method ('bind ' )
60
+ ->with ($ this ->logicalOr (
61
+ $ this ->equalTo ('jdoe ' ),
62
+ $ this ->equalTo ('admin ' )
63
+ ))
64
+ ->willReturn (true );
63
65
64
66
$ this ->assertTrue (Auth::attempt ($ credentials ));
65
67
@@ -73,8 +75,7 @@ public function test_auth_passes_with_persistent_adldap_user()
73
75
{
74
76
$ this ->test_auth_passes ();
75
77
76
- $ this ->assertInstanceOf (User::class, \Auth::user ()->adldapUser );
77
- $ this ->assertInstanceOf (User::class, auth ()->user ()->adldapUser );
78
+ $ this ->assertInstanceOf (User::class, Auth::user ()->adldapUser );
78
79
}
79
80
80
81
public function test_auth_passes_without_persistent_adldap_user ()
@@ -83,72 +84,60 @@ public function test_auth_passes_without_persistent_adldap_user()
83
84
84
85
$ this ->test_auth_passes ();
85
86
86
- $ this ->assertNull (\Auth::user ()->adldapUser );
87
- $ this ->assertNull (auth ()->user ()->adldapUser );
87
+ $ this ->assertNull (Auth::user ()->adldapUser );
88
88
}
89
89
90
- public function test_auth_fails ()
90
+ public function test_auth_fails_when_user_found ()
91
91
{
92
- $ this ->getMockAuth ()->shouldReceive ('attempt ' )->once ()->andReturn (false );
93
-
94
- $ this ->
assertFalse (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]));
95
- }
96
-
97
- public function test_auth_fails_when_user_not_found ()
98
- {
99
- $ mockedProvider = $ this ->mock (Provider::class);
100
- $ mockedBuilder = $ this ->mock (Builder::class);
101
- $ mockedSearch = $ this ->mock (Factory::class);
102
- $ mockedConnection = $ this ->mock (ConnectionInterface::class);
92
+ $ user = $ this ->getMockUser ([
93
+ 'cn ' => '' ,
94
+
95
+ 'samaccountname ' => 'jdoe ' ,
96
+ ]);
103
97
104
- $ mockedConnection -> shouldReceive ( ' isBound ' )-> once ()-> andReturn ( true );
98
+ $ connection = $ this -> getMockConnection ([ ' getLastError ' , ' errNo ' ] );
105
99
106
- $ manager = new Manager ( );
100
+ $ connection -> expects ( $ this -> exactly ( 2 ))-> method ( ' isBound ' )-> willReturn ( true );
107
101
108
- $ manager -> add ( ' default ' , $ mockedProvider );
102
+ $ connection -> expects ( $ this -> exactly ( 1 ))-> method ( ' search ' )-> willReturn ( ' resource ' );
109
103
110
- Adldap::shouldReceive ('getManager ' )->andReturn ($ manager );
104
+ $ connection ->expects ($ this ->exactly (1 ))->method ('getEntries ' )->willReturn ([
105
+ 'count ' => 1 ,
106
+ $ user ->getAttributes (),
107
+ ]);
111
108
112
- $ mockedProvider -> shouldReceive ( ' search ' )-> once ( )->andReturn ( $ mockedSearch );
113
- $ mockedProvider -> shouldReceive ( ' getSchema ' )->andReturn (Schema:: get () );
109
+ $ connection -> expects ( $ this -> exactly ( 1 ))-> method ( ' getLastError ' )->willReturn ( ' Bind Failure. ' );
110
+ $ connection -> expects ( $ this -> exactly ( 1 ))-> method ( ' errNo ' )->willReturn ( 1 );
114
111
115
- $ mockedSearch ->shouldReceive ('users ' )->once ()->andReturn ($ mockedSearch );
116
- $ mockedSearch ->shouldReceive ('select ' )->once ()->andReturn ($ mockedBuilder );
117
- $ mockedBuilder ->shouldReceive ('getConnection ' )->once ()->andReturn ($ mockedConnection );
118
- $ mockedBuilder ->shouldReceive ('where ' )->once ()->andReturn ($ mockedBuilder );
119
- $ mockedBuilder ->shouldReceive ('first ' )->once ()->andReturn (null );
112
+ $ connection ->expects ($ this ->exactly (1 ))->method ('bind ' )
113
+ ->with ($ this ->equalTo ('jdoe ' ))
114
+ ->willReturn (false );
120
115
121
116
$ this ->
assertFalse (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]));
122
117
}
123
118
124
- public function test_credentials_key_does_not_exist ()
119
+ public function test_auth_fails_when_user_not_found ()
125
120
{
126
- $ mockedProvider = $ this ->mock (Provider::class);
127
- $ mockedSearch = $ this ->mock (Factory::class);
128
- $ mockedBuilder = $ this ->mock (Builder::class);
129
- $ mockedConnection = $ this ->mock (ConnectionInterface::class);
130
-
131
- $ mockedConnection ->shouldReceive ('isBound ' )->once ()->andReturn (true );
132
-
133
- $ mockedBuilder ->shouldReceive ('getConnection ' )->once ()->andReturn ($ mockedConnection );
121
+ $ connection = $ this ->getMockConnection ();
134
122
135
- $ mockedSearch -> shouldReceive ( ' select ' )-> once ( )->andReturn ( $ mockedBuilder );
123
+ $ connection -> expects ( $ this -> exactly ( 1 ))-> method ( ' isBound ' )->willReturn ( true );
136
124
137
- $ manager = new Manager ( );
125
+ $ connection -> expects ( $ this -> exactly ( 1 ))-> method ( ' search ' )-> willReturn ( ' resource ' );
138
126
139
- $ manager -> add ( ' default ' , $ mockedProvider );
140
-
141
- Adldap:: shouldReceive ( ' getManager ' )-> andReturn ( $ manager );
127
+ $ connection -> expects ( $ this -> exactly ( 1 ))-> method ( ' getEntries ' )-> willReturn ([
128
+ ' count ' => 0 ,
129
+ ] );
142
130
143
- $ mockedProvider ->shouldReceive ('search ' )->once ()->andReturn ($ mockedSearch );
144
- $ mockedSearch ->shouldReceive ('users ' )->once ()->andReturn ($ mockedSearch );
145
- $ mockedProvider ->shouldReceive ('getSchema ' )->andReturn (Schema::get ());
131
+ $ this ->
assertFalse (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]));
132
+ }
146
133
147
- $ nonExistantInputKey = 'non-existent-key ' ;
134
+ public function test_credentials_key_does_not_exist ()
135
+ {
136
+ $ connection = $ this ->getMockConnection ();
148
137
149
- $ this ->setExpectedException ( ' ErrorException ' );
138
+ $ connection -> expects ( $ this ->exactly ( 1 ))-> method ( ' isBound ' )-> willReturn ( true );
150
139
151
- Auth::
attempt ([
$ nonExistantInputKey =>
'[email protected] ' ,
'password ' =>
'12345 ' ]);
140
+ $ this -> assertFalse ( Auth::
attempt ([
' non-existent-key ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]
) );
152
141
}
153
142
154
143
public function test_config_callback_attribute_handler ()
@@ -168,26 +157,6 @@ public function test_config_login_fallback()
168
157
{
169
158
$ this ->app ['config ' ]->set ('adldap_auth.login_fallback ' , true );
170
159
171
- $ mockedProvider = $ this ->mock (Provider::class);
172
- $ mockedSearch = $ this ->mock (Factory::class);
173
- $ mockedConnection = $ this ->mock (ConnectionInterface::class);
174
-
175
- $ mockedConnection ->shouldReceive ('isBound ' )->twice ()->andReturn (true );
176
-
177
- $ mockedSearch ->shouldReceive ('users ' )->twice ()->andReturn ($ mockedSearch );
178
- $ mockedSearch ->shouldReceive ('select ' )->twice ()->andReturn ($ mockedSearch );
179
- $ mockedSearch ->shouldReceive ('getConnection ' )->twice ()->andReturn ($ mockedConnection );
180
- $ mockedSearch ->shouldReceive ('where ' )->twice ()->andReturn ($ mockedSearch );
181
- $ mockedSearch ->shouldReceive ('first ' )->twice ()->andReturn (null );
182
-
183
- $ manager = new Manager ();
184
-
185
- $ manager ->add ('default ' , $ mockedProvider );
186
- $ mockedProvider ->shouldReceive ('search ' )->twice ()->andReturn ($ mockedSearch );
187
- $ mockedProvider ->shouldReceive ('getSchema ' )->twice ()->andReturn (Schema::get ());
188
-
189
- Adldap::shouldReceive ('getManager ' )->andReturn ($ manager );
190
-
191
160
EloquentUser::create ([
192
161
193
162
'name ' => 'John Doe ' ,
@@ -215,22 +184,9 @@ public function test_config_login_fallback_no_connection()
215
184
{
216
185
$ this ->app ['config ' ]->set ('adldap_auth.login_fallback ' , true );
217
186
218
- $ mockedProvider = $ this ->mock (Provider::class);
219
- $ mockedSearch = $ this ->mock (Factory::class);
220
- $ mockedConnection = $ this ->mock (ConnectionInterface::class);
221
-
222
- $ mockedConnection ->shouldReceive ('isBound ' )->once ()->andReturn (false );
187
+ $ connection = $ this ->getMockConnection ();
223
188
224
- $ mockedSearch ->shouldReceive ('select ' )->once ()->andReturn ($ mockedSearch );
225
- $ mockedSearch ->shouldReceive ('users ' )->once ()->andReturn ($ mockedSearch );
226
- $ mockedSearch ->shouldReceive ('getConnection ' )->once ()->andReturn ($ mockedConnection );
227
-
228
- $ manager = new Manager ();
229
-
230
- $ manager ->add ('default ' , $ mockedProvider );
231
- $ mockedProvider ->shouldReceive ('search ' )->once ()->andReturn ($ mockedSearch );
232
-
233
- Adldap::shouldReceive ('getManager ' )->andReturn ($ manager );
189
+ $ connection ->expects ($ this ->exactly (1 ))->method ('isBound ' )->willReturn (false );
234
190
235
191
EloquentUser::create ([
236
192
@@ -255,12 +211,10 @@ public function test_config_password_sync_enabled()
255
211
{
256
212
$ this ->app ['config ' ]->set ('adldap_auth.password_sync ' , true );
257
213
258
- $ this ->getMockAuth ()->shouldReceive ('attempt ' )->once ()->andReturn (true );
259
-
260
-
214
+
261
215
$ password = '12345 ' ;
262
216
263
- $ this ->assertTrue (Auth:: attempt ( compact ('email ' , 'password ' ) ));
217
+ $ this ->test_auth_passes ( compact ('email ' , 'password ' ));
264
218
265
219
$ user = EloquentUser::first ();
266
220
@@ -274,60 +228,57 @@ public function test_config_password_sync_disabled()
274
228
{
275
229
$ this ->app ['config ' ]->set ('adldap_auth.password_sync ' , false );
276
230
277
- $ this ->getMockAuth ()->shouldReceive ('attempt ' )->once ()->andReturn (true );
278
-
279
-
280
- $ password = '12345 ' ;
281
-
282
- $ this ->assertTrue (Auth::attempt (compact ('email ' , 'password ' )));
283
-
284
- $ user = EloquentUser::first ();
285
-
286
- $ this ->assertInstanceOf (EloquentUser::class, $ user );
287
-
288
- // This check will fail due to password synchronization being disabled.
289
- $ this ->assertFalse (Hash::check ($ password , $ user ->password ));
290
- }
291
-
292
- protected function getMockAuth (User $ user = null )
293
- {
294
- $ mockedProvider = $ this ->mock (Provider::class);
295
- $ mockedBuilder = $ this ->mock (Builder::class);
296
- $ mockedSearch = $ this ->mock (Factory::class);
297
- $ mockedAuth = $ this ->mock (Guard::class);
298
- $ mockedConnection = $ this ->mock (ConnectionInterface::class);
231
+ $ user = $ this ->getMockUser ([
232
+ 'cn ' => '' ,
233
+
234
+ 'samaccountname ' => 'jdoe ' ,
235
+ ]);
299
236
300
- $ mockedConnection -> shouldReceive ( ' isBound ' )-> once ()-> andReturn ( true );
237
+ $ connection = $ this -> getMockConnection ( );
301
238
302
- $ mockedBuilder ->shouldReceive ('getSchema ' )->once ()->andReturn (Schema::get ());
303
- $ mockedBuilder ->shouldReceive ('getConnection ' )->once ()->andReturn ($ mockedConnection );
239
+ $ connection ->expects ($ this ->exactly (2 ))->method ('isBound ' )->willReturn (true );
304
240
305
- $ manager = new Manager ( );
241
+ $ connection -> expects ( $ this -> exactly ( 1 ))-> method ( ' search ' )-> willReturn ( ' resource ' );
306
242
307
- $ manager ->add ('default ' , $ mockedProvider );
243
+ $ connection ->expects ($ this ->exactly (1 ))->method ('getEntries ' )->willReturn ([
244
+ 'count ' => 1 ,
245
+ $ user ->getAttributes (),
246
+ ]);
308
247
309
- Adldap::shouldReceive ('getManager ' )->andReturn ($ manager );
248
+ $ connection ->expects ($ this ->exactly (2 ))->method ('bind ' )
249
+ ->with ($ this ->logicalOr (
250
+ $ this ->equalTo ('jdoe ' ),
251
+ $ this ->equalTo ('admin ' )
252
+ ))
253
+ ->willReturn (true );
310
254
311
- $ mockedProvider ->shouldReceive ('search ' )->once ()->andReturn ($ mockedSearch );
312
- $ mockedProvider ->shouldReceive ('getSchema ' )->andReturn (Schema::get ());
313
- $ mockedProvider ->shouldReceive ('auth ' )->once ()->andReturn ($ mockedAuth );
255
+
256
+ $ password = '12345 ' ;
314
257
315
- $ mockedSearch ->shouldReceive ('users ' )->once ()->andReturn ($ mockedSearch );
316
- $ mockedSearch ->shouldReceive ('select ' )->once ()->andReturn ($ mockedBuilder );
317
- $ mockedBuilder ->shouldReceive ('where ' )->once ()->andReturn ($ mockedBuilder );
318
- $ mockedBuilder ->shouldReceive ('first ' )->once ()->andReturn ($ user ?: $ this ->getMockUser ($ mockedBuilder ));
258
+ $ this ->assertTrue (Auth::attempt (compact ('email ' , 'password ' )));
319
259
320
- return $ mockedAuth ;
260
+ $ user = Auth::user ();
261
+
262
+ // This check will fail due to password synchronization being disabled.
263
+ $ this ->assertFalse (Hash::check ($ password , $ user ->password ));
321
264
}
322
265
323
- protected function getMockUser ($ builder , array $ attributes = [])
266
+ protected function getMockUser (array $ attributes = [])
324
267
{
325
- $ attributes = array_merge ( $ attributes, [
268
+ return Adldap:: getDefaultProvider ()-> make ()-> user ( $ attributes ?: [
326
269
'samaccountname ' => ['jdoe ' ],
327
270
328
271
'cn ' => ['John Doe ' ],
329
272
]);
273
+ }
274
+
275
+ protected function getMockConnection ($ methods = [])
276
+ {
277
+ $ defaults = ['isBound ' , 'search ' , 'getEntries ' , 'bind ' , 'close ' ];
278
+ $ connection = $ this ->getMock (Ldap::class, array_merge ($ defaults , $ methods ));
279
+
280
+ $ this ->app ['adldap ' ]->getDefaultProvider ()->setConnection ($ connection );
330
281
331
- return ( new User ([], $ builder ))-> setRawAttributes ( $ attributes ) ;
282
+ return $ connection ;
332
283
}
333
284
}
0 commit comments