Heimdall Gitea / Detect CI scope (push) Successful in 7s
Heimdall Gitea / Build and tests (push) Successful in 14m48s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 4m27s
Heimdall Gitea / Build and scan agent image (push) Successful in 15m42s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m27s
Heimdall Gitea / CI policy (push) Successful in 2s
Co-authored-by: Arnaud Dartois <admin@tordais.cc>
149 lines
4.6 KiB
C#
149 lines
4.6 KiB
C#
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;
|
|
using Heimdall.Infrastructure.Identity;
|
|
|
|
namespace Heimdall.Infrastructure.Tests;
|
|
|
|
public class AuthDataServiceTests : IDisposable
|
|
{
|
|
private readonly SqliteConnection _connection;
|
|
private readonly DbContextOptions<AppDbContext> _options;
|
|
|
|
public AuthDataServiceTests()
|
|
{
|
|
_connection = new SqliteConnection("DataSource=:memory:");
|
|
_connection.Open();
|
|
_options = new DbContextOptionsBuilder<AppDbContext>()
|
|
.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 HasAnyUsersAsync_NoUsers_ReturnsFalse()
|
|
{
|
|
using var db = CreateDbContext();
|
|
var service = new AuthDataService(db);
|
|
|
|
var result = await service.HasAnyUsersAsync(CancellationToken.None);
|
|
|
|
result.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task HasAnyUsersAsync_WithUsers_ReturnsTrue()
|
|
{
|
|
using (var db = CreateDbContext())
|
|
{
|
|
db.Users.Add(new IdentityAppUser { UserName = "test", DisplayName = "Test" });
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
using var dbRead = CreateDbContext();
|
|
var service = new AuthDataService(dbRead);
|
|
|
|
var result = await service.HasAnyUsersAsync(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<AppDbContext> opts)
|
|
: IDbContextFactory<AppDbContext>
|
|
{
|
|
private static readonly AuditInterceptor Audit = new();
|
|
|
|
public AppDbContext CreateDbContext() => new(opts, Audit);
|
|
public Task<AppDbContext> CreateDbContextAsync(CancellationToken ct = default)
|
|
=> Task.FromResult(new AppDbContext(opts, Audit));
|
|
}
|
|
}
|