Anomalies corrigées : - MudPopoverProvider manquant dans les layouts → circuit Blazor mourait sur l'ouverture de menus/popovers (dashboard jamais rendu après login) - AddIdentity redéfinissait le schéma par défaut sur Identity.Application alors que /_auth/signin signait avec Cookies → cookie posé mais jamais validé, tout rechargement de page redirigeait vers /login. Forçage du schéma par défaut sur Cookies + schémas JwtBearer explicites sur les endpoints API d'AuthController et SettingsController - Policy 'AdminUsers' utilisée par register-admin mais jamais enregistrée → 500 à l'appel. Ajoutée (RequireRole Admin) - HeimdallAuthStateProvider ne lisait que le sessionStorage navigateur : pendant le prerender SSR, HttpContext.User (cookie) n'était pas utilisé → pages protégées redirigées vers /login. Fallback cookie ajouté - SameOriginHttpMessageHandler : résolution de l'origine via IServerAddressesFeature quand IHttpContextAccessor est null (circuit Blazor post-hydratation) au lieu de heimdall.local → DNS cassé - UpdateUserValidator rejetait le thème 'Auto' (défaut de l'UI) → 400 à l'édition. Accepte désormais auto (case-insensitive) Tests : - E2E Playwright complets (login/logout/register/user management) : 33/33 - Integration (Testcontainers PostgreSQL) : 120/120 (valeur 'bool' du valueType, bootstrap admin via /register auto-admin, header X-API-Key, /proc/meminfo optionnel pour les LXC restreints) - Exécution E2E séquentielle (xunit.runner.json) pour éliminer le flakiness
107 lines
3.6 KiB
C#
107 lines
3.6 KiB
C#
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Heimdall.Web.Services;
|
|
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.JSInterop;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Web.Tests.Services;
|
|
|
|
public sealed class TokenRefreshHandlerTests : IDisposable
|
|
{
|
|
private readonly MockInnerHandler _innerHandler;
|
|
private readonly HttpClient _httpClient;
|
|
private readonly AuthState _authState;
|
|
private readonly TokenRefreshHandler _handler;
|
|
|
|
public TokenRefreshHandlerTests()
|
|
{
|
|
_innerHandler = new MockInnerHandler();
|
|
_authState = new AuthState();
|
|
var authStateProvider = Substitute.For<HeimdallAuthStateProvider>(
|
|
new ProtectedSessionStorage(Substitute.For<IJSRuntime>(), Substitute.For<IDataProtectionProvider>()),
|
|
_authState,
|
|
Substitute.For<IHttpContextAccessor>());
|
|
|
|
var config = Substitute.For<IConfiguration>();
|
|
config.GetValue<string>("ApiBaseUrl").Returns("http://localhost:8080");
|
|
|
|
_handler = new TokenRefreshHandler(_authState, authStateProvider, Substitute.For<IHttpContextAccessor>(), config);
|
|
|
|
var pipeline = _innerHandler.CreatePipeline(_handler);
|
|
_httpClient = new HttpClient(pipeline) { BaseAddress = new Uri("http://localhost:8080/") };
|
|
}
|
|
|
|
public void Dispose() => _httpClient.Dispose();
|
|
|
|
[Fact]
|
|
public async Task SendAsync_Non401Response_PassesThrough()
|
|
{
|
|
_innerHandler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var response = await _httpClient.GetAsync("api/sites");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SendAsync_401WithoutRefreshToken_DoesNotRetry()
|
|
{
|
|
_innerHandler.QueueResponse(HttpStatusCode.Unauthorized);
|
|
|
|
var response = await _httpClient.GetAsync("api/sites");
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
Assert.Equal(1, _innerHandler.CallCount);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SendAsync_AuthEndpoint_DoesNotIntercept()
|
|
{
|
|
_innerHandler.QueueResponse(HttpStatusCode.Unauthorized);
|
|
|
|
var response = await _httpClient.PostAsync("api/auth/login", null);
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
Assert.Equal(1, _innerHandler.CallCount);
|
|
}
|
|
}
|
|
|
|
public class MockInnerHandler : HttpMessageHandler
|
|
{
|
|
private readonly Queue<HttpResponseMessage> _responses = new();
|
|
public int CallCount { get; private set; }
|
|
public List<HttpRequestMessage> Requests { get; } = [];
|
|
|
|
public void QueueResponse(HttpStatusCode status, object? body = null)
|
|
{
|
|
var response = new HttpResponseMessage(status);
|
|
if (body is not null)
|
|
response.Content = JsonContent.Create(body, options: new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
});
|
|
_responses.Enqueue(response);
|
|
}
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
|
|
{
|
|
CallCount++;
|
|
Requests.Add(request);
|
|
var response = _responses.Count > 0 ? _responses.Dequeue() : new HttpResponseMessage(HttpStatusCode.OK);
|
|
return Task.FromResult(response);
|
|
}
|
|
|
|
public HttpMessageHandler CreatePipeline(DelegatingHandler handler)
|
|
{
|
|
handler.InnerHandler = this;
|
|
return handler;
|
|
}
|
|
}
|