Files
Heimdall/tests/Heimdall.IntegrationTests/Scenarios/CameraAuthorizationScenarios.cs
T
arnaud.dartois 636154572b
Heimdall Gitea / Detect CI scope (push) Successful in 6s
Heimdall Gitea / Build and tests (push) Successful in 19m19s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 6m13s
Heimdall Gitea / Build and scan agent image (push) Successful in 14m44s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m48s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 19m9s
Heimdall Gitea / CI policy (push) Successful in 10s
Complete remaining Heimdall features and acceptance
Merge pull request #138

Co-authored-by: Arnaud Dartois <admin@tordais.cc>
2026-11-06 11:14:49 +01:00

145 lines
7.0 KiB
C#

using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Heimdall.IntegrationTests.Controllers;
using Shouldly;
namespace Heimdall.IntegrationTests.Scenarios;
[Collection("Postgres")]
public sealed class CameraAuthorizationScenarios : PostgresTestBase
{
public CameraAuthorizationScenarios(PostgreSqlContainerFixture postgres) : base(postgres) { }
[Fact]
public async Task Direct_camera_grant_is_scoped_and_soft_delete_revokes_api_access()
{
var admin = await Factory.CreateAdminClientAsync();
var viewer = await Factory.CreateViewerClientAsync(admin);
var viewerId = UserIdFromAccessToken(viewer);
var siteResponse = await admin.PostAsJsonAsync("api/sites", new
{
name = $"Access matrix {Guid.NewGuid():N}",
timezone = "UTC"
});
siteResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var siteId = (await siteResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
var allowedCameraResponse = await admin.PostAsJsonAsync("api/cameras", new
{
siteId,
name = "Granted camera",
rtspUrl = "rtsp://127.0.0.1:554/stream"
});
allowedCameraResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var allowedCameraId = (await allowedCameraResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
var deniedCameraResponse = await admin.PostAsJsonAsync("api/cameras", new
{
siteId,
name = "Unassigned camera",
rtspUrl = "rtsp://127.0.0.2:554/stream"
});
deniedCameraResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var deniedCameraId = (await deniedCameraResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
var grantResponse = await admin.PostAsJsonAsync("api/user-camera-access", new
{
userId = viewerId,
siteId,
cameraId = allowedCameraId
});
grantResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
// The authorized route reaches the query and returns its normal empty result;
// a same-site camera without a grant is stopped by the resource boundary.
(await viewer.GetAsync($"api/assignments/camera/{allowedCameraId}")).StatusCode
.ShouldBe(HttpStatusCode.NotFound);
(await viewer.GetAsync($"api/assignments/camera/{deniedCameraId}")).StatusCode
.ShouldBe(HttpStatusCode.Forbidden);
var groupResponse = await admin.PostAsJsonAsync("api/groups", new { name = $"Private group {Guid.NewGuid():N}" });
groupResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var groupId = (await groupResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
(await viewer.GetAsync($"api/groups/{groupId}/cameras")).StatusCode.ShouldBe(HttpStatusCode.Forbidden);
(await viewer.PostAsJsonAsync($"api/groups/{groupId}/cameras", new { groupId, cameraId = deniedCameraId }))
.StatusCode.ShouldBe(HttpStatusCode.Forbidden);
(await admin.DeleteAsync($"api/cameras/{allowedCameraId}")).StatusCode.ShouldBe(HttpStatusCode.NoContent);
(await viewer.GetAsync($"api/assignments/camera/{allowedCameraId}")).StatusCode.ShouldBe(HttpStatusCode.Forbidden);
}
[Fact]
public async Task Direct_and_group_grants_form_a_live_union_and_revocation_preserves_other_grants()
{
var admin = await Factory.CreateAdminClientAsync();
var viewer = await Factory.CreateViewerClientAsync(admin);
var viewerId = UserIdFromAccessToken(viewer);
var siteResponse = await admin.PostAsJsonAsync("api/sites", new
{
name = $"Group access {Guid.NewGuid():N}",
timezone = "UTC"
});
siteResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var siteId = (await siteResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
var cameraResponse = await admin.PostAsJsonAsync("api/cameras", new
{
siteId,
name = "Group granted camera",
rtspUrl = "rtsp://127.0.0.1:554/group-test"
});
cameraResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var cameraId = (await cameraResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
var groupResponse = await admin.PostAsJsonAsync("api/groups", new
{
name = $"Camera viewers {Guid.NewGuid():N}"
});
groupResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var groupId = (await groupResponse.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("id").GetGuid();
(await admin.PostAsync($"api/groups/{groupId}/users/{viewerId}", content: null))
.StatusCode.ShouldBe(HttpStatusCode.OK);
(await admin.PostAsJsonAsync($"api/groups/{groupId}/cameras", new { groupId, cameraId }))
.StatusCode.ShouldBe(HttpStatusCode.OK);
(await viewer.GetAsync($"api/cameras/{cameraId}")).StatusCode.ShouldBe(HttpStatusCode.OK);
var directGrantResponse = await admin.PostAsJsonAsync("api/user-camera-access", new
{
userId = viewerId,
siteId,
cameraId
});
directGrantResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
var directGrantId = (await directGrantResponse.Content.ReadFromJsonAsync<JsonElement>())
.GetProperty("id").GetGuid();
(await admin.DeleteAsync($"api/groups/{groupId}/users/{viewerId}")).StatusCode.ShouldBe(HttpStatusCode.OK);
(await viewer.GetAsync($"api/cameras/{cameraId}")).StatusCode.ShouldBe(HttpStatusCode.OK,
"the remaining direct grant continues to authorize the camera");
(await admin.DeleteAsync($"api/user-camera-access/{directGrantId}")).StatusCode.ShouldBe(HttpStatusCode.OK);
(await viewer.GetAsync($"api/cameras/{cameraId}")).StatusCode.ShouldBe(HttpStatusCode.Forbidden);
(await admin.PostAsync($"api/groups/{groupId}/users/{viewerId}", content: null))
.StatusCode.ShouldBe(HttpStatusCode.OK);
(await viewer.GetAsync($"api/cameras/{cameraId}")).StatusCode.ShouldBe(HttpStatusCode.OK);
(await admin.DeleteAsync($"api/groups/{groupId}")).StatusCode.ShouldBe(HttpStatusCode.OK);
(await viewer.GetAsync($"api/cameras/{cameraId}")).StatusCode.ShouldBe(HttpStatusCode.Forbidden,
"soft deleting a group revokes its camera grants immediately");
}
private static Guid UserIdFromAccessToken(HttpClient client)
{
var token = client.DefaultRequestHeaders.Authorization?.Parameter
?? throw new InvalidOperationException("The test client has no access token");
var payload = token.Split('.')[1].Replace('-', '+').Replace('_', '/');
payload = payload.PadRight((payload.Length + 3) / 4 * 4, '=');
using var document = JsonDocument.Parse(Convert.FromBase64String(payload));
return document.RootElement.GetProperty("sub").GetGuid();
}
}