@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\ActiveDirectory;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laravel\Passkeys\Passkey;
|
||||
use Laravel\Passkeys\Passkeys;
|
||||
use LdapRecord\Connection;
|
||||
use LdapRecord\LdapInterface;
|
||||
use LdapRecord\LdapResultResponse;
|
||||
|
||||
function directoryEntry(string $email = '[email protected]', string $guid = '00112233-4455-6677-8899-aabbccddeeff', bool $enabled = true): array
|
||||
{
|
||||
return ['guid' => $guid, 'email' => $email, 'name' => 'Ada Lovelace', 'enabled' => $enabled];
|
||||
}
|
||||
|
||||
function directorySnapshot(array $entries): void
|
||||
{
|
||||
test()->mock(ActiveDirectory::class)->shouldReceive('users')->once()->andReturn($entries);
|
||||
}
|
||||
|
||||
it('imports by canonical GUID and retains the account when its email changes', function () {
|
||||
directorySnapshot([directoryEntry(guid: strtoupper(directoryEntry()['guid']))]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$user = User::sole();
|
||||
expect($user->ad_guid)->toBe(directoryEntry()['guid'])
|
||||
->and($user->email_verified_at)->toBeNull();
|
||||
|
||||
directorySnapshot([directoryEntry('[email protected]')]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
directorySnapshot([directoryEntry('[email protected]')]);
|
||||
$this->artisan('ad:sync')->expectsOutputToContain('unchanged=1')->assertSuccessful();
|
||||
|
||||
expect(User::sole()->id)->toBe($user->id)
|
||||
->and($user->refresh()->email)->toBe('[email protected]');
|
||||
});
|
||||
|
||||
it('refuses to claim a local account by case-insensitive email and applies no partial changes', function () {
|
||||
$local = User::factory()->create(['email' => '[email protected]']);
|
||||
directorySnapshot([directoryEntry('[email protected]', '11111111-1111-1111-1111-111111111111'), directoryEntry()]);
|
||||
$this->artisan('ad:sync')->expectsOutputToContain('email collision')->assertFailed();
|
||||
expect(User::sole()->id)->toBe($local->id)
|
||||
->and($local->refresh()->ad_guid)->toBeNull();
|
||||
});
|
||||
|
||||
it('rejects unsafe snapshots before writing', function (array $entries) {
|
||||
directorySnapshot($entries);
|
||||
$this->artisan('ad:sync --deactivate-missing')->assertFailed();
|
||||
expect(User::count())->toBe(0);
|
||||
})->with([
|
||||
'empty' => fn () => [[]],
|
||||
'bad GUID' => fn () => [[directoryEntry(guid: 'invalid')]],
|
||||
'duplicate GUID' => fn () => [[directoryEntry(), directoryEntry('[email protected]')]],
|
||||
'duplicate email' => fn () => [[directoryEntry(), directoryEntry('[email protected]', '11111111-1111-1111-1111-111111111111')]],
|
||||
]);
|
||||
|
||||
it('keeps dry runs read-only and blocks access for an explicitly disabled directory user', function () {
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync --dry-run')->expectsOutputToContain('created=1')->assertSuccessful();
|
||||
expect(User::count())->toBe(0);
|
||||
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$user = User::sole();
|
||||
$user->update(['password' => 'example-password']);
|
||||
directorySnapshot([directoryEntry(enabled: false)]);
|
||||
$this->artisan('ad:sync --dry-run')->expectsOutputToContain('deactivated=1')->assertSuccessful();
|
||||
expect($user->refresh()->ad_disabled_at)->toBeNull();
|
||||
|
||||
directorySnapshot([directoryEntry(enabled: false)]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
expect($user->refresh()->ad_disabled_at)->not->toBeNull();
|
||||
$this->post(route('login.store'), ['email' => $user->email, 'password' => 'example-password']);
|
||||
$this->assertGuest();
|
||||
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
expect($user->refresh()->ad_disabled_at)->toBeNull();
|
||||
});
|
||||
|
||||
it('requires explicit bounded missing-user deactivation and preserves local accounts', function () {
|
||||
$second = directoryEntry('[email protected]', '11111111-1111-1111-1111-111111111111');
|
||||
$third = directoryEntry('[email protected]', '22222222-2222-2222-2222-222222222222');
|
||||
$local = User::factory()->create();
|
||||
directorySnapshot([directoryEntry(), $second, $third]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
expect(User::whereNotNull('ad_disabled_at')->count())->toBe(0);
|
||||
|
||||
config(['ad-sync.max-deactivations' => 1]);
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync --deactivate-missing')->assertFailed();
|
||||
expect(User::whereNotNull('ad_disabled_at')->count())->toBe(0);
|
||||
|
||||
directorySnapshot([directoryEntry(), $second]);
|
||||
$this->artisan('ad:sync --deactivate-missing')->assertSuccessful();
|
||||
expect(User::whereNotNull('ad_disabled_at')->sole()->email)->toBe($third['email'])
|
||||
->and($local->refresh()->ad_disabled_at)->toBeNull();
|
||||
});
|
||||
|
||||
it('reports directory failures without changing users', function () {
|
||||
$this->mock(ActiveDirectory::class)->shouldReceive('users')->once()->andThrow(new RuntimeException('sensitive bind detail'));
|
||||
$this->artisan('ad:sync --deactivate-missing')->expectsOutputToContain('directory or database error')->assertFailed();
|
||||
expect(User::count())->toBe(0);
|
||||
});
|
||||
|
||||
it('rejects overlapping runs before reading the directory', function () {
|
||||
$lock = Cache::lock('ad:sync', 60);
|
||||
expect($lock->get())->toBeTrue();
|
||||
try {
|
||||
$this->mock(ActiveDirectory::class)->shouldNotReceive('users');
|
||||
$this->artisan('ad:sync')->expectsOutputToContain('already running')->assertFailed();
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
|
||||
it('converts binary AD GUIDs and reads the account-disabled flag across pages', function () {
|
||||
$binaryGuid = hex2bin('33221100554477668899aabbccddeeff');
|
||||
$entry = [
|
||||
'objectguid' => [$binaryGuid],
|
||||
'mail' => ['[email protected]'],
|
||||
'cn' => ['Ada Lovelace'],
|
||||
'useraccountcontrol' => ['514'],
|
||||
];
|
||||
$ldap = Mockery::mock(LdapInterface::class);
|
||||
$ldap->shouldReceive('search')->once()->withArgs(fn (...$args) => ($args[7][0]['value']['cookie'] ?? null) === '')->andReturn('first');
|
||||
$ldap->shouldReceive('search')->once()->withArgs(fn (...$args) => ($args[7][0]['value']['cookie'] ?? null) === 'next')->andReturn('second');
|
||||
$ldap->shouldReceive('parseResult')->with('first')->andReturn(new LdapResultResponse(controls: [
|
||||
LDAP_CONTROL_PAGEDRESULTS => ['value' => ['cookie' => 'next']],
|
||||
]));
|
||||
$ldap->shouldReceive('parseResult')->with('second')->andReturn(new LdapResultResponse(controls: [
|
||||
LDAP_CONTROL_PAGEDRESULTS => ['value' => ['cookie' => '']],
|
||||
]));
|
||||
$ldap->shouldReceive('getEntries')->with('first')->andReturn([0 => $entry, 'count' => 1]);
|
||||
$ldap->shouldReceive('getEntries')->with('second')->andReturn(['count' => 0]);
|
||||
$ldap->shouldReceive('freeResult')->twice()->andReturnTrue();
|
||||
useDirectoryConnection($ldap);
|
||||
|
||||
expect((new ActiveDirectory)->users())->toBe([directoryEntry(enabled: false)]);
|
||||
});
|
||||
|
||||
it('discards the entire snapshot when the server returns a partial LDAP result', function () {
|
||||
$ldap = Mockery::mock(LdapInterface::class);
|
||||
$ldap->shouldReceive('search')->once()->andReturn('partial');
|
||||
$ldap->shouldReceive('parseResult')->with('partial')->andReturn(new LdapResultResponse(errorCode: 4));
|
||||
$ldap->shouldReceive('getEntries')->never();
|
||||
$ldap->shouldReceive('freeResult')->with('partial')->once()->andReturnTrue();
|
||||
useDirectoryConnection($ldap);
|
||||
|
||||
$this->artisan('ad:sync --deactivate-missing')->expectsOutputToContain('directory or database error')->assertFailed();
|
||||
expect(User::count())->toBe(0);
|
||||
});
|
||||
|
||||
function useDirectoryConnection(LdapInterface $ldap): void
|
||||
{
|
||||
$connection = Mockery::mock(Connection::class)->makePartial();
|
||||
$connection->shouldReceive('run')->once()->andReturnUsing(fn (Closure $operation) => $operation($ldap));
|
||||
$manager = LdapRecord\Models\ActiveDirectory\User::getConnectionContainer()->getConnectionManager();
|
||||
$name = $manager->getDefaultConnectionName();
|
||||
$previous = $manager->getConnection($name);
|
||||
$manager->addConnection($connection, $name);
|
||||
test()->beforeApplicationDestroyed(fn () => $manager->addConnection($previous, $name));
|
||||
}
|
||||
|
||||
it('denies existing sessions and passkey logins while a managed user is disabled', function () {
|
||||
$this->withoutVite();
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$user = User::sole();
|
||||
$key = auth()->guard()->getName();
|
||||
$this->withSession([$key => $user->id])->get(route('dashboard'))->assertOk();
|
||||
|
||||
directorySnapshot([directoryEntry(enabled: false)]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
Auth::forgetGuards();
|
||||
$this->withSession([$key => $user->id])->get(route('dashboard'))->assertRedirect(route('login'));
|
||||
|
||||
$passkey = new Passkey;
|
||||
$passkey->setRelation('user', $user->refresh());
|
||||
expect(Passkeys::allowsLogin(request(), $passkey))->toBeFalse();
|
||||
});
|
||||
|
||||
it('logs counts and failure reasons without directory email addresses or exception messages', function () {
|
||||
$log = Log::spy();
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync --dry-run')->assertSuccessful();
|
||||
$log->shouldHaveReceived('info')->once()->with('Active Directory sync completed.', [
|
||||
'dry_run' => true,
|
||||
'deactivate_missing' => false,
|
||||
'created' => 1,
|
||||
'updated' => 0,
|
||||
'unchanged' => 0,
|
||||
'deactivated' => 0,
|
||||
'reactivated' => 0,
|
||||
]);
|
||||
|
||||
$this->mock(ActiveDirectory::class)->shouldReceive('users')->once()->andThrow(new RuntimeException('sensitive bind detail'));
|
||||
$this->artisan('ad:sync')->assertFailed();
|
||||
$log->shouldHaveReceived('error')->once()->with('Active Directory sync failed.', [
|
||||
'dry_run' => false,
|
||||
'deactivate_missing' => false,
|
||||
'exception_type' => RuntimeException::class,
|
||||
]);
|
||||
});
|
||||
@@ -1,8 +1,12 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Stylist\Providers\StylistServiceProvider;
|
||||
|
||||
it('resolves backend theme views', function () {
|
||||
expect(View::exists('dashboard'))->toBeTrue()
|
||||
@@ -30,3 +34,36 @@ it('renders the authenticated security settings', function () {
|
||||
->get(route('security.edit'))
|
||||
->assertOk();
|
||||
});
|
||||
|
||||
it('resolves shared views using the request theme and preserves the backend layout namespace', function (string $uri, string $theme) {
|
||||
$files = new Filesystem;
|
||||
$root = sys_get_temp_dir().'/stylist-precedence-'.Str::uuid();
|
||||
$request = Request::create($uri);
|
||||
app()->instance('request', $request);
|
||||
$console = new ReflectionProperty(app(), 'isRunningInConsole');
|
||||
$console->setValue(app(), false);
|
||||
|
||||
try {
|
||||
foreach (['frontend', 'backend'] as $key) {
|
||||
$files->makeDirectory("$root/$key/views/layouts", recursive: true);
|
||||
$files->copy(resource_path("themes/$key/theme.json"), "$root/$key/theme.json");
|
||||
$files->put("$root/$key/views/layouts/app.blade.php", $key);
|
||||
}
|
||||
$files->put("$root/frontend/views/welcome.blade.php", 'welcome');
|
||||
$files->put("$root/backend/views/dashboard.blade.php", 'dashboard');
|
||||
config(['stylist.themes_path' => $root]);
|
||||
app()->getProvider(StylistServiceProvider::class)->boot();
|
||||
|
||||
$finder = View::getFinder();
|
||||
expect($finder->find('layouts.app'))->toBe("$root/$theme/views/layouts/app.blade.php")
|
||||
->and($finder->find('layouts::app'))->toBe("$root/backend/views/layouts/app.blade.php")
|
||||
->and($finder->find('welcome'))->toBe("$root/frontend/views/welcome.blade.php")
|
||||
->and($finder->find('dashboard'))->toBe("$root/backend/views/dashboard.blade.php");
|
||||
} finally {
|
||||
$console->setValue(app(), true);
|
||||
$files->deleteDirectory($root);
|
||||
}
|
||||
})->with([
|
||||
'frontend request' => ['/', 'frontend'],
|
||||
'backend request' => ['/dashboard', 'backend'],
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user