Skip to content

Commit

Permalink
fix(quetzalcoatl): configure testing environment
Browse files Browse the repository at this point in the history
  • Loading branch information
WarriorsSami committed Jun 19, 2024
1 parent 55af22f commit a5abdfd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
2 changes: 2 additions & 0 deletions quetzalcoatl-auth/Bootstrapper/Bootstrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="DotNetEnv" Version="3.0.0" />
<PackageReference Include="EntityFrameworkCore.Triggered" Version="3.2.2" />
<PackageReference Include="FastEndpoints" Version="5.26.0" />
<PackageReference Include="FastEndpoints.Security" Version="5.26.0" />
Expand All @@ -19,6 +20,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
67 changes: 47 additions & 20 deletions quetzalcoatl-auth/Bootstrapper/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using Domain.Consts;
using DotNetEnv;
using DotNetEnv.Configuration;

Log.Logger = new LoggerConfiguration()
.MinimumLevel
.Override("Microsoft", LogEventLevel.Information)
Expand All @@ -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<JwtConfig>(builder.Configuration.GetSection(nameof(JwtConfig)));
builder.Services.Configure<AdminConfig>(builder.Configuration.GetSection(nameof(AdminConfig)));

Expand All @@ -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
Expand All @@ -45,7 +65,15 @@
.Services
.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(dsnConnectionString);
if (builder.Environment.IsEnvironment(SystemConsts.TestingEnvironment))
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
}
else
{
options.UseSqlServer(dsnConnectionString);
}

options.UseTriggers(
triggerOptions => triggerOptions.AddTrigger<DeleteStaleRefreshTokens>()
);
Expand All @@ -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()
Expand All @@ -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();

Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions quetzalcoatl-auth/Domain/Consts/SystemConsts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Domain.Consts;

public static class SystemConsts
{
public const string TestingEnvironment = "TESTING";
}

0 comments on commit a5abdfd

Please sign in to comment.