@ -1,31 +1,26 @@ | |||||
using Microsoft.AspNetCore.Http; | |||||
using System.Security.Claims; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.FunctionalTests; | |||||
namespace Ordering.FunctionalTests | |||||
class AutoAuthorizeMiddleware | |||||
{ | { | ||||
class AutoAuthorizeMiddleware | |||||
{ | |||||
public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00"; | |||||
public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00"; | |||||
private readonly RequestDelegate _next; | |||||
private readonly RequestDelegate _next; | |||||
public AutoAuthorizeMiddleware(RequestDelegate rd) | |||||
{ | |||||
_next = rd; | |||||
} | |||||
public AutoAuthorizeMiddleware(RequestDelegate rd) | |||||
{ | |||||
_next = rd; | |||||
} | |||||
public async Task Invoke(HttpContext httpContext) | |||||
{ | |||||
var identity = new ClaimsIdentity("cookies"); | |||||
public async Task Invoke(HttpContext httpContext) | |||||
{ | |||||
var identity = new ClaimsIdentity("cookies"); | |||||
identity.AddClaim(new Claim("sub", IDENTITY_ID)); | |||||
identity.AddClaim(new Claim("unique_name", IDENTITY_ID)); | |||||
identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID)); | |||||
identity.AddClaim(new Claim("sub", IDENTITY_ID)); | |||||
identity.AddClaim(new Claim("unique_name", IDENTITY_ID)); | |||||
identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID)); | |||||
httpContext.User.AddIdentity(identity); | |||||
httpContext.User.AddIdentity(identity); | |||||
await _next.Invoke(httpContext); | |||||
} | |||||
await _next.Invoke(httpContext); | |||||
} | } | ||||
} | } |
@ -1,16 +1,11 @@ | |||||
using Microsoft.AspNetCore.TestHost; | |||||
using System; | |||||
using System.Net.Http; | |||||
namespace Ordering.FunctionalTests; | |||||
namespace Ordering.FunctionalTests | |||||
static class HttpClientExtensions | |||||
{ | { | ||||
static class HttpClientExtensions | |||||
public static HttpClient CreateIdempotentClient(this TestServer server) | |||||
{ | { | ||||
public static HttpClient CreateIdempotentClient(this TestServer server) | |||||
{ | |||||
var client = server.CreateClient(); | |||||
client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString()); | |||||
return client; | |||||
} | |||||
var client = server.CreateClient(); | |||||
client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString()); | |||||
return client; | |||||
} | } | ||||
} | } |
@ -1,65 +1,51 @@ | |||||
using Microsoft.AspNetCore.Hosting; | |||||
using Microsoft.AspNetCore.TestHost; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; | |||||
using Microsoft.eShopOnContainers.Services.Ordering.API; | |||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure; | |||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; | |||||
using Microsoft.Extensions.Configuration; | |||||
using Microsoft.Extensions.DependencyInjection; | |||||
using Microsoft.Extensions.Logging; | |||||
using Microsoft.Extensions.Options; | |||||
using System.IO; | |||||
using System.Reflection; | |||||
namespace Ordering.FunctionalTests; | |||||
namespace Ordering.FunctionalTests | |||||
public class OrderingScenarioBase | |||||
{ | { | ||||
public class OrderingScenarioBase | |||||
public TestServer CreateServer() | |||||
{ | { | ||||
public TestServer CreateServer() | |||||
{ | |||||
var path = Assembly.GetAssembly(typeof(OrderingScenarioBase)) | |||||
.Location; | |||||
var path = Assembly.GetAssembly(typeof(OrderingScenarioBase)) | |||||
.Location; | |||||
var hostBuilder = new WebHostBuilder() | |||||
.UseContentRoot(Path.GetDirectoryName(path)) | |||||
.ConfigureAppConfiguration(cb => | |||||
{ | |||||
cb.AddJsonFile("appsettings.json", optional: false) | |||||
.AddEnvironmentVariables(); | |||||
}).UseStartup<OrderingTestsStartup>(); | |||||
var hostBuilder = new WebHostBuilder() | |||||
.UseContentRoot(Path.GetDirectoryName(path)) | |||||
.ConfigureAppConfiguration(cb => | |||||
{ | |||||
cb.AddJsonFile("appsettings.json", optional: false) | |||||
.AddEnvironmentVariables(); | |||||
}).UseStartup<OrderingTestsStartup>(); | |||||
var testServer = new TestServer(hostBuilder); | |||||
var testServer = new TestServer(hostBuilder); | |||||
testServer.Host | |||||
.MigrateDbContext<OrderingContext>((context, services) => | |||||
{ | |||||
var env = services.GetService<IWebHostEnvironment>(); | |||||
var settings = services.GetService<IOptions<OrderingSettings>>(); | |||||
var logger = services.GetService<ILogger<OrderingContextSeed>>(); | |||||
testServer.Host | |||||
.MigrateDbContext<OrderingContext>((context, services) => | |||||
{ | |||||
var env = services.GetService<IWebHostEnvironment>(); | |||||
var settings = services.GetService<IOptions<OrderingSettings>>(); | |||||
var logger = services.GetService<ILogger<OrderingContextSeed>>(); | |||||
new OrderingContextSeed() | |||||
.SeedAsync(context, env, settings, logger) | |||||
.Wait(); | |||||
}) | |||||
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); | |||||
new OrderingContextSeed() | |||||
.SeedAsync(context, env, settings, logger) | |||||
.Wait(); | |||||
}) | |||||
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); | |||||
return testServer; | |||||
} | |||||
public static class Get | |||||
{ | |||||
public static string Orders = "api/v1/orders"; | |||||
return testServer; | |||||
} | |||||
public static string OrderBy(int id) | |||||
{ | |||||
return $"api/v1/orders/{id}"; | |||||
} | |||||
} | |||||
public static class Get | |||||
{ | |||||
public static string Orders = "api/v1/orders"; | |||||
public static class Put | |||||
public static string OrderBy(int id) | |||||
{ | { | ||||
public static string CancelOrder = "api/v1/orders/cancel"; | |||||
public static string ShipOrder = "api/v1/orders/ship"; | |||||
return $"api/v1/orders/{id}"; | |||||
} | } | ||||
} | } | ||||
public static class Put | |||||
{ | |||||
public static string CancelOrder = "api/v1/orders/cancel"; | |||||
public static string ShipOrder = "api/v1/orders/ship"; | |||||
} | |||||
} | } |
@ -1,35 +1,27 @@ | |||||
using Microsoft.AspNetCore.Builder; | |||||
using Microsoft.AspNetCore.Routing; | |||||
using Microsoft.eShopOnContainers.Services.Ordering.API; | |||||
using Microsoft.Extensions.Configuration; | |||||
using Microsoft.Extensions.DependencyInjection; | |||||
using System; | |||||
namespace Ordering.FunctionalTests; | |||||
namespace Ordering.FunctionalTests | |||||
public class OrderingTestsStartup : Startup | |||||
{ | { | ||||
public class OrderingTestsStartup : Startup | |||||
public OrderingTestsStartup(IConfiguration env) : base(env) | |||||
{ | { | ||||
public OrderingTestsStartup(IConfiguration env) : base(env) | |||||
{ | |||||
} | |||||
} | |||||
public override IServiceProvider ConfigureServices(IServiceCollection services) | |||||
public override IServiceProvider ConfigureServices(IServiceCollection services) | |||||
{ | |||||
// Added to avoid the Authorize data annotation in test environment. | |||||
// Property "SuppressCheckForUnhandledSecurityMetadata" in appsettings.json | |||||
services.Configure<RouteOptions>(Configuration); | |||||
return base.ConfigureServices(services); | |||||
} | |||||
protected override void ConfigureAuth(IApplicationBuilder app) | |||||
{ | |||||
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) | |||||
{ | { | ||||
// Added to avoid the Authorize data annotation in test environment. | |||||
// Property "SuppressCheckForUnhandledSecurityMetadata" in appsettings.json | |||||
services.Configure<RouteOptions>(Configuration); | |||||
return base.ConfigureServices(services); | |||||
app.UseMiddleware<AutoAuthorizeMiddleware>(); | |||||
} | } | ||||
protected override void ConfigureAuth(IApplicationBuilder app) | |||||
else | |||||
{ | { | ||||
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) | |||||
{ | |||||
app.UseMiddleware<AutoAuthorizeMiddleware>(); | |||||
} | |||||
else | |||||
{ | |||||
base.ConfigureAuth(app); | |||||
} | |||||
base.ConfigureAuth(app); | |||||
} | } | ||||
} | } | ||||
} | } |