Pest test returns status 403

90 views Asked by At

I am trying to test my Laravel Filament (V3) app with Pest. Fairly new to Pest so i'm not sure what i'm doing wrong:

I have a User Factory:

public function definition(): array
{
    return [
        'name' => fake()->name(),
        'email' => fake()->unique()->safeEmail(),
        'created_at' => now()->subDays(3),
        'updated_at' => now()->subDays(2),
        'email_verified_at' => now()->subDay(),
        'password' => Hash::make('password'),
        'remember_token' => Str::random(10),
    ];
}

In my TestCase.php the setup method is implemented, as the docs say:

protected function setUp(): void
{
    parent::setUp();

    $this->actingAs(User::factory()->create());
}

My ClientTest.php contains the following test:

it('can render page', function () {
    $this->get(ClientResource::getUrl('index'))->assertSuccessful();
});

When I run the test, I keep getting the following error:

Expected response status code [>=200, <300] but received 403.

1

There are 1 answers

0
El Klo On

I have figured it out, so to help others who might be experiencing the same:

I failed to implement the FilamentUser on my User model and give it the canAccessPanel() method, like:

class User extends Authenticatable implements FilamentUser
{

    public function canAccessPanel(Panel $panel): bool
    {
        if ($panel->getId() === 'admin') {
            return $this->hasVerifiedEmail();
        }

        return true;
    }