Heimdall Gitea / Build and tests (pull_request) Failing after 9m7s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
202 lines
7.3 KiB
C#
202 lines
7.3 KiB
C#
using Bunit;
|
|
using Bunit.TestDoubles;
|
|
using Heimdall.Web.Components.Pages;
|
|
using Heimdall.Web.Services;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MudBlazor;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Web.Tests.Components.Pages;
|
|
|
|
public class DiscoveriesPageTests : PageTestBase
|
|
{
|
|
private static readonly Guid SiteId = Guid.NewGuid();
|
|
private static readonly Guid DiscoveryId = Guid.NewGuid();
|
|
|
|
private static PendingDiscoveryItem Discovery(
|
|
string ip = "192.168.1.10",
|
|
string? vendor = "Hikvision",
|
|
string? hardware = "DS-2CD2T47",
|
|
string[]? urls = null) => new()
|
|
{
|
|
Id = DiscoveryId,
|
|
SiteId = SiteId,
|
|
IpAddress = ip,
|
|
MacAddress = "3C:EF:8C:12:34:56",
|
|
Vendor = vendor,
|
|
OnvifHardware = hardware,
|
|
IsReachable = true,
|
|
DetectedType = "Standard",
|
|
DiscoveryMethod = "OnvifWsDiscovery",
|
|
OpenPorts = [80, 554, 3702],
|
|
OnvifDeviceServiceUrl = $"http://{ip}/onvif/device_service",
|
|
OnvifName = vendor,
|
|
OnvifPort = 3702,
|
|
ProbedAt = DateTime.UtcNow.AddMinutes(-2),
|
|
StreamUrls = urls ?? [$"rtsp://{ip}:554/stream1"],
|
|
};
|
|
|
|
private static AgentItem Agent() => new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SiteId = SiteId,
|
|
Name = "Agent 1",
|
|
Hostname = "host1",
|
|
Status = "Online",
|
|
};
|
|
|
|
private void RouteDiscoveries(List<PendingDiscoveryItem> discoveries)
|
|
{
|
|
Handler.OnGet("api/cameras/by-site", new PagedData<CameraItem>([], 0, 1, 100));
|
|
Handler.OnGet("api/discovery/sites/", discoveries);
|
|
Handler.OnGet("api/agents/by-site/", new PagedData<AgentItem>([Agent()], 1, 1, 50));
|
|
Handler.OnGet($"/api/sites/{SiteId}/credentials", Array.Empty<SiteCredentialItem>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// The merged CamerasPage loads cameras + discoveries + agents + credentials
|
|
/// on init. Set up all required routes before rendering.
|
|
/// </summary>
|
|
private void SetupAllRoutes(List<PendingDiscoveryItem>? discoveries = null)
|
|
{
|
|
Handler.OnGet("api/cameras/by-site", new PagedData<CameraItem>([], 0, 1, 100));
|
|
Handler.OnGet("api/discovery/sites/", discoveries ?? []);
|
|
Handler.OnGet("api/agents/by-site/", new PagedData<AgentItem>([Agent()], 1, 1, 50));
|
|
Handler.OnGet($"/api/sites/{SiteId}/credentials", Array.Empty<SiteCredentialItem>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Renders the merged CamerasPage and switches to the "Découverte" tab (index 1).
|
|
/// </summary>
|
|
private IRenderedComponent<HostWithProviders> RenderDiscoveryTab(
|
|
Action<ComponentParameterCollectionBuilder<CamerasPage>>? parameters = null)
|
|
{
|
|
var cut = RenderPage<CamerasPage>(parameters ?? (p => p.Add(x => x.SiteId, SiteId)));
|
|
// Click the "Découverte" tab (index 1)
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Découverte"));
|
|
cut.FindAll("div.mud-tab")[1].Click();
|
|
return cut;
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_AfficheListeDesDecouvertes()
|
|
{
|
|
SetupAllRoutes([Discovery()]);
|
|
LoginAs();
|
|
|
|
var cut = RenderDiscoveryTab();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
cut.Markup.ShouldContain("192.168.1.10");
|
|
cut.Markup.ShouldContain("Hikvision");
|
|
cut.Markup.ShouldContain("DS-2CD2T47");
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_AucuneDecouverte_AfficheMessage()
|
|
{
|
|
SetupAllRoutes([]);
|
|
LoginAs();
|
|
|
|
var cut = RenderDiscoveryTab();
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Aucune caméra détectée"));
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_ScanNow_NotifieAgents()
|
|
{
|
|
SetupAllRoutes([]);
|
|
Handler.On(HttpMethod.Post, "scan-now", new ScanNowResult(2));
|
|
LoginAs();
|
|
|
|
var cut = RenderDiscoveryTab();
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Scanner maintenant"));
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Scanner maintenant")).Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("2 agent", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DiscoveriesPage_Confirmer_AjouteCamera()
|
|
{
|
|
SetupAllRoutes([Discovery()]);
|
|
Handler.On(HttpMethod.Post, "confirm", new CameraItem
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SiteId = SiteId,
|
|
Name = "Hikvision (192.168.1.10)",
|
|
Status = "Online",
|
|
});
|
|
LoginAs();
|
|
|
|
var cut = RenderDiscoveryTab();
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("192.168.1.10"));
|
|
|
|
cut.FindAll("button[aria-label='Confirmer la caméra']")[0].Click();
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Ajouter la caméra 192.168.1.10"));
|
|
|
|
// Le nom par défaut est pré-rempli et l'URL de flux sélectionnée → bouton actif.
|
|
// Find+Click dans InvokeAsync (handler périmé sinon, flaky CI).
|
|
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button")
|
|
.Single(b => b.TextContent.Trim() == "Ajouter une caméra").Click());
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("ajoutée", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DiscoveriesPage_Confirmer_ErreurServeur_AfficheMessageBackend()
|
|
{
|
|
SetupAllRoutes([Discovery()]);
|
|
Handler.On(HttpMethod.Post, "confirm",
|
|
() => RoutedHandler.JsonResponse(System.Net.HttpStatusCode.Conflict,
|
|
new { error = "Caméra déjà enregistrée sur ce site" }));
|
|
LoginAs();
|
|
|
|
var cut = RenderDiscoveryTab();
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("192.168.1.10"));
|
|
|
|
cut.FindAll("button[aria-label='Confirmer la caméra']")[0].Click();
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Ajouter la caméra 192.168.1.10"));
|
|
|
|
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button")
|
|
.Single(b => b.TextContent.Trim() == "Ajouter une caméra").Click());
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Caméra déjà enregistrée sur ce site"));
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_Rejeter_IgnoreDetection()
|
|
{
|
|
SetupAllRoutes([Discovery()]);
|
|
Handler.On(HttpMethod.Post, $"/api/discovery/{DiscoveryId}/reject", System.Net.HttpStatusCode.OK);
|
|
LoginAs();
|
|
|
|
var cut = RenderDiscoveryTab();
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("192.168.1.10"));
|
|
|
|
cut.FindAll("button[aria-label='Ignorer la détection']")[0].Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("ignorée", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
}
|