Skip to content

Commit

Permalink
Add tests, refactor and perform clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed May 8, 2021
1 parent 692965c commit bc49ba5
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

namespace LdapRecord\Laravel\Tests\Feature;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use LdapRecord\Laravel\Auth\DatabaseUserProvider;
use LdapRecord\Laravel\Import\UserSynchronizer;
use LdapRecord\Laravel\LdapRecord;
use LdapRecord\Laravel\LdapUserAuthenticator;
use LdapRecord\Laravel\LdapUserRepository;
use Mockery as m;

class DatabaseUserTest extends DatabaseTestCase
class DatabaseUserProviderTest extends DatabaseTestCase
{
use CreatesTestUsers;

protected function tearDown(): void
{
// Reset static properties.
LdapRecord::$failingQuietly = true;

parent::tearDown();
}

public function test_importer_can_be_retrieved()
{
$synchronizer = new UserSynchronizer(TestUserModelStub::class, []);
Expand Down Expand Up @@ -133,4 +144,19 @@ public function test_method_calls_are_passed_to_eloquent_user_provider()

$this->assertInstanceOf(Model::class, new $model);
}

public function test_failing_loudly_throws_exception_when_resolving_users()
{
LdapRecord::failLoudly();

$provider = m::mock(DatabaseUserProvider::class)->makePartial();

$provider->resolveUsersUsing(function () {
throw new Exception('Failed');
});

$this->expectExceptionMessage('Failed');

$provider->retrieveByCredentials([]);
}
}
6 changes: 3 additions & 3 deletions tests/Feature/Emulator/EmulatedWindowsAuthenticateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ class EmulatedWindowsAuthenticateTest extends DatabaseTestCase
{
use WithFaker;

protected function setUp(): void
protected function tearDown(): void
{
parent::setUp();

// Reset all static properties.
WindowsAuthenticate::$guards = null;
WindowsAuthenticate::$serverKey = 'AUTH_USER';
Expand All @@ -36,6 +34,8 @@ protected function setUp(): void
WindowsAuthenticate::$rememberAuthenticatedUsers = false;
WindowsAuthenticate::$userDomainExtractor = null;
WindowsAuthenticate::$userDomainValidator = UserDomainValidator::class;

parent::tearDown();
}

public function test_windows_authenticated_user_is_signed_in()
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/LdapUserSynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use LdapRecord\Models\Model;
use Mockery as m;

class LdapUserSynchronizerTest extends DatabaseUserTest
class LdapUserSynchronizerTest extends DatabaseUserProviderTest
{
use CreatesTestUsers;

Expand Down
5 changes: 0 additions & 5 deletions tests/Feature/ListenForLdapBindFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

class ListenForLdapBindFailureTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
}

protected function getEnvironmentSetup($app)
{
parent::getEnvironmentSetup($app);
Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/NoDatabaseUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace LdapRecord\Laravel\Tests\Unit;

use Exception;
use LdapRecord\Laravel\Auth\NoDatabaseUserProvider;
use LdapRecord\Laravel\LdapRecord;
use LdapRecord\Laravel\LdapUserAuthenticator;
use LdapRecord\Laravel\LdapUserRepository;
use LdapRecord\Laravel\Tests\TestCase;
Expand All @@ -12,6 +14,14 @@

class NoDatabaseUserProviderTest extends TestCase
{
protected function tearDown(): void
{
// Reset static properties.
LdapRecord::$failingQuietly = true;

parent::tearDown();
}

public function test_user_repository_can_be_retrieved()
{
$repo = new LdapUserRepository(User::class);
Expand Down Expand Up @@ -68,4 +78,19 @@ public function test_validate_credentials_attempts_authentication()

$this->assertTrue($provider->validateCredentials($user, ['password' => 'secret']));
}

public function test_failing_loudly_throws_exception_when_resolving_users()
{
LdapRecord::failLoudly();

$provider = m::mock(NoDatabaseUserProvider::class)->makePartial();

$provider->resolveUsersUsing(function () {
throw new Exception('Failed');
});

$this->expectExceptionMessage('Failed');

$provider->retrieveByCredentials([]);
}
}
Loading

0 comments on commit bc49ba5

Please sign in to comment.