トークン検証にIdentityServer4を使用するAPIがあります。 私は、このAPIをメモリ内TestServerでユニットテストしたいと思っています。私はメモリ内のTestServerにIdentityServerをホストしたいと思います。インメモリIdentityServerによる統合テスト
私はIdentityServerからトークンを作成することができました。
これは、私が来てどのくらいですが、私は異なるポリシーと
のAPIの使用[承認] -attributeエラー「http://localhost:54100/.well-known/openid-configurationから設定を取得できません」を取得。これは私がテストしたいものです。
これを行うことができますか、間違っていますか? 私はIdentityServer4のソースコードを見ようとしましたが、同様の統合テストのシナリオには触れていません。
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();
}
そして、私のユニットテストで:
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;
}
おかげでたくさんの@Lutando:私のユニットテストプロジェクトにTestApi BackChannelHandlerにAuthServer.Handlerの割り当て
。あなたの最初の答えは正しい方向に私を指摘した。 –
ah ok @emedbo私は、偽のテストを二重にするのはちょっとかもしれないと思っていました。しかし、それは動作します:) – Lutando
ええ、偽のプロジェクトは、約20ファイルで構成されているので、責任があります。逆さまですがかなりきれいです!私より多くの知識を持つ人が、それほど複雑ではないと確信しています。 –