TenantAtlas/tests/Unit/Policies/ProviderConnectionPolicyDedicatedTest.php
ahmido bab01f07a9 feat: standardize platform provider identity (#166)
## Summary
- standardize Microsoft provider connections around explicit platform vs dedicated identity modes
- centralize admin-consent URL and runtime identity resolution so platform flows no longer fall back to tenant-local credentials
- add migration classification, richer consent and verification state handling, dedicated override management, and focused regression coverage

## Validation
- focused repo test coverage was added across provider identity, onboarding, audit, policy, guard, and migration flows
- latest explicit passing run in the workspace: `vendor/bin/sail artisan test --compact tests/Feature/AdminConsentCallbackTest.php tests/Feature/Audit/ProviderConnectionConsentAuditTest.php`

## Notes
- branch includes the full Spec 137 artifact set under `specs/137-platform-provider-identity/`
- target base branch: `dev`

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #166
2026-03-13 16:29:08 +00:00

100 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\ProviderConnection;
use App\Models\Tenant;
use App\Models\User;
use App\Models\WorkspaceMembership;
use App\Policies\ProviderConnectionPolicy;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('allows tenant owners to manage dedicated overrides for accessible provider connections', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
$connection = ProviderConnection::factory()->platform()->create([
'workspace_id' => (int) $tenant->workspace_id,
'tenant_id' => (int) $tenant->getKey(),
'provider' => 'microsoft',
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
$result = app(ProviderConnectionPolicy::class)->manageDedicated($user, $connection);
expect($result)->toBeTrue();
});
it('forbids tenant managers from dedicated override actions while still allowing standard updates', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'manager', ensureDefaultMicrosoftProviderConnection: false);
$connection = ProviderConnection::factory()->platform()->create([
'workspace_id' => (int) $tenant->workspace_id,
'tenant_id' => (int) $tenant->getKey(),
'provider' => 'microsoft',
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
$policy = app(ProviderConnectionPolicy::class);
expect($policy->update($user, $connection))->toBeTrue();
$result = $policy->manageDedicated($user, $connection);
expect($result)->toBeFalse();
});
it('returns not found for non-members on dedicated override authorization checks', function (): void {
$tenant = Tenant::factory()->create();
$user = User::factory()->create();
$connection = ProviderConnection::factory()->platform()->create([
'workspace_id' => (int) $tenant->workspace_id,
'tenant_id' => (int) $tenant->getKey(),
'provider' => 'microsoft',
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
$result = app(ProviderConnectionPolicy::class)->manageDedicated($user, $connection);
expect($result)->toBeInstanceOf(Response::class)
->and($result->allowed())->toBeFalse()
->and($result->status())->toBe(404);
});
it('returns not found when the provider connection is outside the active workspace', function (): void {
[$user, $tenantA] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);
$tenantB = Tenant::factory()->create();
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $tenantB->workspace_id,
'user_id' => (int) $user->getKey(),
'role' => 'owner',
]);
$user->tenants()->syncWithoutDetaching([
(int) $tenantB->getKey() => ['role' => 'owner'],
]);
$connection = ProviderConnection::factory()->platform()->create([
'workspace_id' => (int) $tenantB->workspace_id,
'tenant_id' => (int) $tenantB->getKey(),
'provider' => 'microsoft',
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenantA->workspace_id);
$result = app(ProviderConnectionPolicy::class)->manageDedicated($user, $connection);
expect($result)->toBeInstanceOf(Response::class)
->and($result->allowed())->toBeFalse()
->and($result->status())->toBe(404);
});