IdentityShroud/IdentityShroud.Core.Tests/Services/RealmServiceTests.cs

87 lines
2.5 KiB
C#
Raw Normal View History

using FluentResults;
2026-02-15 07:15:11 +01:00
using IdentityShroud.Core.Contracts;
using IdentityShroud.Core.Services;
using IdentityShroud.Core.Tests.Fixtures;
using IdentityShroud.Core.Tests.Substitutes;
using Microsoft.EntityFrameworkCore;
namespace IdentityShroud.Core.Tests.Services;
public class RealmServiceTests : IClassFixture<DbFixture>
{
2026-02-15 07:15:11 +01:00
private readonly DbFixture _dbFixture;
private readonly IEncryptionService _encryptionService = EncryptionServiceSubstitute.CreatePassthrough();
public RealmServiceTests(DbFixture dbFixture)
{
2026-02-15 07:15:11 +01:00
_dbFixture = dbFixture;
using Db db = dbFixture.CreateDbContext();
if (!db.Database.EnsureCreated())
TruncateTables(db);
}
2026-02-15 07:15:11 +01:00
private void TruncateTables(Db db)
{
2026-02-15 07:15:11 +01:00
db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;");
}
[Theory]
[InlineData(null)]
[InlineData("a7c2a39c-3ed9-4790-826e-43bb2e5e480c")]
public async Task Create(string? idString)
{
2026-02-15 07:15:11 +01:00
// Setup
Guid? realmId = null;
if (idString is not null)
realmId = new(idString);
2026-02-15 07:15:11 +01:00
using Db db = _dbFixture.CreateDbContext();
RealmService sut = new(db, _encryptionService);
// Act
var response = await sut.Create(
new(realmId, "slug", "New realm"),
TestContext.Current.CancellationToken);
2026-02-15 07:15:11 +01:00
// Verify
RealmCreateResponse val = ResultAssert.Success(response);
if (realmId.HasValue)
2026-02-08 18:00:24 +01:00
Assert.Equal(realmId, val.Id);
else
2026-02-08 18:00:24 +01:00
Assert.NotEqual(Guid.Empty, val.Id);
2026-02-08 18:00:24 +01:00
Assert.Equal("slug", val.Slug);
Assert.Equal("New realm", val.Name);
// TODO verify data has been stored!
}
2026-02-15 07:15:11 +01:00
[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);
}
}