Heimdall Gitea / Detect CI scope (pull_request) Successful in 6s
Heimdall Gitea / Build and tests (pull_request) Failing after 18m5s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 3s
269 lines
10 KiB
C#
269 lines
10 KiB
C#
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Heimdall.Domain.Entities;
|
|
using Heimdall.Domain.Enums;
|
|
using Heimdall.Infrastructure.Data;
|
|
using Heimdall.IntegrationTests.Factories;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
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 : PostgresTestBase
|
|
{
|
|
private HttpClient? _cachedAdminClient;
|
|
|
|
public DiscoveryControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
|
|
|
/// <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 ConfirmDiscovery_PersistsOnlyVerifiedPortsAndCapabilities()
|
|
{
|
|
var admin = await AdminClientAsync();
|
|
var siteResponse = await admin.PostAsJsonAsync("api/sites", new { name = "Discovery Ports Site", timezone = "UTC" });
|
|
siteResponse.EnsureSuccessStatusCode();
|
|
var site = await siteResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
var siteId = site.GetProperty("id").GetGuid();
|
|
|
|
var discovery = new CameraDiscoveryResult(siteId, "10.44.0.20");
|
|
discovery.MarkReachable(DetectedCameraType.Standard, CameraDiscoveryMethod.OnvifWsDiscovery);
|
|
discovery.SetOpenPorts([554, 3702, 8081]);
|
|
discovery.SetHttpResult(8081, 200, "camera-http");
|
|
// 3702 is the ONVIF WS-Discovery UDP probe port; the device service uses 8088.
|
|
discovery.SetOnvifResult("http://10.44.0.20:8088/onvif/device_service", "Example", "Model", 3702);
|
|
await using (var scope = Factory.Services.CreateAsyncScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
db.CameraDiscoveryResults.Add(discovery);
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
var response = await admin.PostAsJsonAsync("api/discovery/confirm", new
|
|
{
|
|
siteId,
|
|
name = "Discovered camera",
|
|
discoveryResultId = discovery.Id,
|
|
mainStreamUrl = "rtsp://10.44.0.20:8554/primary",
|
|
subStreamUrl = "rtsp://10.44.0.20:8555/secondary"
|
|
});
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
var cameraId = (await response.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
|
|
await using var verifyScope = Factory.Services.CreateAsyncScope();
|
|
var persisted = await verifyScope.ServiceProvider.GetRequiredService<AppDbContext>().Cameras.FindAsync(cameraId);
|
|
persisted.ShouldNotBeNull();
|
|
persisted!.RtspPort.ShouldBe(8554);
|
|
persisted.HttpPort.ShouldBe(8081);
|
|
persisted.OnvifPort.ShouldBe(8088);
|
|
persisted.Capabilities.HasFlag(CameraCapability.RtspStreaming).ShouldBeTrue();
|
|
persisted.Capabilities.HasFlag(CameraCapability.Onvif).ShouldBeTrue();
|
|
persisted.Capabilities.HasFlag(CameraCapability.SubStream).ShouldBeTrue();
|
|
persisted.Capabilities.HasFlag(CameraCapability.OnvifPtz).ShouldBeFalse();
|
|
persisted.Capabilities.HasFlag(CameraCapability.OnvifAudioIn).ShouldBeFalse();
|
|
persisted.HasPtz.ShouldBeFalse();
|
|
persisted.HasAudio.ShouldBeFalse();
|
|
persisted.HasMotionDetection.ShouldBeFalse();
|
|
}
|
|
|
|
[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_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()
|
|
{
|
|
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.Forbidden);
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|