TenantAtlas/tests/Feature/Jobs/PolicySyncIgnoredRevivalTest.php
ahmido d62c8825a1 feat/005-bulk-operations (#5)
## Summary
<!-- Kurz: Was ändert sich und warum? -->

## Spec-Driven Development (SDD)
- [ ] Es gibt eine Spec unter `specs/<NNN>-<feature>/`
- [ ] Enthaltene Dateien: `plan.md`, `tasks.md`, `spec.md`
- [ ] Spec beschreibt Verhalten/Acceptance Criteria (nicht nur Implementation)
- [ ] Wenn sich Anforderungen während der Umsetzung geändert haben: Spec/Plan/Tasks wurden aktualisiert

## Implementation
- [ ] Implementierung entspricht der Spec
- [ ] Edge cases / Fehlerfälle berücksichtigt
- [ ] Keine unbeabsichtigten Änderungen außerhalb des Scopes

## Tests
- [ ] Tests ergänzt/aktualisiert (Pest/PHPUnit)
- [ ] Relevante Tests lokal ausgeführt (`./vendor/bin/sail artisan test` oder `php artisan test`)

## Migration / Config / Ops (falls relevant)
- [ ] Migration(en) enthalten und getestet
- [ ] Rollback bedacht (rückwärts kompatibel, sichere Migration)
- [ ] Neue Env Vars dokumentiert (`.env.example` / Doku)
- [ ] Queue/cron/storage Auswirkungen geprüft

## UI (Filament/Livewire) (falls relevant)
- [ ] UI-Flows geprüft
- [ ] Screenshots/Notizen hinzugefügt

## Notes
<!-- Links, Screenshots, Follow-ups, offene Punkte -->

Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local>
Reviewed-on: #5
2025-12-25 13:32:36 +00:00

160 lines
4.9 KiB
PHP

<?php
use App\Models\Policy;
use App\Models\Tenant;
use App\Services\Graph\GraphClientInterface;
use App\Services\Graph\GraphResponse;
use App\Services\Intune\PolicySyncService;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
class FakeGraphClientForSync implements GraphClientInterface
{
/**
* @param array<string, GraphResponse> $responses
*/
public function __construct(private array $responses = []) {}
public function listPolicies(string $policyType, array $options = []): GraphResponse
{
return $this->responses[$policyType] ?? new GraphResponse(true, []);
}
public function getPolicy(string $policyType, string $policyId, array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function getOrganization(array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function applyPolicy(string $policyType, string $policyId, array $payload, array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function getServicePrincipalPermissions(array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function request(string $method, string $path, array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
}
test('sync revives ignored policies when they exist in Intune', function () {
$tenant = Tenant::create([
'tenant_id' => env('INTUNE_TENANT_ID', 'test-tenant'),
'name' => 'Test Tenant',
'metadata' => [],
'is_current' => true,
]);
// Create an ignored policy
$policy = Policy::create([
'tenant_id' => $tenant->id,
'external_id' => 'policy-123',
'policy_type' => 'deviceConfiguration',
'display_name' => 'Test Policy',
'platform' => 'windows',
'ignored_at' => now(),
]);
expect($policy->ignored_at)->not->toBeNull();
// Mock Graph response with the same policy
$responses = [
'deviceConfiguration' => new GraphResponse(true, [
[
'id' => 'policy-123',
'displayName' => 'Test Policy (Updated)',
'platform' => 'windows',
],
]),
];
app()->instance(GraphClientInterface::class, new FakeGraphClientForSync($responses));
// Sync policies
app(PolicySyncService::class)->syncPolicies($tenant);
// Refresh the policy
$policy->refresh();
// Policy should no longer be ignored
expect($policy->ignored_at)->toBeNull();
expect($policy->display_name)->toBe('Test Policy (Updated)');
expect($policy->last_synced_at)->not->toBeNull();
});
test('sync creates new policies even if ignored ones exist with same external_id', function () {
$tenant = Tenant::create([
'tenant_id' => env('INTUNE_TENANT_ID', 'test-tenant-2'),
'name' => 'Test Tenant 2',
'metadata' => [],
'is_current' => true,
]);
// Create multiple ignored policies
Policy::create([
'tenant_id' => $tenant->id,
'external_id' => 'policy-abc',
'policy_type' => 'deviceConfiguration',
'display_name' => 'Old Policy ABC',
'platform' => 'windows',
'ignored_at' => now()->subDay(),
]);
Policy::create([
'tenant_id' => $tenant->id,
'external_id' => 'policy-def',
'policy_type' => 'deviceCompliancePolicy',
'display_name' => 'Old Policy DEF',
'platform' => 'android',
'ignored_at' => now()->subDay(),
]);
expect(Policy::active()->count())->toBe(0);
expect(Policy::ignored()->count())->toBe(2);
// Mock Graph response with same policy IDs but potentially different data
$responses = [
'deviceConfiguration' => new GraphResponse(true, [
[
'id' => 'policy-abc',
'displayName' => 'Restored Policy ABC',
'platform' => 'windows',
],
]),
'deviceCompliancePolicy' => new GraphResponse(true, [
[
'id' => 'policy-def',
'displayName' => 'Restored Policy DEF',
'platform' => 'android',
],
]),
];
app()->instance(GraphClientInterface::class, new FakeGraphClientForSync($responses));
// Sync policies
app(PolicySyncService::class)->syncPolicies($tenant);
// All policies should now be active
expect(Policy::active()->count())->toBe(2);
expect(Policy::ignored()->count())->toBe(0);
$policyAbc = Policy::where('external_id', 'policy-abc')->first();
expect($policyAbc->display_name)->toBe('Restored Policy ABC');
expect($policyAbc->ignored_at)->toBeNull();
$policyDef = Policy::where('external_id', 'policy-def')->first();
expect($policyDef->display_name)->toBe('Restored Policy DEF');
expect($policyDef->ignored_at)->toBeNull();
});