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

Commit 33602a2

Browse files
committed
Added auth test
1 parent 292aa1c commit 33602a2

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

tests/AdldapTest.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace Adldap\Laravel\Tests;
44

5-
use Adldap\Laravel\AdldapServiceProvider;
6-
use Adldap\Laravel\Exceptions\ConfigurationMissingException;
5+
use Adldap\Models\User;
6+
use Mockery;
7+
use Adldap\Laravel\Facades\Adldap;
8+
use Illuminate\Support\Facades\Auth;
79
use Illuminate\Support\Facades\App;
810

911
class AdldapTest extends FunctionalTestCase
1012
{
1113
public function testConfigurationNotFoundException()
1214
{
15+
$this->app['config']->set('adldap', null);
16+
1317
$this->setExpectedException('Adldap\Laravel\Exceptions\ConfigurationMissingException');
1418

1519
App::make('adldap');
@@ -18,5 +22,41 @@ public function testConfigurationNotFoundException()
1822
public function testRegistration()
1923
{
2024
$this->assertTrue(app()->register('Adldap\Laravel\AdldapServiceProvider'));
25+
$this->assertTrue(app()->register('Adldap\Laravel\AdldapAuthServiceProvider'));
26+
}
27+
28+
public function testAuth()
29+
{
30+
// Get around E Strict warning about mockery's __call signature being different
31+
if(defined('E_STRICT')) error_reporting('E_ALL ^ E_STRICT');
32+
33+
$mockedBuilder = Mockery::mock('Adldap\Query\Builder');
34+
35+
$rawAttributes = [
36+
'samaccountname' => ['jdoe'],
37+
'mail' => ['jdoe@email.com'],
38+
'cn' => ['John Doe'],
39+
];
40+
41+
$adUser = (new User([], $mockedBuilder))->setRawAttributes($rawAttributes);
42+
43+
$mockedSearch = Mockery::mock('Adldap\Classes\Search');
44+
45+
$mockedUsers = Mockery::mock('Adldap\Classes\Users');
46+
47+
$mockedSearch->shouldReceive('first')->once()->andReturn($adUser);
48+
$mockedSearch->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);
49+
50+
$mockedUsers->shouldReceive('search')->once()->andReturn($mockedSearch);
51+
52+
Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);
53+
Adldap::shouldReceive('authenticate')->once()->andReturn(true);
54+
55+
Auth::attempt(['mail' => 'jdoe@email.com', 'password' => '12345']);
56+
57+
$user = \DB::table('users')->where('id', '=', 1)->first();
58+
59+
$this->assertEquals('jdoe@email.com', $user->email);
60+
$this->assertTrue(\Hash::check('12345', $user->password));
2161
}
2262
}

tests/FunctionalTestCase.php

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

33
namespace Adldap\Laravel\Tests;
44

5-
use Adldap\Laravel\AdldapServiceProvider;
6-
use Adldap\Laravel\Facades\Adldap;
75
use Orchestra\Testbench\TestCase;
86

97
class FunctionalTestCase extends TestCase
108
{
9+
public function setUp()
10+
{
11+
parent::setUp();
12+
13+
$this->artisan('migrate', [
14+
'--database' => 'testbench',
15+
'--realpath' => realpath(__DIR__.'/stubs/migrations'),
16+
]);
17+
}
18+
1119
/**
1220
* Define the environment setup.
1321
*
1422
* @param \Illuminate\Foundation\Application $app
1523
*/
1624
protected function getEnvironmentSetup($app)
1725
{
18-
$app['config']->set('adldap', [
19-
'auto_connect' => false,
26+
$app['config']->set('database.default', 'testbench');
27+
$app['config']->set('database.connections.testbench', [
28+
'driver' => 'sqlite',
29+
'database' => ':memory:',
30+
'prefix' => '',
2031
]);
32+
33+
$app['config']->set('adldap.auto_connect', false);
34+
35+
$app['config']->set('auth.driver', 'adldap');
2136
}
2237

2338
/**
@@ -29,6 +44,7 @@ protected function getPackageProviders()
2944
{
3045
return [
3146
'Adldap\Laravel\AdldapServiceProvider',
47+
'Adldap\Laravel\AdldapAuthServiceProvider',
3248
];
3349
}
3450

@@ -37,10 +53,10 @@ protected function getPackageProviders()
3753
*
3854
* @return array
3955
*/
40-
protected function getPackageAliases()
56+
protected function getPackageAliases($app)
4157
{
4258
return [
43-
'Adldap\Laravel\Facades\Adldap',
59+
'Adldap' => 'Adldap\Laravel\Facades\Adldap',
4460
];
4561
}
4662
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Tests\Stubs\Migrations;
4+
5+
use Illuminate\Support\Facades\Schema;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Database\Migrations\Migration;
8+
9+
class CreateUsersTable extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::create('users', function (Blueprint $table) {
19+
$table->increments('id');
20+
$table->string('name');
21+
$table->string('email')->unique();
22+
$table->string('password', 60);
23+
$table->rememberToken();
24+
$table->timestamps();
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::drop('users');
36+
}
37+
}

0 commit comments

Comments
 (0)