Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/SecuredWriteTerminalPurgeTests.cs
T
Joseph Doherty 5a052c6327 fix(ui): apply site scope and the history status filter in the server query, before paging (UI-7, UI-9, UI-10, WP1 task 1.7)
UI-7: Site Calls and the Notification report sent an unscoped query and
dropped other sites' rows client-side after paging - Site Calls pages
went short (Next disabled, own rows hidden), Notifications showed other
sites' counts and empty pages. New additive SourceSitesFilter on
SiteCallQueryRequest / NotificationOutboxQueryRequest (+ SourceSites /
SourceSiteIds on the repository filters; OPENJSON IN-list in the
SiteCalls keyset SQL, Contains in EF) carries the permitted identifiers;
an empty set matches nothing. Next is gated on the server page size.

UI-9: Secured Writes history paged over every status and filtered
terminal rows client-side. Additive ListSecuredWritesCommand.Statuses
reaches the repository query and count.

UI-10: Notification KPIs per-site table is narrowed to the caller's
sites (SiteScopeService.Resolve on the principal).

Tests: MSSQL mixed-site keyset/offset paging + count for both repos and
the secured-write status set; handler forwards Statuses; UI pages send
the permitted set (scoped) / none (system-wide); history requests the
terminal statuses; KPI per-site rows narrowed.
2026-12-03 10:21:01 -05:00

93 lines
3.9 KiB
C#

using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.SecuredWrites;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
namespace ZB.MOM.WW.ScadaBridge.ManagementService.Tests;
/// <summary>
/// Review 2026-10-21 08/G3: terminal <c>PendingSecuredWrites</c> rows were never purged.
/// The Secured Writes list sweep (which already expires overdue Pending rows) now also
/// purges decided/executed rows older than <c>SecuredWriteTerminalRetention</c>.
/// </summary>
public class SecuredWriteTerminalPurgeTests : TestKit
{
private readonly ISecuredWriteRepository _repo = Substitute.For<ISecuredWriteRepository>();
private readonly ServiceCollection _services = new ServiceCollection().AddTestConfigurationTransaction();
/// <summary>Initializes the harness with an empty secured-write table.</summary>
public SecuredWriteTerminalPurgeTests()
{
_services.AddScoped(_ => _repo);
_repo.QueryAsync(Arg.Any<string?>(), Arg.Any<string?>(), Arg.Any<int>(), Arg.Any<int>(),
Arg.Any<IReadOnlyCollection<string>?>(), Arg.Any<IReadOnlyCollection<string>?>(), Arg.Any<CancellationToken>())
.Returns(Array.Empty<PendingSecuredWrite>());
}
private void UseOptions(ManagementServiceOptions options) =>
_services.AddSingleton<IOptions<ManagementServiceOptions>>(Options.Create(options));
private IActorRef CreateActor() =>
Sys.ActorOf(Props.Create(() => new ManagementActor(
_services.BuildServiceProvider(), NullLogger<ManagementActor>.Instance)));
private static ManagementEnvelope List() =>
new(new AuthenticatedUser("admin", "Admin", new[] { "Administrator" }, Array.Empty<string>()),
new ListSecuredWritesCommand(null, null), Guid.NewGuid().ToString("N"));
/// <summary>The list sweep purges terminal rows older than the retention window.</summary>
[Fact]
public void List_PurgesTerminalRowsOlderThanRetention()
{
UseOptions(new ManagementServiceOptions
{
SecuredWriteTerminalRetention = TimeSpan.FromDays(30),
SecuredWriteTerminalPurgeInterval = TimeSpan.Zero,
});
DateTime? cutoff = null;
_repo.PurgeTerminalAsync(Arg.Do<DateTime>(c => cutoff = c), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(3);
CreateActor().Tell(List());
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
Assert.NotNull(cutoff);
Assert.InRange(cutoff!.Value, DateTime.UtcNow.AddDays(-30).AddMinutes(-1), DateTime.UtcNow.AddDays(-30).AddMinutes(1));
}
/// <summary>A non-positive retention disables the purge.</summary>
[Fact]
public void List_WithRetentionDisabled_DoesNotPurge()
{
UseOptions(new ManagementServiceOptions
{
SecuredWriteTerminalRetention = TimeSpan.Zero,
SecuredWriteTerminalPurgeInterval = TimeSpan.Zero,
});
CreateActor().Tell(List());
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
_repo.DidNotReceive().PurgeTerminalAsync(Arg.Any<DateTime>(), Arg.Any<int>(), Arg.Any<CancellationToken>());
}
/// <summary>A purge failure is best-effort — the list still answers.</summary>
[Fact]
public void List_PurgeFailure_DoesNotFailTheList()
{
UseOptions(new ManagementServiceOptions { SecuredWriteTerminalPurgeInterval = TimeSpan.Zero });
_repo.PurgeTerminalAsync(Arg.Any<DateTime>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("db down"));
CreateActor().Tell(List());
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
}
}