fix(ci): répare 31 tests cassés — 401 API, login prerender off, culture bUnit

Trois régressions distinctes cassent la CI depuis le run 207 :

1. Auth API (29 tests d'intégration) : depuis 16d739e, les contrôleurs
   acceptent JwtBearer+Cookies ; le challenge par défaut étant le cookie,
   les requêtes API non authentifiées étaient redirigées 302 vers /login
   au lieu de répondre 401/403 (le client de test suivait → 200 OK).
   → IsApiRequest() dans OnRedirectToLogin/OnRedirectToAccessDenied :
     requête /api/* ou Accept: application/json → 401/403 direct.

2. Get_LoginPage_ReturnsOk : depuis db703be (prerender:false), /login sert
   le shell Blazor sans le contenu du composant en SSR. Le test vérifie
   désormais le shell (_framework/blazor.web.js) au lieu de 'Se connecter'.

3. Tests bUnit (2) : le runner CI a une locale fr-FR → {Confidence:P0}
   rend '87 %' au lieu de '87%'. MudTestBase force en-US (statique +
   thread courant) pour un rendu déterministe partout.

Vérifié : 1042/1042 tests verts, y compris sous LANG=fr_FR.UTF-8.
This commit is contained in:
Arnaud Dartois committed 2026-08-11 12:35:45 +02:00
1 parent 0d848b267a
commit a4503e76ac
3 files changed
+49 -1

No files matched your search

+27
View File
@@ -367,6 +367,14 @@ public partial class Program {
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
logger.LogWarning("Redirect to login: {Path} -> {LoginPath}",
context.Request.Path, options.LoginPath);
// API requests (JSON or /api/*) must get a 401, not a
// browser redirect — otherwise unauthenticated API calls
// silently follow the redirect and report 200.
if (IsApiRequest(context.Request))
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return Task.CompletedTask;
}
context.Response.Redirect(context.RedirectUri);
return Task.CompletedTask;
},
@@ -375,6 +383,11 @@ public partial class Program {
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
logger.LogWarning("Access denied: {Path} -> {AccessDeniedPath}",
context.Request.Path, options.AccessDeniedPath);
if (IsApiRequest(context.Request))
{
context.Response.StatusCode = StatusCodes.Status403Forbidden;
return Task.CompletedTask;
}
context.Response.Redirect(context.RedirectUri);
return Task.CompletedTask;
},
@@ -648,4 +661,18 @@ public partial class Program {
return Results.Ok();
});
}
/// <summary>
/// True for JSON/API requests (Accept: application/json or an /api/* path).
/// Used by the cookie challenge handlers to return 401/403 instead of a
/// browser redirect — API clients must not be silently redirected to /login.
/// </summary>
private static bool IsApiRequest(HttpRequest request)
{
if (request.Path.StartsWithSegments("/api"))
return true;
var accept = request.Headers.Accept.ToString();
return accept.Contains("application/json", StringComparison.OrdinalIgnoreCase);
}
}
@@ -103,7 +103,10 @@ public class AuthFlowTests : IAsyncLifetime
response.StatusCode.ShouldBe(HttpStatusCode.OK);
var body = await response.Content.ReadAsStringAsync();
body.ShouldContain("Heimdall");
body.ShouldContain("Se connecter");
// Prerender is disabled (InteractiveServerRenderMode prerender:false),
// so the static shell is served and the Login component content only
// appears after the SignalR circuit connects. Assert the Blazor shell.
body.ShouldContain("_framework/blazor.web.js");
}
/* ─── API auth tests ───────────────────────────────────────── */
@@ -2,7 +2,9 @@ using Bunit;
using Microsoft.Extensions.DependencyInjection;
using MudBlazor;
using MudBlazor.Services;
using System.Globalization;
using System.Reflection;
using System.Threading;
namespace Heimdall.Web.Tests.Components;
@@ -13,8 +15,24 @@ namespace Heimdall.Web.Tests.Components;
/// </summary>
public abstract class MudTestBase : TestContext
{
static MudTestBase()
{
// Deterministic rendering regardless of the machine/CI locale.
// Without this, {Confidence:P0} renders "87 %" (fr-FR, space) vs
// "87%" (en-US), making format-sensitive assertions flaky across runners.
var culture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
protected MudTestBase()
{
// Also pin the current thread (xUnit may reuse a pooled thread whose
// culture was set before the static ctor ran).
var culture = CultureInfo.GetCultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
var services = (IServiceCollection)Services;
// Register all MudBlazor services except KeyInterceptorService