using System.Net.Http.Json; using System.Text.Json; using Heimdall.IntegrationTests.Factories; using Shouldly; namespace Heimdall.IntegrationTests.Controllers; /// /// Integration tests for DiscoveryController. /// Network-dependent endpoints (probe, scan) are tested with real network /// timeouts in unit tests. These tests focus on the API contract and /// non-network operations. /// public class DiscoveryControllerTests : PostgresTestBase { private HttpClient? _cachedAdminClient; public DiscoveryControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { } /// Shared admin client — reused to avoid rate-limit exhaustion. private async Task AdminClientAsync() { if (_cachedAdminClient is not null) return _cachedAdminClient; _cachedAdminClient = await Factory.CreateAdminClientAsync(); return _cachedAdminClient; } [Fact] public async Task SearchDatabase_WithoutQuery_ReturnsAllResults() { var client = await AdminClientAsync(); var response = await client.GetAsync("api/discovery/search?q="); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("results").GetArrayLength().ShouldBeGreaterThan(0); } [Fact] public async Task SearchDatabase_WithBrandQuery_ReturnsFilteredResults() { var client = await AdminClientAsync(); var response = await client.GetAsync("api/discovery/search?q=hikvision"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("results").GetArrayLength().ShouldBeGreaterThan(0); } [Fact] public async Task BuildStreamUrls_ValidRequest_ReturnsUrls() { var client = await AdminClientAsync(); var response = await client.PostAsJsonAsync("api/discovery/build-urls", new { ids = new[] { "p:rtsp-h264" }, ip = "192.168.1.100", username = "admin", password = "test123", channel = 0 }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("streams").GetArrayLength().ShouldBeGreaterThan(0); } [Fact] public async Task BuildStreamUrls_NoMatchingTemplates_ReturnsGenericUrl() { var client = await AdminClientAsync(); var response = await client.PostAsJsonAsync("api/discovery/build-urls", new { ids = new[] { "unknown-key" }, ip = "192.168.1.200", username = "admin", password = "test" }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); var streams = content.GetProperty("streams"); streams.GetArrayLength().ShouldBe(1); streams[0].GetString().ShouldBe("rtsp://192.168.1.200:554/stream1"); } [Fact] public async Task TestStreams_EmptyUrls_ReturnsBadRequest() { var client = await AdminClientAsync(); var response = await client.PostAsJsonAsync("api/discovery/test-streams", new { sourceId = "test", streams = Array.Empty() }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadRequest); } [Fact] public async Task TestStreams_ValidUrls_ReturnsSession() { var client = await AdminClientAsync(); var response = await client.PostAsJsonAsync("api/discovery/test-streams", new { sourceId = "test", streams = new[] { "rtsp://192.168.1.100:554/stream1" } }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("sessionId").GetGuid().ShouldNotBe(Guid.Empty); content.GetProperty("status").GetString().ShouldBe("running"); } [Fact] public async Task GetTestSession_UnknownId_ReturnsNotFound() { var client = await AdminClientAsync(); var response = await client.GetAsync($"api/discovery/test/{Guid.NewGuid()}"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound); } [Fact] public async Task CancelTestSession_UnknownId_ReturnsOk() { var client = await AdminClientAsync(); var response = await client.PostAsync($"api/discovery/test/{Guid.NewGuid()}/cancel", null); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); } [Fact] public async Task ConfirmDiscovery_WithoutAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.PostAsJsonAsync("api/discovery/confirm", new { siteId = Guid.NewGuid(), name = "Test Camera", discoveryResultId = Guid.NewGuid(), mainStreamUrl = "rtsp://192.168.1.100:554/stream1" }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task ProbeIp_RequiresAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.PostAsJsonAsync("api/discovery/probe", new { siteId = Guid.NewGuid(), ip = "192.168.1.1" }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task SearchDatabase_RequiresAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.GetAsync("api/discovery/search?q=test"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task CheckDuplicate_NonExistentIp_ReturnsNotDuplicate() { var client = await AdminClientAsync(); var response = await client.PostAsJsonAsync("api/discovery/check", new { siteId = Guid.NewGuid(), ip = "10.0.0.99", mac = (string?)null }); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetProperty("isDuplicate").GetBoolean().ShouldBeFalse(); } [Fact] public async Task CheckDuplicate_RequiresAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.PostAsJsonAsync("api/discovery/check", new { siteId = Guid.NewGuid(), ip = "10.0.0.1", mac = (string?)null }); // Without auth, returns 401 (not 404/200) response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } }