TenantAtlas/tests/Unit/GroupResolverTest.php
Ahmed Darrazi 86bb4cdbd6 feat: Phase 1+2 - Assignments & Scope Tags foundation
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)
2025-12-22 02:10:35 +01:00

187 lines
5.7 KiB
PHP

<?php
use App\Services\Graph\GraphException;
use App\Services\Graph\GraphLogger;
use App\Services\Graph\GroupResolver;
use App\Services\Graph\MicrosoftGraphClient;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
beforeEach(function () {
Cache::flush();
$this->graphClient = Mockery::mock(MicrosoftGraphClient::class);
$this->logger = Mockery::mock(GraphLogger::class);
$this->resolver = new GroupResolver($this->graphClient, $this->logger);
});
test('resolves all groups', function () {
$tenantId = 'tenant-123';
$groupIds = ['group-1', 'group-2', 'group-3'];
$graphResponse = [
'value' => [
['id' => 'group-1', 'displayName' => 'All Users'],
['id' => 'group-2', 'displayName' => 'HR Team'],
['id' => 'group-3', 'displayName' => 'Contractors'],
],
];
$this->graphClient
->shouldReceive('post')
->once()
->with('/directoryObjects/getByIds', [
'ids' => $groupIds,
'types' => ['group'],
], $tenantId)
->andReturn($graphResponse);
$this->logger
->shouldReceive('logDebug')
->once();
$result = $this->resolver->resolveGroupIds($groupIds, $tenantId);
expect($result)->toHaveKey('group-1')
->and($result['group-1'])->toBe([
'id' => 'group-1',
'displayName' => 'All Users',
'orphaned' => false,
])
->and($result)->toHaveKey('group-2')
->and($result['group-2']['orphaned'])->toBeFalse()
->and($result)->toHaveKey('group-3')
->and($result['group-3']['orphaned'])->toBeFalse();
});
test('handles orphaned ids', function () {
$tenantId = 'tenant-123';
$groupIds = ['group-1', 'group-2', 'group-3'];
$graphResponse = [
'value' => [
['id' => 'group-1', 'displayName' => 'All Users'],
// group-2 and group-3 are missing (deleted)
],
];
$this->graphClient
->shouldReceive('post')
->once()
->andReturn($graphResponse);
$this->logger
->shouldReceive('logDebug')
->once()
->with('Resolved group IDs', Mockery::on(function ($context) {
return $context['requested'] === 3
&& $context['resolved'] === 1
&& $context['orphaned'] === 2;
}));
$result = $this->resolver->resolveGroupIds($groupIds, $tenantId);
expect($result)->toHaveKey('group-1')
->and($result['group-1']['orphaned'])->toBeFalse()
->and($result)->toHaveKey('group-2')
->and($result['group-2'])->toBe([
'id' => 'group-2',
'displayName' => null,
'orphaned' => true,
])
->and($result)->toHaveKey('group-3')
->and($result['group-3']['orphaned'])->toBeTrue();
});
test('caches results', function () {
$tenantId = 'tenant-123';
$groupIds = ['group-1', 'group-2'];
$graphResponse = [
'value' => [
['id' => 'group-1', 'displayName' => 'All Users'],
['id' => 'group-2', 'displayName' => 'HR Team'],
],
];
// First call - should hit Graph API
$this->graphClient
->shouldReceive('post')
->once()
->andReturn($graphResponse);
$this->logger
->shouldReceive('logDebug')
->once();
$result1 = $this->resolver->resolveGroupIds($groupIds, $tenantId);
// Second call - should use cache (no Graph API call)
$result2 = $this->resolver->resolveGroupIds($groupIds, $tenantId);
expect($result1)->toBe($result2)
->and($result1)->toHaveCount(2);
});
test('returns empty array for empty input', function () {
$result = $this->resolver->resolveGroupIds([], 'tenant-123');
expect($result)->toBe([]);
});
test('handles graph exception gracefully', function () {
$tenantId = 'tenant-123';
$groupIds = ['group-1', 'group-2'];
$this->graphClient
->shouldReceive('post')
->once()
->andThrow(new GraphException('Graph API error', 500, ['request_id' => 'request-id-123']));
$this->logger
->shouldReceive('logWarning')
->once()
->with('Failed to resolve group IDs', Mockery::on(function ($context) use ($groupIds) {
return $context['group_ids'] === $groupIds
&& isset($context['context']['request_id']);
}));
$result = $this->resolver->resolveGroupIds($groupIds, $tenantId);
// All groups should be marked as orphaned on failure
expect($result)->toHaveKey('group-1')
->and($result['group-1']['orphaned'])->toBeTrue()
->and($result['group-1']['displayName'])->toBeNull()
->and($result)->toHaveKey('group-2')
->and($result['group-2']['orphaned'])->toBeTrue();
});
test('cache key is consistent regardless of array order', function () {
$tenantId = 'tenant-123';
$groupIds1 = ['group-1', 'group-2', 'group-3'];
$groupIds2 = ['group-3', 'group-1', 'group-2']; // Different order
$graphResponse = [
'value' => [
['id' => 'group-1', 'displayName' => 'All Users'],
['id' => 'group-2', 'displayName' => 'HR Team'],
['id' => 'group-3', 'displayName' => 'Contractors'],
],
];
// First call with groupIds1
$this->graphClient
->shouldReceive('post')
->once()
->andReturn($graphResponse);
$this->logger
->shouldReceive('logDebug')
->once();
$result1 = $this->resolver->resolveGroupIds($groupIds1, $tenantId);
// Second call with groupIds2 (different order) - should use cache
$result2 = $this->resolver->resolveGroupIds($groupIds2, $tenantId);
expect($result1)->toBe($result2);
});