using System.Net.Http.Json; using System.Text.Json; using Heimdall.IntegrationTests.Factories; using Shouldly; namespace Heimdall.IntegrationTests.Controllers; public class GroupsControllerTests : PostgresTestBase { public GroupsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { } [Fact] public async Task List_WithoutAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.GetAsync("api/groups?page=1&pageSize=20"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task Create_WithoutAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.PostAsJsonAsync("api/groups", new { name = "Test Group" }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task Create_WithAdminAuth_ReturnsOk() { var client = await Factory.CreateAdminClientAsync(); var response = await client.PostAsJsonAsync("api/groups", new { name = $"Test Group {Guid.NewGuid():N}", description = "Integration test group" }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); } [Fact] public async Task Create_GetById_ReturnsGroup() { var client = await Factory.CreateAdminClientAsync(); var createResponse = await client.PostAsJsonAsync("api/groups", new { name = $"GetById Group {Guid.NewGuid():N}" }); var createContent = await createResponse.Content.ReadFromJsonAsync(); var groupId = createContent.GetProperty("id").GetGuid(); var getResponse = await client.GetAsync($"api/groups/{groupId}"); getResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var getContent = await getResponse.Content.ReadFromJsonAsync(); getContent.GetProperty("id").GetGuid().ShouldBe(groupId); } [Fact] public async Task Update_WithAdminAuth_ReturnsOk() { var client = await Factory.CreateAdminClientAsync(); var createResponse = await client.PostAsJsonAsync("api/groups", new { name = $"Update Group {Guid.NewGuid():N}" }); var createContent = await createResponse.Content.ReadFromJsonAsync(); var groupId = createContent.GetProperty("id").GetGuid(); var updateResponse = await client.PutAsJsonAsync($"api/groups/{groupId}", new { name = $"Updated Group {Guid.NewGuid():N}", description = "Updated description" }); updateResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); } [Fact] public async Task Delete_WithAdminAuth_ReturnsOk() { var client = await Factory.CreateAdminClientAsync(); var createResponse = await client.PostAsJsonAsync("api/groups", new { name = $"Delete Group {Guid.NewGuid():N}" }); var createContent = await createResponse.Content.ReadFromJsonAsync(); var groupId = createContent.GetProperty("id").GetGuid(); var deleteResponse = await client.DeleteAsync($"api/groups/{groupId}"); deleteResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); } [Fact] public async Task List_WithAdminAuth_ReturnsOk() { var client = await Factory.CreateAdminClientAsync(); var response = await client.GetAsync("api/groups?page=1&pageSize=20"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); } [Fact] public async Task GetMembers_WithoutAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.GetAsync($"api/groups/{Guid.NewGuid()}/users"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task GetById_NonExistent_ReturnsNotFound() { var client = await Factory.CreateAdminClientAsync(); var response = await client.GetAsync($"api/groups/{Guid.NewGuid()}"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound); } }