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

68 lines
2.0 KiB
PHP

<?php
namespace Tests\Feature\Filament;
use App\Filament\Resources\UserResource;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UserAccessTest extends TestCase
{
use RefreshDatabase;
public function test_admin_can_access_list_create_and_edit_any_user()
{
$admin = User::factory()->admin()->create();
$other = User::factory()->create();
$this->actingAs($admin);
$this->get(UserResource::getUrl('index'))->assertOk();
$this->get(UserResource::getUrl('create'))->assertOk();
$this->get(UserResource::getUrl('edit', ['record' => $other->getKey()]))->assertOk();
}
public function test_non_admin_cannot_access_list_or_create()
{
$this->actingAs(User::factory()->create());
$this->get(UserResource::getUrl('index'))->assertForbidden();
$this->get(UserResource::getUrl('create'))->assertForbidden();
}
public function test_non_admin_cannot_edit_another_user()
{
$user = User::factory()->create();
$other = User::factory()->create();
$this->actingAs($user);
$this->get(UserResource::getUrl('edit', ['record' => $other->getKey()]))
->assertForbidden();
}
public function test_non_admin_can_edit_self()
{
$user = User::factory()->create();
$this->actingAs($user);
$this->get(UserResource::getUrl('edit', ['record' => $user->getKey()]))
->assertOk();
}
public function test_users_nav_hidden_for_non_admin()
{
$this->actingAs(User::factory()->create());
$this->assertFalse(UserResource::canViewAny());
$this->assertFalse(UserResource::shouldRegisterNavigation());
}
public function test_users_nav_visible_for_admin()
{
$this->actingAs(User::factory()->admin()->create());
$this->assertTrue(UserResource::canViewAny());
$this->assertTrue(UserResource::shouldRegisterNavigation());
}
}