using System.Net.Http.Json; using System.Text.Json; using Heimdall.IntegrationTests.Factories; using Shouldly; namespace Heimdall.IntegrationTests.Controllers; public class SitesControllerTests : PostgresTestBase { private static readonly JsonSerializerOptions WebJsonOptions = new(JsonSerializerDefaults.Web); public SitesControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { } [Fact] public async Task GetSites_WithoutAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.GetAsync("api/sites?page=1&pageSize=10"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task CreateSite_WithAuth_ReturnsOk() { var client = await Factory.CreateAdminClientAsync(); var response = await client.PostAsJsonAsync("api/sites", new { name = "Site Test", address = "Paris", timezone = "Europe/Paris", ntfyTopic = "site-test-topic" }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var created = await response.Content.ReadFromJsonAsync(); created.ShouldNotBeNull(); var siteResponse = await client.GetAsync($"api/sites/{created.Id}"); siteResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var site = await siteResponse.Content.ReadFromJsonAsync(); site.ShouldNotBeNull(); site.NtfyTopic.ShouldBe("site-test-topic"); } [Fact] public async Task ListSites_WithAuth_ReturnsOk() { var client = await Factory.CreateAdminClientAsync(); var response = await client.GetAsync("api/sites?page=1&pageSize=10"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); } [Fact] public async Task SiteCredentials_CanBeListedAddedAndDeleted_WithoutReturningPassword() { var client = await Factory.CreateAdminClientAsync(); var siteResponse = await client.PostAsJsonAsync("api/sites", new { name = $"Credential Site {Guid.NewGuid():N}", timezone = "UTC" }); var site = await siteResponse.Content.ReadFromJsonAsync(); site.ShouldNotBeNull(); var viewer = await Factory.CreateViewerClientAsync(client); (await viewer.GetAsync($"api/sites/{site.Id}/credentials")).StatusCode .ShouldBe(System.Net.HttpStatusCode.Forbidden); var emptyResponse = await client.GetAsync($"api/sites/{site.Id}/credentials"); emptyResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); (await emptyResponse.Content.ReadFromJsonAsync>()) .ShouldBeEmpty(); const string submittedPassword = "synthetic-camera-password-only-for-test"; var addResponse = await client.PostAsJsonAsync($"api/sites/{site.Id}/credentials", new { label = "Synthetic camera login", username = "camera-test-user", password = submittedPassword }); addResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var responseBody = await addResponse.Content.ReadAsStringAsync(); responseBody.ShouldNotContain(submittedPassword); var created = JsonSerializer.Deserialize(responseBody, WebJsonOptions); created.ShouldNotBeNull(); var listResponse = await client.GetAsync($"api/sites/{site.Id}/credentials"); var listed = await listResponse.Content.ReadFromJsonAsync>(); listResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); listed.ShouldHaveSingleItem().Id.ShouldBe(created.Id); var deleteResponse = await client.DeleteAsync($"api/sites/{site.Id}/credentials/{created.Id}"); deleteResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.NoContent); var afterDelete = await client.GetFromJsonAsync>( $"api/sites/{site.Id}/credentials"); afterDelete.ShouldBeEmpty(); } [Fact] public async Task SiteCredentials_RequireAuthentication() { var response = await Factory.CreateClient().GetAsync($"api/sites/{Guid.NewGuid()}/credentials"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } }