Tôi có API sử dụng IdentityServer4 để xác thực mã thông báo. Tôi muốn đơn vị kiểm tra API này với một TestServer trong bộ nhớ. Tôi muốn lưu trữ IdentityServer trong TestServer trong bộ nhớ.Thử nghiệm tích hợp với IdentityServer
Tôi đã quản lý để tạo mã thông báo từ IdentityServer.
Đây là cách xa tôi đã đến, nhưng tôi nhận được một lỗi "Không thể để có được cấu hình từ http://localhost:54100/.well-known/openid-configuration"
Việc sử dụng Api [Duyệt] -attribute với các chính sách khác nhau. Đây là những gì tôi muốn thử nghiệm.
Điều này có thể được thực hiện hay không và tôi đang làm gì sai? Tôi đã cố gắng xem xét mã nguồn cho IdentityServer4, nhưng đã không đi qua một kịch bản thử nghiệm tích hợp tương tự.
protected IntegrationTestBase()
{
var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
_contentRoot = SolutionPathUtility.GetProjectPath(@"<my project path>", startupAssembly);
Configure(_contentRoot);
var orderApiServerBuilder = new WebHostBuilder()
.UseContentRoot(_contentRoot)
.ConfigureServices(InitializeServices)
.UseStartup<Startup>();
orderApiServerBuilder.Configure(ConfigureApp);
OrderApiTestServer = new TestServer(orderApiServerBuilder);
HttpClient = OrderApiTestServer.CreateClient();
}
private void InitializeServices(IServiceCollection services)
{
var cert = new X509Certificate2(Path.Combine(_contentRoot, "idsvr3test.pfx"), "idsrv3test");
services.AddIdentityServer(options =>
{
options.IssuerUri = "http://localhost:54100";
})
.AddInMemoryClients(Clients.Get())
.AddInMemoryScopes(Scopes.Get())
.AddInMemoryUsers(Users.Get())
.SetSigningCredential(cert);
services.AddAuthorization(options =>
{
options.AddPolicy(OrderApiConstants.StoreIdPolicyName, policy => policy.Requirements.Add(new StoreIdRequirement("storeId")));
});
services.AddSingleton<IPersistedGrantStore, InMemoryPersistedGrantStore>();
services.AddSingleton(_orderManagerMock.Object);
services.AddMvc();
}
private void ConfigureApp(IApplicationBuilder app)
{
app.UseIdentityServer();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
var options = new IdentityServerAuthenticationOptions
{
Authority = _appsettings.IdentityServerAddress,
RequireHttpsMetadata = false,
ScopeName = _appsettings.IdentityServerScopeName,
AutomaticAuthenticate = false
};
app.UseIdentityServerAuthentication(options);
app.UseMvc();
}
Và trong đơn vị thử nghiệm của tôi:
private HttpMessageHandler _handler;
const string TokenEndpoint = "http://localhost/connect/token";
public Test()
{
_handler = OrderApiTestServer.CreateHandler();
}
[Fact]
public async Task LeTest()
{
var accessToken = await GetToken();
HttpClient.SetBearerToken(accessToken);
var httpResponseMessage = await HttpClient.GetAsync("stores/11/orders/asdf"); // Fails on this line
}
private async Task<string> GetToken()
{
var client = new TokenClient(TokenEndpoint, "client", "secret", innerHttpMessageHandler: _handler);
var response = await client.RequestClientCredentialsAsync("TheMOON.OrderApi");
return response.AccessToken;
}
Thanks a lot @Lutando. Câu trả lời đầu tiên của bạn đã chỉ cho tôi đúng hướng. –
ah ok @emedbo Tôi nghĩ rằng nó có thể là một chút nhiều để làm cho kiểm tra giả gấp đôi. Nhưng nó hoạt động :) – Lutando
Vâng, dự án giả mạo bao gồm khoảng 20 tập tin, do đó, đó là một trách nhiệm pháp lý. Mặc dù sự lộn ngược là khá gọn gàng! Tôi chắc chắn ai đó có nhiều kiến thức hơn tôi có thể làm cho nó ít phức tạp hơn. –