using Xunit; using Npgsql; using Dapper; using KArtSell.Modules.IdentityAccess.ManageIdentityAndRoles.Features.RegisterIdentity; using System.Data; namespace KArtSell.IdentityAccess.IntegrationTests.ManageIdentityAndRoles; [Collection("Database")] public class RegisterIdentityIntegrationTests : IAsyncLifetime { private readonly string _connectionString; private NpgsqlDataSource _dataSource = null!; private RegisterIdentitySql _sql = null!; public RegisterIdentityIntegrationTests() { _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(); _sql = new RegisterIdentitySql(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 identity.identity WHERE email LIKE 'test-integration-%'"); } [Fact] public async Task CreateIdentity_ValidRequest_InsertsAndReturnsId() { var email = "test-integration-001@example.com"; var displayName = "Test User 001"; var correlationId = Guid.NewGuid().ToString(); var identityId = Guid.NewGuid(); var createdId = await _sql.CreateIdentityAsync(identityId, email, displayName, correlationId, CancellationToken.None); Assert.NotEqual(Guid.Empty, createdId); Assert.Equal(identityId, createdId); } [Fact] public async Task CreateIdentity_DuplicateEmail_ReturnsEmpty() { var email = "test-integration-002@example.com"; var displayName = "Test User 002"; var correlationId = Guid.NewGuid().ToString(); var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); var created1 = await _sql.CreateIdentityAsync(id1, email, displayName, correlationId, CancellationToken.None); var created2 = await _sql.CreateIdentityAsync(id2, email, displayName, correlationId, CancellationToken.None); Assert.Equal(id1, created1); Assert.Equal(Guid.Empty, created2); } [Fact] public async Task GetIdentity_AfterCreate_ReturnsCorrectData() { var email = "test-integration-003@example.com"; var displayName = "Test User 003"; var correlationId = Guid.NewGuid().ToString(); var identityId = Guid.NewGuid(); await _sql.CreateIdentityAsync(identityId, email, displayName, correlationId, CancellationToken.None); var (id, returnedEmail, returnedDisplayName, state) = await _sql.GetIdentityAsync(identityId, CancellationToken.None); Assert.Equal(identityId, id); Assert.Equal(email, returnedEmail); Assert.Equal(displayName, returnedDisplayName); Assert.Equal("ACTIVE", state); } [Fact] public async Task EmailExists_WithExistingEmail_ReturnsTrue() { var email = "test-integration-004@example.com"; var displayName = "Test User 004"; var correlationId = Guid.NewGuid().ToString(); var identityId = Guid.NewGuid(); await _sql.CreateIdentityAsync(identityId, email, displayName, correlationId, CancellationToken.None); var exists = await _sql.EmailExistsAsync(email, CancellationToken.None); Assert.True(exists); } [Fact] public async Task EmailExists_WithNonExistentEmail_ReturnsFalse() { var exists = await _sql.EmailExistsAsync("nonexistent-integration-001@example.com", CancellationToken.None); Assert.False(exists); } }