2
2
3
3
namespace Adldap \Laravel \Tests ;
4
4
5
+ use Adldap \Laravel \AdldapAuthServiceProvider ;
6
+ use Mockery as m ;
5
7
use Adldap \Auth \Guard ;
6
8
use Adldap \Connections \Manager ;
7
9
use Adldap \Connections \Provider ;
15
17
use Adldap \Search \Factory ;
16
18
use Illuminate \Support \Facades \App ;
17
19
use Illuminate \Support \Facades \Auth ;
20
+ use Illuminate \Support \Facades \Hash ;
18
21
19
22
class AdldapTest extends FunctionalTestCase
20
23
{
@@ -54,47 +57,18 @@ public function test_contract_resolve()
54
57
$ this ->assertInstanceOf (AdldapInterface::class, $ adldap );
55
58
}
56
59
57
- public function test_auth_passes ()
60
+ public function test_auth_passes ($ credentials = null )
58
61
{
59
- $ mockedProvider = $ this ->mock (Provider::class);
60
- $ mockedBuilder = $ this ->mock (Builder::class);
61
- $ mockedSearch = $ this ->mock (Factory::class);
62
- $ mockedAuth = $ this ->mock (Guard::class);
63
- $ mockedConnection = $ this ->mock (ConnectionInterface::class);
62
+ $ credentials =
$ credentials ?: [
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ];
64
63
65
- $ mockedConnection -> shouldReceive ('isBound ' )->once ()->andReturn (true );
64
+ $ this -> getMockAuth ()-> shouldReceive ('attempt ' )->once ()->andReturn (true );
66
65
67
- $ mockedBuilder ->shouldReceive ('getSchema ' )->once ()->andReturn (Schema::get ());
68
- $ mockedBuilder ->shouldReceive ('getConnection ' )->once ()->andReturn ($ mockedConnection );
69
-
70
- $ adUser = (new User ([], $ mockedBuilder ))->setRawAttributes ([
71
- 'samaccountname ' => ['jdoe ' ],
72
-
73
- 'cn ' => ['John Doe ' ],
74
- ]);
75
-
76
- $ manager = new Manager ();
77
-
78
- $ manager ->add ('default ' , $ mockedProvider );
79
-
80
- Adldap::shouldReceive ('getManager ' )->andReturn ($ manager );
81
-
82
- $ mockedProvider ->shouldReceive ('search ' )->once ()->andReturn ($ mockedSearch );
83
- $ mockedProvider ->shouldReceive ('getSchema ' )->andReturn (Schema::get ());
84
- $ mockedProvider ->shouldReceive ('auth ' )->once ()->andReturn ($ mockedAuth );
85
-
86
- $ mockedSearch ->shouldReceive ('users ' )->once ()->andReturn ($ mockedSearch );
87
- $ mockedSearch ->shouldReceive ('select ' )->once ()->andReturn ($ mockedBuilder );
88
- $ mockedBuilder ->shouldReceive ('where ' )->once ()->andReturn ($ mockedBuilder );
89
- $ mockedBuilder ->shouldReceive ('first ' )->once ()->andReturn ($ adUser );
90
- $ mockedAuth ->shouldReceive ('attempt ' )->once ()->andReturn (true );
91
-
92
- $ this ->
assertTrue (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]));
66
+ $ this ->assertTrue (Auth::attempt ($ credentials ));
93
67
94
68
$ user = Auth::user ();
95
69
96
- $ this ->assertEquals (' jdoe@ email.com ' , $ user ->email );
97
- $ this ->assertTrue (\ Hash::check (' 12345 ' , $ user ->password ));
70
+ $ this ->assertEquals ($ credentials [ ' email ' ] , $ user ->email );
71
+ $ this ->assertTrue (Hash::check ($ credentials [ ' password ' ] , $ user ->password ));
98
72
}
99
73
100
74
public function test_auth_passes_with_persistent_adldap_user ()
@@ -117,38 +91,7 @@ public function test_auth_passes_without_persistent_adldap_user()
117
91
118
92
public function test_auth_fails ()
119
93
{
120
- $ mockedProvider = $ this ->mock (Provider::class);
121
- $ mockedBuilder = $ this ->mock (Builder::class);
122
- $ mockedSearch = $ this ->mock (Factory::class);
123
- $ mockedAuth = $ this ->mock (Guard::class);
124
- $ mockedConnection = $ this ->mock (ConnectionInterface::class);
125
-
126
- $ mockedConnection ->shouldReceive ('isBound ' )->once ()->andReturn (true );
127
-
128
- $ mockedBuilder ->shouldReceive ('getSchema ' )->once ()->andReturn (Schema::get ());
129
- $ mockedBuilder ->shouldReceive ('getConnection ' )->once ()->andReturn ($ mockedConnection );
130
-
131
- $ adUser = (new User ([], $ mockedBuilder ))->setRawAttributes ([
132
- 'samaccountname ' => ['jdoe ' ],
133
-
134
- 'cn ' => ['John Doe ' ],
135
- ]);
136
-
137
- $ manager = new Manager ();
138
-
139
- $ manager ->add ('default ' , $ mockedProvider );
140
-
141
- Adldap::shouldReceive ('getManager ' )->andReturn ($ manager );
142
-
143
- $ mockedProvider ->shouldReceive ('search ' )->once ()->andReturn ($ mockedSearch );
144
- $ mockedProvider ->shouldReceive ('getSchema ' )->andReturn (Schema::get ());
145
- $ mockedProvider ->shouldReceive ('auth ' )->once ()->andReturn ($ mockedAuth );
146
-
147
- $ mockedSearch ->shouldReceive ('users ' )->once ()->andReturn ($ mockedSearch );
148
- $ mockedSearch ->shouldReceive ('select ' )->once ()->andReturn ($ mockedBuilder );
149
- $ mockedBuilder ->shouldReceive ('where ' )->once ()->andReturn ($ mockedBuilder );
150
- $ mockedBuilder ->shouldReceive ('first ' )->once ()->andReturn ($ adUser );
151
- $ mockedAuth ->shouldReceive ('attempt ' )->once ()->andReturn (false );
94
+ $ this ->getMockAuth ()->shouldReceive ('attempt ' )->once ()->andReturn (false );
152
95
153
96
$ this ->
assertFalse (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]));
154
97
}
@@ -258,19 +201,16 @@ public function test_config_login_fallback()
258
201
'password ' => 'Password123 ' ,
259
202
];
260
203
261
- $ outcome = Auth::attempt ($ credentials );
204
+ $ this -> assertTrue ( Auth::attempt ($ credentials) );
262
205
263
- $ user = \ Auth::user ();
206
+ $ user = Auth::user ();
264
207
265
- $ this ->assertTrue ($ outcome );
266
208
$ this ->assertInstanceOf ('Adldap\Laravel\Tests\Models\User ' , $ user );
267
209
$ this ->
assertEquals (
'[email protected] ' ,
$ user->
email );
268
210
269
211
$ this ->app ['config ' ]->set ('adldap_auth.login_fallback ' , false );
270
212
271
- $ outcome = Auth::attempt ($ credentials );
272
-
273
- $ this ->assertFalse ($ outcome );
213
+ $ this ->assertFalse (Auth::attempt ($ credentials ));
274
214
}
275
215
276
216
public function test_config_login_fallback_no_connection ()
@@ -305,12 +245,75 @@ public function test_config_login_fallback_no_connection()
305
245
'password ' => 'Password123 ' ,
306
246
];
307
247
308
- $ outcome = Auth::attempt ($ credentials );
248
+ $ this -> assertTrue ( Auth::attempt ($ credentials) );
309
249
310
- $ user = \ Auth::user ();
250
+ $ user = Auth::user ();
311
251
312
- $ this ->assertTrue ($ outcome );
313
252
$ this ->assertInstanceOf ('Adldap\Laravel\Tests\Models\User ' , $ user );
314
253
$ this ->
assertEquals (
'[email protected] ' ,
$ user->
email );
315
254
}
255
+
256
+ public function test_config_password_sync_enabled ()
257
+ {
258
+ $ this ->app ['config ' ]->set ('adldap_auth.password_sync ' , true );
259
+
260
+ $ this ->getMockAuth ()->shouldReceive ('attempt ' )->once ()->andReturn (true );
261
+
262
+ $ this ->
assertTrue (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]));
263
+
264
+ $ this ->assertInstanceOf (EloquentUser::class, EloquentUser::first ());
265
+ }
266
+
267
+ public function test_config_password_sync_disabled ()
268
+ {
269
+ $ this ->app ['config ' ]->set ('adldap_auth.password_sync ' , false );
270
+
271
+ $ this ->getMockAuth ()->shouldReceive ('attempt ' )->once ()->andReturn (true );
272
+
273
+ $ this ->
assertFalse (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'12345 ' ]));
274
+
275
+ $ this ->assertInstanceOf (EloquentUser::class, EloquentUser::first ());
276
+ }
277
+
278
+ protected function getMockAuth (User $ user = null )
279
+ {
280
+ $ mockedProvider = $ this ->mock (Provider::class);
281
+ $ mockedBuilder = $ this ->mock (Builder::class);
282
+ $ mockedSearch = $ this ->mock (Factory::class);
283
+ $ mockedAuth = $ this ->mock (Guard::class);
284
+ $ mockedConnection = $ this ->mock (ConnectionInterface::class);
285
+
286
+ $ mockedConnection ->shouldReceive ('isBound ' )->once ()->andReturn (true );
287
+
288
+ $ mockedBuilder ->shouldReceive ('getSchema ' )->once ()->andReturn (Schema::get ());
289
+ $ mockedBuilder ->shouldReceive ('getConnection ' )->once ()->andReturn ($ mockedConnection );
290
+
291
+ $ manager = new Manager ();
292
+
293
+ $ manager ->add ('default ' , $ mockedProvider );
294
+
295
+ Adldap::shouldReceive ('getManager ' )->andReturn ($ manager );
296
+
297
+ $ mockedProvider ->shouldReceive ('search ' )->once ()->andReturn ($ mockedSearch );
298
+ $ mockedProvider ->shouldReceive ('getSchema ' )->andReturn (Schema::get ());
299
+ $ mockedProvider ->shouldReceive ('auth ' )->once ()->andReturn ($ mockedAuth );
300
+
301
+ $ mockedSearch ->shouldReceive ('users ' )->once ()->andReturn ($ mockedSearch );
302
+ $ mockedSearch ->shouldReceive ('select ' )->once ()->andReturn ($ mockedBuilder );
303
+ $ mockedBuilder ->shouldReceive ('where ' )->once ()->andReturn ($ mockedBuilder );
304
+ $ mockedBuilder ->shouldReceive ('first ' )->once ()->andReturn ($ user ?: $ this ->getMockUser ($ mockedBuilder ));
305
+
306
+ return $ mockedAuth ;
307
+ }
308
+
309
+ protected function getMockUser ($ builder , array $ attributes = [])
310
+ {
311
+ $ attributes = array_merge ($ attributes , [
312
+ 'samaccountname ' => ['jdoe ' ],
313
+
314
+ 'cn ' => ['John Doe ' ],
315
+ ]);
316
+
317
+ return (new User ([], $ builder ))->setRawAttributes ($ attributes );
318
+ }
316
319
}
0 commit comments