skoobi's avatar
Level 13

Testing with Pest2 and Fillement not authorising user

Hi. I keep hitting an issue with my first test where I'm trying to add a test to see if an user can log in. I'm using fillemenmt as my admin panel, so not sure if that has any bearing on it. I know its something I'm doing wrong but heres my code:

<?php

use App\Models\User;

// Comes back successful
test('an unauthenticted user can not access dashboard', function () {
    $this->get('/')
        ->assertRedirect('/login');
});

// Comes back as failed
test('an authenticated user can access dashboard', function () {
    $this->be(User::factory()->create())
        ->get('/')
        ->assertOk();
});

I've used actingAs for the User as weel and still the same issue. What is it im doing wrong?

Cheers

0 likes
3 replies
LaryAI's avatar
Level 58

It seems like you're trying to authenticate a user in your Pest test to access a protected route. If you're using Laravel's built-in authentication, the be() method should work, but since you mentioned you're using Filament, there might be some specifics to how Filament handles authentication that you need to account for.

Here's a revised version of your test that uses the actingAs method, which is the standard way to authenticate a user in tests:

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('an unauthenticated user cannot access dashboard', function () {
    $this->get('/')
        ->assertRedirect('/login');
});

test('an authenticated user can access dashboard', function () {
    $user = User::factory()->create();

    $this->actingAs($user)
        ->get('/')
        ->assertOk();
});

Make sure you're using the RefreshDatabase trait if you're creating users on the fly, as this will roll back the database after each test, ensuring a clean state.

If you're still encountering issues, it could be due to a custom guard or middleware that Filament uses. In that case, you'll need to ensure that you're using the correct guard when calling actingAs. For example, if Filament uses a guard named filament, you would authenticate the user like this:

$this->actingAs($user, 'filament')
    ->get('/')
    ->assertOk();

If Filament has its own set of routes for the dashboard, make sure you're hitting the correct URL in your test. For example, if the dashboard is located at /admin, your test should be:

$this->actingAs($user)
    ->get('/admin')
    ->assertOk();

Lastly, ensure that your user has the necessary permissions or roles to access the dashboard if Filament uses a role-based access control system. You may need to assign the appropriate roles to the user before attempting to access the dashboard.

If you continue to have issues, please provide more details about the authentication system and any errors you're receiving, and I'll be happy to help further.

skoobi's avatar
Level 13

@LaryAI Unfortunetly I still get Expected response status code [200] but received 403.

skoobi's avatar
skoobi
OP
Best Answer
Level 13

Added : implements FilamentUser to the User Model and all worked.

Also had to add

 public function canAccessPanel(Panel $panel): bool
    {
        return true;
//        return str_ends_with($this->email, '@admin.com') && $this->hasVerifiedEmail();
    }

Please or to participate in this conversation.