using IdentityShroud.Core.Contracts; using IdentityShroud.Core.Services; using IdentityShroud.Core.Tests.Fixtures; using IdentityShroud.TestUtils.Substitutes; using Microsoft.EntityFrameworkCore; namespace IdentityShroud.Core.Tests.Services; public class RealmServiceTests : IClassFixture { private readonly DbFixture _dbFixture; private readonly IEncryptionService _encryptionService = EncryptionServiceSubstitute.CreatePassthrough(); public RealmServiceTests(DbFixture dbFixture) { _dbFixture = dbFixture; using Db db = dbFixture.CreateDbContext(); if (!db.Database.EnsureCreated()) TruncateTables(db); } private void TruncateTables(Db db) { db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;"); } [Theory] [InlineData(null)] [InlineData("a7c2a39c-3ed9-4790-826e-43bb2e5e480c")] public async Task Create(string? idString) { // Setup Guid? realmId = null; if (idString is not null) realmId = new(idString); 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); else Assert.NotEqual(Guid.Empty, val.Id); Assert.Equal("slug", val.Slug); Assert.Equal("New realm", val.Name); // 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); } }