Configure so tests work again

This commit is contained in:
David Fowler 2023-05-02 22:02:05 -07:00
parent 0cbaec81c1
commit 512623e45d
4 changed files with 41 additions and 17 deletions

View File

@ -16,19 +16,30 @@
"Protocols": "Http2" "Protocols": "Http2"
} }
}, },
"OpenApi": {
"Endpoint": {
"Name": ""
},
"Document": {
"Name": "Basket API",
"Description": "Basket API",
"Title": "Basket API",
"Version": "v1"
}
},
"ConnectionStrings": { "ConnectionStrings": {
"Redis": "127.0.0.1" "Redis": "127.0.0.1"
}, },
"Identity": { "Identity": {
"Url": "", "Audience": "basket",
"Auidence": "basket", "Scope": "basket",
"Scope": "basket" "Scopes": {
"basket": "Basket API"
}
}, },
"EventBus": { "EventBus": {
"SubscriptionClientName": "Basket", "SubscriptionClientName": "Basket",
"ConnectionString": "your-event-bus-connection-string", "ConnectionString": "localhost",
"UserName": "your-event-bus-username",
"Password": "your-event-bus-password",
"RetryCount": 5 "RetryCount": 5
}, },
"ApplicationInsights": { "ApplicationInsights": {

View File

@ -10,7 +10,6 @@ public class BasketScenarios :
var content = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); var content = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json");
var uri = "/api/v1/basket/"; var uri = "/api/v1/basket/";
var response = await server.CreateClient().PostAsync(uri, content); var response = await server.CreateClient().PostAsync(uri, content);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
} }

View File

@ -9,8 +9,10 @@
"Microsoft": "Information" "Microsoft": "Information"
} }
}, },
"IdentityUrl": "http://localhost:5105", "Identity": {
"IdentityUrlExternal": "http://localhost:5105", "ExternalUrl": "http://localhost:5105",
"Url": "http://localhost:5105"
},
"ConnectionStrings": { "ConnectionStrings": {
"Redis": "127.0.0.1" "Redis": "127.0.0.1"
}, },

View File

@ -96,14 +96,18 @@ public static class CommonExtensions
var pathBase = configuration["PATH_BASE"]; var pathBase = configuration["PATH_BASE"];
var openApiSection = configuration.GetRequiredSection("OpenApi"); var openApiSection = configuration.GetRequiredSection("OpenApi");
var authSection = openApiSection.GetRequiredSection("Auth"); var authSection = openApiSection.GetSection("Auth");
var endpointSection = openApiSection.GetRequiredSection("Endpoint"); var endpointSection = openApiSection.GetRequiredSection("Endpoint");
var swaggerUrl = endpointSection["Url"] ?? $"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json"; var swaggerUrl = endpointSection["Url"] ?? $"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json";
setup.SwaggerEndpoint(swaggerUrl, endpointSection.GetRequiredValue("Name")); setup.SwaggerEndpoint(swaggerUrl, endpointSection.GetRequiredValue("Name"));
setup.OAuthClientId(authSection.GetRequiredValue("ClientId"));
setup.OAuthAppName(authSection.GetRequiredValue("AppName")); if (authSection.Exists())
{
setup.OAuthClientId(authSection.GetRequiredValue("ClientId"));
setup.OAuthAppName(authSection.GetRequiredValue("AppName"));
}
}); });
return app; return app;
@ -136,7 +140,7 @@ public static class CommonExtensions
var identitySection = configuration.GetSection("Identity"); var identitySection = configuration.GetSection("Identity");
if (identitySection is null) if (!identitySection.Exists())
{ {
// No identity section, so no authentication open api definition // No identity section, so no authentication open api definition
return; return;
@ -152,7 +156,7 @@ public static class CommonExtensions
// } // }
var identityUrlExternal = identitySection.GetRequiredValue("ExternalUrl"); var identityUrlExternal = identitySection.GetRequiredValue("ExternalUrl");
var scopes = openApi.GetRequiredSection("Scopes").AsEnumerable().ToDictionary(p => p.Key, p => p.Value); var scopes = identitySection.GetRequiredSection("Scopes").AsEnumerable().ToDictionary(p => p.Key, p => p.Value);
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{ {
@ -183,7 +187,7 @@ public static class CommonExtensions
var identitySection = configuration.GetSection("Identity"); var identitySection = configuration.GetSection("Identity");
if (identitySection is null) if (!identitySection.Exists())
{ {
// No identity section, so no authentication // No identity section, so no authentication
return services; return services;
@ -266,6 +270,14 @@ public static class CommonExtensions
public static IServiceCollection AddApplicationInsights(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddApplicationInsights(this IServiceCollection services, IConfiguration configuration)
{ {
var appInsightsSection = configuration.GetSection("ApplicationInsights");
// No instrumentation key, so no application insights
if (string.IsNullOrEmpty(appInsightsSection["InstrumentationKey"]))
{
return services;
}
services.AddApplicationInsightsTelemetry(configuration); services.AddApplicationInsightsTelemetry(configuration);
services.AddApplicationInsightsKubernetesEnricher(); services.AddApplicationInsightsKubernetesEnricher();
return services; return services;
@ -288,7 +300,7 @@ public static class CommonExtensions
var eventBusSection = configuration.GetRequiredSection("EventBus"); var eventBusSection = configuration.GetRequiredSection("EventBus");
var eventBusConnectionString = eventBusSection.GetRequiredValue("ConnectionString"); var eventBusConnectionString = eventBusSection.GetRequiredValue("ConnectionString");
return eventBusSection.GetRequiredValue("ProviderName").ToLowerInvariant() switch return eventBusSection["ProviderName"]?.ToLowerInvariant() switch
{ {
"servicebus" => hcBuilder.AddAzureServiceBusTopic( "servicebus" => hcBuilder.AddAzureServiceBusTopic(
eventBusConnectionString, eventBusConnectionString,
@ -438,5 +450,5 @@ public static class CommonExtensions
} }
private static string GetRequiredValue(this IConfiguration configuration, string name) => private static string GetRequiredValue(this IConfiguration configuration, string name) =>
configuration[name] ?? throw new InvalidOperationException($"Configuration missing value for: {(configuration is IConfigurationSection s ? s.Key + ":" + name : name)}"); configuration[name] ?? throw new InvalidOperationException($"Configuration missing value for: {(configuration is IConfigurationSection s ? s.Path + ":" + name : name)}");
} }