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

This commit is contained in:
Mark Gregory
2026-11-11 16:47:59 +00:00
parent 2dcdb3790e
commit 4ebcd12565
3 changed files with 101 additions and 32 deletions
+48 -16
View File
@@ -5,6 +5,7 @@ use App\Services\ActiveDirectory;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Laravel\Passkeys\Passkey;
use Laravel\Passkeys\Passkeys;
use LdapRecord\Connection;
@@ -184,25 +185,56 @@ it('denies existing sessions and passkey logins while a managed user is disabled
expect(Passkeys::allowsLogin(request(), $passkey))->toBeFalse();
});
it('logs counts and failure reasons without directory email addresses or exception messages', function () {
it('correlates run summaries and planned or committed changes without logging field values', function (bool $dryRun) {
$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->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,
'action' => 'created',
'user_id' => $id,
'ad_guid' => directoryEntry()['guid'],
'disabled' => false,
'changed_fields' => ['name', 'email'],
'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();
})->with(['dry run' => [true], 'live run' => [false]]);
it('logs sanitized failures and emits no user changes for a rejected run', function () {
$log = Log::spy();
$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,
]);
$log->shouldHaveReceived('error')->once()->withArgs(fn ($message, $context) => $message === 'Active Directory sync failed.'
&& $context['exception_type'] === RuntimeException::class
&& $context['exception_code'] === 0
&& ! str_contains(json_encode($context), 'sensitive bind detail')
);
directorySnapshot([]);
$this->artisan('ad:sync')->assertFailed();
$log->shouldHaveReceived('warning')->once()->withArgs(fn ($message, $context) => $message === 'Active Directory sync rejected.'
&& $context['reason'] === 'Directory snapshot is empty.'
&& $context['duration_ms'] >= 0
);
$log->shouldNotHaveReceived('info', ['Active Directory sync user change.', Mockery::any()]);
});