TenantAtlas/tests/Feature/PermissionPosture/PostureScoreCalculatorTest.php
Ahmed Darrazi 222a7e0a97 feat(104): implement Provider Permission Posture
- T001-T014: Foundation - StoredReport model/migration, Finding resolved
  lifecycle, badge mappings (resolved status, permission_posture type),
  OperationCatalog + AlertRule constants
- T015-T022: US1 - PermissionPostureFindingGenerator with fingerprint-based
  idempotent upsert, severity from feature-impact count, auto-resolve on
  grant, auto-reopen on revoke, error findings (FR-015), stale finding
  cleanup; GeneratePermissionPostureFindingsJob dispatched from health check;
  PostureResult VO + PostureScoreCalculator
- T023-T026: US2+US4 - Stored report payload validation, temporal ordering,
  polymorphic reusability, score accuracy acceptance tests
- T027-T029: US3 - EvaluateAlertsJob.permissionMissingEvents() wired into
  alert pipeline, AlertRuleResource event type option, cooldown/dedupe tests
- T030-T034: Polish - PruneStoredReportsCommand with config retention,
  scheduled daily, end-to-end integration test, Pint clean

UI bug fixes found during testing:
- FindingResource: hide Diff section for non-drift findings
- TenantRequiredPermissions: fix re-run verification link
- tenant-required-permissions.blade.php: preserve details open state

70 tests (50 PermissionPosture + 20 FindingResolved/Badge/Alert), 216 assertions
2026-02-21 23:31:03 +01:00

154 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Services\PermissionPosture\PostureScoreCalculator;
it('returns 100 when all permissions are granted', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'granted',
'permissions' => [
['key' => 'Perm.A', 'type' => 'application', 'status' => 'granted', 'features' => ['a']],
['key' => 'Perm.B', 'type' => 'application', 'status' => 'granted', 'features' => ['b']],
],
]);
expect($result)->toBe(100);
});
it('returns 86 for 12 of 14 granted', function (): void {
$calculator = new PostureScoreCalculator;
$permissions = [];
for ($i = 0; $i < 12; $i++) {
$permissions[] = ['key' => "Perm.$i", 'type' => 'application', 'status' => 'granted', 'features' => []];
}
for ($i = 12; $i < 14; $i++) {
$permissions[] = ['key' => "Perm.$i", 'type' => 'application', 'status' => 'missing', 'features' => []];
}
$result = $calculator->calculate([
'overall_status' => 'missing',
'permissions' => $permissions,
]);
expect($result)->toBe(86);
});
it('returns 0 when all permissions are missing', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'missing',
'permissions' => [
['key' => 'Perm.A', 'type' => 'application', 'status' => 'missing', 'features' => ['a']],
['key' => 'Perm.B', 'type' => 'application', 'status' => 'missing', 'features' => ['b']],
],
]);
expect($result)->toBe(0);
});
it('returns 100 when 0 permissions required', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'granted',
'permissions' => [],
]);
expect($result)->toBe(100);
});
it('returns 100 for single granted permission', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'granted',
'permissions' => [
['key' => 'Perm.A', 'type' => 'application', 'status' => 'granted', 'features' => ['a']],
],
]);
expect($result)->toBe(100);
});
it('returns 0 for single missing permission', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'missing',
'permissions' => [
['key' => 'Perm.A', 'type' => 'application', 'status' => 'missing', 'features' => ['a']],
],
]);
expect($result)->toBe(0);
});
it('rounds correctly for 1 of 3 granted (33)', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'missing',
'permissions' => [
['key' => 'A', 'type' => 'application', 'status' => 'granted', 'features' => []],
['key' => 'B', 'type' => 'application', 'status' => 'missing', 'features' => []],
['key' => 'C', 'type' => 'application', 'status' => 'missing', 'features' => []],
],
]);
expect($result)->toBe(33);
});
it('rounds correctly for 2 of 3 granted (67)', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'missing',
'permissions' => [
['key' => 'A', 'type' => 'application', 'status' => 'granted', 'features' => []],
['key' => 'B', 'type' => 'application', 'status' => 'granted', 'features' => []],
['key' => 'C', 'type' => 'application', 'status' => 'missing', 'features' => []],
],
]);
expect($result)->toBe(67);
});
it('returns integer not float', function (): void {
$calculator = new PostureScoreCalculator;
$result = $calculator->calculate([
'overall_status' => 'missing',
'permissions' => [
['key' => 'A', 'type' => 'application', 'status' => 'granted', 'features' => []],
['key' => 'B', 'type' => 'application', 'status' => 'missing', 'features' => []],
['key' => 'C', 'type' => 'application', 'status' => 'missing', 'features' => []],
],
]);
expect($result)->toBeInt();
});
it('returns 50 for 7 of 14 granted', function (): void {
$calculator = new PostureScoreCalculator;
$permissions = [];
for ($i = 0; $i < 7; $i++) {
$permissions[] = ['key' => "Perm.$i", 'type' => 'application', 'status' => 'granted', 'features' => []];
}
for ($i = 7; $i < 14; $i++) {
$permissions[] = ['key' => "Perm.$i", 'type' => 'application', 'status' => 'missing', 'features' => []];
}
$result = $calculator->calculate([
'overall_status' => 'missing',
'permissions' => $permissions,
]);
expect($result)->toBe(50);
});