Phase 1: Setup & Database (13 tasks completed) - Add assignments JSONB column to backup_items table - Add group_mapping JSONB column to restore_runs table - Extend BackupItem model with 7 assignment accessor methods - Extend RestoreRun model with 8 group mapping helper methods - Add scopeWithAssignments() query scope to BackupItem - Update graph_contracts.php with assignments endpoints - Create 5 factories: BackupItem, RestoreRun, Tenant, BackupSet, Policy - Add 30 unit tests (15 BackupItem, 15 RestoreRun) - all passing Phase 2: Graph API Integration (16 tasks completed) - Create AssignmentFetcher service with fallback strategy - Create GroupResolver service with orphaned ID handling - Create ScopeTagResolver service with 1-hour caching - Implement fail-soft error handling for all services - Add 17 unit tests (5 AssignmentFetcher, 6 GroupResolver, 6 ScopeTagResolver) - all passing - Total: 71 assertions across all Phase 2 tests Test Results: - Phase 1: 30/30 tests passing (45 assertions) - Phase 2: 17/17 tests passing (71 assertions) - Total: 47/47 tests passing (116 assertions) - Code formatted with Pint (PSR-12 compliant) Next: Phase 3 - US1 Backup with Assignments (12 tasks)
149 lines
4.4 KiB
PHP
149 lines
4.4 KiB
PHP
<?php
|
|
|
|
use App\Services\Graph\AssignmentFetcher;
|
|
use App\Services\Graph\GraphException;
|
|
use App\Services\Graph\GraphLogger;
|
|
use App\Services\Graph\MicrosoftGraphClient;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
uses(TestCase::class, RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
$this->graphClient = Mockery::mock(MicrosoftGraphClient::class);
|
|
$this->logger = Mockery::mock(GraphLogger::class);
|
|
$this->fetcher = new AssignmentFetcher($this->graphClient, $this->logger);
|
|
});
|
|
|
|
test('primary endpoint success', function () {
|
|
$tenantId = 'tenant-123';
|
|
$policyId = 'policy-456';
|
|
$assignments = [
|
|
['id' => 'assign-1', 'target' => ['@odata.type' => '#microsoft.graph.groupAssignmentTarget', 'groupId' => 'group-1']],
|
|
['id' => 'assign-2', 'target' => ['@odata.type' => '#microsoft.graph.groupAssignmentTarget', 'groupId' => 'group-2']],
|
|
];
|
|
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->with("/deviceManagement/configurationPolicies/{$policyId}/assignments", $tenantId)
|
|
->andReturn(['value' => $assignments]);
|
|
|
|
$this->logger
|
|
->shouldReceive('logDebug')
|
|
->once()
|
|
->with('Fetched assignments via primary endpoint', Mockery::any());
|
|
|
|
$result = $this->fetcher->fetch($tenantId, $policyId);
|
|
|
|
expect($result)->toBe($assignments);
|
|
});
|
|
|
|
test('fallback on empty response', function () {
|
|
$tenantId = 'tenant-123';
|
|
$policyId = 'policy-456';
|
|
$assignments = [
|
|
['id' => 'assign-1', 'target' => ['@odata.type' => '#microsoft.graph.groupAssignmentTarget', 'groupId' => 'group-1']],
|
|
];
|
|
|
|
// Primary returns empty
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->with("/deviceManagement/configurationPolicies/{$policyId}/assignments", $tenantId)
|
|
->andReturn(['value' => []]);
|
|
|
|
// Fallback returns assignments
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->with('/deviceManagement/configurationPolicies', $tenantId, [
|
|
'$expand' => 'assignments',
|
|
'$filter' => "id eq '{$policyId}'",
|
|
])
|
|
->andReturn(['value' => [['id' => $policyId, 'assignments' => $assignments]]]);
|
|
|
|
$this->logger
|
|
->shouldReceive('logDebug')
|
|
->twice();
|
|
|
|
$result = $this->fetcher->fetch($tenantId, $policyId);
|
|
|
|
expect($result)->toBe($assignments);
|
|
});
|
|
|
|
test('fail soft on error', function () {
|
|
$tenantId = 'tenant-123';
|
|
$policyId = 'policy-456';
|
|
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->andThrow(new GraphException('Graph API error', 500, ['request_id' => 'request-id-123']));
|
|
|
|
$this->logger
|
|
->shouldReceive('logWarning')
|
|
->once()
|
|
->with('Failed to fetch assignments', Mockery::on(function ($context) use ($tenantId, $policyId) {
|
|
return $context['tenant_id'] === $tenantId
|
|
&& $context['policy_id'] === $policyId
|
|
&& isset($context['context']['request_id']);
|
|
}));
|
|
|
|
$result = $this->fetcher->fetch($tenantId, $policyId);
|
|
|
|
expect($result)->toBe([]);
|
|
});
|
|
|
|
test('returns empty array when both endpoints return empty', function () {
|
|
$tenantId = 'tenant-123';
|
|
$policyId = 'policy-456';
|
|
|
|
// Primary returns empty
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->with("/deviceManagement/configurationPolicies/{$policyId}/assignments", $tenantId)
|
|
->andReturn(['value' => []]);
|
|
|
|
// Fallback returns empty
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->with('/deviceManagement/configurationPolicies', $tenantId, Mockery::any())
|
|
->andReturn(['value' => []]);
|
|
|
|
$this->logger
|
|
->shouldReceive('logDebug')
|
|
->times(2);
|
|
|
|
$result = $this->fetcher->fetch($tenantId, $policyId);
|
|
|
|
expect($result)->toBe([]);
|
|
});
|
|
|
|
test('fallback handles missing assignments key', function () {
|
|
$tenantId = 'tenant-123';
|
|
$policyId = 'policy-456';
|
|
|
|
// Primary returns empty
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->andReturn(['value' => []]);
|
|
|
|
// Fallback returns policy without assignments key
|
|
$this->graphClient
|
|
->shouldReceive('get')
|
|
->once()
|
|
->andReturn(['value' => [['id' => $policyId]]]);
|
|
|
|
$this->logger
|
|
->shouldReceive('logDebug')
|
|
->times(2);
|
|
|
|
$result = $this->fetcher->fetch($tenantId, $policyId);
|
|
|
|
expect($result)->toBe([]);
|
|
});
|