Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit c23341f

Browse files
committed
Revamp tests.
1 parent ec855b2 commit c23341f

File tree

2 files changed

+114
-163
lines changed

2 files changed

+114
-163
lines changed

tests/AdldapTest.php

Lines changed: 99 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,17 @@
22

33
namespace Adldap\Laravel\Tests;
44

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;
87
use Adldap\Contracts\AdldapInterface;
9-
use Adldap\Contracts\Connections\ConnectionInterface;
108
use Adldap\Laravel\Facades\Adldap;
119
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;
1610
use Illuminate\Support\Facades\App;
1711
use Illuminate\Support\Facades\Auth;
1812
use Illuminate\Support\Facades\Hash;
1913

2014
class AdldapTest extends FunctionalTestCase
2115
{
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-
3616
public function test_configuration_not_found_exception()
3717
{
3818
$this->app['config']->set('adldap', null);
@@ -59,7 +39,29 @@ public function test_auth_passes($credentials = null)
5939
{
6040
$credentials = $credentials ?: ['email' => '[email protected]', 'password' => '12345'];
6141

62-
$this->getMockAuth()->shouldReceive('attempt')->once()->andReturn(true);
42+
$user = $this->getMockUser([
43+
'cn' => '',
44+
'mail' => '[email protected]',
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);
6365

6466
$this->assertTrue(Auth::attempt($credentials));
6567

@@ -73,8 +75,7 @@ public function test_auth_passes_with_persistent_adldap_user()
7375
{
7476
$this->test_auth_passes();
7577

76-
$this->assertInstanceOf(User::class, \Auth::user()->adldapUser);
77-
$this->assertInstanceOf(User::class, auth()->user()->adldapUser);
78+
$this->assertInstanceOf(User::class, Auth::user()->adldapUser);
7879
}
7980

8081
public function test_auth_passes_without_persistent_adldap_user()
@@ -83,72 +84,60 @@ public function test_auth_passes_without_persistent_adldap_user()
8384

8485
$this->test_auth_passes();
8586

86-
$this->assertNull(\Auth::user()->adldapUser);
87-
$this->assertNull(auth()->user()->adldapUser);
87+
$this->assertNull(Auth::user()->adldapUser);
8888
}
8989

90-
public function test_auth_fails()
90+
public function test_auth_fails_when_user_found()
9191
{
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+
'mail' => '[email protected]',
95+
'samaccountname' => 'jdoe',
96+
]);
10397

104-
$mockedConnection->shouldReceive('isBound')->once()->andReturn(true);
98+
$connection = $this->getMockConnection(['getLastError', 'errNo']);
10599

106-
$manager = new Manager();
100+
$connection->expects($this->exactly(2))->method('isBound')->willReturn(true);
107101

108-
$manager->add('default', $mockedProvider);
102+
$connection->expects($this->exactly(1))->method('search')->willReturn('resource');
109103

110-
Adldap::shouldReceive('getManager')->andReturn($manager);
104+
$connection->expects($this->exactly(1))->method('getEntries')->willReturn([
105+
'count' => 1,
106+
$user->getAttributes(),
107+
]);
111108

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);
114111

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);
120115

121116
$this->assertFalse(Auth::attempt(['email' => '[email protected]', 'password' => '12345']));
122117
}
123118

124-
public function test_credentials_key_does_not_exist()
119+
public function test_auth_fails_when_user_not_found()
125120
{
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();
134122

135-
$mockedSearch->shouldReceive('select')->once()->andReturn($mockedBuilder);
123+
$connection->expects($this->exactly(1))->method('isBound')->willReturn(true);
136124

137-
$manager = new Manager();
125+
$connection->expects($this->exactly(1))->method('search')->willReturn('resource');
138126

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+
]);
142130

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+
}
146133

147-
$nonExistantInputKey = 'non-existent-key';
134+
public function test_credentials_key_does_not_exist()
135+
{
136+
$connection = $this->getMockConnection();
148137

149-
$this->setExpectedException('ErrorException');
138+
$connection->expects($this->exactly(1))->method('isBound')->willReturn(true);
150139

151-
Auth::attempt([$nonExistantInputKey => '[email protected]', 'password' => '12345']);
140+
$this->assertFalse(Auth::attempt(['non-existent-key' => '[email protected]', 'password' => '12345']));
152141
}
153142

154143
public function test_config_callback_attribute_handler()
@@ -168,26 +157,6 @@ public function test_config_login_fallback()
168157
{
169158
$this->app['config']->set('adldap_auth.login_fallback', true);
170159

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-
191160
EloquentUser::create([
192161
'email' => '[email protected]',
193162
'name' => 'John Doe',
@@ -215,22 +184,9 @@ public function test_config_login_fallback_no_connection()
215184
{
216185
$this->app['config']->set('adldap_auth.login_fallback', true);
217186

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();
223188

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);
234190

235191
EloquentUser::create([
236192
'email' => '[email protected]',
@@ -255,12 +211,10 @@ public function test_config_password_sync_enabled()
255211
{
256212
$this->app['config']->set('adldap_auth.password_sync', true);
257213

258-
$this->getMockAuth()->shouldReceive('attempt')->once()->andReturn(true);
259-
260-
$email = '[email protected]';
214+
$email = '[email protected]';
261215
$password = '12345';
262216

263-
$this->assertTrue(Auth::attempt(compact('email', 'password')));
217+
$this->test_auth_passes(compact('email', 'password'));
264218

265219
$user = EloquentUser::first();
266220

@@ -274,60 +228,57 @@ public function test_config_password_sync_disabled()
274228
{
275229
$this->app['config']->set('adldap_auth.password_sync', false);
276230

277-
$this->getMockAuth()->shouldReceive('attempt')->once()->andReturn(true);
278-
279-
$email = '[email protected]';
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+
'mail' => '[email protected]',
234+
'samaccountname' => 'jdoe',
235+
]);
299236

300-
$mockedConnection->shouldReceive('isBound')->once()->andReturn(true);
237+
$connection = $this->getMockConnection();
301238

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);
304240

305-
$manager = new Manager();
241+
$connection->expects($this->exactly(1))->method('search')->willReturn('resource');
306242

307-
$manager->add('default', $mockedProvider);
243+
$connection->expects($this->exactly(1))->method('getEntries')->willReturn([
244+
'count' => 1,
245+
$user->getAttributes(),
246+
]);
308247

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);
310254

311-
$mockedProvider->shouldReceive('search')->once()->andReturn($mockedSearch);
312-
$mockedProvider->shouldReceive('getSchema')->andReturn(Schema::get());
313-
$mockedProvider->shouldReceive('auth')->once()->andReturn($mockedAuth);
255+
$email = '[email protected]';
256+
$password = '12345';
314257

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')));
319259

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));
321264
}
322265

323-
protected function getMockUser($builder, array $attributes = [])
266+
protected function getMockUser(array $attributes = [])
324267
{
325-
$attributes = array_merge($attributes, [
268+
return Adldap::getDefaultProvider()->make()->user($attributes ?: [
326269
'samaccountname' => ['jdoe'],
327270
'mail' => ['[email protected]'],
328271
'cn' => ['John Doe'],
329272
]);
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);
330281

331-
return (new User([], $builder))->setRawAttributes($attributes);
282+
return $connection;
332283
}
333284
}

0 commit comments

Comments
 (0)