Fixed remaining tests
- Lots of duplication zomg
This commit is contained in:
		
							parent
							
								
									c7edd50b38
								
							
						
					
					
						commit
						e7e0eed9cc
					
				| @ -1,6 +1,7 @@ | |||||||
| CreateWebHostBuilder(args).Build() | // TODO: Don't do this twice... | ||||||
|     .MigrateDbContext<WebhooksContext>((_, __) => { }) | var host = CreateWebHostBuilder(args).Build(); | ||||||
|     .Run(); | host.Services.MigrateDbContext<WebhooksContext>((_, __) => { }); | ||||||
|  | host.Run(); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| IWebHostBuilder CreateWebHostBuilder(string[] args) => | IWebHostBuilder CreateWebHostBuilder(string[] args) => | ||||||
| @ -16,4 +17,4 @@ IWebHostBuilder CreateWebHostBuilder(string[] args) => | |||||||
|                     builder.AddConsole(); |                     builder.AddConsole(); | ||||||
|                     builder.AddDebug(); |                     builder.AddDebug(); | ||||||
|                     builder.AddAzureWebAppDiagnostics(); |                     builder.AddAzureWebAppDiagnostics(); | ||||||
|                 }); |                 }); | ||||||
|  | |||||||
| @ -35,10 +35,17 @@ | |||||||
|     <Content Include="Services\Ordering\appsettings.json"> |     <Content Include="Services\Ordering\appsettings.json"> | ||||||
|       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||||||
|     </Content> |     </Content> | ||||||
|  |     <Content Include="Services\Basket\appsettings.json"> | ||||||
|  |       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||||||
|  |     </Content> | ||||||
|  |     <Content Include="Services\Catalog\appsettings.json"> | ||||||
|  |       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||||||
|  |     </Content> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
| 
 | 
 | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="Microsoft.NET.Test.Sdk" /> |     <PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||||||
|  |     <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" /> | ||||||
|     <PackageReference Include="Microsoft.AspNetCore.TestHost" /> |     <PackageReference Include="Microsoft.AspNetCore.TestHost" /> | ||||||
|     <PackageReference Include="xunit" /> |     <PackageReference Include="xunit" /> | ||||||
|     <PackageReference Include="xunit.runner.visualstudio"> |     <PackageReference Include="xunit.runner.visualstudio"> | ||||||
|  | |||||||
| @ -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.API.Infrastructure; | ||||||
| global using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; | global using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; | ||||||
| global using FunctionalTests.Services.Catalog; | 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; | ||||||
|  | |||||||
| @ -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<BasketProgram> | ||||||
| { | { | ||||||
|     private const string ApiUrlBase = "api/v1/basket"; |     private const string ApiUrlBase = "api/v1/basket"; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     public TestServer CreateServer() |     public TestServer CreateServer() | ||||||
|     { |     { | ||||||
|         var path = Assembly.GetAssembly(typeof(BasketScenariosBase)) |         return Server; | ||||||
|             .Location; |     } | ||||||
| 
 | 
 | ||||||
|         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<IStartupFilter, AuthStartupFilter>(); | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         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 |     public static class Get | ||||||
| @ -39,4 +50,17 @@ public class BasketScenariosBase | |||||||
|         public static string CreateBasket = $"{ApiUrlBase}/"; |         public static string CreateBasket = $"{ApiUrlBase}/"; | ||||||
|         public static string CheckoutOrder = $"{ApiUrlBase}/checkout"; |         public static string CheckoutOrder = $"{ApiUrlBase}/checkout"; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     private class AuthStartupFilter : IStartupFilter | ||||||
|  |     { | ||||||
|  |         public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||||||
|  |         { | ||||||
|  |             return app => | ||||||
|  |             { | ||||||
|  |                 app.UseMiddleware<AutoAuthorizeMiddleware>(); | ||||||
|  | 
 | ||||||
|  |                 next(app); | ||||||
|  |             }; | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,37 +1,39 @@ | |||||||
| namespace FunctionalTests.Services.Catalog; | namespace FunctionalTests.Services.Catalog; | ||||||
| using Microsoft.eShopOnContainers.Services.Catalog.API; |  | ||||||
| 
 | 
 | ||||||
| public class CatalogScenariosBase | using FunctionalTests.Services.Ordering; | ||||||
|  | using Microsoft.AspNetCore.Mvc.Testing; | ||||||
|  | using Microsoft.eShopOnContainers.Services.Catalog.API; | ||||||
|  | using Microsoft.Extensions.Hosting; | ||||||
|  | 
 | ||||||
|  | public class CatalogScenariosBase : WebApplicationFactory<CatalogProgram> | ||||||
| { | { | ||||||
|     public TestServer CreateServer() |     public TestServer CreateServer() | ||||||
|     { |     { | ||||||
|         var path = Assembly.GetAssembly(typeof(CatalogScenariosBase)) |         Services.MigrateDbContext<CatalogContext>((context, services) => | ||||||
|             .Location; |         { | ||||||
|  |             var env = services.GetService<IWebHostEnvironment>(); | ||||||
|  |             var settings = services.GetService<IOptions<CatalogSettings>>(); | ||||||
|  |             var logger = services.GetService<ILogger<CatalogContextSeed>>(); | ||||||
| 
 | 
 | ||||||
|         var hostBuilder = new WebHostBuilder() |             new CatalogContextSeed() | ||||||
|             .UseContentRoot(Path.GetDirectoryName(path)) |             .SeedAsync(context, env, settings, logger) | ||||||
|             .ConfigureAppConfiguration(cb => |             .Wait(); | ||||||
|             { |         }) | ||||||
|                 cb.AddJsonFile("Services/Catalog/appsettings.json", optional: false) |         .MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); | ||||||
|                 .AddEnvironmentVariables(); |  | ||||||
|             }); |  | ||||||
| 
 | 
 | ||||||
|         var testServer = new TestServer(hostBuilder); |         return Server; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         testServer.Host |     protected override IHost CreateHost(IHostBuilder builder) | ||||||
|             .MigrateDbContext<CatalogContext>((context, services) => |     { | ||||||
|             { |         builder.ConfigureAppConfiguration(c => | ||||||
|                 var env = services.GetService<IWebHostEnvironment>(); |         { | ||||||
|                 var settings = services.GetService<IOptions<CatalogSettings>>(); |             var directory = Path.GetDirectoryName(typeof(CatalogScenariosBase).Assembly.Location)!; | ||||||
|                 var logger = services.GetService<ILogger<CatalogContextSeed>>(); |  | ||||||
| 
 | 
 | ||||||
|                 new CatalogContextSeed() |             c.AddJsonFile(Path.Combine(directory, "Services/Catalog/appsettings.json"), optional: false); | ||||||
|                 .SeedAsync(context, env, settings, logger) |         }); | ||||||
|                 .Wait(); |  | ||||||
|             }) |  | ||||||
|             .MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); |  | ||||||
| 
 | 
 | ||||||
|         return testServer; |         return base.CreateHost(builder); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static class Get |     public static class Get | ||||||
|  | |||||||
| @ -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<OrderingProgram> | ||||||
| { | { | ||||||
|     public TestServer CreateServer() |     public TestServer CreateServer() | ||||||
|     { |     { | ||||||
|         var path = Assembly.GetAssembly(typeof(OrderingScenariosBase)) |         Services | ||||||
|             .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 |  | ||||||
|             .MigrateDbContext<OrderingContext>((context, services) => |             .MigrateDbContext<OrderingContext>((context, services) => | ||||||
|             { |             { | ||||||
|                 var env = services.GetService<IWebHostEnvironment>(); |                 var env = services.GetService<IWebHostEnvironment>(); | ||||||
| @ -30,7 +22,24 @@ public class OrderingScenariosBase | |||||||
|             }) |             }) | ||||||
|             .MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); |             .MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); | ||||||
| 
 | 
 | ||||||
|         return testServer; |         return Server; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected override IHost CreateHost(IHostBuilder builder) | ||||||
|  |     { | ||||||
|  |         builder.ConfigureServices(servies => | ||||||
|  |         { | ||||||
|  |             servies.AddSingleton<IStartupFilter, AuthStartupFilter>(); | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         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 |     public static class Get | ||||||
| @ -60,4 +69,17 @@ public class OrderingScenariosBase | |||||||
|             return $"api/v1/orders/{id}"; |             return $"api/v1/orders/{id}"; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     private class AuthStartupFilter : IStartupFilter | ||||||
|  |     { | ||||||
|  |         public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||||||
|  |         { | ||||||
|  |             return app => | ||||||
|  |             { | ||||||
|  |                 app.UseMiddleware<AutoAuthorizeMiddleware>(); | ||||||
|  | 
 | ||||||
|  |                 next(app); | ||||||
|  |             }; | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user