From e7e0eed9cc0888c6fd7154a8552a779743d9a070 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 1 May 2023 17:43:48 -0700 Subject: [PATCH] Fixed remaining tests - Lots of duplication zomg --- src/Services/Webhooks/Webhooks.API/Program.cs | 9 +-- .../Application.FunctionalTests.csproj | 7 +++ .../GlobalUsings.cs | 5 ++ .../Services/Basket/BasketScenariosBase.cs | 50 +++++++++++----- .../Services/Catalog/CatalogScenariosBase.cs | 58 ++++++++++--------- .../Ordering/OrderingScenariosBase.cs | 56 ++++++++++++------ 6 files changed, 123 insertions(+), 62 deletions(-) diff --git a/src/Services/Webhooks/Webhooks.API/Program.cs b/src/Services/Webhooks/Webhooks.API/Program.cs index 027814057..bc5d6c29f 100644 --- a/src/Services/Webhooks/Webhooks.API/Program.cs +++ b/src/Services/Webhooks/Webhooks.API/Program.cs @@ -1,6 +1,7 @@ -CreateWebHostBuilder(args).Build() - .MigrateDbContext((_, __) => { }) - .Run(); +// TODO: Don't do this twice... +var host = CreateWebHostBuilder(args).Build(); +host.Services.MigrateDbContext((_, __) => { }); +host.Run(); IWebHostBuilder CreateWebHostBuilder(string[] args) => @@ -16,4 +17,4 @@ IWebHostBuilder CreateWebHostBuilder(string[] args) => builder.AddConsole(); builder.AddDebug(); builder.AddAzureWebAppDiagnostics(); - }); \ No newline at end of file + }); diff --git a/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj b/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj index 8ea12ff3c..dab397d78 100644 --- a/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj +++ b/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj @@ -35,10 +35,17 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + diff --git a/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs b/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs index b09d08dd0..493b29f63 100644 --- a/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs +++ b/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs @@ -26,3 +26,8 @@ global using Microsoft.eShopOnContainers.Services.Ordering.API; global using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure; global using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; global using FunctionalTests.Services.Catalog; + +// This is a bit of a hack, since we need to differentiate each of these app's program +global using BasketProgram = Microsoft.eShopOnContainers.Services.Basket.API.BasketSettings; +global using CatalogProgram = Microsoft.eShopOnContainers.Services.Catalog.API.CatalogSettings; +global using OrderingProgram = Microsoft.eShopOnContainers.Services.Ordering.API.OrderingSettings; diff --git a/src/Tests/Services/Application.FunctionalTests/Services/Basket/BasketScenariosBase.cs b/src/Tests/Services/Application.FunctionalTests/Services/Basket/BasketScenariosBase.cs index 0cb668b8a..88ab903bf 100644 --- a/src/Tests/Services/Application.FunctionalTests/Services/Basket/BasketScenariosBase.cs +++ b/src/Tests/Services/Application.FunctionalTests/Services/Basket/BasketScenariosBase.cs @@ -1,24 +1,35 @@ -namespace FunctionalTests.Services.Basket; +using FunctionalTests.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.Hosting; -public class BasketScenariosBase +namespace FunctionalTests.Services.Basket; + +public class BasketScenariosBase : WebApplicationFactory { private const string ApiUrlBase = "api/v1/basket"; - public TestServer CreateServer() { - var path = Assembly.GetAssembly(typeof(BasketScenariosBase)) - .Location; + return Server; + } - var hostBuilder = new WebHostBuilder() - .UseContentRoot(Path.GetDirectoryName(path)) - .ConfigureAppConfiguration(cb => - { - cb.AddJsonFile("Services/Basket/appsettings.json", optional: false) - .AddEnvironmentVariables(); - }); - return new TestServer(hostBuilder); + protected override IHost CreateHost(IHostBuilder builder) + { + builder.ConfigureServices(servies => + { + servies.AddSingleton(); + }); + + builder.ConfigureAppConfiguration(c => + { + var directory = Path.GetDirectoryName(typeof(BasketScenariosBase).Assembly.Location)!; + + c.AddJsonFile(Path.Combine(directory, "Services/Basket/appsettings.json"), optional: false); + }); + + return base.CreateHost(builder); } public static class Get @@ -39,4 +50,17 @@ public class BasketScenariosBase public static string CreateBasket = $"{ApiUrlBase}/"; public static string CheckoutOrder = $"{ApiUrlBase}/checkout"; } + + private class AuthStartupFilter : IStartupFilter + { + public Action Configure(Action next) + { + return app => + { + app.UseMiddleware(); + + next(app); + }; + } + } } diff --git a/src/Tests/Services/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs b/src/Tests/Services/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs index 0fcf32e3c..830d24d97 100644 --- a/src/Tests/Services/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs +++ b/src/Tests/Services/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs @@ -1,37 +1,39 @@ namespace FunctionalTests.Services.Catalog; + +using FunctionalTests.Services.Ordering; +using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.eShopOnContainers.Services.Catalog.API; +using Microsoft.Extensions.Hosting; -public class CatalogScenariosBase +public class CatalogScenariosBase : WebApplicationFactory { public TestServer CreateServer() { - var path = Assembly.GetAssembly(typeof(CatalogScenariosBase)) - .Location; - - var hostBuilder = new WebHostBuilder() - .UseContentRoot(Path.GetDirectoryName(path)) - .ConfigureAppConfiguration(cb => - { - cb.AddJsonFile("Services/Catalog/appsettings.json", optional: false) - .AddEnvironmentVariables(); - }); - - var testServer = new TestServer(hostBuilder); - - testServer.Host - .MigrateDbContext((context, services) => - { - var env = services.GetService(); - var settings = services.GetService>(); - var logger = services.GetService>(); - - new CatalogContextSeed() - .SeedAsync(context, env, settings, logger) - .Wait(); - }) - .MigrateDbContext((_, __) => { }); - - return testServer; + Services.MigrateDbContext((context, services) => + { + var env = services.GetService(); + var settings = services.GetService>(); + var logger = services.GetService>(); + + new CatalogContextSeed() + .SeedAsync(context, env, settings, logger) + .Wait(); + }) + .MigrateDbContext((_, __) => { }); + + return Server; + } + + protected override IHost CreateHost(IHostBuilder builder) + { + builder.ConfigureAppConfiguration(c => + { + var directory = Path.GetDirectoryName(typeof(CatalogScenariosBase).Assembly.Location)!; + + c.AddJsonFile(Path.Combine(directory, "Services/Catalog/appsettings.json"), optional: false); + }); + + return base.CreateHost(builder); } public static class Get diff --git a/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs b/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs index 3d419eb83..906cf70a5 100644 --- a/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs +++ b/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs @@ -1,23 +1,15 @@ -namespace FunctionalTests.Services.Ordering; +using FunctionalTests.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.Hosting; -public class OrderingScenariosBase +namespace FunctionalTests.Services.Ordering; + +public class OrderingScenariosBase : WebApplicationFactory { public TestServer CreateServer() { - var path = Assembly.GetAssembly(typeof(OrderingScenariosBase)) - .Location; - - var hostBuilder = new WebHostBuilder() - .UseContentRoot(Path.GetDirectoryName(path)) - .ConfigureAppConfiguration(cb => - { - cb.AddJsonFile("Services/Ordering/appsettings.json", optional: false) - .AddEnvironmentVariables(); - }); - - var testServer = new TestServer(hostBuilder); - - testServer.Host + Services .MigrateDbContext((context, services) => { var env = services.GetService(); @@ -30,7 +22,24 @@ public class OrderingScenariosBase }) .MigrateDbContext((_, __) => { }); - return testServer; + return Server; + } + + protected override IHost CreateHost(IHostBuilder builder) + { + builder.ConfigureServices(servies => + { + servies.AddSingleton(); + }); + + builder.ConfigureAppConfiguration(c => + { + var directory = Path.GetDirectoryName(typeof(OrderingScenariosBase).Assembly.Location)!; + + c.AddJsonFile(Path.Combine(directory, "Services/Ordering/appsettings.json"), optional: false); + }); + + return base.CreateHost(builder); } public static class Get @@ -60,4 +69,17 @@ public class OrderingScenariosBase return $"api/v1/orders/{id}"; } } + + private class AuthStartupFilter : IStartupFilter + { + public Action Configure(Action next) + { + return app => + { + app.UseMiddleware(); + + next(app); + }; + } + } }