Skip to content

Commit

Permalink
Merge pull request #39 from CodeWithDennis/add-missing-tests
Browse files Browse the repository at this point in the history
Add auth and profile test cases
  • Loading branch information
CodeWithDennis authored Oct 30, 2024
2 parents afc0851 + e085ca4 commit c0cc194
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
46 changes: 46 additions & 0 deletions tests/Feature/Filament/Pages/Auth/LoginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use App\Filament\Pages\Auth\Login;
use Filament\Facades\Filament;

use function Pest\Livewire\livewire;

test('an unauthenticated user can access the login page', function () {
auth()->logout();

$this->get(Filament::getLoginUrl())
->assertOk();
});

test('an unauthenticated user can not access the admin panel', function () {
auth()->logout();

$this->get('admin')
->assertRedirect(Filament::getLoginUrl());
});

test('an unauthenticated user can login', function () {
auth()->logout();

livewire(Login::class)
->fillForm([
'email' => config('app.default_user.email'),
'password' => config('app.default_user.password'),
])
->assertActionExists('authenticate')
->call('authenticate')
->assertHasNoFormErrors();
});

test('an authenticated user can access the admin panel', function () {
$this->get('admin')
->assertOk();
});

test('an authenticated user can logout', function () {
$this->get('admin')
->assertOk();

$this->post('admin/logout')
->assertRedirect(Filament::getLoginUrl());
});
100 changes: 100 additions & 0 deletions tests/Feature/Filament/Pages/ProfileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

use App\Filament\Pages\App\Profile;
use App\Models\User;
use Illuminate\Support\Str;

use function Pest\Livewire\livewire;

it('can render the profile page', function () {
livewire(Profile::class)
->assertSuccessful();
});

it('can update profile', function () {
$updatedUser = User::factory()->make();

livewire(Profile::class)
->fillForm([
'name' => $updatedUser->name,
'email' => $updatedUser->email,
])
->assertActionExists('save')
->call('save')
->assertHasNoFormErrors();

$this->assertDatabaseHas(User::class, [
'name' => $updatedUser->name,
'email' => $updatedUser->email,
]);
});

it('can update password and authenticate', function () {
$user = auth()->user();

livewire(Profile::class)
->fillForm([
'password' => 'new-password',
'passwordConfirmation' => 'new-password',
])
->assertActionExists('save')
->call('save')
->assertHasNoFormErrors();

auth()->logout();
$this->assertGuest();

$this->assertTrue(auth()->attempt([
'email' => $user->email,
'password' => 'new-password',
]));
});

it('can validate password confirmation', function () {
livewire(Profile::class)
->fillForm([
'password' => 'password',
'passwordConfirmation' => 'different-password',
])
->assertActionExists('save')
->call('save')
->assertHasFormErrors(['password' => ['same']]);
});

it('can validate required', function ($column) {
livewire(Profile::class)
->fillForm([$column => null])
->assertActionExists('save')
->call('save')
->assertHasFormErrors([$column => ['required']]);
})->with(['name', 'email']);

it('can validate email', function () {
livewire(Profile::class)
->fillForm([
'email' => 'invalid-email-format',
])
->assertActionExists('save')
->call('save')
->assertHasFormErrors(['email' => ['email']]);
});

it('can validate unique email', function () {
$user = User::factory()->create();

livewire(Profile::class)
->fillForm([
'email' => $user->email,
])
->assertActionExists('save')
->call('save')
->assertHasFormErrors(['email' => ['unique']]);
});

it('can validate max length', function (string $column) {
livewire(Profile::class)
->fillForm([$column => Str::random(256)])
->assertActionExists('save')
->call('save')
->assertHasFormErrors([$column => ['max:255']]);
})->with(['name', 'email']);
6 changes: 6 additions & 0 deletions tests/Feature/Filament/Resources/UserResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
use App\Models\User;
use Filament\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

use function Pest\Livewire\livewire;

beforeEach(function () {
/* The TestCase setup generates a user before each test, so we need to clear the table to make sure we have a clean slate. */
DB::table('users')->truncate();
});

it('can render the index page', function () {
livewire(ListUsers::class)
->assertSuccessful();
Expand Down
13 changes: 12 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

namespace Tests;

use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
protected function setUp(): void
{
parent::setUp();

$this->actingAs(User::factory()->create([
'email' => config('app.default_user.email'),
'password' => config('app.default_user.password'),
]));

$this->withoutVite();
}
}

0 comments on commit c0cc194

Please sign in to comment.