From 274fe04ca2d8fd52e45b588e232acc677a69bfc6 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sat, 15 Mar 2025 23:13:31 -0500 Subject: [PATCH] Working test using in-memory database --- .../ApplicationFactory.cs | 5 --- ...ClientFixture.cs => ApplicationFixture.cs} | 8 ++-- LANCommander.Server.Tests/BaseTest.cs | 26 +++++++++++ .../Services/UserServiceTests.cs | 44 +++++++++++++++++++ 4 files changed, 75 insertions(+), 8 deletions(-) rename LANCommander.Server.Tests/{ClientFixture.cs => ApplicationFixture.cs} (54%) create mode 100644 LANCommander.Server.Tests/BaseTest.cs create mode 100644 LANCommander.Server.Tests/Services/UserServiceTests.cs diff --git a/LANCommander.Server.Tests/ApplicationFactory.cs b/LANCommander.Server.Tests/ApplicationFactory.cs index 5604dc3f..1ffa3abd 100644 --- a/LANCommander.Server.Tests/ApplicationFactory.cs +++ b/LANCommander.Server.Tests/ApplicationFactory.cs @@ -32,11 +32,6 @@ public class ApplicationFactory { optionsBuilder.UseInMemoryDatabase("Test"); }); - - services.AddDbContext(optionsBuilder => - { - optionsBuilder.UseInMemoryDatabase("Test"); - }); #endregion }); } diff --git a/LANCommander.Server.Tests/ClientFixture.cs b/LANCommander.Server.Tests/ApplicationFixture.cs similarity index 54% rename from LANCommander.Server.Tests/ClientFixture.cs rename to LANCommander.Server.Tests/ApplicationFixture.cs index 9321beaa..f5d0fdb2 100644 --- a/LANCommander.Server.Tests/ClientFixture.cs +++ b/LANCommander.Server.Tests/ApplicationFixture.cs @@ -2,25 +2,27 @@ using Microsoft.AspNetCore.Mvc.Testing; namespace LANCommander.Server.Tests; -public class ApplicationFixture : WebApplicationFactory +public class ApplicationFixture : ApplicationFactory { public static ApplicationFixture Instance; public SDK.Client Client { get; set; } + public IServiceProvider ServiceProvider { get; set; } - public ApplicationFixture(WebApplicationFactory factory) + public ApplicationFixture(ApplicationFactory factory) { if (Instance != null) return; Client = new SDK.Client(factory.CreateClient(), "C:\\Games"); + ServiceProvider = factory.Services; Instance = this; } } [CollectionDefinition("Application")] -public class ApplicationCollection : ICollectionFixture> +public class ApplicationCollection : ICollectionFixture> { } \ No newline at end of file diff --git a/LANCommander.Server.Tests/BaseTest.cs b/LANCommander.Server.Tests/BaseTest.cs new file mode 100644 index 00000000..aa5bd8bd --- /dev/null +++ b/LANCommander.Server.Tests/BaseTest.cs @@ -0,0 +1,26 @@ +using System.Formats.Asn1; +using Microsoft.Extensions.DependencyInjection; + +namespace LANCommander.Server.Tests; + +public abstract class BaseTest : IClassFixture, IDisposable +{ + protected readonly SDK.Client Client = ApplicationFixture.Instance.Client; + protected readonly IServiceProvider ServiceProvider; + + private AsyncServiceScope? _scope; + + public BaseTest(ApplicationFixture fixture) + { + _scope = ApplicationFixture.Instance.ServiceProvider.CreateAsyncScope(); + + ServiceProvider = _scope?.ServiceProvider; + } + + protected T GetService() => ServiceProvider.GetService(); + + public void Dispose() + { + _scope?.Dispose(); + } +} \ No newline at end of file diff --git a/LANCommander.Server.Tests/Services/UserServiceTests.cs b/LANCommander.Server.Tests/Services/UserServiceTests.cs new file mode 100644 index 00000000..8969c4d6 --- /dev/null +++ b/LANCommander.Server.Tests/Services/UserServiceTests.cs @@ -0,0 +1,44 @@ +using LANCommander.Server.Data.Models; +using LANCommander.Server.Services; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; + +namespace LANCommander.Server.Tests.Services; + +[Collection("Application")] +public class UserServiceTests(ApplicationFixture fixture) : BaseTest(fixture) +{ + [Fact] + public async Task CreateAdminUserShouldWork() + { + var roleService = GetService(); + var userService = GetService(); + + roleService.AddAsync(new Role + { + Name = RoleService.AdministratorRoleName, + }); + + var user = await userService.AddAsync(new User + { + UserName = "admin", + }); + + await userService.ChangePassword(user.UserName, "Password1234"); + await userService.AddToRoleAsync(user.UserName, RoleService.AdministratorRoleName); + + user = await userService + .Query(q => + { + return q + .Include(u => u.UserRoles) + .ThenInclude(ur => ur.Role); + }) + .FirstOrDefaultAsync(u => u.UserName.ToLower() == user.UserName.ToLower()); + + user.UserName.ShouldBe("admin"); + user.NormalizedUserName.ShouldBe("ADMIN"); + user.Roles.ShouldContain(r => r.Name == RoleService.AdministratorRoleName); + } +} \ No newline at end of file