From 1d2e25e57fb244bcc9afb7f0ed5eb2aa13218cc5 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Thu, 30 Apr 2026 08:56:32 +0700 Subject: [PATCH] test: add feature tests for user authentication, login, and logout flows --- tests/Feature/Auth/LoginTest.php | 84 ++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/Feature/Auth/LoginTest.php diff --git a/tests/Feature/Auth/LoginTest.php b/tests/Feature/Auth/LoginTest.php new file mode 100644 index 0000000..754982f --- /dev/null +++ b/tests/Feature/Auth/LoginTest.php @@ -0,0 +1,84 @@ +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(); + }); +});