## Summary - implement Spec 177 inventory coverage truth across resolver, badges, KPIs, coverage page, and operation run detail surfaces - add repo-native spec artifacts for the feature under `specs/177-inventory-coverage-truth` - add unit, feature, and browser coverage for truth derivation, continuity, and inventory item filter/pagination smoke paths ## Testing - `vendor/bin/sail bin pint --dirty --format agent` - focused Spec 177 browser smoke file passed with 2 tests / 57 assertions - extended inventory-focused test pack passed with 52 tests / 434 assertions Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #208
196 lines
7.2 KiB
PHP
196 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Filament\Resources\InventoryItemResource;
|
|
use App\Filament\Resources\InventoryItemResource\Pages\ListInventoryItems;
|
|
use App\Models\InventoryItem;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Support\Inventory\InventoryCoverage as InventoryCoveragePayload;
|
|
use App\Support\Workspaces\WorkspaceContext;
|
|
use Filament\Facades\Filament;
|
|
use Livewire\Livewire;
|
|
|
|
describe('Inventory item resource authorization', function () {
|
|
it('is not visible for non-members', function () {
|
|
$user = User::factory()->create();
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
$this->actingAs($user);
|
|
$tenant->makeCurrent();
|
|
|
|
expect(InventoryItemResource::canViewAny())->toBeFalse();
|
|
});
|
|
|
|
it('is visible for readonly members', function () {
|
|
[$user, $tenant] = createUserWithTenant(role: 'readonly');
|
|
|
|
$this->actingAs($user);
|
|
$tenant->makeCurrent();
|
|
|
|
expect(InventoryItemResource::canViewAny())->toBeTrue();
|
|
});
|
|
|
|
it('prevents viewing inventory items from other tenants', function () {
|
|
[$user, $tenant] = createUserWithTenant(role: 'owner');
|
|
$otherTenant = Tenant::factory()->create();
|
|
|
|
$this->actingAs($user);
|
|
$tenant->makeCurrent();
|
|
|
|
$record = InventoryItem::factory()->create([
|
|
'tenant_id' => $otherTenant->getKey(),
|
|
]);
|
|
|
|
expect(InventoryItemResource::canView($record))->toBeFalse();
|
|
});
|
|
|
|
it('allows viewing inventory items from the current tenant', function () {
|
|
[$user, $tenant] = createUserWithTenant(role: 'readonly');
|
|
|
|
$this->actingAs($user);
|
|
$tenant->makeCurrent();
|
|
|
|
$record = InventoryItem::factory()->create([
|
|
'tenant_id' => $tenant->getKey(),
|
|
]);
|
|
|
|
expect(InventoryItemResource::canView($record))->toBeTrue();
|
|
});
|
|
|
|
it('keeps composed persisted inventory filters scoped to the active tenant', function () {
|
|
$tenantA = Tenant::factory()->create();
|
|
[$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');
|
|
|
|
$tenantB = Tenant::factory()->create([
|
|
'workspace_id' => (int) $tenantA->workspace_id,
|
|
]);
|
|
|
|
createUserWithTenant(tenant: $tenantB, user: $user, role: 'owner');
|
|
|
|
$tenantAFresh = InventoryItem::factory()->create([
|
|
'tenant_id' => (int) $tenantA->getKey(),
|
|
'display_name' => 'Tenant A Fresh Windows',
|
|
'platform' => 'windows',
|
|
'last_seen_at' => now(),
|
|
]);
|
|
|
|
$tenantAStale = InventoryItem::factory()->create([
|
|
'tenant_id' => (int) $tenantA->getKey(),
|
|
'display_name' => 'Tenant A Stale Windows',
|
|
'platform' => 'windows',
|
|
'last_seen_at' => now()->subDays(3),
|
|
]);
|
|
|
|
$tenantBStale = InventoryItem::factory()->create([
|
|
'tenant_id' => (int) $tenantB->getKey(),
|
|
'display_name' => 'Tenant B Stale Windows',
|
|
'platform' => 'windows',
|
|
'last_seen_at' => now()->subDays(3),
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
$tenantA->makeCurrent();
|
|
Filament::setTenant($tenantA, true);
|
|
|
|
Livewire::test(ListInventoryItems::class)
|
|
->filterTable('platform', 'windows')
|
|
->filterTable('stale', '1')
|
|
->assertCanSeeTableRecords([$tenantAStale])
|
|
->assertCanNotSeeTableRecords([$tenantAFresh, $tenantBStale]);
|
|
|
|
Livewire::test(ListInventoryItems::class)
|
|
->assertSet('tableFilters.platform.value', 'windows')
|
|
->assertSet('tableFilters.stale.value', '1')
|
|
->assertCanSeeTableRecords([$tenantAStale])
|
|
->assertCanNotSeeTableRecords([$tenantAFresh, $tenantBStale]);
|
|
});
|
|
|
|
it('keeps persisted admin inventory search and filters inside the remembered canonical tenant after tenant changes', function () {
|
|
$tenantA = Tenant::factory()->create();
|
|
[$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');
|
|
|
|
$tenantB = Tenant::factory()->create([
|
|
'workspace_id' => (int) $tenantA->workspace_id,
|
|
]);
|
|
|
|
createUserWithTenant(tenant: $tenantB, user: $user, role: 'owner');
|
|
|
|
$tenantARecord = InventoryItem::factory()->create([
|
|
'tenant_id' => (int) $tenantA->getKey(),
|
|
'display_name' => 'Shared Windows Device',
|
|
'platform' => 'windows',
|
|
'last_seen_at' => now()->subDays(3),
|
|
]);
|
|
|
|
$tenantBRecord = InventoryItem::factory()->create([
|
|
'tenant_id' => (int) $tenantB->getKey(),
|
|
'display_name' => 'Shared Windows Device',
|
|
'platform' => 'windows',
|
|
'last_seen_at' => now()->subDays(3),
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
Filament::setCurrentPanel('admin');
|
|
Filament::setTenant(null, true);
|
|
Filament::bootCurrentPanel();
|
|
|
|
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenantA->workspace_id);
|
|
session()->put(WorkspaceContext::LAST_TENANT_IDS_SESSION_KEY, [
|
|
(string) $tenantA->workspace_id => (int) $tenantA->getKey(),
|
|
]);
|
|
|
|
Livewire::actingAs($user)->test(ListInventoryItems::class)
|
|
->searchTable('Shared Windows')
|
|
->filterTable('platform', 'windows')
|
|
->filterTable('stale', '1')
|
|
->assertCanSeeTableRecords([$tenantARecord])
|
|
->assertCanNotSeeTableRecords([$tenantBRecord]);
|
|
|
|
session()->put(WorkspaceContext::LAST_TENANT_IDS_SESSION_KEY, [
|
|
(string) $tenantA->workspace_id => (int) $tenantB->getKey(),
|
|
]);
|
|
|
|
Livewire::actingAs($user)->test(ListInventoryItems::class)
|
|
->assertSet('tableSearch', 'Shared Windows')
|
|
->assertSet('tableFilters.platform.value', 'windows')
|
|
->assertSet('tableFilters.stale.value', '1')
|
|
->assertCanSeeTableRecords([$tenantBRecord])
|
|
->assertCanNotSeeTableRecords([$tenantARecord]);
|
|
});
|
|
|
|
it('shows coverage truth without a basis-run link for readonly members missing inventory-sync capability', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'readonly');
|
|
|
|
OperationRun::factory()->create([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'type' => 'inventory_sync',
|
|
'status' => 'completed',
|
|
'outcome' => 'failed',
|
|
'context' => [
|
|
'inventory' => [
|
|
'coverage' => InventoryCoveragePayload::buildPayload([
|
|
'deviceConfiguration' => [
|
|
'status' => InventoryCoveragePayload::StatusFailed,
|
|
'item_count' => 0,
|
|
'error_code' => 'graph_forbidden',
|
|
],
|
|
], []),
|
|
],
|
|
],
|
|
'completed_at' => now(),
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->get(InventoryItemResource::getUrl('index', tenant: $tenant))
|
|
->assertOk()
|
|
->assertSee('Coverage basis')
|
|
->assertSee('Latest run detail is not available with your current role.')
|
|
->assertDontSee('Open basis run');
|
|
});
|
|
});
|