From a5abdfdfb17d75c6d732041d9bca251fd9f1bb53 Mon Sep 17 00:00:00 2001 From: WarriorsSami Date: Wed, 19 Jun 2024 20:45:01 +0300 Subject: [PATCH] fix(quetzalcoatl): configure testing environment --- .../Bootstrapper/Bootstrapper.csproj | 2 + quetzalcoatl-auth/Bootstrapper/Program.cs | 67 +++++++++++++------ .../Domain/Consts/SystemConsts.cs | 6 ++ 3 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 quetzalcoatl-auth/Domain/Consts/SystemConsts.cs diff --git a/quetzalcoatl-auth/Bootstrapper/Bootstrapper.csproj b/quetzalcoatl-auth/Bootstrapper/Bootstrapper.csproj index 4a77b90..e8d2536 100644 --- a/quetzalcoatl-auth/Bootstrapper/Bootstrapper.csproj +++ b/quetzalcoatl-auth/Bootstrapper/Bootstrapper.csproj @@ -9,6 +9,7 @@ + @@ -19,6 +20,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/quetzalcoatl-auth/Bootstrapper/Program.cs b/quetzalcoatl-auth/Bootstrapper/Program.cs index 9052b06..add1d9b 100644 --- a/quetzalcoatl-auth/Bootstrapper/Program.cs +++ b/quetzalcoatl-auth/Bootstrapper/Program.cs @@ -1,3 +1,7 @@ +using Domain.Consts; +using DotNetEnv; +using DotNetEnv.Configuration; + Log.Logger = new LoggerConfiguration() .MinimumLevel .Override("Microsoft", LogEventLevel.Information) @@ -13,6 +17,13 @@ var builder = WebApplication.CreateBuilder(args); + if (builder.Environment.IsEnvironment(SystemConsts.TestingEnvironment)) + { + _ = builder.Configuration + .AddDotNetEnv(".env.template", LoadOptions.TraversePath()) + .Build(); + } + builder.Services.Configure(builder.Configuration.GetSection(nameof(JwtConfig))); builder.Services.Configure(builder.Configuration.GetSection(nameof(AdminConfig))); @@ -28,7 +39,16 @@ }; var dsnConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"); - builder.Services.AddHealthChecks().AddSqlServer(dsnConnectionString!); + if (builder.Environment.IsEnvironment(SystemConsts.TestingEnvironment)) + { + builder.Services.AddEntityFrameworkInMemoryDatabase(); + } + else + { + builder.Services + .AddHealthChecks() + .AddSqlServer(dsnConnectionString!); + } builder .Host @@ -45,7 +65,15 @@ .Services .AddDbContext(options => { - options.UseSqlServer(dsnConnectionString); + if (builder.Environment.IsEnvironment(SystemConsts.TestingEnvironment)) + { + options.UseInMemoryDatabase("InMemoryDbForTesting"); + } + else + { + options.UseSqlServer(dsnConnectionString); + } + options.UseTriggers( triggerOptions => triggerOptions.AddTrigger() ); @@ -69,11 +97,11 @@ .Services .AddCors(options => { - options.AddDefaultPolicy(builder => + options.AddDefaultPolicy(corsPolicyBuilder => { - builder + corsPolicyBuilder .WithOrigins( - corsOrigins ?? new[] { "http://localhost:10000", "https://pantheonix.live" } + corsOrigins ?? ["http://localhost:10000", "https://pantheonix.live"] ) .AllowAnyMethod() .AllowAnyHeader() @@ -83,23 +111,19 @@ .AddFastEndpoints(options => { options.DisableAutoDiscovery = true; - options.Assemblies = new[] - { + options.Assemblies = + [ typeof(IApiMarker).Assembly, typeof(IApplicationMarker).Assembly - }; + ]; }) .AddSingleton(tokenValidationParameters) .AddAuthenticationJwtBearer(opt => { - opt.SigningKey = jwtConfig!.SecretKey; + opt.SigningKey = jwtConfig.SecretKey; }) - .AddAutoMapper(typeof(IApiMarker), typeof(IApplicationMarker)) - .AddSwaggerDocument(settings => - { - settings.Title = "Quetzalcoatl Auth API"; - settings.Version = "v1"; - }); + .AddAuthorization() + .AddAutoMapper(typeof(IApiMarker), typeof(IApplicationMarker)); var app = builder.Build(); @@ -108,11 +132,14 @@ await app.UseSeedData(); } - app.MapHealthChecks( - "/_health", - new HealthCheckOptions { ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, } - ) - .RequireHost("*:5210"); + if (!app.Environment.IsEnvironment(SystemConsts.TestingEnvironment)) + { + app.MapHealthChecks( + "/_health", + new HealthCheckOptions { ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, } + ) + .RequireHost("*:5210"); + } app.UseSerilogRequestLogging() .UseDefaultExceptionHandler() diff --git a/quetzalcoatl-auth/Domain/Consts/SystemConsts.cs b/quetzalcoatl-auth/Domain/Consts/SystemConsts.cs new file mode 100644 index 0000000..85dc8f3 --- /dev/null +++ b/quetzalcoatl-auth/Domain/Consts/SystemConsts.cs @@ -0,0 +1,6 @@ +namespace Domain.Consts; + +public static class SystemConsts +{ + public const string TestingEnvironment = "TESTING"; +} \ No newline at end of file