Update integration tests to work with authentication in the Order.Api

This commit is contained in:
dsanz 2017-03-06 16:06:32 +01:00
parent 0fc35d7b4e
commit 52bddc51a2
5 changed files with 71 additions and 12 deletions

View File

@ -109,15 +109,7 @@
app.UseCors("CorsPolicy"); app.UseCors("CorsPolicy");
var identityUrl = Configuration.GetValue<string>("IdentityUrl"); ConfigureAuth(app);
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = identityUrl.ToString(),
ScopeName = "orders",
RequireHttpsMetadata = false
});
app.UseMvcWithDefaultRoute(); app.UseMvcWithDefaultRoute();
@ -126,5 +118,16 @@
OrderingContextSeed.SeedAsync(app).Wait(); OrderingContextSeed.SeedAsync(app).Wait();
} }
protected virtual void ConfigureAuth(IApplicationBuilder app)
{
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = identityUrl.ToString(),
ScopeName = "orders",
RequireHttpsMetadata = false
});
}
} }
} }

View File

@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace FunctionalTests.Middleware
{
class AutoAuthorizeMiddleware
{
private readonly RequestDelegate _next;
public AutoAuthorizeMiddleware(RequestDelegate rd)
{
_next = rd;
}
public async Task Invoke(HttpContext httpContext)
{
var identity = new ClaimsIdentity();
identity.AddClaim(new Claim("sub", "1234"));
httpContext.User.AddIdentity(identity);
await _next.Invoke(httpContext);
}
}
}

View File

@ -9,9 +9,9 @@
{ {
public TestServer CreateServer() public TestServer CreateServer()
{ {
var webHostBuilder = new WebHostBuilder(); var webHostBuilder = new WebHostBuilder();
webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory()); webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
webHostBuilder.UseStartup<Startup>(); webHostBuilder.UseStartup<OrderingTestsStartup>();
return new TestServer(webHostBuilder); return new TestServer(webHostBuilder);
} }

View File

@ -0,0 +1,29 @@
using Microsoft.eShopOnContainers.Services.Ordering.API;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using FunctionalTests.Middleware;
namespace FunctionalTests.Services.Ordering
{
public class OrderingTestsStartup : Startup
{
public OrderingTestsStartup(IHostingEnvironment env) : base(env)
{
}
protected override void ConfigureAuth(IApplicationBuilder app)
{
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
{
app.UseMiddleware<AutoAuthorizeMiddleware>();
}
else
{
base.ConfigureAuth(app);
}
}
}
}

View File

@ -1,4 +1,5 @@
{ {
"ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;", "ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;",
"IdentityUrl": "http://localhost:5105" "IdentityUrl": "http://localhost:5105",
"isTest": "true"
} }