74 lines
3.0 KiB
PHP
74 lines
3.0 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Users;
|
|
use App\Models\Customer;
|
|
use App\Models\Instance;
|
|
use App\Models\Order;
|
|
use App\Models\Seat;
|
|
use App\Models\User;
|
|
use Livewire\Livewire;
|
|
|
|
function seatSetup(string $plan = 'team'): array
|
|
{
|
|
$user = User::factory()->create(['email' => 'o@seat.test', 'is_admin' => false]);
|
|
$customer = Customer::factory()->create(['email' => 'o@seat.test', 'user_id' => $user->id, 'name' => 'Owner Co']);
|
|
$order = Order::factory()->create(['customer_id' => $customer->id, 'plan' => $plan]);
|
|
Instance::factory()->create(['customer_id' => $customer->id, 'order_id' => $order->id, 'plan' => $plan, 'status' => 'active']);
|
|
|
|
return compact('user', 'customer');
|
|
}
|
|
|
|
it('creates an owner seat on first visit', function () {
|
|
['user' => $user, 'customer' => $customer] = seatSetup();
|
|
|
|
Livewire::actingAs($user)->test(Users::class)->assertOk();
|
|
|
|
$owner = $customer->seats()->where('role', 'owner')->first();
|
|
expect($owner)->not->toBeNull()->and($owner->email)->toBe('o@seat.test');
|
|
});
|
|
|
|
it('invites a seat and blocks duplicates', function () {
|
|
['user' => $user, 'customer' => $customer] = seatSetup();
|
|
|
|
Livewire::actingAs($user)->test(Users::class)
|
|
->set('inviteEmail', 'new@seat.test')->set('inviteRole', 'member')->call('invite')
|
|
->assertHasNoErrors();
|
|
expect($customer->seats()->where('email', 'new@seat.test')->exists())->toBeTrue();
|
|
|
|
Livewire::actingAs($user)->test(Users::class)
|
|
->set('inviteEmail', 'new@seat.test')->call('invite')
|
|
->assertHasErrors(['inviteEmail']);
|
|
expect($customer->seats()->where('email', 'new@seat.test')->count())->toBe(1);
|
|
});
|
|
|
|
it('enforces the plan seat limit', function () {
|
|
['user' => $user, 'customer' => $customer] = seatSetup('start'); // 5-seat plan
|
|
// Fill all 5 seats so the plan allowance is exhausted.
|
|
Seat::factory()->count(5)->create(['customer_id' => $customer->id]);
|
|
|
|
Livewire::actingAs($user)->test(Users::class)
|
|
->set('inviteEmail', 'over@seat.test')->call('invite')
|
|
->assertHasErrors(['inviteEmail']);
|
|
expect($customer->seats()->where('email', 'over@seat.test')->exists())->toBeFalse();
|
|
});
|
|
|
|
it('will not remove or demote the last owner', function () {
|
|
['user' => $user, 'customer' => $customer] = seatSetup();
|
|
Livewire::actingAs($user)->test(Users::class); // creates owner
|
|
$owner = $customer->seats()->where('role', 'owner')->first();
|
|
|
|
Livewire::actingAs($user)->test(Users::class)->call('revoke', $owner->uuid);
|
|
expect($customer->seats()->whereKey($owner->id)->exists())->toBeTrue();
|
|
|
|
Livewire::actingAs($user)->test(Users::class)->call('setRole', $owner->uuid, 'member');
|
|
expect($owner->fresh()->role)->toBe('owner');
|
|
});
|
|
|
|
it('revokes a non-owner seat', function () {
|
|
['user' => $user, 'customer' => $customer] = seatSetup();
|
|
$seat = Seat::factory()->create(['customer_id' => $customer->id, 'role' => 'member']);
|
|
|
|
Livewire::actingAs($user)->test(Users::class)->call('revoke', $seat->uuid);
|
|
expect($customer->seats()->whereKey($seat->id)->exists())->toBeFalse();
|
|
});
|