Files
dyncoll-dev/backend/tests/Feature/UserRoleControllerTest.php
2026-06-23 12:29:02 +02:00

65 lines
2.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Lists\UserRole;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UserRoleControllerTest extends TestCase
{
use RefreshDatabase;
// --- Lettura (tier.app) --------------------------------------------------
public function test_index_lists_roles_for_an_operational_user(): void
{
$this->actingAs($this->operationalUser(), 'sanctum');
UserRole::factory()->count(2)->create();
$this->getJson('/api/user-roles')
->assertOk()
->assertJsonStructure(['message', 'data' => [['id', 'name', 'description']]]);
}
public function test_show_returns_a_single_role(): void
{
$this->actingAs($this->operationalUser(), 'sanctum');
$role = UserRole::factory()->create();
$this->getJson("/api/user-roles/{$role->id}")
->assertOk()
->assertJsonPath('data.id', $role->id)
->assertJsonPath('data.name', $role->name);
}
// --- Statistiche (tier.admin) ---------------------------------------------
public function test_admin_can_see_the_user_count_of_a_role(): void
{
$this->actingAs($this->adminUser(), 'sanctum');
$role = UserRole::factory()->create();
$this->operationalUser(['role_id' => $role->id]);
$this->operationalUser(['role_id' => $role->id]);
$this->getJson("/api/user-roles/{$role->id}/usage")
->assertOk()
->assertJsonPath('user_count', 2);
}
public function test_a_non_admin_cannot_see_usage_stats(): void
{
$this->actingAs($this->operationalUser(), 'sanctum');
$role = UserRole::factory()->create();
$this->getJson("/api/user-roles/{$role->id}/usage")->assertForbidden();
}
// --- Gating ---------------------------------------------------------------
public function test_guests_cannot_access_roles(): void
{
$this->getJson('/api/user-roles')->assertUnauthorized();
}
}