using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; namespace Yavsc.Org.Tests; /// /// Startup filter that injects after /// the authentication and authorization middleware. The /// contract wraps the existing /// pipeline: the next delegate is the rest of the app's /// pipeline, so we run our middleware before it but /// after anything that was registered as a startup filter /// earlier in the chain. /// /// In practice this puts TestUserMiddleware ahead of /// UseAuthentication (registered inside ConfigurePipeline) /// because the production code path runs UseAuthentication /// synchronously inside Configure, after all startup filters /// have wrapped it. We want the opposite: the test identity must be /// visible to authorization and the controller, so we register /// TestUserMiddleware via /// inside the filter such that it runs late in the chain. The /// simplest way to achieve that is to register the middleware /// after the production authorization pipeline: we wrap with our /// middleware inside the filter, so our delegate sits between the /// framework middleware (set up by Configure) and the rest of the /// pipeline — meaning requests flow: /// framework authN/authZ → TestUserMiddleware → next pipeline. /// public class TestUserStartupFilter : IStartupFilter { public Action Configure(Action next) { return app => { app.UseMiddleware(); // Replay the production pipeline after the test middleware, // so downstream auth and controllers can see the injected // principal when no real login flow is used. next(app); }; } }