Tests for RealmService.FindBySlug
This commit is contained in:
parent
7a5cb703ec
commit
a80c133e2a
3 changed files with 49 additions and 16 deletions
|
|
@ -19,8 +19,8 @@ public class DbFixture : IAsyncLifetime
|
|||
private string Password => "password";
|
||||
private string DbHostname => _postgresqlServer.Hostname;
|
||||
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()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using FluentResults;
|
||||
using IdentityShroud.Core.Contracts;
|
||||
using IdentityShroud.Core.Services;
|
||||
using IdentityShroud.Core.Tests.Fixtures;
|
||||
using IdentityShroud.Core.Tests.Substitutes;
|
||||
|
|
@ -8,19 +9,20 @@ namespace IdentityShroud.Core.Tests.Services;
|
|||
|
||||
public class RealmServiceTests : IClassFixture<DbFixture>
|
||||
{
|
||||
private readonly Db _db;
|
||||
private readonly DbFixture _dbFixture;
|
||||
private readonly IEncryptionService _encryptionService = EncryptionServiceSubstitute.CreatePassthrough();
|
||||
|
||||
public RealmServiceTests(DbFixture dbFixture)
|
||||
{
|
||||
_db = dbFixture.CreateDbContext("realmservice");
|
||||
|
||||
if (!_db.Database.EnsureCreated())
|
||||
TruncateTables();
|
||||
_dbFixture = dbFixture;
|
||||
using Db db = dbFixture.CreateDbContext();
|
||||
if (!db.Database.EnsureCreated())
|
||||
TruncateTables(db);
|
||||
}
|
||||
|
||||
private void TruncateTables()
|
||||
private void TruncateTables(Db db)
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;");
|
||||
db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -28,17 +30,20 @@ public class RealmServiceTests : IClassFixture<DbFixture>
|
|||
[InlineData("a7c2a39c-3ed9-4790-826e-43bb2e5e480c")]
|
||||
public async Task Create(string? idString)
|
||||
{
|
||||
// Setup
|
||||
Guid? realmId = null;
|
||||
if (idString is not null)
|
||||
realmId = new(idString);
|
||||
|
||||
var encryptionService = EncryptionServiceSubstitute.CreatePassthrough();
|
||||
RealmService sut = new(_db, encryptionService);
|
||||
|
||||
using Db db = _dbFixture.CreateDbContext();
|
||||
RealmService sut = new(db, _encryptionService);
|
||||
// Act
|
||||
|
||||
var response = await sut.Create(
|
||||
new(realmId, "slug", "New realm"),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
// Verify
|
||||
RealmCreateResponse val = ResultAssert.Success(response);
|
||||
if (realmId.HasValue)
|
||||
Assert.Equal(realmId, val.Id);
|
||||
|
|
@ -50,4 +55,33 @@ public class RealmServiceTests : IClassFixture<DbFixture>
|
|||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ using IdentityShroud.Core.Contracts;
|
|||
using IdentityShroud.Core.Helpers;
|
||||
using IdentityShroud.Core.Messages.Realm;
|
||||
using IdentityShroud.Core.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IdentityShroud.Core.Services;
|
||||
|
||||
|
|
@ -12,9 +13,9 @@ public class RealmService(
|
|||
Db db,
|
||||
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)
|
||||
|
|
@ -26,8 +27,6 @@ public class RealmService(
|
|||
Name = request.Name,
|
||||
Keys = [ CreateKey() ],
|
||||
};
|
||||
|
||||
|
||||
|
||||
db.Add(realm);
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue