trypost/tests/Feature/AcceptInviteControllerTest.php

208 lines
6.3 KiB
PHP
Raw Normal View History

2026-01-20 19:53:54 +00:00
<?php
declare(strict_types=1);
use App\Models\Account;
use App\Models\Invite;
2026-01-20 19:53:54 +00:00
use App\Models\User;
use App\Models\Workspace;
beforeEach(function () {
$this->account = Account::factory()->create();
$this->owner = User::factory()->create([
'account_id' => $this->account->id,
]);
$this->account->update(['owner_id' => $this->owner->id]);
$this->workspace = Workspace::factory()->create([
'account_id' => $this->account->id,
'user_id' => $this->owner->id,
]);
2026-01-20 19:53:54 +00:00
});
test('show invite displays invite details for guest', function () {
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
'email' => 'newuser@example.com',
'workspaces' => [$this->workspace->id],
2026-01-20 19:53:54 +00:00
]);
$response = $this->get(route('app.invites.show', $invite));
2026-01-20 19:53:54 +00:00
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('auth/AcceptInvite', false)
->has('invite')
->where('invite.id', $invite->id)
->where('invite.email', 'newuser@example.com')
->where('invite.account.name', $this->account->name)
2026-01-20 19:53:54 +00:00
);
});
test('show invite displays invite details for authenticated user', function () {
$user = User::factory()->create([
'email' => 'invitee@example.com',
]);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
'email' => 'invitee@example.com',
'workspaces' => [$this->workspace->id],
2026-01-20 19:53:54 +00:00
]);
$response = $this->actingAs($user)->get(route('app.invites.show', $invite));
2026-01-20 19:53:54 +00:00
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('auth/AcceptInvite', false)
->has('invite')
);
});
test('show invite returns 404 for non-existent invite', function () {
$response = $this->get(route('app.invites.show', 'non-existent-uuid'));
2026-01-20 19:53:54 +00:00
$response->assertNotFound();
});
test('accept invite requires authentication', function () {
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
]);
$response = $this->post(route('app.invites.accept', $invite));
2026-01-20 19:53:54 +00:00
$response->assertRedirect(route('login'));
});
test('accept invite adds user to account and workspaces', function () {
2026-01-20 19:53:54 +00:00
$user = User::factory()->create([
'email' => 'invitee@example.com',
]);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
'email' => 'invitee@example.com',
'workspaces' => [$this->workspace->id],
2026-01-20 19:53:54 +00:00
]);
$response = $this->actingAs($user)->post(route('app.invites.accept', $invite));
2026-01-20 19:53:54 +00:00
$response->assertRedirect(route('app.calendar'));
2026-01-20 19:53:54 +00:00
// User should be added to the account
$user->refresh();
expect($user->account_id)->toBe($this->account->id);
2026-01-20 19:53:54 +00:00
// User should be member of workspace
expect($this->workspace->members()->where('user_id', $user->id)->exists())->toBeTrue();
2026-01-20 19:53:54 +00:00
// User's current workspace should be updated
expect($user->current_workspace_id)->toBe($this->workspace->id);
// Invite should be marked as accepted
$invite->refresh();
expect($invite->accepted_at)->not->toBeNull();
2026-01-20 19:53:54 +00:00
});
test('accept invite fails for wrong email', function () {
$user = User::factory()->create([
'email' => 'different@example.com',
]);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
'email' => 'invitee@example.com',
'workspaces' => [$this->workspace->id],
2026-01-20 19:53:54 +00:00
]);
$response = $this->actingAs($user)->post(route('app.invites.accept', $invite));
2026-01-20 19:53:54 +00:00
$response->assertRedirect(route('app.calendar'));
2026-01-20 19:53:54 +00:00
$response->assertSessionHas('flash.bannerStyle', 'danger');
// Invite should NOT be accepted
$invite->refresh();
expect($invite->accepted_at)->toBeNull();
2026-01-20 19:53:54 +00:00
});
test('accept invite handles already member of account', function () {
2026-01-20 19:53:54 +00:00
$user = User::factory()->create([
'email' => 'invitee@example.com',
'account_id' => $this->account->id,
2026-01-20 19:53:54 +00:00
]);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
'email' => 'invitee@example.com',
'workspaces' => [$this->workspace->id],
2026-01-20 19:53:54 +00:00
]);
$response = $this->actingAs($user)->post(route('app.invites.accept', $invite));
2026-01-20 19:53:54 +00:00
$response->assertRedirect(route('app.calendar'));
2026-01-20 19:53:54 +00:00
$response->assertSessionHas('flash.bannerStyle', 'info');
// Invite should be marked as accepted
$invite->refresh();
expect($invite->accepted_at)->not->toBeNull();
2026-01-20 19:53:54 +00:00
});
test('decline invite requires authentication', function () {
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
]);
$response = $this->post(route('app.invites.decline', $invite));
2026-01-20 19:53:54 +00:00
$response->assertRedirect(route('login'));
});
test('decline invite deletes the invite', function () {
$user = User::factory()->create([
'email' => 'invitee@example.com',
]);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
'email' => 'invitee@example.com',
'workspaces' => [$this->workspace->id],
2026-01-20 19:53:54 +00:00
]);
$response = $this->actingAs($user)->post(route('app.invites.decline', $invite));
2026-01-20 19:53:54 +00:00
$response->assertRedirect(route('app.calendar'));
2026-01-20 19:53:54 +00:00
$response->assertSessionHas('flash.bannerStyle', 'info');
// Invite should be deleted
expect(Invite::find($invite->id))->toBeNull();
2026-01-20 19:53:54 +00:00
});
test('decline invite fails for wrong email', function () {
$user = User::factory()->create([
'email' => 'different@example.com',
]);
$invite = Invite::factory()->create([
'account_id' => $this->account->id,
'invited_by' => $this->owner->id,
2026-01-20 19:53:54 +00:00
'email' => 'invitee@example.com',
'workspaces' => [$this->workspace->id],
2026-01-20 19:53:54 +00:00
]);
$response = $this->actingAs($user)->post(route('app.invites.decline', $invite));
2026-01-20 19:53:54 +00:00
$response->assertRedirect(route('app.calendar'));
2026-01-20 19:53:54 +00:00
$response->assertSessionHas('flash.bannerStyle', 'danger');
// Invite should NOT be deleted
expect(Invite::find($invite->id))->not->toBeNull();
2026-01-20 19:53:54 +00:00
});