test: add feature tests for user authentication, login, and logout flows
This commit is contained in:
parent
57a171b6a2
commit
1d2e25e57f
84
tests/Feature/Auth/LoginTest.php
Normal file
84
tests/Feature/Auth/LoginTest.php
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
use function Pest\Laravel\actingAs;
|
||||||
|
use function Pest\Laravel\assertAuthenticated;
|
||||||
|
use function Pest\Laravel\assertGuest;
|
||||||
|
use function Pest\Laravel\get;
|
||||||
|
use function Pest\Laravel\post;
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Authentication / Login Tests
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
describe('Authentication - Login Page', function () {
|
||||||
|
it('can access the login page', function () {
|
||||||
|
get(route('login'))
|
||||||
|
->assertOk()
|
||||||
|
->assertInertia(fn ($page) => $page
|
||||||
|
->component('auth/login')
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('redirects to dashboard if already authenticated', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
actingAs($user)
|
||||||
|
->get(route('login'))
|
||||||
|
->assertRedirect();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Authentication - Login Actions', function () {
|
||||||
|
it('can login with valid credentials', function () {
|
||||||
|
$user = User::factory()->create([
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'password' => bcrypt('password123'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
post(route('login'), [
|
||||||
|
'login' => 'test@example.com',
|
||||||
|
'password' => 'password123',
|
||||||
|
])->assertRedirect();
|
||||||
|
|
||||||
|
assertAuthenticated();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('cannot login with invalid password', function () {
|
||||||
|
$user = User::factory()->create([
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'password' => bcrypt('password123'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
post(route('login'), [
|
||||||
|
'login' => 'test@example.com',
|
||||||
|
'password' => 'wrongpassword',
|
||||||
|
])->assertSessionHasErrors(['login']); // Fortify typically returns error on username field
|
||||||
|
|
||||||
|
assertGuest();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('cannot login with non-existent email', function () {
|
||||||
|
post(route('login'), [
|
||||||
|
'login' => 'notfound@example.com',
|
||||||
|
'password' => 'password123',
|
||||||
|
])->assertSessionHasErrors(['login']);
|
||||||
|
|
||||||
|
assertGuest();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Authentication - Logout Actions', function () {
|
||||||
|
it('can logout successfully', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
actingAs($user);
|
||||||
|
assertAuthenticated();
|
||||||
|
|
||||||
|
post(route('logout'))->assertRedirect('/');
|
||||||
|
assertGuest();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user