Tests for RealmService.FindBySlug

This commit is contained in:
eelke 2026-02-15 07:15:11 +01:00
parent 7a5cb703ec
commit a80c133e2a
3 changed files with 49 additions and 16 deletions

View file

@ -19,8 +19,8 @@ public class DbFixture : IAsyncLifetime
private string Password => "password"; private string Password => "password";
private string DbHostname => _postgresqlServer.Hostname; private string DbHostname => _postgresqlServer.Hostname;
private int DbPort => _postgresqlServer.GetMappedPublicPort(PostgreSqlBuilder.PostgreSqlPort); private int DbPort => _postgresqlServer.GetMappedPublicPort(PostgreSqlBuilder.PostgreSqlPort);
public Db CreateDbContext(string dbName) public Db CreateDbContext(string dbName = "testdb")
{ {
var db = new Db(Options.Create<DbConfiguration>(new() var db = new Db(Options.Create<DbConfiguration>(new()
{ {

View file

@ -1,4 +1,5 @@
using FluentResults; using FluentResults;
using IdentityShroud.Core.Contracts;
using IdentityShroud.Core.Services; using IdentityShroud.Core.Services;
using IdentityShroud.Core.Tests.Fixtures; using IdentityShroud.Core.Tests.Fixtures;
using IdentityShroud.Core.Tests.Substitutes; using IdentityShroud.Core.Tests.Substitutes;
@ -8,19 +9,20 @@ namespace IdentityShroud.Core.Tests.Services;
public class RealmServiceTests : IClassFixture<DbFixture> public class RealmServiceTests : IClassFixture<DbFixture>
{ {
private readonly Db _db; private readonly DbFixture _dbFixture;
private readonly IEncryptionService _encryptionService = EncryptionServiceSubstitute.CreatePassthrough();
public RealmServiceTests(DbFixture dbFixture) public RealmServiceTests(DbFixture dbFixture)
{ {
_db = dbFixture.CreateDbContext("realmservice"); _dbFixture = dbFixture;
using Db db = dbFixture.CreateDbContext();
if (!_db.Database.EnsureCreated()) if (!db.Database.EnsureCreated())
TruncateTables(); TruncateTables(db);
} }
private void TruncateTables() private void TruncateTables(Db db)
{ {
_db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;"); db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;");
} }
[Theory] [Theory]
@ -28,17 +30,20 @@ public class RealmServiceTests : IClassFixture<DbFixture>
[InlineData("a7c2a39c-3ed9-4790-826e-43bb2e5e480c")] [InlineData("a7c2a39c-3ed9-4790-826e-43bb2e5e480c")]
public async Task Create(string? idString) public async Task Create(string? idString)
{ {
// Setup
Guid? realmId = null; Guid? realmId = null;
if (idString is not null) if (idString is not null)
realmId = new(idString); realmId = new(idString);
var encryptionService = EncryptionServiceSubstitute.CreatePassthrough(); using Db db = _dbFixture.CreateDbContext();
RealmService sut = new(_db, encryptionService); RealmService sut = new(db, _encryptionService);
// Act
var response = await sut.Create( var response = await sut.Create(
new(realmId, "slug", "New realm"), new(realmId, "slug", "New realm"),
TestContext.Current.CancellationToken); TestContext.Current.CancellationToken);
// Verify
RealmCreateResponse val = ResultAssert.Success(response); RealmCreateResponse val = ResultAssert.Success(response);
if (realmId.HasValue) if (realmId.HasValue)
Assert.Equal(realmId, val.Id); Assert.Equal(realmId, val.Id);
@ -50,4 +55,33 @@ public class RealmServiceTests : IClassFixture<DbFixture>
// TODO verify data has been stored! // TODO verify data has been stored!
} }
[Theory]
[InlineData("slug", null)]
[InlineData("foo", "Foo")]
public async Task FindBySlug(string slug, string? name)
{
using (var setupContext = _dbFixture.CreateDbContext())
{
setupContext.Realms.Add(new()
{
Slug = "foo",
Name = "Foo",
});
setupContext.Realms.Add(new()
{
Slug = "bar",
Name = "Bar",
});
await setupContext.SaveChangesAsync(TestContext.Current.CancellationToken);
}
using Db actContext = _dbFixture.CreateDbContext();
RealmService sut = new(actContext, _encryptionService);
// Act
var result = await sut.FindBySlug(slug, TestContext.Current.CancellationToken);
Assert.Equal(name, result?.Name);
}
} }

View file

@ -3,6 +3,7 @@ using IdentityShroud.Core.Contracts;
using IdentityShroud.Core.Helpers; using IdentityShroud.Core.Helpers;
using IdentityShroud.Core.Messages.Realm; using IdentityShroud.Core.Messages.Realm;
using IdentityShroud.Core.Model; using IdentityShroud.Core.Model;
using Microsoft.EntityFrameworkCore;
namespace IdentityShroud.Core.Services; namespace IdentityShroud.Core.Services;
@ -12,9 +13,9 @@ public class RealmService(
Db db, Db db,
IEncryptionService encryptionService) : IRealmService IEncryptionService encryptionService) : IRealmService
{ {
public Task<Realm?> FindBySlug(string slug, CancellationToken ct = default) public async Task<Realm?> FindBySlug(string slug, CancellationToken ct = default)
{ {
throw new NotImplementedException(); return await db.Realms.SingleOrDefaultAsync(r => r.Slug == slug, ct);
} }
public async Task<Result<RealmCreateResponse>> Create(RealmCreateRequest request, CancellationToken ct = default) public async Task<Result<RealmCreateResponse>> Create(RealmCreateRequest request, CancellationToken ct = default)
@ -26,8 +27,6 @@ public class RealmService(
Name = request.Name, Name = request.Name,
Keys = [ CreateKey() ], Keys = [ CreateKey() ],
}; };
db.Add(realm); db.Add(realm);
await db.SaveChangesAsync(ct); await db.SaveChangesAsync(ct);