Included file-scope namespace statement for using

This commit is contained in:
Sumit Ghosh 2021-10-14 18:19:09 +05:30
parent 6833ad3621
commit 074025644a
4 changed files with 81 additions and 113 deletions

View File

@ -1,31 +1,26 @@
using Microsoft.AspNetCore.Http; namespace Ordering.FunctionalTests;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Ordering.FunctionalTests class AutoAuthorizeMiddleware
{ {
class AutoAuthorizeMiddleware public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00";
private readonly RequestDelegate _next;
public AutoAuthorizeMiddleware(RequestDelegate rd)
{ {
public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00"; _next = rd;
}
private readonly RequestDelegate _next; public async Task Invoke(HttpContext httpContext)
{
var identity = new ClaimsIdentity("cookies");
public AutoAuthorizeMiddleware(RequestDelegate rd) identity.AddClaim(new Claim("sub", IDENTITY_ID));
{ identity.AddClaim(new Claim("unique_name", IDENTITY_ID));
_next = rd; identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID));
}
public async Task Invoke(HttpContext httpContext) httpContext.User.AddIdentity(identity);
{
var identity = new ClaimsIdentity("cookies");
identity.AddClaim(new Claim("sub", IDENTITY_ID)); await _next.Invoke(httpContext);
identity.AddClaim(new Claim("unique_name", IDENTITY_ID));
identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID));
httpContext.User.AddIdentity(identity);
await _next.Invoke(httpContext);
}
} }
} }

View File

@ -1,16 +1,11 @@
using Microsoft.AspNetCore.TestHost; namespace Ordering.FunctionalTests;
using System;
using System.Net.Http;
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());
var client = server.CreateClient(); return client;
client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString());
return client;
}
} }
} }

View File

@ -1,65 +1,51 @@
using Microsoft.AspNetCore.Hosting; namespace Ordering.FunctionalTests;
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 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() var hostBuilder = new WebHostBuilder()
.UseContentRoot(Path.GetDirectoryName(path)) .UseContentRoot(Path.GetDirectoryName(path))
.ConfigureAppConfiguration(cb => .ConfigureAppConfiguration(cb =>
{
cb.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables();
}).UseStartup<OrderingTestsStartup>();
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>>();
new OrderingContextSeed()
.SeedAsync(context, env, settings, logger)
.Wait();
})
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
return testServer;
}
public static class Get
{
public static string Orders = "api/v1/orders";
public static string OrderBy(int id)
{ {
return $"api/v1/orders/{id}"; cb.AddJsonFile("appsettings.json", optional: false)
} .AddEnvironmentVariables();
} }).UseStartup<OrderingTestsStartup>();
public static class Put 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>>();
new OrderingContextSeed()
.SeedAsync(context, env, settings, logger)
.Wait();
})
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
return testServer;
}
public static class Get
{
public static string Orders = "api/v1/orders";
public static string OrderBy(int id)
{ {
public static string CancelOrder = "api/v1/orders/cancel"; return $"api/v1/orders/{id}";
public static string ShipOrder = "api/v1/orders/ship";
} }
} }
public static class Put
{
public static string CancelOrder = "api/v1/orders/cancel";
public static string ShipOrder = "api/v1/orders/ship";
}
} }

View File

@ -1,35 +1,27 @@
using Microsoft.AspNetCore.Builder; namespace Ordering.FunctionalTests;
using Microsoft.AspNetCore.Routing;
using Microsoft.eShopOnContainers.Services.Ordering.API;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
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. app.UseMiddleware<AutoAuthorizeMiddleware>();
// Property "SuppressCheckForUnhandledSecurityMetadata" in appsettings.json
services.Configure<RouteOptions>(Configuration);
return base.ConfigureServices(services);
} }
protected override void ConfigureAuth(IApplicationBuilder app) else
{ {
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) base.ConfigureAuth(app);
{
app.UseMiddleware<AutoAuthorizeMiddleware>();
}
else
{
base.ConfigureAuth(app);
}
} }
} }
} }