TenantAtlas/apps/platform/tests/Unit/Policies/ManagedEnvironmentOnboardingSessionPolicyTest.php
ahmido 292d555eac refactor: consolidate internal tenant model naming (#355)
## Summary
- consolidate internal platform naming from `Tenant` to `Environment` / `ManagedEnvironment` across models, controllers, services, and Filament resources
- rename environment-scoped UI surfaces such as dashboards, chooser flows, navigation, and related widgets to match the updated environment-first domain language
- align middleware, onboarding/review lifecycle services, jobs, and route/context controllers with the new environment-scoped architecture

## Validation
- not rerun as part of this commit/push/PR request

## Notes
- branch is 1 commit ahead of `platform-dev`
- main commit: `refactor: consolidate internal tenant model naming`

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #355
2026-05-14 11:13:28 +00:00

121 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Models\WorkspaceMembership;
use App\Policies\ManagedEnvironmentOnboardingSessionPolicy;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('returns not found for actors outside the active workspace when viewing a draft', function (): void {
$tenant = ManagedEnvironment::factory()->onboarding()->create();
$owner = User::factory()->create();
$otherWorkspaceUser = User::factory()->create();
$otherWorkspace = \App\Models\Workspace::factory()->create();
createUserWithTenant(
tenant: $tenant,
user: $owner,
role: 'owner',
workspaceRole: 'owner',
ensureDefaultMicrosoftProviderConnection: false,
);
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $otherWorkspace->getKey(),
'user_id' => (int) $otherWorkspaceUser->getKey(),
'role' => 'owner',
]);
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'started_by' => $owner,
'updated_by' => $owner,
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $otherWorkspace->getKey());
$result = app(ManagedEnvironmentOnboardingSessionPolicy::class)->view($otherWorkspaceUser, $draft);
expect($result)->toBeInstanceOf(Response::class)
->and($result->allowed())->toBeFalse()
->and($result->status())->toBe(404);
});
it('returns not found for workspace members missing linked tenant entitlement', function (): void {
$tenant = ManagedEnvironment::factory()->onboarding()->create();
$owner = User::factory()->create();
$workspaceOnlyUser = User::factory()->create();
createUserWithTenant(
tenant: $tenant,
user: $owner,
role: 'owner',
workspaceRole: 'owner',
ensureDefaultMicrosoftProviderConnection: false,
);
WorkspaceMembership::factory()->create([
'workspace_id' => (int) $tenant->workspace_id,
'user_id' => (int) $workspaceOnlyUser->getKey(),
'role' => 'owner',
]);
$otherTenant = ManagedEnvironment::factory()->create([
'workspace_id' => (int) $tenant->workspace_id,
]);
createUserWithTenant(tenant: $otherTenant, user: $workspaceOnlyUser, role: 'owner');
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'started_by' => $owner,
'updated_by' => $owner,
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
$result = app(ManagedEnvironmentOnboardingSessionPolicy::class)->view($workspaceOnlyUser, $draft);
expect($result)->toBeInstanceOf(Response::class)
->and($result->allowed())->toBeFalse()
->and($result->status())->toBe(404);
});
it('returns an honest forbidden message for entitled actors missing onboarding capability', function (): void {
$tenant = ManagedEnvironment::factory()->onboarding()->create();
$readonlyUser = User::factory()->create();
createUserWithTenant(
tenant: $tenant,
user: $readonlyUser,
role: 'readonly',
workspaceRole: 'readonly',
ensureDefaultMicrosoftProviderConnection: false,
);
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'started_by' => $readonlyUser,
'updated_by' => $readonlyUser,
]);
session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
$viewResult = app(ManagedEnvironmentOnboardingSessionPolicy::class)->view($readonlyUser, $draft);
$cancelResult = app(ManagedEnvironmentOnboardingSessionPolicy::class)->cancel($readonlyUser, $draft);
expect($viewResult)->toBeInstanceOf(Response::class)
->and($viewResult->allowed())->toBeFalse()
->and($viewResult->message())->toBe('You do not have permission to continue this onboarding draft.')
->and($cancelResult)->toBeInstanceOf(Response::class)
->and($cancelResult->allowed())->toBeFalse()
->and($cancelResult->message())->toBe('You do not have permission to cancel this onboarding draft.');
});