Skip to content

Commit 52bddc5

Browse files
committed
Update integration tests to work with authentication in the Order.Api
1 parent 0fc35d7 commit 52bddc5

5 files changed

Lines changed: 71 additions & 12 deletions

File tree

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
109109

110110
app.UseCors("CorsPolicy");
111111

112-
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
113-
114-
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
115-
{
116-
Authority = identityUrl.ToString(),
117-
ScopeName = "orders",
118-
RequireHttpsMetadata = false
119-
});
120-
112+
ConfigureAuth(app);
121113

122114
app.UseMvcWithDefaultRoute();
123115

@@ -126,5 +118,16 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
126118

127119
OrderingContextSeed.SeedAsync(app).Wait();
128120
}
121+
122+
protected virtual void ConfigureAuth(IApplicationBuilder app)
123+
{
124+
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
125+
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
126+
{
127+
Authority = identityUrl.ToString(),
128+
ScopeName = "orders",
129+
RequireHttpsMetadata = false
130+
});
131+
}
129132
}
130133
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Security.Claims;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace FunctionalTests.Middleware
9+
{
10+
class AutoAuthorizeMiddleware
11+
{
12+
private readonly RequestDelegate _next;
13+
public AutoAuthorizeMiddleware(RequestDelegate rd)
14+
{
15+
_next = rd;
16+
}
17+
18+
public async Task Invoke(HttpContext httpContext)
19+
{
20+
var identity = new ClaimsIdentity();
21+
identity.AddClaim(new Claim("sub", "1234"));
22+
httpContext.User.AddIdentity(identity);
23+
await _next.Invoke(httpContext);
24+
}
25+
}
26+
}

test/Services/FunctionalTests/Services/Ordering/OrderingScenarioBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class OrderingScenarioBase
99
{
1010
public TestServer CreateServer()
1111
{
12-
var webHostBuilder = new WebHostBuilder();
12+
var webHostBuilder = new WebHostBuilder();
1313
webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
14-
webHostBuilder.UseStartup<Startup>();
14+
webHostBuilder.UseStartup<OrderingTestsStartup>();
1515

1616
return new TestServer(webHostBuilder);
1717
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.eShopOnContainers.Services.Ordering.API;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Builder;
7+
using FunctionalTests.Middleware;
8+
9+
namespace FunctionalTests.Services.Ordering
10+
{
11+
public class OrderingTestsStartup : Startup
12+
{
13+
public OrderingTestsStartup(IHostingEnvironment env) : base(env)
14+
{
15+
}
16+
17+
protected override void ConfigureAuth(IApplicationBuilder app)
18+
{
19+
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
20+
{
21+
app.UseMiddleware<AutoAuthorizeMiddleware>();
22+
}
23+
else
24+
{
25+
base.ConfigureAuth(app);
26+
}
27+
}
28+
}
29+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;",
3-
"IdentityUrl": "http://localhost:5105"
3+
"IdentityUrl": "http://localhost:5105",
4+
"isTest": "true"
45
}

0 commit comments

Comments
 (0)