using Microsoft.Extensions.DependencyInjection; using cuqmbr.TravelGuide.Configuration.Configuration; using cuqmbr.TravelGuide.Configuration.Logging; using cuqmbr.TravelGuide.Configuration.Application; using cuqmbr.TravelGuide.Configuration.Persistence; using Moq; using System.Globalization; using cuqmbr.TravelGuide.Application.Common.Services; using cuqmbr.TravelGuide.Domain.Enums; using cuqmbr.TravelGuide.Configuration.Infrastructure; namespace cuqmbr.TravelGuide.Application.IntegrationTests; public abstract class TestBase : IDisposable { protected readonly IServiceCollection _serviceCollection; private IServiceScope _scope; public TestBase() { _serviceCollection = new ServiceCollection(); _serviceCollection .ConfigureConfiguration( new string[] { "--Application:Datastore:Type", "inmemory", "--Application:Logging:LogLevel", "None" }) .ConfigureLogging() .ConfigureApplication() .ConfigureInfrastructure() .ConfigurePersistence(); SetCulture("en-US"); SetTimeZone("Europe/Kyiv"); } public T GetService() { var serviceProvider = _serviceCollection.BuildServiceProvider(); _scope = serviceProvider.CreateScope(); return _scope.ServiceProvider.GetRequiredService(); } public void Dispose() { _scope.Dispose(); GC.SuppressFinalize(this); } protected void SetAuthenticatedUserRoles(IdentityRole[] roles = default!) { _serviceCollection .AddScoped(_ => { var mock = new Mock(); var guid = Guid.NewGuid(); mock.Setup(s => s.Email).Returns(guid.ToString()); mock.Setup(s => s.Guid).Returns(guid); mock.Setup(s => s.Guid).Returns(Guid.NewGuid()); mock.Setup(s => s.IsAuthenticated).Returns(true); mock.Setup(s => s.Roles).Returns(roles); return mock.Object; }); } protected void SetUnAuthenticatedUser() { _serviceCollection .AddScoped(_ => { var mock = new Mock(); mock.Setup(s => s.IsAuthenticated).Returns(false); return mock.Object; }); } public void SetCulture(string culture) { var cultureInfo = CultureInfo.GetCultureInfo(culture); _serviceCollection .AddScoped(_ => { var mock = new Mock(); mock .Setup(s => s.Culture) .Returns(cultureInfo); return mock.Object; }); CultureInfo.DefaultThreadCurrentCulture = cultureInfo; CultureInfo.DefaultThreadCurrentUICulture = cultureInfo; } public void SetTimeZone(string timeZone) { _serviceCollection .AddScoped(_ => { var mock = new Mock(); mock .Setup(s => s.TimeZone) .Returns(TimeZoneInfo.FindSystemTimeZoneById(timeZone)); return mock.Object; }); } }