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
krekas3mo ago
refer to the livewire docs for testing
Mister Nicolaz
Mister Nicolaz3mo ago
That raises more questions for me than answers
krekas
krekas3mo ago
what's not clear? filament uses livewire so you test livewire components
krekas
krekas3mo ago
Laravel
Testing | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Mister Nicolaz
Mister Nicolaz3mo 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
krekas3mo ago
Livewire tests... But I don't understand why do you want to test core features?
Mister Nicolaz
Mister Nicolaz3mo 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
krekas3mo 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 Nicolaz3mo 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
krekas3mo ago
You can using github actions. Plenty tutorials
Mister Nicolaz
Mister Nicolaz3mo 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
More Posts
Render JSON payload inside tableI have a json column inside the table and want to render the alll the column name and value inside tmake reactive custom field optionsHello everyone, i have custom form field with options ```php S3Uploader::make('url') Is there a way to upload file and saving it to database without having to submit the form?Currently I am trying to upload files from a modal. The files has to be saved permanently after theyUse topbar instead of sidebar also on mobileHello, I would like to disable the sidebar entirely but want to keep my logo in the topbar as well aParameter being null after table actionI have a widget that refreshes the page with a custom parameter. I use this parameter to manipulate Unable to add form actions to modalHi, I've got a resource with a simple form for creating / editing. I've got a table that displays Is it possible to adjust the navigation panel width to be even smaller?I already sent the width of the content to the maximum possible option but there's still so much wasFilament 3 - Multi Tenancy Upload Image ErrorI encountered this error: Unable to retrieve the file_size for file at location: livewire-tmp/GfJyK6Updating Standalone Action's modal description based on the argument passed to the formI have a standalone action that launches a modal. I want to update the modal description based on thSpark Billable Slug redirects to wrong urlI am using the Spark Billing Portal with Filament and everything is working so far, except the redi