feat(mvp): complete Blazor UI + User Management + integration tests
This commit is contained in:
1 parent
b217c932e8
commit
38816aec8a
26 files changed
+1893
-45
No files matched your search
+57
-16
@@ -12,10 +12,17 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// ── Services ──────────────────────────────────────────────────────
|
||||
|
||||
builder.Services.AddControllers(options =>
|
||||
if (!builder.Environment.IsEnvironment("Testing"))
|
||||
{
|
||||
options.Filters.Add<ExceptionFilter>();
|
||||
});
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<ExceptionFilter>();
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Services.AddControllers();
|
||||
}
|
||||
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
@@ -23,13 +30,14 @@ builder.Services.AddOpenApi();
|
||||
builder.Services.AddApplication();
|
||||
|
||||
// ── Infrastructure layer ──────────────────────────────────────────
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
builder.Services.AddInfrastructure(builder.Configuration, builder.Environment.EnvironmentName);
|
||||
|
||||
// ── Authentication ────────────────────────────────────────────────
|
||||
var jwtKey = builder.Configuration["Jwt:Key"]
|
||||
?? Environment.GetEnvironmentVariable("JWT__KEY")
|
||||
?? throw new InvalidOperationException("JWT:Key is not configured");
|
||||
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "heimdall";
|
||||
var jwtAudience = builder.Configuration["Jwt:Audience"] ?? "heimdall-api";
|
||||
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? Environment.GetEnvironmentVariable("JWT__ISSUER") ?? "heimdall";
|
||||
var jwtAudience = builder.Configuration["Jwt:Audience"] ?? Environment.GetEnvironmentVariable("JWT__AUDIENCE") ?? "heimdall-api";
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
@@ -71,24 +79,38 @@ var allowedOrigins = builder.Configuration["Cors:AllowedOrigins"]?.Split(',')
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("ApiPolicy", policy =>
|
||||
policy.WithOrigins(allowedOrigins)
|
||||
.AllowCredentials()
|
||||
.AllowAnyHeader()
|
||||
.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH"));
|
||||
var isWildcard = allowedOrigins.Length == 1 && allowedOrigins[0] == "*";
|
||||
if (isWildcard)
|
||||
{
|
||||
options.AddPolicy("ApiPolicy", policy =>
|
||||
policy.AllowAnyOrigin()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod());
|
||||
}
|
||||
else
|
||||
{
|
||||
options.AddPolicy("ApiPolicy", policy =>
|
||||
policy.WithOrigins(allowedOrigins)
|
||||
.AllowCredentials()
|
||||
.AllowAnyHeader()
|
||||
.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH"));
|
||||
}
|
||||
});
|
||||
|
||||
// ── Pipeline ──────────────────────────────────────────────────────
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Apply migrations on startup
|
||||
using (var scope = app.Services.CreateScope())
|
||||
// Apply migrations on startup (skip in Testing with InMemory)
|
||||
if (!app.Environment.IsEnvironment("Testing"))
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
if (db.Database.GetPendingMigrations().Any())
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
db.Database.Migrate();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
if (db.Database.GetPendingMigrations().Any())
|
||||
{
|
||||
db.Database.Migrate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +119,8 @@ using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var roleManager = scope.ServiceProvider
|
||||
.GetRequiredService<Microsoft.AspNetCore.Identity.RoleManager<Heimdall.Domain.Entities.AppRole>>();
|
||||
var userManager = scope.ServiceProvider
|
||||
.GetRequiredService<Microsoft.AspNetCore.Identity.UserManager<Heimdall.Domain.Entities.AppUser>>();
|
||||
|
||||
foreach (var roleName in AppRoles.All)
|
||||
{
|
||||
@@ -105,6 +129,23 @@ using (var scope = app.Services.CreateScope())
|
||||
await roleManager.CreateAsync(new Heimdall.Domain.Entities.AppRole(roleName));
|
||||
}
|
||||
}
|
||||
|
||||
// In Testing, seed an admin user for integration tests
|
||||
if (app.Environment.IsEnvironment("Testing"))
|
||||
{
|
||||
var testAdmin = await userManager.FindByEmailAsync("testadmin@heimdall.test");
|
||||
if (testAdmin is null)
|
||||
{
|
||||
testAdmin = new Heimdall.Domain.Entities.AppUser
|
||||
{
|
||||
UserName = "testadmin@heimdall.test",
|
||||
Email = "testadmin@heimdall.test",
|
||||
DisplayName = "Test Admin"
|
||||
};
|
||||
await userManager.CreateAsync(testAdmin, "TestAdmin123!");
|
||||
await userManager.AddToRoleAsync(testAdmin, AppRoles.Admin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
|
||||
Reference in new issue
Block a user