|
|
@ -1,36 +1,42 @@ |
|
|
|
namespace Ordering.FunctionalTests; |
|
|
|
using Microsoft.AspNetCore.Mvc.Testing; |
|
|
|
using Microsoft.Extensions.Hosting; |
|
|
|
|
|
|
|
public class OrderingScenarioBase |
|
|
|
namespace Ordering.FunctionalTests; |
|
|
|
|
|
|
|
public class OrderingScenarioBase : WebApplicationFactory<Program> |
|
|
|
{ |
|
|
|
public TestServer CreateServer() |
|
|
|
{ |
|
|
|
var path = Assembly.GetAssembly(typeof(OrderingScenarioBase)) |
|
|
|
.Location; |
|
|
|
Services.MigrateDbContext<OrderingContext>((context, services) => |
|
|
|
{ |
|
|
|
var env = services.GetService<IWebHostEnvironment>(); |
|
|
|
var settings = services.GetService<IOptions<OrderingSettings>>(); |
|
|
|
var logger = services.GetService<ILogger<OrderingContextSeed>>(); |
|
|
|
|
|
|
|
var hostBuilder = new WebHostBuilder() |
|
|
|
.UseContentRoot(Path.GetDirectoryName(path)) |
|
|
|
.ConfigureAppConfiguration(cb => |
|
|
|
{ |
|
|
|
cb.AddJsonFile("appsettings.json", optional: false) |
|
|
|
.AddEnvironmentVariables(); |
|
|
|
}); |
|
|
|
new OrderingContextSeed() |
|
|
|
.SeedAsync(context, env, settings, logger) |
|
|
|
.Wait(); |
|
|
|
}) |
|
|
|
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); |
|
|
|
|
|
|
|
return Server; |
|
|
|
} |
|
|
|
|
|
|
|
var testServer = new TestServer(hostBuilder); |
|
|
|
protected override IHost CreateHost(IHostBuilder builder) |
|
|
|
{ |
|
|
|
builder.ConfigureServices(servies => |
|
|
|
{ |
|
|
|
servies.AddSingleton<IStartupFilter, AuthStartupFilter>(); |
|
|
|
}); |
|
|
|
|
|
|
|
testServer.Host |
|
|
|
.MigrateDbContext<OrderingContext>((context, services) => |
|
|
|
{ |
|
|
|
var env = services.GetService<IWebHostEnvironment>(); |
|
|
|
var settings = services.GetService<IOptions<OrderingSettings>>(); |
|
|
|
var logger = services.GetService<ILogger<OrderingContextSeed>>(); |
|
|
|
builder.ConfigureAppConfiguration(c => |
|
|
|
{ |
|
|
|
var directory = Path.GetDirectoryName(typeof(OrderingScenarioBase).Assembly.Location)!; |
|
|
|
|
|
|
|
new OrderingContextSeed() |
|
|
|
.SeedAsync(context, env, settings, logger) |
|
|
|
.Wait(); |
|
|
|
}) |
|
|
|
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); |
|
|
|
c.AddJsonFile(Path.Combine(directory, "appsettings.json"), optional: false); |
|
|
|
}); |
|
|
|
|
|
|
|
return testServer; |
|
|
|
return base.CreateHost(builder); |
|
|
|
} |
|
|
|
|
|
|
|
public static class Get |
|
|
@ -48,4 +54,17 @@ public class OrderingScenarioBase |
|
|
|
public static string CancelOrder = "api/v1/orders/cancel"; |
|
|
|
public static string ShipOrder = "api/v1/orders/ship"; |
|
|
|
} |
|
|
|
|
|
|
|
private class AuthStartupFilter : IStartupFilter |
|
|
|
{ |
|
|
|
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) |
|
|
|
{ |
|
|
|
return app => |
|
|
|
{ |
|
|
|
app.UseMiddleware<AutoAuthorizeMiddleware>(); |
|
|
|
|
|
|
|
next(app); |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |