updates for active directory now with docs
tests / ci (push) Canceled after 0s

This commit is contained in:
Mark Gregory
2026-11-11 17:15:42 +00:00
parent 90f8655595
commit 9c3c82e7c6
9 changed files with 121 additions and 41 deletions
+50 -29
View File
@@ -68,17 +68,20 @@ it('keeps dry runs read-only and blocks access for an explicitly disabled direct
$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();
expect($user->refresh()->ad_disabled_at)->toBeNull()
->and($user->deleted_at)->toBeNull();
directorySnapshot([directoryEntry(enabled: false)]);
$this->artisan('ad:sync')->assertSuccessful();
expect($user->refresh()->ad_disabled_at)->not->toBeNull();
expect($user->refresh()->ad_disabled_at)->not->toBeNull()
->and($user->deleted_at)->toEqual($user->ad_disabled_at);
$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();
expect($user->refresh()->ad_disabled_at)->toBeNull()
->and($user->deleted_at)->toBeNull();
});
it('requires explicit bounded missing-user deactivation and preserves local accounts', function () {
@@ -99,7 +102,9 @@ it('requires explicit bounded missing-user deactivation and preserves local acco
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();
->and(User::whereNotNull('deleted_at')->sole()->email)->toBe($third['email'])
->and($local->refresh()->ad_disabled_at)->toBeNull()
->and($local->deleted_at)->toBeNull();
});
it('reports directory failures without changing users', function () {
@@ -186,38 +191,26 @@ it('denies existing sessions and passkey logins while a managed user is disabled
});
it('correlates run summaries and planned or committed changes without logging field values', function (bool $dryRun) {
$log = Log::spy();
$events = [];
Log::shouldReceive('info')->times(3)->andReturnUsing(function ($message, $context) use (&$events): void {
$events[$message] = $context;
});
directorySnapshot([directoryEntry()]);
$this->artisan($dryRun ? 'ad:sync --dry-run' : 'ad:sync')->assertSuccessful();
$id = $dryRun ? null : User::sole()->id;
$runId = null;
$log->shouldHaveReceived('info')->withArgs(function ($message, $context) use (&$runId, $dryRun) {
if ($message !== 'Active Directory sync started.') {
return false;
}
$runId = $context['run_id'];
return $context['dry_run'] === $dryRun && Str::isUuid($runId);
})->once();
$log->shouldHaveReceived('info')->withArgs(fn ($message, $context) => $message === 'Active Directory sync user change.'
&& $context === [
'run_id' => $runId,
'dry_run' => $dryRun,
'deactivate_missing' => false,
$start = $events['Active Directory sync started.'];
expect(Str::isUuid($start['run_id']))->toBeTrue()
->and($start['dry_run'])->toBe($dryRun)
->and($events['Active Directory sync user change.'])->toBe($start + [
'action' => 'created',
'user_id' => $id,
'user_id' => $dryRun ? null : User::sole()->id,
'ad_guid' => directoryEntry()['guid'],
'disabled' => false,
'changed_fields' => ['name', 'email'],
'changed_fields' => ['name', 'email', 'email_verified_at'],
'reason' => null,
]
)->once();
$log->shouldHaveReceived('info')->withArgs(fn ($message, $context) => $message === 'Active Directory sync completed.'
&& $context['run_id'] === $runId
&& $context['created'] === 1
&& $context['duration_ms'] >= 0
)->once();
])
->and($events['Active Directory sync completed.']['run_id'])->toBe($start['run_id'])
->and($events['Active Directory sync completed.']['created'])->toBe(1);
})->with(['dry run' => [true], 'live run' => [false]]);
it('logs sanitized failures and emits no user changes for a rejected run', function () {
@@ -238,3 +231,31 @@ it('logs sanitized failures and emits no user changes for a rejected run', funct
);
$log->shouldNotHaveReceived('info', ['Active Directory sync user change.', Mockery::any()]);
});
it('will neither claim an existing GUID nor deactivate missing users from another directory scope', function () {
directorySnapshot([directoryEntry()]);
$this->artisan('ad:sync')->assertSuccessful();
$existing = User::sole();
config(['ad-sync.base-dn' => 'OU=Other,DC=ad,DC=test']);
directorySnapshot([directoryEntry()]);
$this->artisan('ad:sync')->expectsOutputToContain('another directory scope')->assertFailed();
directorySnapshot([directoryEntry('[email protected]', '11111111-1111-1111-1111-111111111111')]);
$this->artisan('ad:sync --deactivate-missing')->assertSuccessful();
expect($existing->refresh()->deleted_at)->toBeNull();
});
it('registers an overlap-protected ten-minute job only when scheduling is enabled', function (bool $enabled) {
config(['ad-sync.scheduled' => $enabled]);
$schedule = new Illuminate\Console\Scheduling\Schedule;
Illuminate\Support\Facades\Schedule::swap($schedule);
require base_path('routes/console.php');
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'ad:sync'));
expect($events)->toHaveCount($enabled ? 1 : 0);
if ($enabled) {
expect($events->sole()->expression)->toBe('*/10 * * * *')
->and($events->sole()->withoutOverlapping)->toBeTrue()
->and($events->sole()->command)->not->toContain('--deactivate-missing');
}
})->with(['enabled' => [true], 'disabled' => [false]]);