diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 0f75b6370..f1d58be1d 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -109,15 +109,7 @@ app.UseCors("CorsPolicy"); - var identityUrl = Configuration.GetValue("IdentityUrl"); - - app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions - { - Authority = identityUrl.ToString(), - ScopeName = "orders", - RequireHttpsMetadata = false - }); - + ConfigureAuth(app); app.UseMvcWithDefaultRoute(); @@ -126,5 +118,16 @@ OrderingContextSeed.SeedAsync(app).Wait(); } + + protected virtual void ConfigureAuth(IApplicationBuilder app) + { + var identityUrl = Configuration.GetValue("IdentityUrl"); + app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions + { + Authority = identityUrl.ToString(), + ScopeName = "orders", + RequireHttpsMetadata = false + }); + } } } diff --git a/test/Services/FunctionalTests/Middleware/AutoAuthorizeMiddleware.cs b/test/Services/FunctionalTests/Middleware/AutoAuthorizeMiddleware.cs new file mode 100644 index 000000000..72248e29d --- /dev/null +++ b/test/Services/FunctionalTests/Middleware/AutoAuthorizeMiddleware.cs @@ -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); + } + } +} diff --git a/test/Services/FunctionalTests/Services/Ordering/OrderingScenarioBase.cs b/test/Services/FunctionalTests/Services/Ordering/OrderingScenarioBase.cs index ee069ffc4..bf267d5f7 100644 --- a/test/Services/FunctionalTests/Services/Ordering/OrderingScenarioBase.cs +++ b/test/Services/FunctionalTests/Services/Ordering/OrderingScenarioBase.cs @@ -9,9 +9,9 @@ { public TestServer CreateServer() { - var webHostBuilder = new WebHostBuilder(); + var webHostBuilder = new WebHostBuilder(); webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory()); - webHostBuilder.UseStartup(); + webHostBuilder.UseStartup(); return new TestServer(webHostBuilder); } diff --git a/test/Services/FunctionalTests/Services/Ordering/OrderingTestsStartup.cs b/test/Services/FunctionalTests/Services/Ordering/OrderingTestsStartup.cs new file mode 100644 index 000000000..02237113f --- /dev/null +++ b/test/Services/FunctionalTests/Services/Ordering/OrderingTestsStartup.cs @@ -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(); + } + else + { + base.ConfigureAuth(app); + } + } + } +} diff --git a/test/Services/FunctionalTests/settings.json b/test/Services/FunctionalTests/settings.json index 8d56435f4..3b6023956 100644 --- a/test/Services/FunctionalTests/settings.json +++ b/test/Services/FunctionalTests/settings.json @@ -1,4 +1,5 @@ { "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" }