fix: seed Identity roles in all environments (including Testing)
Roles (Admin, Viewer, etc.) must exist before any user registration attempts to add a user to a role. The seeding was only in the old Heimdall.Api/Program.cs — moved to Web/Program.cs and runs in ALL environments (not skipped in Testing). Now 54/61 integration tests pass (was 6/61). Remaining 7 failures are all 'expected 401 got 400' — pre-existing auth middleware difference.
This commit is contained in:
1 parent
1d70455abb
commit
501da8dd0d
1 file changed
+64
@@ -440,6 +440,70 @@ if (!app.Environment.IsEnvironment("Testing"))
|
||||
}
|
||||
}
|
||||
|
||||
// ── Seed Identity roles (required in ALL environments, including Testing) ─
|
||||
// This must run before any user registration — AuthService.AddToRoleAsync
|
||||
// throws "Role X does not exist" if the role hasn't been created yet.
|
||||
using (var seedScope = app.Services.CreateScope())
|
||||
{
|
||||
var roleManager = seedScope.ServiceProvider
|
||||
.GetRequiredService<Microsoft.AspNetCore.Identity.RoleManager<Heimdall.Domain.Entities.AppRole>>();
|
||||
var userManager = seedScope.ServiceProvider
|
||||
.GetRequiredService<Microsoft.AspNetCore.Identity.UserManager<Heimdall.Domain.Entities.AppUser>>();
|
||||
|
||||
foreach (var roleName in AppRoles.All)
|
||||
{
|
||||
if (!await roleManager.RoleExistsAsync(roleName))
|
||||
{
|
||||
await roleManager.CreateAsync(new Heimdall.Domain.Entities.AppRole(roleName));
|
||||
}
|
||||
}
|
||||
|
||||
// Seed default groups (idempotent)
|
||||
var groupDb = seedScope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
var defaultGroups = new[] { "Administrateurs", "Superviseurs", "Visiteurs", "Techniciens" };
|
||||
foreach (var groupName in defaultGroups)
|
||||
{
|
||||
if (!await groupDb.Groups.AnyAsync(g => g.Name == groupName))
|
||||
{
|
||||
groupDb.Groups.Add(new Heimdall.Domain.Entities.Group(groupName,
|
||||
groupName switch
|
||||
{
|
||||
"Administrateurs" => "Accès complet à toutes les fonctionnalités",
|
||||
"Superviseurs" => "Gestion des sites, caméras et événements",
|
||||
"Visiteurs" => "Consultation en lecture seule",
|
||||
"Techniciens" => "Maintenance technique et support",
|
||||
_ => null
|
||||
}));
|
||||
}
|
||||
}
|
||||
await groupDb.SaveChangesWithRetryAsync();
|
||||
|
||||
// Seed demo admin + demo site in Development only
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
var testAdmin = await userManager.FindByEmailAsync("admin@heimdall.test");
|
||||
if (testAdmin is null)
|
||||
{
|
||||
testAdmin = new Heimdall.Domain.Entities.AppUser
|
||||
{
|
||||
UserName = "admin@heimdall.test",
|
||||
Email = "admin@heimdall.test",
|
||||
DisplayName = "Admin"
|
||||
};
|
||||
await userManager.CreateAsync(testAdmin, "Admin123!Pass");
|
||||
await userManager.AddToRoleAsync(testAdmin, AppRoles.Admin);
|
||||
}
|
||||
|
||||
var demoSite = await groupDb.Sites.FirstOrDefaultAsync(s => s.Name == "Site Démo");
|
||||
if (demoSite is null)
|
||||
{
|
||||
demoSite = new Heimdall.Domain.Entities.Site("Site Démo", "Paris", "Europe/Paris");
|
||||
groupDb.Sites.Add(demoSite);
|
||||
await groupDb.SaveChangesWithRetryAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
app.Run();
|
||||
|
||||
// ─── DTO for the cookie sign-in request ───────────────────────────
|
||||
|
||||
Reference in new issue
Block a user