201ca44088
RawTree gains an opt-in single-select NodePickMode (any entity kind); new UnsRootPicker and DataRootPickerModal; cluster-scoped trees built from the draft snapshot. Stored kinds and validation unchanged.
189 lines
11 KiB
C#
189 lines
11 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 Microsoft.JSInterop;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
|
using ZB.MOM.WW.OtOpcUa.Security.Auth;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
|
|
|
/// <summary>#1077: the export-root / remote-root fields are cluster-scoped tree pickers.</summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class DataConnectionRootPickerTests : BunitContext
|
|
{
|
|
private const string Cluster = RawTreeTestDb.ClusterId;
|
|
|
|
private sealed class NoDiagnostics : IFleetDiagnosticsClient
|
|
{
|
|
public Task<NodeDiagnosticsSnapshot> GetDiagnosticsAsync(NodeId nodeId, CancellationToken ct) => throw new NotSupportedException();
|
|
}
|
|
|
|
private sealed class PrincipalState : AuthenticationStateProvider
|
|
{
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync() => Task.FromResult(
|
|
new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity([new(ClaimTypes.Name, "fixture"), new(ClaimTypes.Role, "Administrator")], "fixture"))));
|
|
}
|
|
|
|
public DataConnectionRootPickerTests()
|
|
{
|
|
// Node-pick mode never mutates or lazy-loads, so RawTree's mutation dialogs are stubbed and its service refuses any call.
|
|
var names = new HashSet<string> { "DriverConfigModal", "DeviceModal", "RawTagModal", "RawManualTagEntryModal", "RawCsvImportModal", "RawCsvExportModal", "RawBrowseModal", "RawNameDialog", "RawConfirmDialog", "RawDriverTypeDialog", "RawStubModal" };
|
|
ComponentFactories.AddStub(type => names.Contains(type.Name));
|
|
Services.AddSingleton(System.Reflection.DispatchProxy.Create<IRawTreeService, RefusingRawService>());
|
|
JSInterop.Mode = JSRuntimeMode.Loose;
|
|
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true); // ModalFrame closes itself when activation is refused.
|
|
}
|
|
|
|
public class RefusingRawService : System.Reflection.DispatchProxy
|
|
{
|
|
protected override object? Invoke(System.Reflection.MethodInfo? method, object?[]? args) => throw new NotSupportedException("Node-pick mode must not call " + method?.Name);
|
|
}
|
|
|
|
private IRenderedComponent<CascadingValue<Task<AuthenticationState>>> RenderPage()
|
|
{
|
|
Services.RemoveAll<IAuthorizationService>();
|
|
Services.RemoveAll<IAuthorizationPolicyProvider>();
|
|
Services.AddAuthorizationCore(o => o.AddPolicy(AdminUiPolicies.ConfigEditor, p => p.RequireRole("Administrator", "Designer")));
|
|
Services.AddCascadingAuthenticationState();
|
|
Services.AddSingleton<AuthenticationStateProvider>(new PrincipalState());
|
|
Services.AddSingleton<IFleetDiagnosticsClient>(new NoDiagnostics());
|
|
var name = RawTreeTestDb.SeedFresh();
|
|
using (var db = RawTreeTestDb.CreateNamed(name))
|
|
{
|
|
db.UnsAreas.Add(new UnsArea { UnsAreaId = "AREA-1", ClusterId = Cluster, Name = "Packaging" });
|
|
db.UnsLines.Add(new UnsLine { UnsLineId = "LINE-1", UnsAreaId = "AREA-1", Name = "Line one" });
|
|
// Another cluster's entities must be unreachable from this cluster's pickers.
|
|
db.ServerClusters.Add(new ServerCluster { ClusterId = "OTHER", Name = "Other", Enterprise = "zb", Site = "x", RedundancyMode = RedundancyMode.None, CreatedBy = "test" });
|
|
db.UnsAreas.Add(new UnsArea { UnsAreaId = "AREA-OTHER", ClusterId = "OTHER", Name = "Foreign area" });
|
|
db.DataConnections.Add(new DataConnection { DataConnectionId = "CONN-1", ClusterId = Cluster, ExpectedClusterId = "OTHER" });
|
|
db.SaveChanges();
|
|
}
|
|
Services.AddSingleton(RawTreeTestDb.Factory(name));
|
|
Services.AddSingleton<DataConnectionAdminService>();
|
|
var state = new PrincipalState().GetAuthenticationStateAsync();
|
|
return Render<CascadingValue<Task<AuthenticationState>>>(p => p.Add(x => x.Value, state)
|
|
.AddChildContent<ClusterDataConnections>(c => c.Add(x => x.ClusterId, Cluster)));
|
|
}
|
|
|
|
[Fact]
|
|
public void ExportRoot_RawTreePicker_PicksADeviceAndFillsTheField()
|
|
{
|
|
var cut = RenderPage();
|
|
cut.WaitForAssertion(() => cut.Find("button.btn-primary:contains('New export')"));
|
|
cut.Find("button:contains('New export')").Click();
|
|
cut.WaitForAssertion(() => cut.Find("#export-root"));
|
|
cut.Find("#export-root").GetAttribute("value").ShouldBe("No root chosen");
|
|
cut.Find("#export-root-browse").Click();
|
|
// A dialog opened by a click renders on a later pass.
|
|
cut.WaitForAssertion(() => cut.Find("[role=dialog]"));
|
|
cut.WaitForAssertion(() => cut.FindAll("input[type=radio]").Count.ShouldBeGreaterThan(0));
|
|
cut.Find("#root-picker-confirm").HasAttribute("disabled").ShouldBeTrue();
|
|
// Expand down to the device: cluster root is open; folder -> driver -> device.
|
|
cut.Find("button[aria-label='Expand PLCs']").Click();
|
|
cut.WaitForElement("button[aria-label='Expand modbus-1']").Click();
|
|
cut.WaitForElement("input[title='Device dev:" + RawTreeTestDb.FolderedDeviceId + "']").Change(true);
|
|
cut.WaitForAssertion(() => cut.Find("#root-picker-selected").TextContent.ShouldBe("RawDevice " + RawTreeTestDb.FolderedDeviceId));
|
|
cut.Find("#root-picker-confirm").Click();
|
|
cut.WaitForAssertion(() => cut.Find("#export-root").GetAttribute("value").ShouldBe("RawDevice " + RawTreeTestDb.FolderedDeviceId));
|
|
cut.FindAll("[role=dialog]").Count.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void ExportRoot_UnsTreePicker_IsClusterScopedAndPicksAnArea()
|
|
{
|
|
var cut = RenderPage();
|
|
cut.WaitForElement("button:contains('New export')").Click();
|
|
cut.WaitForElement("#export-realm").Change("Uns");
|
|
cut.Find("#export-root-browse").Click();
|
|
cut.WaitForAssertion(() => cut.Find("[role=dialog]"));
|
|
cut.WaitForAssertion(() => cut.FindAll(".uns-root-picker input[type=radio]").Count.ShouldBeGreaterThan(0));
|
|
cut.Markup.ShouldContain("Packaging");
|
|
cut.Find("[role=dialog]").InnerHtml.ShouldNotContain("Foreign area");
|
|
cut.Find("input[title='UnsArea AREA-1']").Change(true);
|
|
cut.Find("#root-picker-confirm").Click();
|
|
cut.WaitForAssertion(() => cut.Find("#export-root").GetAttribute("value").ShouldBe("UnsArea AREA-1"));
|
|
}
|
|
|
|
[Fact]
|
|
public void RemoteRoot_PickerBrowsesTheTargetClusterOnly_AndFillsTheRootId()
|
|
{
|
|
var cut = RenderPage();
|
|
cut.WaitForElement("button:contains('New mapping')").Click();
|
|
cut.WaitForElement("#remote-root-browse").HasAttribute("disabled").ShouldBeTrue(); // no connection chosen yet
|
|
cut.Find("#mapping-connection").Change("CONN-1");
|
|
cut.Find("#mapping-realm").Change("Uns");
|
|
cut.Find("#remote-root-browse").HasAttribute("disabled").ShouldBeFalse();
|
|
cut.Find("#remote-root-browse").Click();
|
|
cut.WaitForElement("[role=dialog]");
|
|
cut.WaitForAssertion(() => cut.FindAll(".uns-root-picker input[type=radio]").Count.ShouldBeGreaterThan(0));
|
|
cut.Find("[role=dialog]").InnerHtml.ShouldContain("Foreign area");
|
|
cut.Find("[role=dialog]").InnerHtml.ShouldNotContain("Packaging");
|
|
cut.Find("input[title='UnsArea AREA-OTHER']").Change(true);
|
|
cut.Find("#root-picker-confirm").Click();
|
|
cut.WaitForAssertion(() => cut.Find("#remote-root").GetAttribute("value").ShouldBe("AREA-OTHER"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Cancel_LeavesTheFieldUnchanged()
|
|
{
|
|
var cut = RenderPage();
|
|
cut.WaitForElement("button:contains('New export')").Click();
|
|
cut.WaitForElement("#export-root-browse").Click();
|
|
cut.WaitForAssertion(() => cut.Find("[role=dialog]"));
|
|
cut.Find("[role=dialog] .modal-footer button:contains('Cancel')").Click();
|
|
cut.WaitForAssertion(() => cut.FindAll("[role=dialog]").Count.ShouldBe(0));
|
|
cut.Find("#export-root").GetAttribute("value").ShouldBe("No root chosen");
|
|
}
|
|
|
|
[Fact]
|
|
public void RawTree_NodePickMode_OffersARadioOnEveryEntityKind_AndLeavesTagPickerModeAlone()
|
|
{
|
|
var tag = new RawNode { Kind = RawNodeKind.Tag, Key = "tag:t", EntityId = "t", DisplayName = "T" };
|
|
var device = new RawNode { Kind = RawNodeKind.Device, Key = "dev:d", EntityId = "d", DisplayName = "D", Expanded = true };
|
|
device.Children.Add(tag);
|
|
RawNode? picked = null;
|
|
var cut = Render<RawTree>(p => p.Add(x => x.Roots, new[] { device }).Add(x => x.NodePickMode, true)
|
|
.Add(x => x.OnNodePicked, n => picked = n));
|
|
cut.FindAll("input[type=radio]").Count.ShouldBe(2);
|
|
cut.Find("input[title='Device dev:d']").Change(true);
|
|
picked.ShouldBeSameAs(device);
|
|
|
|
// The tag picker mode is unchanged: a checkbox on tags only, no radios.
|
|
var multi = Render<RawTree>(p => p.Add(x => x.Roots, new[] { device }).Add(x => x.PickerMode, true).Add(x => x.SelectedTagIds, new HashSet<string>()));
|
|
multi.FindAll("input[type=radio]").Count.ShouldBe(0);
|
|
multi.FindAll("input[type=checkbox]").Count.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Trees_AreClusterScoped_AndNestFoldersGroupsAndTags()
|
|
{
|
|
var name = RawTreeTestDb.SeedFresh();
|
|
using var db = RawTreeTestDb.CreateNamed(name);
|
|
var snapshot = await DraftSnapshotFactory.FromConfigDbAsync(db, CancellationToken.None);
|
|
var raw = DataRootTrees.BuildRaw(snapshot, Cluster);
|
|
raw.Children.Select(c => c.EntityId).ShouldContain(RawTreeTestDb.RootFolderId);
|
|
var group = raw.Children.Single(c => c.EntityId == RawTreeTestDb.RootFolderId).Children.Single().Children.Single().Children.Single();
|
|
group.Kind.ShouldBe(RawNodeKind.TagGroup);
|
|
group.Children.Select(c => c.EntityId).ShouldBe([RawTreeTestDb.HistorizedTagId, RawTreeTestDb.PlainTagId], ignoreOrder: true);
|
|
DataRootTrees.BuildRaw(snapshot, "NOPE").Children.ShouldBeEmpty();
|
|
DataRootTrees.BuildRaw(snapshot, Cluster, includeTags: false).Children.SelectMany(Flatten).ShouldNotContain(n => n.Kind == RawNodeKind.Tag);
|
|
}
|
|
|
|
private static IEnumerable<RawNode> Flatten(RawNode n) => new[] { n }.Concat(n.Children.SelectMany(Flatten));
|
|
}
|