This is a common situation when testing Filament with Pest (and/or Orchestra): you get a 403 instead of 200 after calling $this->actingAs(). Here are typical reasons and how to solve it:
1. Your User Lacks Proper Roles or Permissions
In Filament, you usually need a user who can access the Filament admin panel. Merely being authenticated is not enough; by default, Filament calls canAccessPanel() or checks permissions/roles.
Solution:
Either, assign a specific role or permission to your test user, or implement a canAccessFilament/canAccessPanel method that returns true for your test user.
Option A: Add can_access_filament attribute or role
$user = User::factory()->create([
// Fill any attributes required for access
'is_admin' => true, // if your FilamentServiceProvider uses this attribute
]);
$this->actingAs($user);
Option B: Quickly Enable Access in Your User Model (for tests)
Add this to your User model (you can use a trait or directly):
public function canAccessFilament(): bool
{
return true;
}
Option C: If Using Permissions/Roles
If you have Spatie's laravel-permission package:
$user = User::factory()->create();
$user->assignRole('admin'); // or whatever your gate requires
$this->actingAs($user);
2. Policies/Gates Preventing Access
Check your AuthorPolicy and ensure that the user can viewAny or view authors if the route requires it. If your policy returns false by default for unprivileged users, your test user ends up forbidden.
Double check:
Does your policy check for a certain ability or role? Make your test user pass those checks!
3. Testing Environment and Guards
Make sure that you're using the same authentication guard as Filament (by default, Filament uses the web guard). In Pest, make sure you're not overriding guards accidentally.
4. Updated Test Example
Your working test (assuming Filament uses canAccessFilament):
use Workbench\App\Models\User;
test('Only users can access authors.', function () {
$this->get('/admin/authors')->assertStatus(302)->assertRedirect('/admin/login');
$user = User::factory()->create([
// Add any attributes required for admin access
]);
// Optionally, add: $user->assignRole('admin');
$this->actingAs($user);
$this->get('/admin/authors')->assertStatus(200);
});
If you're using custom access logic, implement it on your User model just for tests.
5. Summary Checklist
- ✅ User model is recognized as able to access the Filament admin panel
- ✅ Any relevant policy/role/permission is satisfied
- ✅ You're using the correct guard
Reference:
If you implement canAccessFilament on your User model (or whatever Filament expects), your test should pass.