Test authentication with Pest

How could you best test the login of filament dashboard, since there is no POST option on the login route?
test('Admin user can login with valid credentials', function () {
$user = createUser('Admin');

$response = $this->post('admin/login', [
'email' => $user->email,
'password' => 'password',
]);

$response->assertRedirect(route('filament.admin.pages.dashboard'));
$this->assertAuthenticatedAs($user);
});
test('Admin user can login with valid credentials', function () {
$user = createUser('Admin');

$response = $this->post('admin/login', [
'email' => $user->email,
'password' => 'password',
]);

$response->assertRedirect(route('filament.admin.pages.dashboard'));
$this->assertAuthenticatedAs($user);
});
12 Replies
krekas
krekas8mo ago
refer to the livewire docs for testing
Mister Nicolaz
Mister NicolazOP8mo ago
That raises more questions for me than answers
krekas
krekas8mo ago
what's not clear? filament uses livewire so you test livewire components
krekas
krekas8mo ago
Laravel
Testing | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Mister Nicolaz
Mister NicolazOP8mo ago
Yes, I understand that, but I only see examples for a PostResource and I specifically want to test the authentication. So logging in, registering and logging out.
krekas
krekas8mo ago
Livewire tests... But I don't understand why do you want to test core features?
Mister Nicolaz
Mister NicolazOP8mo ago
You have a good point there 😅 Filament is still quite new to me, but I have worked with Laravel before in the past. What I mainly miss are good guidelines on how you should/shouldn't set up tests (from A to Z).
krekas
krekas8mo ago
Maybe you should first learn livewire and how to test components/actions. I think there should be some good tests in filament repo
Mister Nicolaz
Mister NicolazOP8mo ago
I've now managed to make some specific tests with livewire 🙂 Is there a useful CI/CD workflow that I could use on Github, so that I can run the tests at each PR?
krekas
krekas8mo ago
You can using github actions. Plenty tutorials
Mister Nicolaz
Mister NicolazOP8mo ago
Still a question about testing authentication with pest Given this function in the User model:
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return $this->hasRole(['Super Admin', 'Moderator']);
}
return false;
}
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return $this->hasRole(['Super Admin', 'Moderator']);
}
return false;
}
How could I test that a normal user with the role user does not have access to the panel? In the filament repository I came across a test that would best suit this:
test('unauthenticated user cannot login admin panel', function () {
Filament::setCurrentPanel(Filament::getPanel('admin'));

livewire(Login::class)
->fillForm([
'email' => $this->user->email,
'password' => 'password',
])
->call('authenticate')
->assertHasFormErrors(['email']);

$this->assertGuest();
});
test('unauthenticated user cannot login admin panel', function () {
Filament::setCurrentPanel(Filament::getPanel('admin'));

livewire(Login::class)
->fillForm([
'email' => $this->user->email,
'password' => 'password',
])
->call('authenticate')
->assertHasFormErrors(['email']);

$this->assertGuest();
});
As a test it passes, but it does not ensure that I get code coverage on the return false; 🤔 I could of course also omit the return false, but phpstan actually expects that there is a return value in any case
Want results from more Discord servers?
Add your server