public async Task MapTestAuthSuccess(Type type, bool authSuccess) { // Arrange ServiceProvider provider = null; Action <AuthorizationPolicyBuilder> policyAction = policy => policy.RequireClaim("scope", "invalidscope"); if (authSuccess) { policyAction = policy => policy.RequireClaim("scope", "actuators.read"); // Set up for success } var hostBuilder = new HostBuilder().ConfigureWebHost(builder => builder.UseTestServer() .ConfigureServices((context, s) => { s.AddRouting(); s.AddTraceActuator(context.Configuration, MediaTypeVersion.V1); s.AddThreadDumpActuator(context.Configuration, MediaTypeVersion.V1); s.AddCloudFoundryActuator(context.Configuration); s.AddAllActuators(context.Configuration); // Add all of them, but map one at a time s.AddAuthentication(TestAuthHandler.AuthenticationScheme) .AddScheme <AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.AuthenticationScheme, options => { }); s.AddAuthorization(options => options.AddPolicy("TestAuth", policyAction)); // setup Auth based on test Case provider = s.BuildServiceProvider(); }) .Configure(a => a.UseRouting().UseAuthentication().UseAuthorization().UseEndpoints(endpoints => endpoints.MapActuatorEndpoint(type).RequireAuthorization("TestAuth")))); // Act var host = await hostBuilder.AddDynamicLogging().StartAsync(); // Assert var(middleware, optionsType) = ActuatorRouteBuilderExtensions.LookupMiddleware(type); var options = provider.GetService(optionsType) as IEndpointOptions; var mgmtContext = type.IsAssignableFrom(typeof(CloudFoundryEndpoint)) ? (IManagementOptions)provider.GetRequiredService <CloudFoundryManagementOptions>() : (IManagementOptions)provider.GetRequiredService <ActuatorManagementOptions>(); var path = options.GetContextPath(mgmtContext); Assert.NotNull(path); var response = host.GetTestServer().CreateClient().GetAsync(path); var expected = authSuccess ? HttpStatusCode.OK : HttpStatusCode.Unauthorized; Assert.True(expected == response.Result.StatusCode, $"Expected {expected}, but got {response.Result.StatusCode} for {path} and type {type}"); }
public void LookupMiddlewareTest(Type type) { var(middleware, options) = ActuatorRouteBuilderExtensions.LookupMiddleware(type); Assert.NotNull(middleware); Assert.NotNull(options); }