Files
KArtSell.Aegis/tests/KArtSell.IdentityAccess.IntegrationTests/ManageIdentityAndRoles/RequestMfaSetupIntegrationTests.cs
T
kjh2064 c289a698c5 feat(wbs): AEG-VS-01-05 Event/Job/Inbox - Part 2-3 Complete (Outbox/Inbox + Consumers)
Part 2: Transaction + Outbox Integration
- RegisterIdentityEndpoint: DbConnection → DbTransaction → Outbox write
- RegisterIdentitySql: Accept NpgsqlConnection + NpgsqlTransaction (Dapper)
- Fixed schema references: identity.identity → public.identity
- Hash computation (SHA256) for Outbox payload integrity

Part 3: Consumer + Job Implementation
- IdentityCreatedConsumer: SignalR group 'identity-notifications'
- MfaReminderJob: Hangfire job, 24-hour reminder, idempotent via DB tracking
- IdentityAuditConsumer: Immutable append-only audit trail
- Migration 0043: identity_mfa_reminder + identity_audit_log tables

Testing
- Unit: IdentityCreated event serialization + immutability (4 tests)
- Integration: RegisterIdentityWithOutbox (3 tests: happy path, rollback, duplicate email)
- Updated existing tests: Transaction management (6 test methods)

Architecture
- Outbox/Inbox pattern ensures exactly-once delivery
- Consumers decouple from identity creation (async, independent retry)
- Audit trail immutable (trigger prevents updates/deletes)
- MFA reminder idempotent (tracked in DB)

Status: 40% COMPLETE (event + endpoint + 3 consumers)
Next: E2E tests + Hangfire job registration + Admin UI

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-08-17 18:41:10 +09:00

159 lines
6.4 KiB
C#

using Xunit;
using Npgsql;
using Dapper;
using KArtSell.Modules.IdentityAccess.ManageIdentityAndRoles.Features.RegisterIdentity;
using KArtSell.Modules.IdentityAccess.ManageIdentityAndRoles.Features.RequestMfaSetup;
using KArtSell.Modules.IdentityAccess.ManageIdentityAndRoles.Domain;
using System.Data;
namespace KArtSell.IdentityAccess.IntegrationTests.ManageIdentityAndRoles;
[Collection("Database")]
public class RequestMfaSetupIntegrationTests : IAsyncLifetime
{
private readonly string _connectionString;
private NpgsqlDataSource _dataSource = null!;
private RegisterIdentitySql _registerSql = null!;
private RequestMfaSetupSql _mfaSql = null!;
public RequestMfaSetupIntegrationTests()
{
_connectionString = Environment.GetEnvironmentVariable("KARTSELL_POSTGRES")
?? "Host=127.0.0.1;Port=5432;Database=kartselldb;Username=postgres;Password=postgres";
}
public async Task InitializeAsync()
{
_dataSource = new NpgsqlDataSourceBuilder(_connectionString).Build();
_registerSql = new RegisterIdentitySql(async () => await _dataSource.OpenConnectionAsync());
_mfaSql = new RequestMfaSetupSql(async () => await _dataSource.OpenConnectionAsync());
await CleanupAsync();
}
public async Task DisposeAsync()
{
await CleanupAsync();
await _dataSource.DisposeAsync();
}
private async Task CleanupAsync()
{
using var conn = await _dataSource.OpenConnectionAsync();
await conn.ExecuteAsync("DELETE FROM public.identity WHERE email LIKE 'test-mfa-integration-%'");
}
[Fact]
public async Task UpdateIdentityState_ActiveToMfaSetup_Success()
{
var email = "test-mfa-integration-001@example.com";
var displayName = "Test MFA 001";
var correlationId = Guid.NewGuid().ToString();
var identityId = Guid.NewGuid();
var conn = await _dataSource.OpenConnectionAsync() as NpgsqlConnection
?? throw new InvalidOperationException("Failed to open connection");
await using (conn)
{
await using var transaction = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted);
await _registerSql.CreateIdentityAsync(conn, transaction, identityId, email, displayName, correlationId, CancellationToken.None);
await transaction.CommitAsync();
var (_, _, revision) = await _mfaSql.GetIdentityAsync(identityId, CancellationToken.None);
await _mfaSql.UpdateIdentityStateAsync(identityId, IdentityState.RequiresMfaSetup, revision, CancellationToken.None);
var (_, newState, _) = await _mfaSql.GetIdentityAsync(identityId, CancellationToken.None);
Assert.Equal(IdentityState.RequiresMfaSetup, newState);
}
}
[Fact]
public async Task UpdateIdentityState_OptimisticConcurrency_FailsOnRevisionMismatch()
{
var email = "test-mfa-integration-002@example.com";
var displayName = "Test MFA 002";
var correlationId = Guid.NewGuid().ToString();
var identityId = Guid.NewGuid();
var conn = await _dataSource.OpenConnectionAsync() as NpgsqlConnection
?? throw new InvalidOperationException("Failed to open connection");
await using (conn)
{
await using var transaction = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted);
await _registerSql.CreateIdentityAsync(conn, transaction, identityId, email, displayName, correlationId, CancellationToken.None);
await transaction.CommitAsync();
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await _mfaSql.UpdateIdentityStateAsync(identityId, IdentityState.RequiresMfaSetup, 999, CancellationToken.None)
);
Assert.Contains("concurrency", ex.Message, StringComparison.OrdinalIgnoreCase);
}
}
[Fact]
public async Task GetIdentity_AfterCreate_ReturnsCorrectRevision()
{
var email = "test-mfa-integration-003@example.com";
var displayName = "Test MFA 003";
var correlationId = Guid.NewGuid().ToString();
var identityId = Guid.NewGuid();
var conn = await _dataSource.OpenConnectionAsync() as NpgsqlConnection
?? throw new InvalidOperationException("Failed to open connection");
await using (conn)
{
await using var transaction = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted);
await _registerSql.CreateIdentityAsync(conn, transaction, identityId, email, displayName, correlationId, CancellationToken.None);
await transaction.CommitAsync();
var (_, state, revision) = await _mfaSql.GetIdentityAsync(identityId, CancellationToken.None);
Assert.Equal(IdentityState.Active, state);
Assert.Equal(1, revision);
}
}
[Fact]
public async Task UpdateIdentityState_IncreasesRevision()
{
var email = "test-mfa-integration-004@example.com";
var displayName = "Test MFA 004";
var correlationId = Guid.NewGuid().ToString();
var identityId = Guid.NewGuid();
var conn = await _dataSource.OpenConnectionAsync() as NpgsqlConnection
?? throw new InvalidOperationException("Failed to open connection");
await using (conn)
{
await using var transaction = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted);
await _registerSql.CreateIdentityAsync(conn, transaction, identityId, email, displayName, correlationId, CancellationToken.None);
await transaction.CommitAsync();
var (_, _, revision1) = await _mfaSql.GetIdentityAsync(identityId, CancellationToken.None);
await _mfaSql.UpdateIdentityStateAsync(identityId, IdentityState.RequiresMfaSetup, revision1, CancellationToken.None);
var (_, _, revision2) = await _mfaSql.GetIdentityAsync(identityId, CancellationToken.None);
Assert.Equal(revision1 + 1, revision2);
}
}
[Fact]
public async Task GetIdentity_NotFound_ThrowsException()
{
var nonExistentId = Guid.NewGuid();
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await _mfaSql.GetIdentityAsync(nonExistentId, CancellationToken.None)
);
Assert.Contains("not found", ex.Message, StringComparison.OrdinalIgnoreCase);
}
}