118 lines
7.8 KiB
C#
118 lines
7.8 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Acls;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P09")]
|
|
public sealed class GrpcDataAuthoringTests
|
|
{
|
|
[Fact]
|
|
public async Task Editor_saves_a_disabled_draft_and_read_only_user_cannot_modify_it()
|
|
{
|
|
var factory = AclScopeTestDb.Factory(Guid.NewGuid().ToString());
|
|
var editor = new DataConnectionAdminService(factory, new Auth(true), new User());
|
|
var row = new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer" };
|
|
await editor.SaveAsync("MAIN", row, TestContext.Current.CancellationToken);
|
|
var loaded = (await editor.LoadAsync(TestContext.Current.CancellationToken)).DataConnections.ShouldHaveSingleItem();
|
|
loaded.Enabled.ShouldBeFalse();
|
|
var reader = new DataConnectionAdminService(factory, new Auth(false), new User());
|
|
await Should.ThrowAsync<UnauthorizedAccessException>(() => reader.DeleteAsync("MAIN", loaded, TestContext.Current.CancellationToken));
|
|
(await editor.LoadAsync(TestContext.Current.CancellationToken)).DataConnections.Count.ShouldBe(1);
|
|
}
|
|
[Fact]
|
|
public async Task A_secret_value_is_rejected_by_the_authoritative_validator()
|
|
{
|
|
var service = new DataConnectionAdminService(AclScopeTestDb.Factory(Guid.NewGuid().ToString()), new Auth(true), new User());
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("MAIN", new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer", SecretReference = "raw-key" }, TestContext.Current.CancellationToken));
|
|
(await service.LoadAsync(TestContext.Current.CancellationToken)).DataConnections.ShouldBeEmpty();
|
|
}
|
|
[Fact]
|
|
public async Task Edits_advance_revision_and_stale_edits_or_deletes_are_refused()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var service = new DataConnectionAdminService(AclScopeTestDb.Factory(Guid.NewGuid().ToString()), new Auth(true), new User());
|
|
await service.SaveAsync("MAIN", new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer" }, ct);
|
|
var original = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
var stale = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
original.ExpectedClusterId = "OTHER";
|
|
await service.SaveAsync("MAIN", original, ct);
|
|
(await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem().Revision.ShouldBe(2);
|
|
await Should.ThrowAsync<DbUpdateConcurrencyException>(() => service.SaveAsync("MAIN", stale, ct));
|
|
await Should.ThrowAsync<DbUpdateConcurrencyException>(() => service.DeleteAsync("MAIN", stale, ct));
|
|
var current = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
await service.DeleteAsync("MAIN", current, ct);
|
|
(await service.LoadAsync(ct)).DataConnections.ShouldBeEmpty();
|
|
}
|
|
[Fact]
|
|
public async Task Row_identity_cannot_be_moved_renamed_or_deleted_by_a_forged_id()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var service = new DataConnectionAdminService(AclScopeTestDb.Factory(Guid.NewGuid().ToString()), new Auth(true), new User());
|
|
await service.SaveAsync("MAIN", new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer" }, ct);
|
|
var row = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
row.DataConnectionId = "other";
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("MAIN", row, ct));
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.DeleteAsync("MAIN", row, ct));
|
|
row.ClusterId = "OTHER";
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("OTHER", row, ct));
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.DeleteAsync("OTHER", row, ct));
|
|
(await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem().DataConnectionId.ShouldBe("peer");
|
|
}
|
|
[Fact]
|
|
public async Task Read_only_cannot_create_or_enable_write()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var factory = AclScopeTestDb.Factory(Guid.NewGuid().ToString());
|
|
var reader = new DataConnectionAdminService(factory, new Auth(false), new User());
|
|
var row = new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer", Enabled = true,
|
|
AllowedOperations = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.DataOperations.Write };
|
|
await Should.ThrowAsync<UnauthorizedAccessException>(() => reader.SaveAsync("MAIN", row, ct));
|
|
(await reader.LoadAsync(ct)).DataConnections.ShouldBeEmpty();
|
|
}
|
|
[Fact]
|
|
public async Task Connection_with_enabled_mapping_cannot_be_deleted()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var name = Guid.NewGuid().ToString();
|
|
var factory = AclScopeTestDb.Factory(name);
|
|
await using (var db = await factory.CreateDbContextAsync(ct))
|
|
{
|
|
AclScopeTestDb.Seed(db);
|
|
db.ClusterNodes.Add(new ClusterNode { NodeId = "A", ClusterId = "MAIN", Host = "a", ApplicationUri = "urn:a", CreatedBy = "test" });
|
|
db.DataConnections.Add(new DataConnection { DataConnectionId = "peer", ClusterId = "MAIN", Enabled = true,
|
|
ConsumerNodeIds = ["A"], TargetMode = DataTargetMode.Node, ExpectedClusterId = "REMOTE", ExpectedNodeId = "B",
|
|
Endpoints = ["https://peer.example"], SecretReference = "secret:data/peer", SecretScopeClusterId = "MAIN",
|
|
AllowedOperations = DataOperations.Read });
|
|
db.RemoteNamespaceMappings.Add(new RemoteNamespaceMapping { RemoteNamespaceMappingId = "mount", ClusterId = "MAIN", Enabled = true,
|
|
DataConnectionId = "peer", ConsumerNodeIds = ["A"], Realm = DataRealm.Raw, RemoteRealm = DataRealm.Raw,
|
|
LocalParentKind = "Cluster", LocalParentId = "MAIN", BranchName = "remote", RemoteRootEntityId = "root",
|
|
RemoteNamespaceUri = V3NodeIds.RawNamespaceUri, RemoteIdentifierKind = "String", RemoteIdentifier = "root", AllowedOperations = DataOperations.Read });
|
|
await db.SaveChangesAsync(ct);
|
|
}
|
|
var service = new DataConnectionAdminService(factory, new Auth(true), new User());
|
|
var snapshot = await service.LoadAsync(ct);
|
|
DataConnectionValidator.Validate(snapshot).ShouldBeEmpty();
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.DeleteAsync("MAIN", snapshot.DataConnections.ShouldHaveSingleItem(), ct));
|
|
(await service.LoadAsync(ct)).DataConnections.Count.ShouldBe(1);
|
|
}
|
|
private sealed class User : AuthenticationStateProvider
|
|
{
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync() => Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity("test"))));
|
|
}
|
|
private sealed class Auth(bool allowed) : IAuthorizationService
|
|
{
|
|
public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object? resource, IEnumerable<IAuthorizationRequirement> requirements) => Task.FromResult(allowed ? AuthorizationResult.Success() : AuthorizationResult.Failed());
|
|
public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object? resource, string policyName) => Task.FromResult(allowed ? AuthorizationResult.Success() : AuthorizationResult.Failed());
|
|
}
|
|
}
|