TenantAtlas/tests/Feature/Filament/SettingsCatalogPolicySyncTest.php
2025-12-14 20:23:18 +01:00

129 lines
4.1 KiB
PHP

<?php
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\Tenant;
use App\Models\User;
use App\Services\Graph\GraphClientInterface;
use App\Services\Graph\GraphResponse;
use App\Services\Intune\PolicySyncService;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
class SettingsCatalogFakeGraphClient 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('settings catalog policies sync as their own type and render in the list', function () {
$responses = [
'deviceConfiguration' => new GraphResponse(true, [
[
'id' => 'config-1',
'displayName' => 'Device Config 1',
'platform' => 'windows',
],
]),
'settingsCatalogPolicy' => new GraphResponse(true, [
[
'id' => 'scp-1',
'name' => 'Settings Catalog Alpha',
'platform' => 'windows',
'@odata.type' => '#microsoft.graph.deviceManagementConfigurationPolicy',
'settings' => [
['displayName' => 'Setting A', 'value' => 'on'],
],
],
]),
];
app()->instance(GraphClientInterface::class, new SettingsCatalogFakeGraphClient($responses));
$tenant = Tenant::create([
'tenant_id' => env('INTUNE_TENANT_ID', 'local-tenant'),
'name' => 'Tenant One',
'metadata' => [],
'is_current' => true,
]);
putenv('INTUNE_TENANT_ID='.$tenant->tenant_id);
$_ENV['INTUNE_TENANT_ID'] = $tenant->tenant_id;
$_SERVER['INTUNE_TENANT_ID'] = $tenant->tenant_id;
$tenant->makeCurrent();
expect(Tenant::current()->id)->toBe($tenant->id);
app(PolicySyncService::class)->syncPolicies($tenant);
$settingsPolicy = Policy::where('policy_type', 'settingsCatalogPolicy')->first();
expect($settingsPolicy)->not->toBeNull();
expect($settingsPolicy->display_name)->toBe('Settings Catalog Alpha');
expect($settingsPolicy->platform)->toBe('windows');
expect(Policy::where('policy_type', 'deviceConfiguration')->count())->toBe(1);
expect(Policy::where('policy_type', 'deviceConfiguration')->where('external_id', 'scp-1')->exists())->toBeFalse();
PolicyVersion::create([
'tenant_id' => $tenant->id,
'policy_id' => $settingsPolicy->id,
'version_number' => 1,
'policy_type' => $settingsPolicy->policy_type,
'platform' => $settingsPolicy->platform,
'created_by' => 'tester@example.com',
'captured_at' => CarbonImmutable::now(),
'snapshot' => [
'@odata.type' => '#microsoft.graph.deviceManagementConfigurationPolicy',
'settings' => [
['displayName' => 'Setting A', 'value' => 'on'],
],
],
]);
$user = User::factory()->create();
$response = $this
->actingAs($user)
->get(route('filament.admin.resources.policies.index'));
$response->assertOk();
$response->assertSee('Settings Catalog Policy');
$response->assertSee('Settings Catalog Alpha');
$response->assertSee('Available');
});