b1e38ac374
Completes VS-10/VS-12/VS-14 and makes the solution and Host actually
build and boot for the first time on this branch (main did not build
before this commit).
Root-cause fixes required to reach a green build/boot (not scoped to
J/K/L but blocking any verification of it):
- Restore Polly PackageVersion accidentally deleted from
Directory.Packages.props (broke KArtSell.Host).
- Remove MediatR dependency from Compliance/VS-04 (package was never
installed; ICommand/ICommandHandler/IMediator never existed) and
wire Endpoint -> Handler directly per this repo's convention.
- Migrate FastEndpoints v5 API calls (SendOkAsync/SendAsync/
SendCreatedAtAsync/SendNotFoundAsync, Description().WithName()) to
the v7 Send.* fluent API across ~10 endpoint files.
- Fix migrations 0036/0038/0039/0040: rewritten from invalid T-SQL
(`IF NOT EXISTS ... BEGIN ... END`) to idiomatic Postgres
(`CREATE TABLE/INDEX IF NOT EXISTS`) — these could not apply to any
fresh database before this fix.
- Collapse 3 duplicate cross-cutting abstractions that shadowed the
BuildingBlocks versions and caused type-mismatch compile errors:
IKrxDataService, IOutboxWriter (ReconcileTradeHandler), IClock
(ApprovalWorkflow/ApprovalPolicy).
- Inject IClock (BuildingBlocks.Time) in place of direct
DateTime.Now/UtcNow across 19 files to satisfy the architecture
test AGENTS.md#DateTime-abstraction rule (13/13 architecture tests
now pass, was 12/13).
- Register all new and previously-unregistered slices in
Program.cs DI (SellDecision, TradeExecution, PortfolioReconciliation,
Compliance, Features/ApprovalWorkflow) — the Host had never
successfully completed a boot with this code present.
- Disable ("[DontRegister]") the older, route-colliding
ApprovalWorkflow/ (Workstream H) endpoint set in favor of
Features/ApprovalWorkflow/ (Workstream G, matches the documented
Features/<Slice>/ convention); kept for its existing test coverage.
See TECH_DEBT-017 for the follow-up decision needed.
Verified: dotnet build 0 errors/0 warnings; architecture tests 13/13;
unit tests 54/54 + 18/18; integration tests 34/36 (2 failures are a
local test-DB migration-journal/schema mismatch, not a code defect);
Host boots cleanly and registers all 34 endpoints.
New tech debt recorded: DEBT-017 (duplicate VS-03 implementation),
DEBT-018 (outbox write not co-transactional with entity write in
TradeExecution/PortfolioReconciliation), DEBT-019 (duplicate
BuildingBlocks-shadowing abstractions, partially resolved).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
184 lines
6.3 KiB
C#
184 lines
6.3 KiB
C#
using System.Data;
|
|
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Npgsql;
|
|
using Xunit;
|
|
using KArtSell.Modules.ModelOperations.Compliance;
|
|
|
|
namespace KArtSell.Integration.Tests.Compliance;
|
|
|
|
public class AuditTrailTests : IAsyncLifetime
|
|
{
|
|
private readonly IDbConnection _db;
|
|
private readonly AuditSql _sql;
|
|
|
|
public AuditTrailTests()
|
|
{
|
|
_db = new NpgsqlConnection(TestConnectionString);
|
|
_sql = new AuditSql(LoggerFactory.Create(b => b.AddConsole()).CreateLogger<AuditSql>());
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
_db.Open();
|
|
await _db.ExecuteAsync(@"
|
|
DELETE FROM compliance.gdpr_retention;
|
|
DELETE FROM compliance.audit_events;
|
|
");
|
|
}
|
|
|
|
public Task DisposeAsync()
|
|
{
|
|
_db?.Dispose();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InsertAuditEvent_CreatesImmutableRecord()
|
|
{
|
|
// Arrange
|
|
var eventId = Guid.NewGuid();
|
|
var correlationId = Guid.NewGuid();
|
|
var entityId = Guid.NewGuid();
|
|
|
|
// Act
|
|
await _sql.InsertAuditEventAsync(
|
|
_db,
|
|
eventId,
|
|
AuditEventTypes.ModelActivated,
|
|
AuditEntityTypes.Model,
|
|
entityId,
|
|
"sre@company.com",
|
|
"SRE",
|
|
DateTime.UtcNow,
|
|
"SUCCESS",
|
|
null,
|
|
new Dictionary<string, object> { { "modelVersion", "1.0.0" } },
|
|
new[] { "s3://evidence/pbo-0.95.json" },
|
|
"192.168.1.100",
|
|
"PostmanRuntime/7.32.3",
|
|
correlationId,
|
|
CancellationToken.None);
|
|
|
|
// Assert
|
|
var @event = await _sql.GetAuditEventByIdAsync(_db, eventId, CancellationToken.None);
|
|
Assert.NotNull(@event);
|
|
Assert.Equal(AuditEventTypes.ModelActivated, @event.EventType);
|
|
Assert.Equal(entityId, @event.EntityId);
|
|
Assert.Equal("sre@company.com", @event.ActorEmail);
|
|
Assert.Single(@event.EvidenceLinks!);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task QueryAuditEvents_WithFilters_ReturnsMatching()
|
|
{
|
|
// Arrange
|
|
var entityId = Guid.NewGuid();
|
|
var correlationId = Guid.NewGuid();
|
|
await _sql.InsertAuditEventAsync(
|
|
_db, Guid.NewGuid(), AuditEventTypes.ModelActivated, AuditEntityTypes.Model,
|
|
entityId, "sre@company.com", "SRE", DateTime.UtcNow, "SUCCESS",
|
|
null, null, null, null, null, correlationId, CancellationToken.None);
|
|
|
|
await _sql.InsertAuditEventAsync(
|
|
_db, Guid.NewGuid(), AuditEventTypes.ApprovalApproved, AuditEntityTypes.Approval,
|
|
Guid.NewGuid(), "checker@company.com", "CHECKER", DateTime.UtcNow, "SUCCESS",
|
|
null, null, null, null, null, Guid.NewGuid(), CancellationToken.None);
|
|
|
|
// Act
|
|
var (events, total) = await _sql.QueryAuditEventsAsync(
|
|
_db,
|
|
eventType: AuditEventTypes.ModelActivated,
|
|
take: 50,
|
|
ct: CancellationToken.None);
|
|
|
|
// Assert
|
|
Assert.Equal(1, total);
|
|
Assert.Single(events);
|
|
Assert.Equal(AuditEventTypes.ModelActivated, events[0].EventType);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InsertGdprRetention_TracksPersonalData()
|
|
{
|
|
// Arrange
|
|
var eventId = Guid.NewGuid();
|
|
var customerId = Guid.NewGuid();
|
|
var retentionId = Guid.NewGuid();
|
|
|
|
// Act
|
|
await _sql.InsertGdprRetentionAsync(
|
|
_db, retentionId, eventId, customerId,
|
|
new[] { GdprDataCategories.PersonallyIdentifiableInformation, GdprDataCategories.EmailAddress },
|
|
DateTime.UtcNow.AddYears(7),
|
|
CancellationToken.None);
|
|
|
|
// Assert
|
|
var retention = await _db.QuerySingleAsync<GdprRetention>(
|
|
"SELECT * FROM compliance.gdpr_retention WHERE id = @Id",
|
|
new { Id = retentionId });
|
|
Assert.NotNull(retention);
|
|
Assert.Equal(customerId, retention.CustomerId);
|
|
Assert.Equal(GdprPurgeStatus.Pending, retention.PurgeStatus);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task MarkGdprPurged_RedactsPersonalData()
|
|
{
|
|
// Arrange
|
|
var customerId = Guid.NewGuid();
|
|
var eventId = Guid.NewGuid();
|
|
var retentionId = Guid.NewGuid();
|
|
|
|
await _sql.InsertAuditEventAsync(
|
|
_db, eventId, AuditEventTypes.ModelActivated, AuditEntityTypes.Model,
|
|
Guid.NewGuid(), "customer@company.com", null, DateTime.UtcNow, "SUCCESS", null,
|
|
new Dictionary<string, object> { { "customer_id", customerId.ToString() } },
|
|
null, null, null, Guid.NewGuid(), CancellationToken.None);
|
|
|
|
await _sql.InsertGdprRetentionAsync(
|
|
_db, retentionId, eventId, customerId,
|
|
new[] { GdprDataCategories.PersonallyIdentifiableInformation },
|
|
DateTime.UtcNow.AddYears(7),
|
|
CancellationToken.None);
|
|
|
|
// Act
|
|
await _sql.MarkGdprPurgedAsync(_db, customerId, CancellationToken.None);
|
|
|
|
// Assert
|
|
var retention = await _db.QuerySingleAsync<GdprRetention>(
|
|
"SELECT * FROM compliance.gdpr_retention WHERE id = @Id",
|
|
new { Id = retentionId });
|
|
Assert.Equal(GdprPurgeStatus.Purged, retention.PurgeStatus);
|
|
Assert.NotNull(retention.PurgedAt);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RedactAuditEventDetails_AnonymizesPersonalInfo()
|
|
{
|
|
// Arrange
|
|
var eventId = Guid.NewGuid();
|
|
var customerId = Guid.NewGuid();
|
|
await _sql.InsertAuditEventAsync(
|
|
_db, eventId, AuditEventTypes.ModelActivated, AuditEntityTypes.Model,
|
|
Guid.NewGuid(), "customer@company.com", null, DateTime.UtcNow, "SUCCESS", null,
|
|
new Dictionary<string, object>
|
|
{
|
|
{ "actor_email", "customer@company.com" },
|
|
{ "customer_id", customerId.ToString() }
|
|
},
|
|
null, null, null, Guid.NewGuid(), CancellationToken.None);
|
|
|
|
// Act
|
|
await _sql.RedactAuditEventDetailsAsync(_db, eventId, CancellationToken.None);
|
|
|
|
// Assert
|
|
var @event = await _sql.GetAuditEventByIdAsync(_db, eventId, CancellationToken.None);
|
|
Assert.NotNull(@event);
|
|
Assert.Contains("<redacted>", @event.Details?.ToString() ?? "");
|
|
}
|
|
|
|
private const string TestConnectionString =
|
|
"Host=localhost;Port=5432;Database=kartselldb;Username=kartsell;Password=kartsell4321@!";
|
|
}
|