2026-07-23 15:27:34 +00:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Heimdall.IntegrationTests.Factories;
|
2026-11-04 10:26:07 +01:00
|
|
|
using System.Net.Http.Headers;
|
2026-07-23 15:27:34 +00:00
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
|
|
|
|
|
|
public class RolesControllerTests : PostgresTestBase
|
|
|
|
|
{
|
|
|
|
|
public RolesControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task List_WithoutAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
|
|
|
|
var client = Factory.CreateClient();
|
|
|
|
|
var response = await client.GetAsync("api/roles");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task List_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/roles");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.GetProperty("roles").GetArrayLength().ShouldBeGreaterThan(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetByName_WithAdminAuth_ReturnsRole()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/roles/Admin");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.GetProperty("name").GetString().ShouldBe("Admin");
|
|
|
|
|
content.GetProperty("permissions").GetArrayLength().ShouldBeGreaterThan(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetByName_NonExistent_ReturnsNotFound()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/roles/NonExistentRole");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task List_ContainsExpectedRoles()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/roles");
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var roles = content.GetProperty("roles").EnumerateArray().ToList();
|
|
|
|
|
|
|
|
|
|
roles.Any(r => r.GetProperty("name").GetString() == "Admin").ShouldBeTrue();
|
|
|
|
|
roles.Any(r => r.GetProperty("name").GetString() == "Viewer").ShouldBeTrue();
|
|
|
|
|
}
|
2026-11-04 10:26:07 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Admin_can_change_viewer_permissions_and_existing_viewer_sessions_are_revoked()
|
|
|
|
|
{
|
|
|
|
|
var admin = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var email = $"dynamic-viewer-{Guid.NewGuid():N}@heimdall.test";
|
|
|
|
|
const string password = "DynamicViewer123!";
|
|
|
|
|
var created = await admin.PostAsJsonAsync("api/users", new
|
|
|
|
|
{
|
|
|
|
|
email, password, displayName = "Dynamic Viewer", roles = new[] { "Viewer" }
|
|
|
|
|
});
|
|
|
|
|
created.EnsureSuccessStatusCode();
|
|
|
|
|
using var viewer = Factory.CreateClient();
|
|
|
|
|
var login = await viewer.PostAsJsonAsync("api/auth/login", new { email, password });
|
|
|
|
|
login.EnsureSuccessStatusCode();
|
|
|
|
|
var loginBody = await login.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var oldToken = loginBody.GetProperty("accessToken").GetString()!;
|
|
|
|
|
viewer.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", oldToken);
|
|
|
|
|
var forbiddenUpdate = await viewer.PutAsJsonAsync("api/roles/Viewer/permissions", new
|
|
|
|
|
{
|
|
|
|
|
permissions = new[] { "Site.Read" }
|
|
|
|
|
});
|
|
|
|
|
forbiddenUpdate.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden);
|
|
|
|
|
|
|
|
|
|
var update = await admin.PutAsJsonAsync("api/roles/Viewer/permissions", new
|
|
|
|
|
{
|
|
|
|
|
permissions = new[] { "Site.Read", "Camera.Read", "Event.Read", "Camera.Write" }
|
|
|
|
|
});
|
|
|
|
|
update.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var staleSession = await viewer.GetAsync("api/users/me");
|
|
|
|
|
staleSession.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
|
|
|
|
|
var freshLogin = await viewer.PostAsJsonAsync("api/auth/login", new { email, password });
|
|
|
|
|
freshLogin.EnsureSuccessStatusCode();
|
|
|
|
|
var freshBody = await freshLogin.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
freshBody.GetProperty("user").GetProperty("permissions").EnumerateArray()
|
|
|
|
|
.Select(p => p.GetString()).ShouldContain("Camera.Write");
|
|
|
|
|
|
|
|
|
|
var list = await admin.GetFromJsonAsync<JsonElement>("api/roles");
|
|
|
|
|
var viewerRole = list.GetProperty("roles").EnumerateArray()
|
|
|
|
|
.Single(r => r.GetProperty("name").GetString() == "Viewer");
|
|
|
|
|
viewerRole.GetProperty("permissions").EnumerateArray().Select(p => p.GetString())
|
|
|
|
|
.ShouldNotContain("Camera.Live");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Admin_role_permissions_are_immutable()
|
|
|
|
|
{
|
|
|
|
|
var admin = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var update = await admin.PutAsJsonAsync("api/roles/Admin/permissions", new
|
|
|
|
|
{
|
|
|
|
|
permissions = new[] { "Site.Read" }
|
|
|
|
|
});
|
|
|
|
|
update.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadRequest);
|
|
|
|
|
|
|
|
|
|
var role = await admin.GetFromJsonAsync<JsonElement>("api/roles/Admin");
|
|
|
|
|
var permissions = role.GetProperty("permissions").EnumerateArray().Select(p => p.GetString()).ToArray();
|
|
|
|
|
permissions.ShouldContain("Admin.Users");
|
|
|
|
|
permissions.ShouldContain("Admin.Manage");
|
|
|
|
|
}
|
2026-07-23 15:27:34 +00:00
|
|
|
}
|