Files
Jeremy d2ca105091 test: cover UserResource role-based page access
feat: add Role enum (Admin/User)

feat: add role column to users, backfill existing to admin

feat: cast user role to enum and add isAdmin helper

feat: user factory defaults to User role with admin() state

feat: add UserPolicy (admin manages all, users manage self)

feat: restrict AppSettingsPage to admins

feat: admin-only role select on user form with role-based resource access

test: prove non-admins cannot escalate role; guard global search

Add canGloballySearch() to UserResource so the user list is never
surfaced in Filament global search for non-admins (defense-in-depth
alongside the broad canAccess() needed for self-edit). Commit the
adversarial escalation regression tests that prove both fillForm and
raw data.role injection cannot persist a role change for a non-admin.

feat: seed test@test.com as admin, another@test.com as user
2026-06-13 13:08:33 +10:00

54 lines
1.6 KiB
PHP

<?php
namespace Tests\Feature\Filament;
use App\Enums\Role;
use App\Filament\Resources\UserResource\Pages\CreateUser;
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class UserRoleResourceTest extends TestCase
{
use RefreshDatabase;
public function test_admin_sees_role_field_on_create()
{
$this->actingAs(User::factory()->admin()->create());
Livewire::test(CreateUser::class)
->assertFormFieldExists('role')
->assertFormFieldIsVisible('role');
}
public function test_admin_can_create_user_with_chosen_role()
{
$this->actingAs(User::factory()->admin()->create());
Livewire::test(CreateUser::class)
->fillForm([
'name' => 'New Admin',
'email' => 'newadmin@example.com',
'password' => 'password',
'role' => Role::Admin->value,
])
->call('create')
->assertHasNoFormErrors();
$this->assertSame(Role::Admin, User::whereEmail('newadmin@example.com')->sole()->role);
}
public function test_non_admin_does_not_see_role_field_when_editing_self()
{
$user = User::factory()->create();
$this->actingAs($user);
// The field is in the schema but hidden via ->visible(isAdmin),
// so assert it is hidden rather than absent.
Livewire::test(EditUser::class, ['record' => $user->getKey()])
->assertFormFieldIsHidden('role');
}
}