trypost/tests/Feature/WorkspaceInviteControllerTest.php

285 lines
9.1 KiB
PHP
Raw Normal View History

2026-01-18 23:33:45 +00:00
<?php
declare(strict_types=1);
2026-01-18 23:33:45 +00:00
use App\Enums\UserWorkspace\Role as WorkspaceRole;
use App\Mail\WorkspaceInvite as WorkspaceInviteMail;
use App\Models\Account;
use App\Models\Invite;
2026-01-18 23:33:45 +00:00
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Mail;
beforeEach(function () {
Mail::fake();
config(['trypost.self_hosted' => true]);
$this->account = Account::factory()->create();
$this->user = User::factory()->create([
'account_id' => $this->account->id,
]);
$this->account->update(['owner_id' => $this->user->id]);
$this->workspace = Workspace::factory()->create([
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$this->workspace->members()->attach($this->user->id, ['role' => WorkspaceRole::Admin->value]);
2026-01-18 23:33:45 +00:00
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
// Index tests
test('members index requires authentication', function () {
$response = $this->get(route('app.members'));
2026-01-18 23:33:45 +00:00
$response->assertRedirect(route('login'));
});
test('members page shows members and invites', function () {
Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->user->id,
'workspaces' => [$this->workspace->id],
2026-01-18 23:33:45 +00:00
]);
$response = $this->actingAs($this->user)->get(route('app.members'));
2026-01-18 23:33:45 +00:00
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('settings/Members', false)
2026-01-18 23:33:45 +00:00
->has('workspace')
->has('members')
->has('invites')
->has('owner')
->has('roles')
2026-01-18 23:33:45 +00:00
);
});
// Store invite tests
test('store invite requires authentication', function () {
$response = $this->post(route('app.invites.store'), [
2026-01-18 23:33:45 +00:00
'email' => 'test@example.com',
'role' => WorkspaceRole::Member->value,
]);
$response->assertRedirect(route('login'));
});
test('store invite creates invite and sends email', function () {
$response = $this->actingAs($this->user)->post(route('app.invites.store'), [
2026-01-18 23:33:45 +00:00
'email' => 'newmember@example.com',
'role' => WorkspaceRole::Member->value,
]);
$response->assertRedirect();
$this->assertDatabaseHas('invites', [
'account_id' => $this->account->id,
2026-01-18 23:33:45 +00:00
'email' => 'newmember@example.com',
]);
Mail::assertQueued(WorkspaceInviteMail::class);
});
test('store invite fails if invite already exists', function () {
Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->user->id,
2026-01-18 23:33:45 +00:00
'email' => 'existing@example.com',
'workspaces' => [$this->workspace->id],
2026-01-18 23:33:45 +00:00
]);
$response = $this->actingAs($this->user)->post(route('app.invites.store'), [
2026-01-18 23:33:45 +00:00
'email' => 'existing@example.com',
'role' => WorkspaceRole::Member->value,
]);
$response->assertSessionHasErrors('email');
});
test('store invite fails if user is already member', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
2026-01-18 23:33:45 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Member->value]);
$response = $this->actingAs($this->user)->post(route('app.invites.store'), [
2026-01-18 23:33:45 +00:00
'email' => $member->email,
'role' => WorkspaceRole::Member->value,
]);
$response->assertSessionHasErrors('email');
});
// Destroy invite tests
test('destroy invite requires authentication', function () {
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->user->id,
'workspaces' => [$this->workspace->id],
2026-01-18 23:33:45 +00:00
]);
$response = $this->delete(route('app.invites.destroy', $invite));
2026-01-18 23:33:45 +00:00
$response->assertRedirect(route('login'));
});
test('destroy invite deletes invite', function () {
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->user->id,
'workspaces' => [$this->workspace->id],
2026-01-18 23:33:45 +00:00
]);
$response = $this->actingAs($this->user)->delete(route('app.invites.destroy', $invite));
2026-01-18 23:33:45 +00:00
$response->assertRedirect();
expect(Invite::find($invite->id))->toBeNull();
2026-01-18 23:33:45 +00:00
});
test('destroy invite returns 404 for other account invite', function () {
$otherAccount = Account::factory()->create();
$invite = Invite::factory()->create([
'account_id' => $otherAccount->id,
'workspaces' => [],
2026-01-18 23:33:45 +00:00
]);
$response = $this->actingAs($this->user)->delete(route('app.invites.destroy', $invite));
2026-01-18 23:33:45 +00:00
$response->assertNotFound();
});
// Remove member tests
test('remove member requires authentication', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
2026-01-18 23:33:45 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Member->value]);
$response = $this->delete(route('app.members.remove', $member));
2026-01-18 23:33:45 +00:00
$response->assertRedirect(route('login'));
});
test('remove member removes user from workspace', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
2026-01-18 23:33:45 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Member->value]);
$response = $this->actingAs($this->user)->delete(route('app.members.remove', $member));
2026-01-18 23:33:45 +00:00
$response->assertRedirect();
expect($this->workspace->hasMember($member))->toBeFalse();
});
test('remove member fails for owner', function () {
$response = $this->actingAs($this->user)->delete(route('app.members.remove', $this->user));
2026-01-18 23:33:45 +00:00
$response->assertSessionHasErrors('member');
});
refactor: auth split layout, subscribe redesign, onboarding, i18n, cookie locale Auth pages: - Create AuthSplitLayout with animated feature slides (6 slides, 3 languages) - All auth pages use split layout (form left, visual right) - Add show/hide password toggle with tooltip on Register - Legal footer only shown on Register via showLegal prop Subscribe page: - Redesign to match auth card pattern (centered, clean) - Platform icons, feature checklist, dynamic trial days (trialDays - 1) - Add "Switch workspace" link - Full i18n (en, es, pt-BR) Onboarding: - Rename URLs: step1 -> role, step2 -> connect - Add enforceStep() to prevent skipping/going back steps - Redirect /onboarding to /onboarding/role - Redesign Step2 with AuthSplitLayout and compact platform list - 21 tests covering all step enforcement scenarios Workspaces page: - Redesign with AuthSplitLayout (list with avatars, current badge) Language system: - Move locale from DB to cookie (forever, unencrypted, session.domain) - Create SetLocale middleware (sets cookie if missing, validates against config) - Rename lang/pt-br to lang/pt-BR - Add dayjs es locale Other: - Copy utils.ts from sendkit (formatNumber, formatMoney, copyToClipboard) - ConfirmDeleteModal with text confirmation (sendkit pattern) - i18n for ConfirmDeleteModal internal strings (common.php) - EmptyState component for posts index - Exact match for "All" posts in sidebar - Posts breadcrumbs show current status filter - DialogFooter buttons aligned left - API Keys page redesign with Table, DropdownMenu, EmptyState - Extract CreateApiKeyDialog and InviteMemberDialog to components - Remove API Keys from sidebar - DropdownMenuItem destructive variant for Remove action
2026-03-30 14:53:42 +00:00
// Update role tests
test('update role requires authentication', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
refactor: auth split layout, subscribe redesign, onboarding, i18n, cookie locale Auth pages: - Create AuthSplitLayout with animated feature slides (6 slides, 3 languages) - All auth pages use split layout (form left, visual right) - Add show/hide password toggle with tooltip on Register - Legal footer only shown on Register via showLegal prop Subscribe page: - Redesign to match auth card pattern (centered, clean) - Platform icons, feature checklist, dynamic trial days (trialDays - 1) - Add "Switch workspace" link - Full i18n (en, es, pt-BR) Onboarding: - Rename URLs: step1 -> role, step2 -> connect - Add enforceStep() to prevent skipping/going back steps - Redirect /onboarding to /onboarding/role - Redesign Step2 with AuthSplitLayout and compact platform list - 21 tests covering all step enforcement scenarios Workspaces page: - Redesign with AuthSplitLayout (list with avatars, current badge) Language system: - Move locale from DB to cookie (forever, unencrypted, session.domain) - Create SetLocale middleware (sets cookie if missing, validates against config) - Rename lang/pt-br to lang/pt-BR - Add dayjs es locale Other: - Copy utils.ts from sendkit (formatNumber, formatMoney, copyToClipboard) - ConfirmDeleteModal with text confirmation (sendkit pattern) - i18n for ConfirmDeleteModal internal strings (common.php) - EmptyState component for posts index - Exact match for "All" posts in sidebar - Posts breadcrumbs show current status filter - DialogFooter buttons aligned left - API Keys page redesign with Table, DropdownMenu, EmptyState - Extract CreateApiKeyDialog and InviteMemberDialog to components - Remove API Keys from sidebar - DropdownMenuItem destructive variant for Remove action
2026-03-30 14:53:42 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Member->value]);
$response = $this->put(route('app.members.update-role', $member), [
'role' => WorkspaceRole::Admin->value,
]);
$response->assertRedirect(route('login'));
});
test('update role changes member to admin', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
refactor: auth split layout, subscribe redesign, onboarding, i18n, cookie locale Auth pages: - Create AuthSplitLayout with animated feature slides (6 slides, 3 languages) - All auth pages use split layout (form left, visual right) - Add show/hide password toggle with tooltip on Register - Legal footer only shown on Register via showLegal prop Subscribe page: - Redesign to match auth card pattern (centered, clean) - Platform icons, feature checklist, dynamic trial days (trialDays - 1) - Add "Switch workspace" link - Full i18n (en, es, pt-BR) Onboarding: - Rename URLs: step1 -> role, step2 -> connect - Add enforceStep() to prevent skipping/going back steps - Redirect /onboarding to /onboarding/role - Redesign Step2 with AuthSplitLayout and compact platform list - 21 tests covering all step enforcement scenarios Workspaces page: - Redesign with AuthSplitLayout (list with avatars, current badge) Language system: - Move locale from DB to cookie (forever, unencrypted, session.domain) - Create SetLocale middleware (sets cookie if missing, validates against config) - Rename lang/pt-br to lang/pt-BR - Add dayjs es locale Other: - Copy utils.ts from sendkit (formatNumber, formatMoney, copyToClipboard) - ConfirmDeleteModal with text confirmation (sendkit pattern) - i18n for ConfirmDeleteModal internal strings (common.php) - EmptyState component for posts index - Exact match for "All" posts in sidebar - Posts breadcrumbs show current status filter - DialogFooter buttons aligned left - API Keys page redesign with Table, DropdownMenu, EmptyState - Extract CreateApiKeyDialog and InviteMemberDialog to components - Remove API Keys from sidebar - DropdownMenuItem destructive variant for Remove action
2026-03-30 14:53:42 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Member->value]);
$response = $this->actingAs($this->user)->put(route('app.members.update-role', $member), [
'role' => WorkspaceRole::Admin->value,
]);
$response->assertRedirect();
expect($this->workspace->members()->where('user_id', $member->id)->first()->pivot->role)->toBe(WorkspaceRole::Admin->value);
});
test('update role changes admin to member', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
refactor: auth split layout, subscribe redesign, onboarding, i18n, cookie locale Auth pages: - Create AuthSplitLayout with animated feature slides (6 slides, 3 languages) - All auth pages use split layout (form left, visual right) - Add show/hide password toggle with tooltip on Register - Legal footer only shown on Register via showLegal prop Subscribe page: - Redesign to match auth card pattern (centered, clean) - Platform icons, feature checklist, dynamic trial days (trialDays - 1) - Add "Switch workspace" link - Full i18n (en, es, pt-BR) Onboarding: - Rename URLs: step1 -> role, step2 -> connect - Add enforceStep() to prevent skipping/going back steps - Redirect /onboarding to /onboarding/role - Redesign Step2 with AuthSplitLayout and compact platform list - 21 tests covering all step enforcement scenarios Workspaces page: - Redesign with AuthSplitLayout (list with avatars, current badge) Language system: - Move locale from DB to cookie (forever, unencrypted, session.domain) - Create SetLocale middleware (sets cookie if missing, validates against config) - Rename lang/pt-br to lang/pt-BR - Add dayjs es locale Other: - Copy utils.ts from sendkit (formatNumber, formatMoney, copyToClipboard) - ConfirmDeleteModal with text confirmation (sendkit pattern) - i18n for ConfirmDeleteModal internal strings (common.php) - EmptyState component for posts index - Exact match for "All" posts in sidebar - Posts breadcrumbs show current status filter - DialogFooter buttons aligned left - API Keys page redesign with Table, DropdownMenu, EmptyState - Extract CreateApiKeyDialog and InviteMemberDialog to components - Remove API Keys from sidebar - DropdownMenuItem destructive variant for Remove action
2026-03-30 14:53:42 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Admin->value]);
$response = $this->actingAs($this->user)->put(route('app.members.update-role', $member), [
'role' => WorkspaceRole::Member->value,
]);
$response->assertRedirect();
expect($this->workspace->members()->where('user_id', $member->id)->first()->pivot->role)->toBe(WorkspaceRole::Member->value);
});
test('update role fails for workspace owner', function () {
$response = $this->actingAs($this->user)->put(route('app.members.update-role', $this->user), [
'role' => WorkspaceRole::Member->value,
]);
$response->assertSessionHasErrors('role');
});
test('update role fails with invalid role', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
refactor: auth split layout, subscribe redesign, onboarding, i18n, cookie locale Auth pages: - Create AuthSplitLayout with animated feature slides (6 slides, 3 languages) - All auth pages use split layout (form left, visual right) - Add show/hide password toggle with tooltip on Register - Legal footer only shown on Register via showLegal prop Subscribe page: - Redesign to match auth card pattern (centered, clean) - Platform icons, feature checklist, dynamic trial days (trialDays - 1) - Add "Switch workspace" link - Full i18n (en, es, pt-BR) Onboarding: - Rename URLs: step1 -> role, step2 -> connect - Add enforceStep() to prevent skipping/going back steps - Redirect /onboarding to /onboarding/role - Redesign Step2 with AuthSplitLayout and compact platform list - 21 tests covering all step enforcement scenarios Workspaces page: - Redesign with AuthSplitLayout (list with avatars, current badge) Language system: - Move locale from DB to cookie (forever, unencrypted, session.domain) - Create SetLocale middleware (sets cookie if missing, validates against config) - Rename lang/pt-br to lang/pt-BR - Add dayjs es locale Other: - Copy utils.ts from sendkit (formatNumber, formatMoney, copyToClipboard) - ConfirmDeleteModal with text confirmation (sendkit pattern) - i18n for ConfirmDeleteModal internal strings (common.php) - EmptyState component for posts index - Exact match for "All" posts in sidebar - Posts breadcrumbs show current status filter - DialogFooter buttons aligned left - API Keys page redesign with Table, DropdownMenu, EmptyState - Extract CreateApiKeyDialog and InviteMemberDialog to components - Remove API Keys from sidebar - DropdownMenuItem destructive variant for Remove action
2026-03-30 14:53:42 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Member->value]);
$response = $this->actingAs($this->user)->put(route('app.members.update-role', $member), [
'role' => 'invalid',
]);
$response->assertSessionHasErrors('role');
});
test('update role requires authorization', function () {
$member = User::factory()->create([
'account_id' => $this->account->id,
]);
refactor: auth split layout, subscribe redesign, onboarding, i18n, cookie locale Auth pages: - Create AuthSplitLayout with animated feature slides (6 slides, 3 languages) - All auth pages use split layout (form left, visual right) - Add show/hide password toggle with tooltip on Register - Legal footer only shown on Register via showLegal prop Subscribe page: - Redesign to match auth card pattern (centered, clean) - Platform icons, feature checklist, dynamic trial days (trialDays - 1) - Add "Switch workspace" link - Full i18n (en, es, pt-BR) Onboarding: - Rename URLs: step1 -> role, step2 -> connect - Add enforceStep() to prevent skipping/going back steps - Redirect /onboarding to /onboarding/role - Redesign Step2 with AuthSplitLayout and compact platform list - 21 tests covering all step enforcement scenarios Workspaces page: - Redesign with AuthSplitLayout (list with avatars, current badge) Language system: - Move locale from DB to cookie (forever, unencrypted, session.domain) - Create SetLocale middleware (sets cookie if missing, validates against config) - Rename lang/pt-br to lang/pt-BR - Add dayjs es locale Other: - Copy utils.ts from sendkit (formatNumber, formatMoney, copyToClipboard) - ConfirmDeleteModal with text confirmation (sendkit pattern) - i18n for ConfirmDeleteModal internal strings (common.php) - EmptyState component for posts index - Exact match for "All" posts in sidebar - Posts breadcrumbs show current status filter - DialogFooter buttons aligned left - API Keys page redesign with Table, DropdownMenu, EmptyState - Extract CreateApiKeyDialog and InviteMemberDialog to components - Remove API Keys from sidebar - DropdownMenuItem destructive variant for Remove action
2026-03-30 14:53:42 +00:00
$this->workspace->members()->attach($member->id, ['role' => WorkspaceRole::Member->value]);
$nonAdmin = User::factory()->create([
'account_id' => $this->account->id,
]);
refactor: auth split layout, subscribe redesign, onboarding, i18n, cookie locale Auth pages: - Create AuthSplitLayout with animated feature slides (6 slides, 3 languages) - All auth pages use split layout (form left, visual right) - Add show/hide password toggle with tooltip on Register - Legal footer only shown on Register via showLegal prop Subscribe page: - Redesign to match auth card pattern (centered, clean) - Platform icons, feature checklist, dynamic trial days (trialDays - 1) - Add "Switch workspace" link - Full i18n (en, es, pt-BR) Onboarding: - Rename URLs: step1 -> role, step2 -> connect - Add enforceStep() to prevent skipping/going back steps - Redirect /onboarding to /onboarding/role - Redesign Step2 with AuthSplitLayout and compact platform list - 21 tests covering all step enforcement scenarios Workspaces page: - Redesign with AuthSplitLayout (list with avatars, current badge) Language system: - Move locale from DB to cookie (forever, unencrypted, session.domain) - Create SetLocale middleware (sets cookie if missing, validates against config) - Rename lang/pt-br to lang/pt-BR - Add dayjs es locale Other: - Copy utils.ts from sendkit (formatNumber, formatMoney, copyToClipboard) - ConfirmDeleteModal with text confirmation (sendkit pattern) - i18n for ConfirmDeleteModal internal strings (common.php) - EmptyState component for posts index - Exact match for "All" posts in sidebar - Posts breadcrumbs show current status filter - DialogFooter buttons aligned left - API Keys page redesign with Table, DropdownMenu, EmptyState - Extract CreateApiKeyDialog and InviteMemberDialog to components - Remove API Keys from sidebar - DropdownMenuItem destructive variant for Remove action
2026-03-30 14:53:42 +00:00
$this->workspace->members()->attach($nonAdmin->id, ['role' => WorkspaceRole::Member->value]);
$nonAdmin->update(['current_workspace_id' => $this->workspace->id]);
$response = $this->actingAs($nonAdmin)->put(route('app.members.update-role', $member), [
'role' => WorkspaceRole::Admin->value,
]);
$response->assertForbidden();
});
test('store invite validates email is required', function () {
$response = $this->actingAs($this->user)->post(route('app.invites.store'), []);
$response->assertSessionHasErrors('email');
});
test('store invite validates email format', function () {
$response = $this->actingAs($this->user)->post(route('app.invites.store'), [
'email' => 'not-an-email',
]);
$response->assertSessionHasErrors('email');
});
test('store invite validates role must be valid', function () {
$response = $this->actingAs($this->user)->post(route('app.invites.store'), [
'email' => 'test@example.com',
'role' => 'owner',
]);
$response->assertSessionHasErrors('role');
});