Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests/RealAuditDatabase.cs
T
Joseph Doherty 5ca998dc56 fix(audit): persist config audit rows written after the final save (DEP-1, MGT-1, TPL-3, WP1 task 1.4)
IAuditService.LogAsync only stages the row; every caller that audited
after its last SaveChangesAsync, outside the configuration-command
transaction, lost the row with the DI scope (rig: zero Disable/Enable/
Delete/DeployArtifacts rows, 16 Deploy rows for 62 deployments, one
Create/Instance row for ids up to 3876).

- New IAuditService.LogAndSaveAsync: stage + save, best-effort (logs and
  swallows any failure incl. cancellation, detaches the unsaved row).
- DeploymentService (deploy/failed/reconciled/lifecycle + timeouts),
  ArtifactDeploymentService (DeployArtifacts now before the best-effort
  stamp, RetryArtifactDeployment, DeploySiteSecrets), InstanceService,
  AreaService, SiteService: post-save LogAsync -> LogAndSaveAsync.
- ManagementActor: AuditAndSaveAsync for the non-configuration handlers
  (API keys, shared schemas, ES methods, data-connection move); move now
  audits the scrubbed public shape (MGT-4) so the fix does not start
  writing protocol credentials into AuditLogEntries.
- HandleCreateInstance no longer stages a second Create row on top of
  InstanceService's own.

Tests: real-context (SQLite) tests per family read the row back through
a second context - DeploymentService, ArtifactDeploymentService,
InstanceService, ManagementActor; each fails against the old code.
Test projects gain a ConfigurationDatabase + EF Sqlite reference.
2026-12-03 09:59:19 -05:00

53 lines
2.1 KiB
C#

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Services;
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests;
/// <summary>
/// A real <see cref="AuditService"/> over an in-memory SQLite configuration database, for the
/// code review 2026-12-02 DEP-1 / MGT-1 / TPL-3 regression tests. The pre-fix unit tests
/// mocked <c>IAuditService</c> and only asserted the call, which passed while the staged row
/// was never saved; these tests read the row back through a SECOND context on the same
/// connection, so only a row that was actually committed counts.
/// </summary>
internal sealed class RealAuditDatabase : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions<ScadaBridgeDbContext> _options;
/// <summary>Opens the database and builds the audit service over a fresh context.</summary>
public RealAuditDatabase()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
_options = new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlite(_connection).Options;
Context = new ScadaBridgeDbContext(_options);
Context.Database.EnsureCreated();
AuditService = new AuditService(Context, new AuditCorrelationContext());
}
/// <summary>The unit-of-work context the audit service writes through.</summary>
public ScadaBridgeDbContext Context { get; }
/// <summary>The real audit service under test.</summary>
public AuditService AuditService { get; }
/// <summary>Reads the committed audit rows through an independent context.</summary>
/// <returns>Every persisted audit row.</returns>
public async Task<List<AuditLogEntry>> PersistedRowsAsync()
{
await using var reader = new ScadaBridgeDbContext(_options);
return await reader.AuditLogEntries.AsNoTracking().ToListAsync();
}
/// <inheritdoc />
public void Dispose()
{
Context.Dispose();
_connection.Dispose();
}
}