22
33namespace Adldap \Laravel \Tests ;
44
5+ use Adldap \Laravel \AdldapAuthServiceProvider ;
6+ use Mockery as m ;
57use Adldap \Auth \Guard ;
68use Adldap \Connections \Manager ;
79use Adldap \Connections \Provider ;
1517use Adldap \Search \Factory ;
1618use Illuminate \Support \Facades \App ;
1719use Illuminate \Support \Facades \Auth ;
20+ use Illuminate \Support \Facades \Hash ;
1821
1922class AdldapTest extends FunctionalTestCase
2023{
@@ -54,47 +57,18 @@ public function test_contract_resolve()
5457 $ this ->assertInstanceOf (AdldapInterface::class, $ adldap );
5558 }
5659
57- public function test_auth_passes ()
60+ public function test_auth_passes ($ credentials = null )
5861 {
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 ' => 'jdoe@email.com ' , 'password ' => '12345 ' ];
6463
65- $ mockedConnection -> shouldReceive ('isBound ' )->once ()->andReturn (true );
64+ $ this -> getMockAuth ()-> shouldReceive ('attempt ' )->once ()->andReturn (true );
6665
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- 'mail ' => ['jdoe@email.com ' ],
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 ' => 'jdoe@email.com ' , 'password ' => '12345 ' ]));
66+ $ this ->assertTrue (Auth::attempt ($ credentials ));
9367
9468 $ user = Auth::user ();
9569
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 ));
9872 }
9973
10074 public function test_auth_passes_with_persistent_adldap_user ()
@@ -117,38 +91,7 @@ public function test_auth_passes_without_persistent_adldap_user()
11791
11892 public function test_auth_fails ()
11993 {
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- 'mail ' => ['jdoe@email.com ' ],
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 );
15295
15396 $ this ->assertFalse (Auth::attempt (['email ' => 'jdoe@email.com ' , 'password ' => '12345 ' ]));
15497 }
@@ -258,19 +201,16 @@ public function test_config_login_fallback()
258201 'password ' => 'Password123 ' ,
259202 ];
260203
261- $ outcome = Auth::attempt ($ credentials );
204+ $ this -> assertTrue ( Auth::attempt ($ credentials) );
262205
263- $ user = \ Auth::user ();
206+ $ user = Auth::user ();
264207
265- $ this ->assertTrue ($ outcome );
266208 $ this ->assertInstanceOf ('Adldap\Laravel\Tests\Models\User ' , $ user );
267209 $ this ->assertEquals ('jdoe@email.com ' , $ user ->email );
268210
269211 $ this ->app ['config ' ]->set ('adldap_auth.login_fallback ' , false );
270212
271- $ outcome = Auth::attempt ($ credentials );
272-
273- $ this ->assertFalse ($ outcome );
213+ $ this ->assertFalse (Auth::attempt ($ credentials ));
274214 }
275215
276216 public function test_config_login_fallback_no_connection ()
@@ -305,12 +245,75 @@ public function test_config_login_fallback_no_connection()
305245 'password ' => 'Password123 ' ,
306246 ];
307247
308- $ outcome = Auth::attempt ($ credentials );
248+ $ this -> assertTrue ( Auth::attempt ($ credentials) );
309249
310- $ user = \ Auth::user ();
250+ $ user = Auth::user ();
311251
312- $ this ->assertTrue ($ outcome );
313252 $ this ->assertInstanceOf ('Adldap\Laravel\Tests\Models\User ' , $ user );
314253 $ this ->assertEquals ('jdoe@email.com ' , $ user ->email );
315254 }
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 ' => 'jdoe@email.com ' , '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 ' => 'jdoe@email.com ' , '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+ 'mail ' => ['jdoe@email.com ' ],
314+ 'cn ' => ['John Doe ' ],
315+ ]);
316+
317+ return (new User ([], $ builder ))->setRawAttributes ($ attributes );
318+ }
316319}
0 commit comments