using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests.DataConnections;
///
/// #1084: — the export-level opt-in a secured (caller-identity) write over a data
/// connection needs on top of the driver's own MxAccess.SecuredWritesOverDataConnections. Default off, valid only with
/// the Write grant it qualifies, added by a default-false column.
///
[Trait("Category", "Unit"), Trait("Feature", "GrpcData")]
public sealed class GrpcDataSecuredWriteExportConfigurationTests
{
[Fact]
public void A_new_export_does_not_allow_secured_writes()
{
new DataExport().AllowSecuredWrites.ShouldBeFalse("fail closed: the opt-in defaults off");
}
[Fact]
public void AllowSecuredWrites_is_valid_only_with_Write_at_authoring_and_on_a_deployed_row()
{
var draft = GrpcDataReservedOperationAuthoringTests.Snapshot(DataOperations.Browse | DataOperations.Read);
DataConnectionValidator.Validate(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
draft.DataExports[0].AllowSecuredWrites = true;
DataConnectionValidator.Validate(draft).ShouldContain(x => x.Code == "DataExportSecuredWriteInvalid" && x.Context == "export");
DataConnectionValidator.ValidateDeployed(draft).ShouldContain(x => x.Code == "DataExportSecuredWriteInvalid" && x.Context == "export");
// With the Write grant the flag authors and deploys cleanly.
draft.DataExports[0].AllowedOperations = DataOperations.Browse | DataOperations.Read | DataOperations.Write;
DataConnectionValidator.Validate(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
DataConnectionValidator.ValidateDeployed(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
// A disabled export is not validated (it grants nothing either way).
draft.DataExports[0].AllowedOperations = DataOperations.Read;
draft.DataExports[0].Enabled = false;
DataConnectionValidator.Validate(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
}
[Fact]
public void Migration_adds_a_default_false_column_reversibly_and_no_constraint()
{
using var db = new OtOpcUaConfigDbContext(new DbContextOptionsBuilder()
.UseSqlServer("Server=127.0.0.1,1;Database=design;Integrated Security=true;TrustServerCertificate=true").Options);
var migrations = db.Database.GetMigrations().ToArray();
var index = Array.FindIndex(migrations, x => x.EndsWith("_AddDataExportAllowSecuredWrites", StringComparison.Ordinal));
index.ShouldBeGreaterThan(0);
var migrator = db.GetService();
var up = migrator.GenerateScript(migrations[index - 1], migrations[index]);
up.ShouldContain("ALTER TABLE [DataExport] ADD [AllowSecuredWrites] bit NOT NULL DEFAULT CAST(0 AS bit)");
up.ShouldNotContain("CHECK", Case.Sensitive, "no constraint, so the upgrade preflight needs no MigrationPreChecks twin");
var down = migrator.GenerateScript(migrations[index], migrations[index - 1]);
down.ShouldContain("DROP COLUMN [AllowSecuredWrites]");
db.Database.HasPendingModelChanges().ShouldBeFalse();
}
}