AuthControllerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Tests\Feature\Auth;
  3. use App\Models\Role;
  4. use App\Models\User;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Tests\TestCase;
  7. class AuthControllerTest extends TestCase
  8. {
  9. use RefreshDatabase;
  10. protected $seed = true;
  11. // ==================== Login Form ====================
  12. public function test_login_page_is_accessible_to_guest(): void
  13. {
  14. $response = $this->get('/login');
  15. $response->assertStatus(200);
  16. }
  17. public function test_authenticated_user_is_redirected_from_login_page(): void
  18. {
  19. $user = User::factory()->create(['role' => Role::MANAGER]);
  20. $response = $this->actingAs($user)->get('/login');
  21. $response->assertRedirect('/home');
  22. }
  23. // ==================== Login ====================
  24. public function test_user_can_login_with_correct_credentials(): void
  25. {
  26. $user = User::factory()->create([
  27. 'role' => Role::MANAGER,
  28. 'password' => bcrypt('secret123'),
  29. ]);
  30. $response = $this->post('/login', [
  31. 'email' => $user->email,
  32. 'password' => 'secret123',
  33. ]);
  34. $response->assertRedirect('/order');
  35. $this->assertAuthenticatedAs($user);
  36. }
  37. public function test_login_fails_with_wrong_password(): void
  38. {
  39. $user = User::factory()->create([
  40. 'password' => bcrypt('correct-password'),
  41. ]);
  42. $response = $this->post('/login', [
  43. 'email' => $user->email,
  44. 'password' => 'wrong-password',
  45. ]);
  46. $response->assertSessionHasErrors();
  47. $this->assertGuest();
  48. }
  49. public function test_login_fails_with_nonexistent_email(): void
  50. {
  51. $response = $this->post('/login', [
  52. 'email' => 'nonexistent@example.com',
  53. 'password' => 'password',
  54. ]);
  55. $response->assertSessionHasErrors();
  56. $this->assertGuest();
  57. }
  58. public function test_login_requires_email(): void
  59. {
  60. $response = $this->post('/login', [
  61. 'password' => 'password',
  62. ]);
  63. $response->assertSessionHasErrors('email');
  64. $this->assertGuest();
  65. }
  66. public function test_login_requires_password(): void
  67. {
  68. $user = User::factory()->create();
  69. $response = $this->post('/login', [
  70. 'email' => $user->email,
  71. ]);
  72. $response->assertSessionHasErrors('password');
  73. $this->assertGuest();
  74. }
  75. // ==================== Logout ====================
  76. public function test_authenticated_user_can_logout(): void
  77. {
  78. $user = User::factory()->create(['role' => Role::MANAGER]);
  79. $response = $this->actingAs($user)->post('/logout');
  80. $response->assertRedirect('/');
  81. $this->assertGuest();
  82. }
  83. public function test_guest_cannot_access_protected_route(): void
  84. {
  85. $response = $this->get('/order');
  86. $response->assertRedirect('/login');
  87. }
  88. public function test_guest_redirected_to_login_from_any_auth_route(): void
  89. {
  90. $routes = ['/order', '/reclamations', '/schedule'];
  91. foreach ($routes as $route) {
  92. $response = $this->get($route);
  93. $response->assertRedirect('/login');
  94. }
  95. }
  96. }