using JdeScoping.Core.Interfaces;
using JdeScoping.Infrastructure.Auth;
using JdeScoping.Core.Models;
using JdeScoping.Core.Models.Inventory;
using JdeScoping.Core.Models.Organization;
using JdeScoping.Core.Models.Search;
using JdeScoping.Core.Models.WorkOrders;
using JdeScoping.Core.ViewModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
namespace JdeScoping.Api.IntegrationTests;
///
/// Test web application factory for integration tests
///
public class TestWebApplicationFactory : WebApplicationFactory
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
// Add test configuration with dummy connection strings
builder.ConfigureAppConfiguration((context, config) =>
{
config.AddInMemoryCollection(new Dictionary
{
["ConnectionStrings:SqlServer"] = "Server=localhost;Database=TestDb;Trusted_Connection=true;",
["ConnectionStrings:JDE"] = "Data Source=localhost;User Id=test;Password=test;",
["ConnectionStrings:CMS"] = "Data Source=localhost;User Id=test;Password=test;",
["DataAccess:ConnectionStringName"] = "SqlServer",
["DataSource:JDE:ConnectionStringName"] = "JDE",
["DataSource:CMS:ConnectionStringName"] = "CMS"
});
});
builder.ConfigureServices(services =>
{
// Remove the real repository and add a mock
var repositoryDescriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(ILotFinderRepository));
if (repositoryDescriptor != null)
{
services.Remove(repositoryDescriptor);
}
// Add mock repository
var mockRepository = CreateMockRepository();
services.AddSingleton(mockRepository);
// Ensure fake auth is used for tests
var authServiceDescriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(IAuthService));
if (authServiceDescriptor != null)
{
services.Remove(authServiceDescriptor);
}
services.AddSingleton();
});
}
private static ILotFinderRepository CreateMockRepository()
{
var repository = Substitute.For();
// Setup default returns for search operations
repository.GetUserSearchesAsync(Arg.Any(), Arg.Any())
.Returns(new List());
repository.GetQueuedSearchesAsync(Arg.Any())
.Returns(new List());
repository.GetSearchAsync(Arg.Any(), Arg.Any())
.Returns((Search?)null);
repository.GetSearchResultsAsync(Arg.Any(), Arg.Any())
.Returns((byte[]?)null);
repository.SubmitSearchAsync(Arg.Any(), Arg.Any())
.Returns(1);
// Setup default returns for lookup operations
repository.SearchItemsAsync(Arg.Any(), Arg.Any())
.Returns(new List- ());
repository.SearchProfitCentersAsync(Arg.Any(), Arg.Any())
.Returns(new List());
repository.SearchWorkCentersAsync(Arg.Any(), Arg.Any())
.Returns(new List());
repository.SearchUsersAsync(Arg.Any(), Arg.Any())
.Returns(new List());
// Setup default returns for file upload lookups
repository.LookupWorkordersAsync(Arg.Any
>(), Arg.Any())
.Returns(new List());
repository.LookupItemsAsync(Arg.Any>(), Arg.Any())
.Returns(new List- ());
repository.LookupLotsAsync(Arg.Any
>(), Arg.Any())
.Returns(new List());
return repository;
}
}