Browse Source

Fixed remaining tests

- Lots of duplication zomg
rebond/dev
David Fowler 1 year ago
parent
commit
70036a5e3c
6 changed files with 123 additions and 62 deletions
  1. +5
    -4
      src/Services/Webhooks/Webhooks.API/Program.cs
  2. +7
    -0
      src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj
  3. +5
    -0
      src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs
  4. +37
    -13
      src/Tests/Services/Application.FunctionalTests/Services/Basket/BasketScenariosBase.cs
  5. +30
    -28
      src/Tests/Services/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs
  6. +39
    -17
      src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs

+ 5
- 4
src/Services/Webhooks/Webhooks.API/Program.cs View File

@ -1,6 +1,7 @@
CreateWebHostBuilder(args).Build()
.MigrateDbContext<WebhooksContext>((_, __) => { })
.Run();
// TODO: Don't do this twice...
var host = CreateWebHostBuilder(args).Build();
host.Services.MigrateDbContext<WebhooksContext>((_, __) => { });
host.Run();
IWebHostBuilder CreateWebHostBuilder(string[] args) =>
@ -16,4 +17,4 @@ IWebHostBuilder CreateWebHostBuilder(string[] args) =>
builder.AddConsole();
builder.AddDebug();
builder.AddAzureWebAppDiagnostics();
});
});

+ 7
- 0
src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj View File

@ -35,10 +35,17 @@
<Content Include="Services\Ordering\appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Services\Basket\appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Services\Catalog\appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">


+ 5
- 0
src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs View File

@ -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;

+ 37
- 13
src/Tests/Services/Application.FunctionalTests/Services/Basket/BasketScenariosBase.cs View File

@ -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";
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<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
@ -39,4 +50,17 @@ public class BasketScenariosBase
public static string CreateBasket = $"{ApiUrlBase}/";
public static string CheckoutOrder = $"{ApiUrlBase}/checkout";
}
private class AuthStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UseMiddleware<AutoAuthorizeMiddleware>();
next(app);
};
}
}
}

+ 30
- 28
src/Tests/Services/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs View File

@ -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<CatalogProgram>
{
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<CatalogContext>((context, services) =>
{
var env = services.GetService<IWebHostEnvironment>();
var settings = services.GetService<IOptions<CatalogSettings>>();
var logger = services.GetService<ILogger<CatalogContextSeed>>();
new CatalogContextSeed()
.SeedAsync(context, env, settings, logger)
.Wait();
})
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
return testServer;
Services.MigrateDbContext<CatalogContext>((context, services) =>
{
var env = services.GetService<IWebHostEnvironment>();
var settings = services.GetService<IOptions<CatalogSettings>>();
var logger = services.GetService<ILogger<CatalogContextSeed>>();
new CatalogContextSeed()
.SeedAsync(context, env, settings, logger)
.Wait();
})
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
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


+ 39
- 17
src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs View File

@ -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()
{
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<OrderingContext>((context, services) =>
{
var env = services.GetService<IWebHostEnvironment>();
@ -30,7 +22,24 @@ public class OrderingScenariosBase
})
.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
@ -60,4 +69,17 @@ public class OrderingScenariosBase
return $"api/v1/orders/{id}";
}
}
private class AuthStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UseMiddleware<AutoAuthorizeMiddleware>();
next(app);
};
}
}
}

Loading…
Cancel
Save