Files
pricebuddy/tests/Feature/Filament/UserRoleEscalationAdversarialTest.php
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

81 lines
2.4 KiB
PHP

<?php
namespace Tests\Feature\Filament;
use App\Enums\Role;
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class UserRoleEscalationAdversarialTest extends TestCase
{
use RefreshDatabase;
public function test_non_admin_cannot_persist_forged_role_via_fill_form(): void
{
$user = User::factory()->create(['role' => Role::User]);
$this->actingAs($user);
Livewire::test(EditUser::class, ['record' => $user->getKey()])
->fillForm([
'name' => 'Still Normal',
'role' => Role::Admin->value,
])
->call('save')
->assertHasNoFormErrors();
$this->assertSame(
Role::User,
$user->refresh()->role,
'Non-admin must NOT be able to escalate their own role.'
);
}
public function test_non_admin_cannot_persist_forged_role_via_raw_data_set(): void
{
$user = User::factory()->create(['role' => Role::User]);
$this->actingAs($user);
// Directly inject role into the Livewire data bag, bypassing the form UI.
Livewire::test(EditUser::class, ['record' => $user->getKey()])
->set('data.role', Role::Admin->value)
->call('save')
->assertHasNoFormErrors();
$this->assertSame(
Role::User,
$user->refresh()->role,
'Forged data.role injection must not escalate a non-admin.'
);
}
public function test_non_admin_cannot_see_delete_action_on_own_edit_page(): void
{
$user = User::factory()->create(['role' => Role::User]);
$this->actingAs($user);
Livewire::test(EditUser::class, ['record' => $user->getKey()])
->assertActionHidden('delete');
}
public function test_admin_sees_delete_action(): void
{
$admin = User::factory()->admin()->create();
$other = User::factory()->create();
$this->actingAs($admin);
Livewire::test(EditUser::class, ['record' => $other->getKey()])
->assertActionVisible('delete');
}
public function test_guest_is_redirected_from_user_resource(): void
{
$user = User::factory()->create();
$this->get(\App\Filament\Resources\UserResource::getUrl('edit', ['record' => $user->getKey()]))
->assertRedirect();
}
}