| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace Tests\Feature\Auth;
- use App\Models\Role;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class AuthControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- // ==================== Login Form ====================
- public function test_login_page_is_accessible_to_guest(): void
- {
- $response = $this->get('/login');
- $response->assertStatus(200);
- }
- public function test_authenticated_user_is_redirected_from_login_page(): void
- {
- $user = User::factory()->create(['role' => Role::MANAGER]);
- $response = $this->actingAs($user)->get('/login');
- $response->assertRedirect('/home');
- }
- // ==================== Login ====================
- public function test_user_can_login_with_correct_credentials(): void
- {
- $user = User::factory()->create([
- 'role' => Role::MANAGER,
- 'password' => bcrypt('secret123'),
- ]);
- $response = $this->post('/login', [
- 'email' => $user->email,
- 'password' => 'secret123',
- ]);
- $response->assertRedirect('/order');
- $this->assertAuthenticatedAs($user);
- }
- public function test_login_fails_with_wrong_password(): void
- {
- $user = User::factory()->create([
- 'password' => bcrypt('correct-password'),
- ]);
- $response = $this->post('/login', [
- 'email' => $user->email,
- 'password' => 'wrong-password',
- ]);
- $response->assertSessionHasErrors();
- $this->assertGuest();
- }
- public function test_login_fails_with_nonexistent_email(): void
- {
- $response = $this->post('/login', [
- 'email' => 'nonexistent@example.com',
- 'password' => 'password',
- ]);
- $response->assertSessionHasErrors();
- $this->assertGuest();
- }
- public function test_login_requires_email(): void
- {
- $response = $this->post('/login', [
- 'password' => 'password',
- ]);
- $response->assertSessionHasErrors('email');
- $this->assertGuest();
- }
- public function test_login_requires_password(): void
- {
- $user = User::factory()->create();
- $response = $this->post('/login', [
- 'email' => $user->email,
- ]);
- $response->assertSessionHasErrors('password');
- $this->assertGuest();
- }
- // ==================== Logout ====================
- public function test_authenticated_user_can_logout(): void
- {
- $user = User::factory()->create(['role' => Role::MANAGER]);
- $response = $this->actingAs($user)->post('/logout');
- $response->assertRedirect('/');
- $this->assertGuest();
- }
- public function test_guest_cannot_access_protected_route(): void
- {
- $response = $this->get('/order');
- $response->assertRedirect('/login');
- }
- public function test_guest_redirected_to_login_from_any_auth_route(): void
- {
- $routes = ['/order', '/reclamations', '/schedule'];
- foreach ($routes as $route) {
- $response = $this->get($route);
- $response->assertRedirect('/login');
- }
- }
- }
|