Files
Heimdall/tests/Heimdall.IntegrationTests/Controllers/DiscoveryControllerTests.cs
T
Arnaud Dartois 2152fca3c6 feat(discovery): implement camera detection and dedup pipeline
Manual camera configuration does not scale beyond a few devices.
Automated discovery is needed to support deployments with 50+
cameras across heterogeneous protocols (ONVIF, mDNS, HTTP, ARP).

Phase 1 introduces 5 network probe services, a stream tester, an
orchestrating discovery service with IP+MAC deduplication, 11 REST
endpoints, a pre-verification endpoint, 57 tests, and DI registration.
2026-07-19 12:00:16 +00:00

201 lines
7.0 KiB
C#

using System.Net.Http.Json;
using System.Text.Json;
using Heimdall.IntegrationTests.Factories;
using Shouldly;
namespace Heimdall.IntegrationTests.Controllers;
/// <summary>
/// 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.
/// </summary>
public class DiscoveryControllerTests : IClassFixture<HeimdallWebApplicationFactory>
{
private readonly HeimdallWebApplicationFactory _factory;
private HttpClient? _cachedAdminClient;
public DiscoveryControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
/// <summary>Shared admin client — reused to avoid rate-limit exhaustion.</summary>
private async Task<HttpClient> 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<JsonElement>();
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<JsonElement>();
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<JsonElement>();
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<JsonElement>();
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<string>()
});
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<JsonElement>();
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<JsonElement>();
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);
}
}