3ae3f1bcef
Interface goes to Commons.Interfaces.Services and the state record to Commons.Messages.Health, so CentralCommunicationActor no longer needs the HealthMonitoring namespace. Implementation stays in HealthMonitoring. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
48 lines
2.3 KiB
C#
48 lines
2.3 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>();
|
|
using var provider = new ServiceCollection().AddSingleton(repo).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[] { "42" })));
|
|
await repo.Received(1).GetExecutionTreeAsync(execution, Arg.Any<CancellationToken>(),
|
|
Arg.Is<IReadOnlyList<string>?>(x => x != null && x.SequenceEqual(new[] { "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"))));
|
|
}
|
|
}
|