using Heimdall.Domain.Entities; using Heimdall.Infrastructure.Data; using Heimdall.Infrastructure.Data.Interceptors; using Heimdall.Infrastructure.Security; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Shouldly; namespace Heimdall.Infrastructure.Tests; public class AuthDataServiceTests : IDisposable { private readonly SqliteConnection _connection; private readonly DbContextOptions _options; public AuthDataServiceTests() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); _options = new DbContextOptionsBuilder() .UseSqlite(_connection) .Options; using var ctx = new AppDbContext(_options, new AuditInterceptor()); ctx.Database.EnsureCreated(); } public void Dispose() { _connection?.Close(); _connection?.Dispose(); GC.SuppressFinalize(this); } private AppDbContext CreateDbContext() => new(_options, new AuditInterceptor()); [Fact] public async Task GetSettingValueAsync_ExistingKey_ReturnsValue() { using (var db = CreateDbContext()) { db.AppSettings.Add(new AppSetting("test.key", "test-value", "General")); await db.SaveChangesAsync(); } using var dbRead = CreateDbContext(); var service = new AuthDataService(dbRead); var value = await service.GetSettingValueAsync("test.key", CancellationToken.None); value.ShouldBe("test-value"); } [Fact] public async Task GetSettingValueAsync_UnknownKey_ReturnsNull() { using var db = CreateDbContext(); var service = new AuthDataService(db); var value = await service.GetSettingValueAsync("nonexistent.key", CancellationToken.None); value.ShouldBeNull(); } [Fact] public async Task IsFirstUserAsync_NoUsers_ReturnsFalse() { using var db = CreateDbContext(); var service = new AuthDataService(db); var result = await service.IsFirstUserAsync(CancellationToken.None); result.ShouldBeFalse(); } [Fact] public async Task IsFirstUserAsync_WithUsers_ReturnsTrue() { using (var db = CreateDbContext()) { db.Users.Add(new AppUser { UserName = "test", DisplayName = "Test" }); await db.SaveChangesAsync(); } using var dbRead = CreateDbContext(); var service = new AuthDataService(dbRead); var result = await service.IsFirstUserAsync(CancellationToken.None); result.ShouldBeTrue(); } [Fact] public async Task IsRefreshTokenRevokedAsync_ExistingHash_ReturnsTrue() { using (var db = CreateDbContext()) { var userId = Guid.NewGuid(); db.RevokedRefreshTokens.Add(new RevokedRefreshToken("sha256hash", userId, DateTime.UtcNow.AddDays(7))); await db.SaveChangesAsync(); } using var dbRead = CreateDbContext(); var service = new AuthDataService(dbRead); var result = await service.IsRefreshTokenRevokedAsync("sha256hash", CancellationToken.None); result.ShouldBeTrue(); } [Fact] public async Task IsRefreshTokenRevokedAsync_UnknownHash_ReturnsFalse() { using var db = CreateDbContext(); var service = new AuthDataService(db); var result = await service.IsRefreshTokenRevokedAsync("unknownhash", CancellationToken.None); result.ShouldBeFalse(); } [Fact] public async Task RevokeRefreshTokenAsync_StoresToken() { using var db = CreateDbContext(); var service = new AuthDataService(db); var rawToken = "token-to-revoke"; // ExtractJti fallback: if token is not a valid JWT, it stores SHA256(rawToken) var expectedHash = Convert.ToHexString(System.Security.Cryptography.SHA256.HashData( System.Text.Encoding.UTF8.GetBytes(rawToken))); await service.RevokeRefreshTokenAsync(rawToken, Guid.NewGuid(), CancellationToken.None); var isRevoked = await service.IsRefreshTokenRevokedAsync(expectedHash, CancellationToken.None); isRevoked.ShouldBeTrue(); } private sealed class TestDbContextFactory(DbContextOptions opts) : IDbContextFactory { private static readonly AuditInterceptor Audit = new(); public AppDbContext CreateDbContext() => new(opts, Audit); public Task CreateDbContextAsync(CancellationToken ct = default) => Task.FromResult(new AppDbContext(opts, Audit)); } }