TenantAtlas/tests/Unit/TenantCurrentTest.php
Ahmed Darrazi 6d14d2544f feat: TenantPilot v1 - Complete implementation (Phases 1-12)
Complete implementation of TenantPilot v1 Intune Management Platform with
comprehensive backup, versioning, and restore capabilities.

CONSTITUTION & SPEC
- Ratified constitution v1.0.0 with 7 core principles
- Complete spec.md with 7 user stories (US1-7)
- Detailed plan.md with constitution compliance check
- Task breakdown with 125+ tasks across 12 phases

CORE FEATURES (US1-4)
- Policy inventory with Graph-based sync (US1)
- Backup creation with immutable JSONB snapshots (US2)
- Version history with diff viewer (human + JSON) (US3)
- Defensive restore with preview/dry-run (US4)

TENANT MANAGEMENT (US6-7)
- Full tenant CRUD with Entra ID app configuration
- Admin consent callback flow integration
- Tenant connectivity verification
- Permission health status monitoring
- 'Highlander' pattern: single current tenant with is_current flag

GRAPH ABSTRACTION
- Complete isolation layer (7 classes)
- GraphClientInterface with mockable implementations
- Error mapping, logging, and standardized responses
- Rate-limit aware design

DOMAIN SERVICES
- BackupService: immutable snapshot creation
- RestoreService: preview, selective restore, conflict detection
- VersionService: immutable version capture
- VersionDiff: human-readable and structured diffs
- PolicySyncService: Graph-based policy import
- TenantConfigService: connectivity testing
- TenantPermissionService: permission health checks
- AuditLogger: comprehensive audit trail

DATA MODEL
- 11 migrations with tenant-aware schema
- 8 Eloquent models with proper relationships
- SoftDeletes on Tenant, BackupSet, BackupItem, PolicyVersion, RestoreRun
- JSONB storage for snapshots, metadata, permissions
- Encrypted storage for client secrets
- Partial unique index for is_current tenant

FILAMENT ADMIN UI
- 5 main resources: Tenant, Policy, PolicyVersion, BackupSet, RestoreRun
- RelationManagers: Versions (Policy), BackupItems (BackupSet)
- Actions: Verify config, Admin consent, Make current, Delete/Force delete
- Filters: Status, Type, Platform, Archive state
- Permission panel with status indicators
- ActionGroup pattern for cleaner row actions

HOUSEKEEPING (Phases 10-12)
- Soft delete with archive status for all entities
- Force delete protection (blocks if dependencies exist)
- Tenant deactivation with cascade prevention
- Audit logging for all delete operations

TESTING
- 36 tests passing (125 assertions, 11.21s)
- Feature tests: Policy, Backup, Restore, Version, Tenant, Housekeeping
- Unit tests: VersionDiff, TenantCurrent, Permissions, Scopes
- Full TDD coverage for critical flows

CONFIGURATION
- config/tenantpilot.php: 10+ policy types with metadata
- config/intune_permissions.php: required Graph permissions
- config/graph.php: Graph client configuration

SAFETY & COMPLIANCE
- Constitution compliance: 7/7 principles ✓
- Safety-first operations: preview, confirmation, validation
- Immutable versioning: no in-place modifications
- Defensive restore: dry-run, selective, conflict detection
- Comprehensive auditability: all critical operations logged
- Tenant-aware architecture: multi-tenant ready
- Graph abstraction: isolated, mockable, testable
- Spec-driven development: spec → plan → tasks → implementation

OPERATIONAL READINESS
- Laravel Sail for local development
- Dokploy deployment documentation
- Queue/worker ready architecture
- Migration safety notes
- Environment variable documentation

Tests: 36 passed
Duration: 11.21s
Status: Production-ready (98% complete)
2025-12-12 02:27:54 +01:00

111 lines
2.8 KiB
PHP

<?php
use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
function restoreIntuneTenantId(string|false $original): void
{
$original !== false
? putenv("INTUNE_TENANT_ID={$original}")
: putenv('INTUNE_TENANT_ID');
}
it('returns env-configured tenant when active', function () {
$originalEnv = getenv('INTUNE_TENANT_ID');
putenv('INTUNE_TENANT_ID=tenant-env');
$preferred = Tenant::create([
'tenant_id' => 'tenant-env',
'name' => 'Preferred Tenant',
'is_current' => false,
]);
Tenant::create([
'tenant_id' => 'other-tenant',
'name' => 'Other Tenant',
'is_current' => true,
]);
$resolved = Tenant::current();
expect($resolved->id)->toBe($preferred->id);
restoreIntuneTenantId($originalEnv);
});
it('throws when env tenant is missing or inactive', function () {
$originalEnv = getenv('INTUNE_TENANT_ID');
putenv('INTUNE_TENANT_ID=missing-tenant');
expect(fn () => Tenant::current())->toThrow(
\RuntimeException::class,
'Configured INTUNE_TENANT_ID tenant is missing or inactive.'
);
restoreIntuneTenantId($originalEnv);
});
it('returns tenant marked as current when no env override', function () {
$originalEnv = getenv('INTUNE_TENANT_ID');
putenv('INTUNE_TENANT_ID=');
$current = Tenant::create([
'tenant_id' => 'tenant-current',
'name' => 'Current Tenant',
'is_current' => true,
]);
Tenant::create([
'tenant_id' => 'tenant-inactive',
'name' => 'Inactive Current Flag',
'status' => 'archived',
]);
$resolved = Tenant::current();
expect($resolved->id)->toBe($current->id);
restoreIntuneTenantId($originalEnv);
});
it('throws when no current tenant is selected', function () {
$originalEnv = getenv('INTUNE_TENANT_ID');
putenv('INTUNE_TENANT_ID=');
Tenant::create([
'tenant_id' => 'tenant-one',
'name' => 'Tenant One',
'is_current' => false,
]);
expect(fn () => Tenant::current())->toThrow(\RuntimeException::class, 'No current tenant selected.');
restoreIntuneTenantId($originalEnv);
});
it('makeCurrent toggles flags within a transaction', function () {
$originalEnv = getenv('INTUNE_TENANT_ID');
putenv('INTUNE_TENANT_ID=');
$first = Tenant::create([
'tenant_id' => 'tenant-first',
'name' => 'First Tenant',
'is_current' => true,
]);
$second = Tenant::create([
'tenant_id' => 'tenant-second',
'name' => 'Second Tenant',
]);
$second->makeCurrent();
expect($second->fresh()->is_current)->toBeTrue();
expect($first->fresh()->is_current)->toBeFalse();
restoreIntuneTenantId($originalEnv);
});