|
7 | 7 | using BuildingBlocks.Mongo;
|
8 | 8 | using BuildingBlocks.PersistMessageProcessor;
|
9 | 9 | using BuildingBlocks.Web;
|
| 10 | +using Duende.IdentityServer.EntityFramework.Entities; |
10 | 11 | using EasyNetQ.Management.Client;
|
11 | 12 | using Grpc.Net.Client;
|
12 | 13 | using MassTransit;
|
13 | 14 | using MassTransit.Testing;
|
14 | 15 | using MediatR;
|
| 16 | +using Microsoft.AspNetCore.Authorization; |
15 | 17 | using Microsoft.AspNetCore.Hosting;
|
16 | 18 | using Microsoft.AspNetCore.Http;
|
17 | 19 | using Microsoft.AspNetCore.Mvc.Testing;
|
@@ -57,16 +59,15 @@ public HttpClient HttpClient
|
57 | 59 | {
|
58 | 60 | get
|
59 | 61 | {
|
60 |
| - var claims = |
61 |
| - new Dictionary<string, object> |
62 |
| - { |
63 |
| - {ClaimTypes.Name, "[email protected]"}, |
64 |
| - {ClaimTypes.Role, "admin"}, |
65 |
| - {"scope", "flight-api"} |
66 |
| - }; |
| 62 | + var claims = new Dictionary<string, object> |
| 63 | + { |
| 64 | + { ClaimTypes.Name, "[email protected]" }, |
| 65 | + { ClaimTypes.Role, "admin" }, |
| 66 | + { "scope", "flight-api" } |
| 67 | + }; |
67 | 68 |
|
68 |
| - var httpClient = _factory?.CreateClient(); |
69 |
| - httpClient.SetFakeBearerToken(claims); |
| 69 | + var httpClient = _factory.CreateClient(); |
| 70 | + httpClient.SetFakeBearerToken(claims); // Uses FakeJwtBearer |
70 | 71 | return httpClient;
|
71 | 72 | }
|
72 | 73 | }
|
@@ -106,19 +107,28 @@ protected TestFixture()
|
106 | 107 | .AsImplementedInterfaces()
|
107 | 108 | .WithScopedLifetime());
|
108 | 109 |
|
109 |
| - // add authentication using a fake jwt bearer - we can use SetAdminUser method to set authenticate user to existing HttContextAccessor |
| 110 | + // Add Fake JWT Authentication - we can use SetAdminUser method to set authenticate user to existing HttContextAccessor |
110 | 111 | // https://github.com/webmotions/fake-authentication-jwtbearer
|
111 | 112 | // https://github.com/webmotions/fake-authentication-jwtbearer/issues/14
|
112 | 113 | services.AddAuthentication(
|
113 | 114 | options =>
|
114 | 115 | {
|
115 |
| - options.DefaultAuthenticateScheme = |
116 |
| - FakeJwtBearerDefaults.AuthenticationScheme; |
| 116 | + options.DefaultAuthenticateScheme = FakeJwtBearerDefaults.AuthenticationScheme; |
117 | 117 |
|
118 |
| - options.DefaultChallengeScheme = |
119 |
| - FakeJwtBearerDefaults.AuthenticationScheme; |
| 118 | + options.DefaultChallengeScheme = FakeJwtBearerDefaults.AuthenticationScheme; |
120 | 119 | })
|
121 | 120 | .AddFakeJwtBearer();
|
| 121 | + |
| 122 | + // Mock Authorization Policies |
| 123 | + services.AddAuthorization(options => |
| 124 | + { |
| 125 | + options.AddPolicy(nameof(ApiScope), policy => |
| 126 | + { |
| 127 | + policy.AddAuthenticationSchemes(FakeJwtBearerDefaults.AuthenticationScheme); |
| 128 | + policy.RequireAuthenticatedUser(); |
| 129 | + policy.RequireClaim("scope", "flight-api"); // Test-specific scope |
| 130 | + }); |
| 131 | + }); |
122 | 132 | });
|
123 | 133 | });
|
124 | 134 | }
|
|
0 commit comments