using System.Net.Http.Json; using System.Text.Json; using Heimdall.IntegrationTests.Factories; using Shouldly; namespace Heimdall.IntegrationTests.Controllers; public class RolesControllerTests : PostgresTestBase { public RolesControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { } [Fact] public async Task List_WithoutAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.GetAsync("api/roles"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task List_WithAdminAuth_ReturnsOk() { var client = await Factory.CreateAdminClientAsync(); var response = await client.GetAsync("api/roles"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("roles").GetArrayLength().ShouldBeGreaterThan(0); } [Fact] public async Task GetByName_WithAdminAuth_ReturnsRole() { var client = await Factory.CreateAdminClientAsync(); var response = await client.GetAsync("api/roles/Admin"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("name").GetString().ShouldBe("Admin"); content.GetProperty("permissions").GetArrayLength().ShouldBeGreaterThan(0); } [Fact] public async Task GetByName_NonExistent_ReturnsNotFound() { var client = await Factory.CreateAdminClientAsync(); var response = await client.GetAsync("api/roles/NonExistentRole"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound); } [Fact] public async Task List_ContainsExpectedRoles() { var client = await Factory.CreateAdminClientAsync(); var response = await client.GetAsync("api/roles"); var content = await response.Content.ReadFromJsonAsync(); var roles = content.GetProperty("roles").EnumerateArray().ToList(); roles.Any(r => r.GetProperty("name").GetString() == "Admin").ShouldBeTrue(); roles.Any(r => r.GetProperty("name").GetString() == "Viewer").ShouldBeTrue(); } }