Integration tests in the new world of tighter integration with ASP.NET Core

When using .NET Core Identity Auth, I’m confused how integration tests should be set up in the context of Authentication/Authorization.

In the old world when using SS auth, my test AppHost would just register the same auth providers as the production AppHost. But in this new world, SS depends on the on the ASP.NET pipeline for auth (when using a NetCoreIdentityAuthProvider for example). I haven’t seen any examples of tests that recreate the pipeline. Should we be setting up a WebApplication with an identical pipeline now to run tests? Is there an example of this in any of the template projects? (all the ones I have looked at so far set up Auth but don’t have any services that require auth, and therefore no tests that require auth).

You basically need to configure it the same was you would your ASP .NET Core Web App, the only difference is that you would start the Web App with app.StartAsync() so it doesn’t block, here’s a rough outline:

class ExampleTest
{
    public ExampleTest()
    {
        var builder = WebApplication.CreateBuilder(new WebApplicationOptions
        {
            ContentRootPath = contentRootPath,
            WebRootPath = contentRootPath,
        });

        services.AddAuthentication(options => {  });
        services.AddAuthorization();
        services.AddServiceStack(typeof(MyServices).Assembly);

        var app = builder.Build();
        app.UseAuthorization();

        app.StartAsync(BaseUrl);
    }

    [OneTimeTearDown]
    public void TestFixtureTearDown() => AppHostBase.DisposeApp();
}

You can have a look at IdentityJwtAuthProviderTests.cs for a complete working example.