117 lines
6.1 KiB
PHP
117 lines
6.1 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Services\ActiveDirectory;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
beforeEach(function () {
|
|
$this->admin = User::factory()->create();
|
|
config(['ad-sync.admin-id' => $this->admin->id, 'ad-sync.scheduled' => true]);
|
|
});
|
|
|
|
function adminDirectorySnapshot(bool $includeSecond = true, bool $renameFirst = false): void
|
|
{
|
|
$first = ['guid' => '00112233-4455-6677-8899-aabbccddeeff', 'email' => '[email protected]', 'name' => $renameFirst ? 'Renamed' : 'First', 'enabled' => true];
|
|
$second = ['guid' => '11111111-2222-3333-4444-555555555555', 'email' => '[email protected]', 'name' => 'Second', 'enabled' => true];
|
|
test()->mock(ActiveDirectory::class)->shouldReceive('users')->once()->andReturn($includeSecond ? [$first, $second] : [$first]);
|
|
}
|
|
|
|
it('restricts the directory status and actions to the configured local administrator', function () {
|
|
$this->get(route('admin.directory-sync'))->assertRedirect(route('login'));
|
|
$this->actingAs(User::factory()->create())->get(route('admin.directory-sync'))->assertForbidden();
|
|
$this->post(route('admin.directory-sync.preview'))->assertForbidden();
|
|
$this->post(route('admin.directory-sync.apply'))->assertForbidden();
|
|
$this->actingAs($this->admin)->get(route('admin.directory-sync'))->assertOk()->assertSee('Stale');
|
|
});
|
|
|
|
it('displays live success, counts, and failures and flags stale success', function () {
|
|
adminDirectorySnapshot();
|
|
$this->artisan('ad:sync')->assertSuccessful();
|
|
$this->assertDatabaseHas('ad_sync_runs', ['status' => 'completed', 'dry_run' => false]);
|
|
$this->mock(ActiveDirectory::class)->shouldReceive('users')->once()->andReturn([]);
|
|
$this->artisan('ad:sync')->assertFailed();
|
|
$this->actingAs($this->admin)->get(route('admin.directory-sync'))->assertOk()
|
|
->assertSee('Scheduled every 10 minutes')
|
|
->assertSee('Directory snapshot is empty.');
|
|
|
|
$this->travel(31)->minutes();
|
|
$this->get(route('admin.directory-sync'))->assertSee('Stale');
|
|
});
|
|
|
|
it('previews missing users and applies only the reviewed deactivations', function () {
|
|
adminDirectorySnapshot();
|
|
$this->artisan('ad:sync')->assertSuccessful();
|
|
|
|
adminDirectorySnapshot(includeSecond: false);
|
|
$this->actingAs($this->admin)->post(route('admin.directory-sync.preview'))->assertRedirect(route('admin.directory-sync'));
|
|
$this->get(route('admin.directory-sync'))->assertSee('[email protected]');
|
|
$this->assertDatabaseHas('users', ['email' => '[email protected]', 'ad_disabled_at' => null]);
|
|
|
|
adminDirectorySnapshot(includeSecond: false, renameFirst: true);
|
|
$this->post(route('admin.directory-sync.apply'), ['review_token' => session('directory-sync-review.token')])
|
|
->assertSessionHas('sync_status');
|
|
$this->assertDatabaseHas('users', ['email' => '[email protected]', 'name' => 'First']);
|
|
$this->assertDatabaseHas('users', ['email' => '[email protected]', 'ad_disabled_reason' => 'missing_from_snapshot']);
|
|
expect(DB::table('users')->where('email', '[email protected]')->value('deleted_at'))->not->toBeNull();
|
|
$this->get(route('admin.directory-sync'))->assertSee('Missing from the directory');
|
|
$this->post(route('admin.directory-sync.apply'))->assertSessionHas('sync_error');
|
|
});
|
|
|
|
it('rejects a review when the directory membership changes before apply', function () {
|
|
adminDirectorySnapshot();
|
|
$this->artisan('ad:sync')->assertSuccessful();
|
|
adminDirectorySnapshot(includeSecond: false);
|
|
$this->actingAs($this->admin)->post(route('admin.directory-sync.preview'))->assertRedirect();
|
|
|
|
adminDirectorySnapshot();
|
|
$this->post(route('admin.directory-sync.apply'), ['review_token' => session('directory-sync-review.token')])
|
|
->assertSessionHas('sync_error');
|
|
$this->assertDatabaseHas('users', ['email' => '[email protected]', 'deleted_at' => null]);
|
|
$this->assertDatabaseHas('ad_sync_runs', ['status' => 'rejected', 'failure' => 'Missing users changed since the preview. Review the list again.']);
|
|
});
|
|
|
|
it('keeps the current review when an old browser tab submits its token', function () {
|
|
adminDirectorySnapshot();
|
|
$this->artisan('ad:sync')->assertSuccessful();
|
|
$this->actingAs($this->admin);
|
|
|
|
adminDirectorySnapshot(includeSecond: false);
|
|
$this->post(route('admin.directory-sync.preview'))->assertRedirect();
|
|
$oldToken = session('directory-sync-review.token');
|
|
|
|
adminDirectorySnapshot(includeSecond: false);
|
|
$this->post(route('admin.directory-sync.preview'))->assertRedirect();
|
|
$currentToken = session('directory-sync-review.token');
|
|
|
|
$this->post(route('admin.directory-sync.apply'), ['review_token' => $oldToken])
|
|
->assertSessionHas('sync_error')
|
|
->assertSessionHas('directory-sync-review.token', $currentToken);
|
|
$this->assertDatabaseHas('users', ['email' => '[email protected]', 'deleted_at' => null]);
|
|
});
|
|
|
|
it('expires reviews and leaves all users unchanged', function () {
|
|
adminDirectorySnapshot();
|
|
$this->artisan('ad:sync')->assertSuccessful();
|
|
adminDirectorySnapshot(includeSecond: false);
|
|
$this->actingAs($this->admin)->post(route('admin.directory-sync.preview'))->assertRedirect();
|
|
$token = session('directory-sync-review.token');
|
|
|
|
$this->travel(10)->minutes();
|
|
$this->post(route('admin.directory-sync.apply'), ['review_token' => $token])
|
|
->assertSessionHas('sync_error')
|
|
->assertSessionMissing('directory-sync-review');
|
|
$this->assertDatabaseHas('users', ['email' => '[email protected]', 'deleted_at' => null]);
|
|
});
|
|
|
|
it('reports an empty candidate list and ignores successful dry runs for health', function () {
|
|
adminDirectorySnapshot();
|
|
$this->artisan('ad:sync --dry-run')->assertSuccessful();
|
|
$this->assertDatabaseHas('ad_sync_runs', ['status' => 'completed', 'dry_run' => true]);
|
|
adminDirectorySnapshot();
|
|
$this->actingAs($this->admin)->post(route('admin.directory-sync.preview'))->assertRedirect();
|
|
$this->get(route('admin.directory-sync'))->assertSee('No missing users found.')->assertSee('Stale');
|
|
$this->post(route('admin.directory-sync.apply'), ['review_token' => session('directory-sync-review.token')])
|
|
->assertSessionHas('sync_error');
|
|
expect(User::count())->toBe(1);
|
|
});
|