141 lines
5.2 KiB
PHP
141 lines
5.2 KiB
PHP
<?php
|
|
|
|
use App\Models\Finding;
|
|
use App\Models\Policy;
|
|
use App\Models\PolicyVersion;
|
|
use App\Models\WorkspaceSetting;
|
|
use App\Services\Drift\DriftFindingGenerator;
|
|
|
|
test('uses medium severity for drift findings when no severity mapping exists', function () {
|
|
[, $tenant] = createUserWithTenant(role: 'manager');
|
|
|
|
$scopeKey = hash('sha256', 'scope-policy-snapshot-default-severity');
|
|
|
|
$baseline = createInventorySyncOperationRun($tenant, [
|
|
'selection_hash' => $scopeKey,
|
|
'selection_payload' => ['policy_types' => ['deviceConfiguration']],
|
|
'status' => 'success',
|
|
'finished_at' => now()->subDays(2),
|
|
]);
|
|
|
|
$current = createInventorySyncOperationRun($tenant, [
|
|
'selection_hash' => $scopeKey,
|
|
'selection_payload' => ['policy_types' => ['deviceConfiguration']],
|
|
'status' => 'success',
|
|
'finished_at' => now()->subDay(),
|
|
]);
|
|
|
|
$policy = Policy::factory()->for($tenant)->create([
|
|
'policy_type' => 'deviceConfiguration',
|
|
'platform' => 'windows10',
|
|
]);
|
|
|
|
PolicyVersion::factory()->for($tenant)->for($policy)->create([
|
|
'version_number' => 1,
|
|
'policy_type' => $policy->policy_type,
|
|
'platform' => $policy->platform,
|
|
'captured_at' => $baseline->finished_at->copy()->subMinute(),
|
|
'snapshot' => ['customSettingFoo' => 'Old value'],
|
|
'assignments' => [],
|
|
]);
|
|
|
|
PolicyVersion::factory()->for($tenant)->for($policy)->create([
|
|
'version_number' => 2,
|
|
'policy_type' => $policy->policy_type,
|
|
'platform' => $policy->platform,
|
|
'captured_at' => $current->finished_at->copy()->subMinute(),
|
|
'snapshot' => ['customSettingFoo' => 'New value'],
|
|
'assignments' => [],
|
|
]);
|
|
|
|
$generator = app(DriftFindingGenerator::class);
|
|
$created = $generator->generate($tenant, $baseline, $current, $scopeKey);
|
|
|
|
expect($created)->toBe(1);
|
|
|
|
$finding = Finding::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->where('scope_key', $scopeKey)
|
|
->where('subject_type', 'policy')
|
|
->first();
|
|
|
|
expect($finding)->not->toBeNull();
|
|
expect($finding->severity)->toBe(Finding::SEVERITY_MEDIUM);
|
|
expect($finding->subject_external_id)->toBe($policy->external_id);
|
|
expect($finding->evidence_jsonb)->toHaveKey('change_type', 'modified');
|
|
expect($finding->evidence_jsonb)
|
|
->toHaveKey('summary.changed_fields')
|
|
->and($finding->evidence_jsonb['summary']['changed_fields'])->toContain('snapshot_hash')
|
|
->and($finding->evidence_jsonb)->toHaveKey('baseline.snapshot_hash')
|
|
->and($finding->evidence_jsonb)->toHaveKey('current.snapshot_hash')
|
|
->and($finding->evidence_jsonb)->not->toHaveKey('baseline.assignments_hash')
|
|
->and($finding->evidence_jsonb)->not->toHaveKey('current.assignments_hash');
|
|
});
|
|
|
|
test('applies workspace drift severity mapping when configured', function () {
|
|
[$user, $tenant] = createUserWithTenant(role: 'manager');
|
|
|
|
WorkspaceSetting::query()->create([
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'domain' => 'drift',
|
|
'key' => 'severity_mapping',
|
|
'value' => ['drift' => 'critical'],
|
|
'updated_by_user_id' => (int) $user->getKey(),
|
|
]);
|
|
|
|
$scopeKey = hash('sha256', 'scope-policy-snapshot-mapped-severity');
|
|
|
|
$baseline = createInventorySyncOperationRun($tenant, [
|
|
'selection_hash' => $scopeKey,
|
|
'selection_payload' => ['policy_types' => ['deviceConfiguration']],
|
|
'status' => 'success',
|
|
'finished_at' => now()->subDays(2),
|
|
]);
|
|
|
|
$current = createInventorySyncOperationRun($tenant, [
|
|
'selection_hash' => $scopeKey,
|
|
'selection_payload' => ['policy_types' => ['deviceConfiguration']],
|
|
'status' => 'success',
|
|
'finished_at' => now()->subDay(),
|
|
]);
|
|
|
|
$policy = Policy::factory()->for($tenant)->create([
|
|
'policy_type' => 'deviceConfiguration',
|
|
'platform' => 'windows10',
|
|
]);
|
|
|
|
PolicyVersion::factory()->for($tenant)->for($policy)->create([
|
|
'version_number' => 1,
|
|
'policy_type' => $policy->policy_type,
|
|
'platform' => $policy->platform,
|
|
'captured_at' => $baseline->finished_at->copy()->subMinute(),
|
|
'snapshot' => ['customSettingFoo' => 'Old value'],
|
|
'assignments' => [],
|
|
]);
|
|
|
|
PolicyVersion::factory()->for($tenant)->for($policy)->create([
|
|
'version_number' => 2,
|
|
'policy_type' => $policy->policy_type,
|
|
'platform' => $policy->platform,
|
|
'captured_at' => $current->finished_at->copy()->subMinute(),
|
|
'snapshot' => ['customSettingFoo' => 'New value'],
|
|
'assignments' => [],
|
|
]);
|
|
|
|
$generator = app(DriftFindingGenerator::class);
|
|
$created = $generator->generate($tenant, $baseline, $current, $scopeKey);
|
|
|
|
expect($created)->toBe(1);
|
|
|
|
$finding = Finding::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->where('scope_key', $scopeKey)
|
|
->where('subject_type', 'policy')
|
|
->first();
|
|
|
|
expect($finding)->not->toBeNull();
|
|
expect($finding->severity)->toBe(Finding::SEVERITY_CRITICAL);
|
|
});
|