Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Services/AuditTreeScopeTests.cs
T
Joseph Doherty 2162fad8e9 fix(audit): scope audit reads by site identifier, not numeric site id (MGT-2, UI-5, WP1 task 1.6)
PermittedSiteIds / SiteId claims are numeric Site.Id values while
AuditLog.SourceSiteId holds the site identifier. /api/audit/query and
/export compared the two (a scoped query was always empty, an explicit
--site of one's own site was a 403), /api/audit/tree and Flow Debugger
Replay dropped every site-side node, and the Central UI Audit Log grid
applied no scope at all.

New Commons AuditSiteScope: translate permitted ids to identifiers
(Administrator / no claims = unscoped; unresolvable = fail closed) and
apply them to a filter (never an empty SourceSiteIds, which the
repository reads as unfiltered). Used by AuditEndpoints (query, export,
tree), FlowDebugRunner.ReplayAsync, and AuditLogQueryService (grid
QueryAsync - pushed into the query before paging - and the tree).
ApplySiteScope gains the Administrator exemption.

Tests: scoped query filters by identifier, own-site explicit filter
allowed and foreign one 403, admin with claims unscoped, tree/replay get
identifiers, UI grid scoped/out-of-scope/unscoped, tree per-call scope.
2026-12-03 10:10:58 -05:00

53 lines
2.8 KiB
C#

using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
using ZB.MOM.WW.ScadaBridge.Security;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Services;
public class AuditTreeScopeTests
{
/// <summary>Verifies that the production tree read resolves a fresh caller scope on every call.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ProductionTreeRead_UsesFreshCallerScope_EveryTime()
{
var repo = Substitute.For<IAuditLogRepository>();
// Code review 2026-12-02 UI-5: the numeric SiteId claim is translated to the site
// IDENTIFIER AuditLog.SourceSiteId holds before it reaches the repository.
var sites = Substitute.For<ISiteRepository>();
sites.GetSiteByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(ci => new ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.Site($"Site {ci.Arg<int>()}", $"site-{ci.Arg<int>()}") { Id = ci.Arg<int>() });
using var provider = new ServiceCollection().AddSingleton(repo).AddSingleton(sites).BuildServiceProvider();
var auth = new ChangingAuth();
var service = new AuditLogQueryService(provider.GetRequiredService<IServiceScopeFactory>(),
Substitute.For<ICentralHealthAggregator>(), auth);
var execution = Guid.NewGuid();
auth.Site = "42";
await service.GetExecutionTreeAsync(execution);
auth.Site = "43";
await service.GetExecutionTreeAsync(execution);
await repo.Received(1).GetExecutionTreeAsync(execution, Arg.Any<CancellationToken>(),
Arg.Is<IReadOnlyList<string>?>(x => x != null && x.SequenceEqual(new[] { "site-42" })));
await repo.Received(1).GetExecutionTreeAsync(execution, Arg.Any<CancellationToken>(),
Arg.Is<IReadOnlyList<string>?>(x => x != null && x.SequenceEqual(new[] { "site-43" })));
}
private sealed class ChangingAuth : AuthenticationStateProvider
{
/// <summary>Gets or sets the site id claim that the next authentication state carries.</summary>
public string Site { get; set; } = "42";
/// <inheritdoc />
public override Task<AuthenticationState> GetAuthenticationStateAsync() => Task.FromResult(new AuthenticationState(
new ClaimsPrincipal(new ClaimsIdentity(new[] {
new Claim(JwtTokenService.RoleClaimType, Roles.Viewer),
new Claim(JwtTokenService.SiteIdClaimType, Site)
}, "test"))));
}
}