dress/tests/Feature/Auth/LoginTest.php

100 lines
2.8 KiB
PHP

<?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 if account is inactive', function () {
$user = User::factory()->create([
'email' => 'inactive@example.com',
'password' => bcrypt('password123'),
'is_active' => false,
]);
post(route('login'), [
'login' => 'inactive@example.com',
'password' => 'password123',
])->assertSessionHasErrors(['login' => __('auth.inactive')]);
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();
});
});