AuthorizationTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Order;
  4. use App\Models\Role;
  5. use App\Models\User;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Tests\TestCase;
  8. class AuthorizationTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. protected $seed = true;
  12. private User $adminUser;
  13. private User $managerUser;
  14. private User $brigadierUser;
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  19. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  20. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  21. }
  22. // ==================== Admin-only routes ====================
  23. public function test_only_admin_can_access_admin_areas(): void
  24. {
  25. // Guest
  26. $this->get(route('admin.area.index'))->assertRedirect(route('login'));
  27. // Brigadier
  28. $this->actingAs($this->brigadierUser)
  29. ->get(route('admin.area.index'))
  30. ->assertStatus(403);
  31. // Manager
  32. $this->actingAs($this->managerUser)
  33. ->get(route('admin.area.index'))
  34. ->assertStatus(403);
  35. // Admin
  36. $this->actingAs($this->adminUser)
  37. ->get(route('admin.area.index'))
  38. ->assertStatus(200);
  39. }
  40. public function test_only_admin_can_access_admin_districts(): void
  41. {
  42. $this->get(route('admin.district.index'))->assertRedirect(route('login'));
  43. $this->actingAs($this->brigadierUser)
  44. ->get(route('admin.district.index'))
  45. ->assertStatus(403);
  46. $this->actingAs($this->managerUser)
  47. ->get(route('admin.district.index'))
  48. ->assertStatus(403);
  49. $this->actingAs($this->adminUser)
  50. ->get(route('admin.district.index'))
  51. ->assertStatus(200);
  52. }
  53. public function test_only_admin_can_access_user_management(): void
  54. {
  55. $this->get(route('user.index'))->assertRedirect(route('login'));
  56. $this->actingAs($this->brigadierUser)
  57. ->get(route('user.index'))
  58. ->assertStatus(403);
  59. $this->actingAs($this->managerUser)
  60. ->get(route('user.index'))
  61. ->assertStatus(403);
  62. $this->actingAs($this->adminUser)
  63. ->get(route('user.index'))
  64. ->assertStatus(200);
  65. }
  66. // ==================== Manager cannot delete users ====================
  67. public function test_manager_cannot_delete_user(): void
  68. {
  69. $targetUser = User::factory()->create(['role' => Role::BRIGADIER]);
  70. $this->actingAs($this->managerUser)
  71. ->delete(route('user.destroy', $targetUser))
  72. ->assertStatus(403);
  73. }
  74. public function test_admin_can_delete_user(): void
  75. {
  76. $targetUser = User::factory()->create(['role' => Role::BRIGADIER]);
  77. $this->actingAs($this->adminUser)
  78. ->delete(route('user.destroy', $targetUser))
  79. ->assertRedirect();
  80. $this->assertSoftDeleted('users', ['id' => $targetUser->id]);
  81. }
  82. // ==================== Brigadier access restrictions ====================
  83. public function test_brigadier_can_access_orders_index(): void
  84. {
  85. $this->actingAs($this->brigadierUser)
  86. ->get(route('order.index'))
  87. ->assertStatus(200);
  88. }
  89. public function test_brigadier_cannot_access_admin_panel(): void
  90. {
  91. $this->actingAs($this->brigadierUser)
  92. ->get(route('admin.area.index'))
  93. ->assertStatus(403);
  94. $this->actingAs($this->brigadierUser)
  95. ->get(route('admin.district.index'))
  96. ->assertStatus(403);
  97. }
  98. public function test_brigadier_cannot_access_user_management(): void
  99. {
  100. $this->actingAs($this->brigadierUser)
  101. ->get(route('user.index'))
  102. ->assertStatus(403);
  103. }
  104. // ==================== Manager access ====================
  105. public function test_manager_can_access_orders(): void
  106. {
  107. $this->actingAs($this->managerUser)
  108. ->get(route('order.index'))
  109. ->assertStatus(200);
  110. }
  111. public function test_manager_can_access_reclamations(): void
  112. {
  113. $this->actingAs($this->managerUser)
  114. ->get(route('reclamations.index'))
  115. ->assertStatus(200);
  116. }
  117. // ==================== Guest redirects ====================
  118. public function test_guest_redirected_from_order(): void
  119. {
  120. $this->get(route('order.index'))->assertRedirect(route('login'));
  121. }
  122. public function test_guest_redirected_from_reclamations(): void
  123. {
  124. $this->get(route('reclamations.index'))->assertRedirect(route('login'));
  125. }
  126. public function test_guest_redirected_from_schedule(): void
  127. {
  128. $this->get(route('schedule.index'))->assertRedirect(route('login'));
  129. }
  130. }