From a4503e76acd85e01da8b5bd5dc4a96a5566595a5 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Tue, 11 Aug 2026 12:35:45 +0200 Subject: [PATCH] =?UTF-8?q?fix(ci):=20r=C3=A9pare=2031=20tests=20cass?= =?UTF-8?q?=C3=A9s=20=E2=80=94=20401=20API,=20login=20prerender=20off,=20c?= =?UTF-8?q?ulture=20bUnit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/Heimdall.Web/Program.cs | 27 +++++++++++++++++++ .../Controllers/AuthFlowTests.cs | 5 +++- .../Components/MudTestBase.cs | 18 +++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index a75f9ce..e031b77 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -367,6 +367,14 @@ public partial class Program { var logger = context.HttpContext.RequestServices.GetRequiredService>(); 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>(); 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(); }); } + + /// + /// 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. + /// + 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); + } } diff --git a/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs b/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs index 82b6fd0..2c16ced 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs @@ -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 ───────────────────────────────────────── */ diff --git a/tests/Heimdall.Web.Tests/Components/MudTestBase.cs b/tests/Heimdall.Web.Tests/Components/MudTestBase.cs index 1cabcda..03ca75a 100644 --- a/tests/Heimdall.Web.Tests/Components/MudTestBase.cs +++ b/tests/Heimdall.Web.Tests/Components/MudTestBase.cs @@ -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; /// 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