Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/ClusterOverviewActionPolicyTests.cs
T

65 lines
3.5 KiB
C#

using System.Security.Claims;
using Bunit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters;
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Security.Auth;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
/// <summary>Overview readers see only action destinations authorized by their existing route policy.</summary>
[Trait("Category", "Unit")]
public sealed class ClusterOverviewActionPolicyTests : BunitContext
{
[Theory]
[InlineData("Reader", false)]
[InlineData("Operator", false)]
[InlineData("Designer", true)]
[InlineData("Administrator", true)]
public void AuthoringActions_FollowExactDestinationPolicy(string role, bool allowed)
{
// bUnit's fail-closed guard registrations use TryAdd slots; replace them with the real policy engine.
Services.RemoveAll<IAuthorizationService>();
Services.RemoveAll<IAuthorizationPolicyProvider>();
Services.AddAuthorizationCore(options => options.AddPolicy(AdminUiPolicies.ConfigEditor,
policy => policy.RequireRole("Administrator", "Designer")));
Services.AddCascadingAuthenticationState();
Services.AddSingleton<AuthenticationStateProvider>(new PrincipalState(role));
var name = Guid.NewGuid().ToString("N");
using (var db = RawTreeTestDb.CreateNamed(name))
{
db.ServerClusters.Add(new ServerCluster { ClusterId="C1", Name="Readable cluster", Enterprise="zb", Site="local", RedundancyMode=ZB.MOM.WW.OtOpcUa.Configuration.Enums.RedundancyMode.None, CreatedBy="fixture" });
db.ClusterNodes.Add(new ClusterNode { ClusterId="C1", NodeId="N1", Host="local", ApplicationUri="urn:local", CreatedBy="fixture" });
db.SaveChanges();
}
Services.AddSingleton(RawTreeTestDb.Factory(name));
var state = new PrincipalState(role).GetAuthenticationStateAsync();
var cut = Render<CascadingValue<Task<AuthenticationState>>>(p => p.Add(x => x.Value,state)
.AddChildContent<ClusterOverview>(child => child.Add(x => x.ClusterId,"C1")));
cut.WaitForAssertion(() => cut.FindAll("h1").Count.ShouldBe(1, cut.Markup));
cut.Markup.ShouldNotContain("page-error-boundary");
cut.Find("h1").TextContent.ShouldBe("Readable cluster");
foreach (var path in new[] { "/clusters/C1/edit", "/clusters/C1/nodes/new", "/clusters/C1/nodes/N1", "/deployments" })
cut.FindAll($"a[href='{path}']").Count.ShouldBe(allowed ? 1 : 0, path);
cut.Markup.ShouldContain("N1");
foreach (var type in new[] { typeof(ClusterEdit), typeof(NodeEdit), typeof(ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Deployments) })
type.GetCustomAttributes(typeof(AuthorizeAttribute), true).Cast<AuthorizeAttribute>()
.ShouldContain(x => x.Policy == AdminUiPolicies.ConfigEditor);
}
private sealed class PrincipalState(string role) : AuthenticationStateProvider
{
public override Task<AuthenticationState> GetAuthenticationStateAsync() => Task.FromResult(
new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity([
new(ClaimTypes.Name,"fixture"), new(ClaimTypes.Role,role)], "fixture"))));
}
}