2026-07-17 06:18:56 +00:00
|
|
|
using System.Net.Http.Json;
|
2026-07-17 13:15:25 +00:00
|
|
|
using System.Text.Json;
|
2026-07-17 06:18:56 +00:00
|
|
|
using Heimdall.IntegrationTests.Factories;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
|
|
2026-07-21 10:24:11 +00:00
|
|
|
public class CamerasControllerTests : PostgresTestBase
|
2026-07-17 06:18:56 +00:00
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
public CamerasControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
2026-07-17 06:18:56 +00:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CreateCamera_WithAuth_ReturnsOk()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
2026-07-17 06:18:56 +00:00
|
|
|
|
|
|
|
|
var siteResponse = await client.PostAsJsonAsync("api/sites", new
|
|
|
|
|
{
|
|
|
|
|
name = "Camera Test Site",
|
|
|
|
|
timezone = "UTC"
|
|
|
|
|
});
|
2026-07-17 13:15:25 +00:00
|
|
|
var siteContent = await siteResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var siteId = siteContent.GetProperty("id").GetGuid();
|
2026-07-17 06:18:56 +00:00
|
|
|
|
|
|
|
|
var cameraResponse = await client.PostAsJsonAsync("api/cameras", new
|
|
|
|
|
{
|
|
|
|
|
siteId,
|
|
|
|
|
name = "Caméra Entrée",
|
|
|
|
|
rtspUrl = "rtsp://192.168.1.100:554/stream1",
|
|
|
|
|
manufacturer = "Hikvision",
|
|
|
|
|
model = "DS-2CD2T47G2-L"
|
|
|
|
|
});
|
|
|
|
|
cameraResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ListCameras_BySite_ReturnsOk()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
2026-07-17 06:18:56 +00:00
|
|
|
var response = await client.GetAsync($"api/cameras/by-site/{Guid.NewGuid()}?page=1&pageSize=10");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
2026-08-19 22:38:34 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ToggleStatus_WithAuth_TogglesCamera()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
|
|
|
|
|
// Create a site first
|
|
|
|
|
var siteResponse = await client.PostAsJsonAsync("api/sites", new
|
|
|
|
|
{
|
|
|
|
|
name = "Toggle Test Site",
|
|
|
|
|
timezone = "UTC"
|
|
|
|
|
});
|
|
|
|
|
var siteContent = await siteResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var siteId = siteContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
// Create a camera (starts as Disabled)
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/cameras", new
|
|
|
|
|
{
|
|
|
|
|
siteId,
|
|
|
|
|
name = "Toggle Cam",
|
|
|
|
|
rtspUrl = "rtsp://192.168.1.100:554/stream1"
|
|
|
|
|
});
|
|
|
|
|
createResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
var cameraContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var cameraId = cameraContent.GetProperty("id").GetGuid();
|
|
|
|
|
cameraContent.GetProperty("status").GetString().ShouldBe("Disabled");
|
|
|
|
|
|
|
|
|
|
// Toggle → Online
|
|
|
|
|
var toggleResponse = await client.PostAsync($"api/cameras/{cameraId}/toggle-status", null);
|
|
|
|
|
toggleResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
var toggled = await toggleResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
toggled.GetProperty("status").GetString().ShouldBe("Online");
|
|
|
|
|
|
|
|
|
|
// Toggle back → Disabled
|
|
|
|
|
var toggleBack = await client.PostAsync($"api/cameras/{cameraId}/toggle-status", null);
|
|
|
|
|
toggleBack.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
var toggledBack = await toggleBack.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
toggledBack.GetProperty("status").GetString().ShouldBe("Disabled");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-10-20 07:38:05 +02:00
|
|
|
public async Task ToggleStatus_Nonexistent_ReturnsForbidden()
|
2026-08-19 22:38:34 +02:00
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.PostAsync($"api/cameras/{Guid.NewGuid()}/toggle-status", null);
|
2026-10-20 07:38:05 +02:00
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden);
|
2026-08-19 22:38:34 +02:00
|
|
|
}
|
2026-07-17 06:18:56 +00:00
|
|
|
}
|