using JobTrackerApi.Data; using JobTrackerApi.Models; using JobTrackerApi.Services; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Moq; namespace JobTrackerApi.Tests.TestSupport; public static class TestHostFactory { // Keep the EF-backed controller tests on the same minimal setup so they fail for product // reasons, not because each file drifted into a slightly different fake host configuration. public static JobTrackerContext CreateInMemoryDb(string userId = "user-1") { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var currentUser = new Mock(); currentUser.SetupGet(service => service.UserId).Returns(userId); return new JobTrackerContext(options, currentUser.Object); } public static Mock> CreateUserManager(ApplicationUser? lookupUser = null) { var store = new Mock>(); var manager = new Mock>( store.Object, Options.Create(new IdentityOptions()), new PasswordHasher(), Array.Empty>(), Array.Empty>(), new UpperInvariantLookupNormalizer(), new IdentityErrorDescriber(), null!, new NullLogger>() ); if (lookupUser is not null) { manager .Setup(x => x.FindByIdAsync(It.IsAny())) .ReturnsAsync((string id) => lookupUser.Id == id ? lookupUser : null); } return manager; } }