2026-07-19 12:00:16 +00:00
|
|
|
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>
|
2026-07-21 10:24:11 +00:00
|
|
|
public class DiscoveryControllerTests : PostgresTestBase
|
2026-07-19 12:00:16 +00:00
|
|
|
{
|
|
|
|
|
private HttpClient? _cachedAdminClient;
|
|
|
|
|
|
2026-07-21 10:24:11 +00:00
|
|
|
public DiscoveryControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
2026-07-19 12:00:16 +00:00
|
|
|
|
|
|
|
|
/// <summary>Shared admin client — reused to avoid rate-limit exhaustion.</summary>
|
|
|
|
|
private async Task<HttpClient> AdminClientAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_cachedAdminClient is not null)
|
|
|
|
|
return _cachedAdminClient;
|
2026-07-21 10:24:11 +00:00
|
|
|
_cachedAdminClient = await Factory.CreateAdminClientAsync();
|
2026-07-19 12:00:16 +00:00
|
|
|
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()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = Factory.CreateClient();
|
2026-07-19 12:00:16 +00:00
|
|
|
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()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = Factory.CreateClient();
|
2026-07-19 12:00:16 +00:00
|
|
|
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()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = Factory.CreateClient();
|
2026-07-19 12:00:16 +00:00
|
|
|
var response = await client.GetAsync("api/discovery/search?q=test");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-10-20 07:38:05 +02:00
|
|
|
public async Task CheckDuplicate_NonExistentIpInAccessibleSite_ReturnsNotDuplicate()
|
|
|
|
|
{
|
|
|
|
|
var client = await AdminClientAsync();
|
|
|
|
|
var siteResponse = await client.PostAsJsonAsync("api/sites", new { name = "Discovery Test Site", timezone = "UTC" });
|
|
|
|
|
siteResponse.EnsureSuccessStatusCode();
|
|
|
|
|
var site = await siteResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/discovery/check", new
|
|
|
|
|
{
|
|
|
|
|
siteId = site.GetProperty("id").GetGuid(),
|
|
|
|
|
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_UnknownSite_ReturnsForbidden()
|
2026-07-19 12:00:16 +00:00
|
|
|
{
|
|
|
|
|
var client = await AdminClientAsync();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/discovery/check", new
|
|
|
|
|
{
|
|
|
|
|
siteId = Guid.NewGuid(),
|
|
|
|
|
ip = "10.0.0.99",
|
|
|
|
|
mac = (string?)null
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-20 07:38:05 +02:00
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden);
|
2026-07-19 12:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CheckDuplicate_RequiresAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = Factory.CreateClient();
|
2026-07-19 12:00:16 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|