From 146652ca6ad64f793dbd0c6de1c1d8bf521a99dc Mon Sep 17 00:00:00 2001 From: Reuben Bond Date: Fri, 28 Apr 2023 14:49:57 -0700 Subject: [PATCH] BAD MISC - playing with tests --- .../Controllers/BasketController.cs | 2 +- src/Services/Basket/Basket.API/Program.cs | 31 +++++------ .../Base/AutoAuthorizeMiddleware.cs | 1 + .../Base/HttpClientExtensions.cs | 13 ----- .../Basket.FunctionalTests/BasketScenarios.cs | 54 ++++++++++++++----- .../HttpClientExtensions.cs | 11 ---- .../OrderingScenarios.cs | 14 +++-- src/Services/Payment/Payment.API/Program.cs | 24 +-------- .../Application.FunctionalTests.csproj | 2 +- .../Extensions/HttpClientExtensions.cs | 11 ---- .../GlobalUsings.cs | 3 -- .../Services/Ordering/OrderingScenarios.cs | 21 ++++++-- src/Web/WebMVC/Services/BasketService.cs | 2 +- 13 files changed, 88 insertions(+), 101 deletions(-) delete mode 100644 src/Services/Basket/Basket.FunctionalTests/Base/HttpClientExtensions.cs delete mode 100644 src/Services/Ordering/Ordering.FunctionalTests/HttpClientExtensions.cs delete mode 100644 src/Tests/Services/Application.FunctionalTests/Extensions/HttpClientExtensions.cs diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index 5468bbc15..302fc0768 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -1,7 +1,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers; [Route("api/v1/[controller]")] -[Authorize] +//[Authorize] [ApiController] public class BasketController : ControllerBase { diff --git a/src/Services/Basket/Basket.API/Program.cs b/src/Services/Basket/Basket.API/Program.cs index 9554841ec..8f98a3e04 100644 --- a/src/Services/Basket/Basket.API/Program.cs +++ b/src/Services/Basket/Basket.API/Program.cs @@ -1,9 +1,4 @@ -using Autofac.Core; -using Microsoft.Azure.Amqp.Framing; -using Microsoft.Extensions.Configuration; - -var appName = "Basket.API"; -var builder = WebApplication.CreateBuilder(new WebApplicationOptions +var builder = WebApplication.CreateBuilder(new WebApplicationOptions { Args = args, ApplicationName = typeof(Program).Assembly.FullName, @@ -63,6 +58,7 @@ JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); var identityUrl = builder.Configuration.GetValue("IdentityUrl"); +/* builder.Services.AddAuthentication("Bearer").AddJwtBearer(options => { options.Authority = identityUrl; @@ -78,6 +74,7 @@ builder.Services.AddAuthorization(options => policy.RequireClaim("scope", "basket"); }); }); +*/ builder.Services.AddCustomHealthCheck(builder.Configuration); @@ -164,7 +161,6 @@ builder.WebHost.UseKestrel(options => }); }); -builder.WebHost.CaptureStartupErrors(false); builder.Host.UseSerilog(CreateSerilogLogger(builder.Configuration)); builder.WebHost.UseFailing(options => { @@ -172,6 +168,7 @@ builder.WebHost.UseFailing(options => options.NotFilteredPaths.AddRange(new[] { "/hc", "/liveness" }); }); var app = builder.Build(); +app.MapGet("hello", () => "hello"); if (app.Environment.IsDevelopment()) { @@ -196,13 +193,19 @@ app.UseSwagger() setup.OAuthAppName("Basket Swagger UI"); }); +app.Use(del => ctx => +{ + ctx.Response.StatusCode = 200; + ctx.Response.WriteAsync("hello"); + return Task.CompletedTask; + //return del(ctx); +}); app.UseRouting(); app.UseCors("CorsPolicy"); -app.UseAuthentication(); -app.UseAuthorization(); +//app.UseAuthentication(); +//app.UseAuthorization(); app.UseStaticFiles(); - app.MapGrpcService(); app.MapDefaultControllerRoute(); app.MapControllers(); @@ -271,6 +274,7 @@ Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) var port = config.GetValue("PORT", 80); return (port, grpcPort); } + void ConfigureEventBus(IApplicationBuilder app) { var eventBus = app.ApplicationServices.GetRequiredService(); @@ -278,18 +282,15 @@ void ConfigureEventBus(IApplicationBuilder app) eventBus.Subscribe(); eventBus.Subscribe(); } + public partial class Program { - - public static string Namespace = typeof(Program).Assembly.GetName().Name; + private static string Namespace = typeof(Program).Assembly.GetName().Name; public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); } - public static class CustomExtensionMethods { - - public static IServiceCollection RegisterEventBus(this IServiceCollection services, IConfiguration configuration) { if (configuration.GetValue("AzureServiceBusEnabled")) diff --git a/src/Services/Basket/Basket.FunctionalTests/Base/AutoAuthorizeMiddleware.cs b/src/Services/Basket/Basket.FunctionalTests/Base/AutoAuthorizeMiddleware.cs index 6343dbe68..551e3069e 100644 --- a/src/Services/Basket/Basket.FunctionalTests/Base/AutoAuthorizeMiddleware.cs +++ b/src/Services/Basket/Basket.FunctionalTests/Base/AutoAuthorizeMiddleware.cs @@ -18,6 +18,7 @@ class AutoAuthorizeMiddleware identity.AddClaim(new Claim("sub", IDENTITY_ID)); identity.AddClaim(new Claim("unique_name", IDENTITY_ID)); identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID)); + identity.AddClaim(new Claim("scope", "basket")); httpContext.User.AddIdentity(identity); diff --git a/src/Services/Basket/Basket.FunctionalTests/Base/HttpClientExtensions.cs b/src/Services/Basket/Basket.FunctionalTests/Base/HttpClientExtensions.cs deleted file mode 100644 index 45910df14..000000000 --- a/src/Services/Basket/Basket.FunctionalTests/Base/HttpClientExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Basket.FunctionalTests.Base; - -static class HttpClientExtensions -{ - public static HttpClient CreateIdempotentClient(this TestServer server) - { - var client = server.CreateClient(); - - client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString()); - - return client; - } -} diff --git a/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs b/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs index f727b999e..cff801e15 100644 --- a/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs +++ b/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs @@ -1,15 +1,43 @@ -namespace Basket.FunctionalTests; +using System.Linq; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.Hosting; -public class BasketScenarios - : BasketScenarioBase +namespace Basket.FunctionalTests; + +public class TestWebApplicationFactory : WebApplicationFactory where TProgram : class +{ + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + base.ConfigureWebHost(builder); + builder.Configure(app => + { + app.UseMiddleware(); + }); + } + + protected override IHost CreateHost(IHostBuilder builder) + { + return base.CreateHost(builder); + } +} + +public class BasketScenarios : BasketScenarioBase, IClassFixture> { + private readonly TestWebApplicationFactory _factory; + private readonly HttpClient _httpClient; + + public BasketScenarios(TestWebApplicationFactory factory) + { + _factory = factory; + _httpClient = _factory.CreateClient(); + } + [Fact] public async Task Post_basket_and_response_ok_status_code() { - using var server = CreateServer(); var content = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); - var response = await server.CreateClient() - .PostAsync(Post.Basket, content); + var uri = "/api/v1/basket/"; + var response = await _httpClient.PostAsync(uri, content); response.EnsureSuccessStatusCode(); } @@ -17,25 +45,25 @@ public class BasketScenarios [Fact] public async Task Get_basket_and_response_ok_status_code() { - using var server = CreateServer(); - var response = await server.CreateClient() + var response = await _httpClient .GetAsync(Get.GetBasket(1)); - response.EnsureSuccessStatusCode(); } [Fact] public async Task Send_Checkout_basket_and_response_ok_status_code() { - using var server = CreateServer(); var contentBasket = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); - await server.CreateClient() + await _httpClient .PostAsync(Post.Basket, contentBasket); - var contentCheckout = new StringContent(BuildCheckout(), UTF8Encoding.UTF8, "application/json"); + var contentCheckout = new StringContent(BuildCheckout(), UTF8Encoding.UTF8, "application/json") + { + Headers = { { "x-requestid", Guid.NewGuid().ToString() } } + }; - var response = await server.CreateIdempotentClient() + var response = await _httpClient .PostAsync(Post.CheckoutOrder, contentCheckout); response.EnsureSuccessStatusCode(); diff --git a/src/Services/Ordering/Ordering.FunctionalTests/HttpClientExtensions.cs b/src/Services/Ordering/Ordering.FunctionalTests/HttpClientExtensions.cs deleted file mode 100644 index a4a0dd9c2..000000000 --- a/src/Services/Ordering/Ordering.FunctionalTests/HttpClientExtensions.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ordering.FunctionalTests; - -static class HttpClientExtensions -{ - public static HttpClient CreateIdempotentClient(this TestServer server) - { - var client = server.CreateClient(); - client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString()); - return client; - } -} diff --git a/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarios.cs b/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarios.cs index 6c7ed17a9..252cc02ff 100644 --- a/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarios.cs +++ b/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarios.cs @@ -23,8 +23,11 @@ namespace Ordering.FunctionalTests public async Task Cancel_order_no_order_created_bad_request_response() { using var server = CreateServer(); - var content = new StringContent(BuildOrder(), UTF8Encoding.UTF8, "application/json"); - var response = await server.CreateIdempotentClient() + var content = new StringContent(BuildOrder(), UTF8Encoding.UTF8, "application/json") + { + Headers = { { "x-requestid", Guid.NewGuid().ToString() } } + }; + var response = await server.CreateClient() .PutAsync(Put.CancelOrder, content); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); @@ -34,8 +37,11 @@ namespace Ordering.FunctionalTests public async Task Ship_order_no_order_created_bad_request_response() { using var server = CreateServer(); - var content = new StringContent(BuildOrder(), UTF8Encoding.UTF8, "application/json"); - var response = await server.CreateIdempotentClient() + var content = new StringContent(BuildOrder(), UTF8Encoding.UTF8, "application/json") + { + Headers = { { "x-requestid", Guid.NewGuid().ToString() } } + }; + var response = await server.CreateClient() .PutAsync(Put.ShipOrder, content); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); diff --git a/src/Services/Payment/Payment.API/Program.cs b/src/Services/Payment/Payment.API/Program.cs index acfc73217..e6f92f4e3 100644 --- a/src/Services/Payment/Payment.API/Program.cs +++ b/src/Services/Payment/Payment.API/Program.cs @@ -1,5 +1,4 @@ -var appName = "Payment.API"; -var builder = WebApplication.CreateBuilder(new WebApplicationOptions +var builder = WebApplication.CreateBuilder(new WebApplicationOptions { Args = args, ApplicationName = typeof(Program).Assembly.FullName, @@ -165,27 +164,6 @@ Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) .CreateLogger(); } -IConfiguration GetConfiguration() -{ - var builder = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddEnvironmentVariables(); - - var config = builder.Build(); - - if (config.GetValue("UseVault", false)) - { - TokenCredential credential = new ClientSecretCredential( - config["Vault:TenantId"], - config["Vault:ClientId"], - config["Vault:ClientSecret"]); - builder.AddAzureKeyVault(new Uri($"https://{config["Vault:Name"]}.vault.azure.net/"), credential); - } - - return builder.Build(); -} - public partial class Program { public static string Namespace = typeof(Program).Assembly.GetName().Name; diff --git a/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj b/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj index 3825af1b5..8ea12ff3c 100644 --- a/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj +++ b/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj @@ -41,7 +41,7 @@ - + all runtime; build; native; contentfiles; analyzers diff --git a/src/Tests/Services/Application.FunctionalTests/Extensions/HttpClientExtensions.cs b/src/Tests/Services/Application.FunctionalTests/Extensions/HttpClientExtensions.cs deleted file mode 100644 index b17edce91..000000000 --- a/src/Tests/Services/Application.FunctionalTests/Extensions/HttpClientExtensions.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace FunctionalTests.Extensions; - -static class HttpClientExtensions -{ - public static HttpClient CreateIdempotentClient(this TestServer server) - { - var client = server.CreateClient(); - client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString()); - return client; - } -} diff --git a/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs b/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs index dd46c8aca..b09d08dd0 100644 --- a/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs +++ b/src/Tests/Services/Application.FunctionalTests/GlobalUsings.cs @@ -8,14 +8,11 @@ global using Microsoft.AspNetCore.Hosting; global using Microsoft.Extensions.Configuration; global using System.IO; global using System.Reflection; -global using FunctionalTests.Middleware; -global using Microsoft.AspNetCore.Builder; global using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; global using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Options; -global using FunctionalTests.Extensions; global using FunctionalTests.Services.Basket; global using Microsoft.eShopOnContainers.Services.Basket.API.Model; global using Microsoft.eShopOnContainers.WebMVC.ViewModels; diff --git a/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenarios.cs b/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenarios.cs index 46291b81a..35ee35f1a 100644 --- a/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenarios.cs +++ b/src/Tests/Services/Application.FunctionalTests/Services/Ordering/OrderingScenarios.cs @@ -11,21 +11,32 @@ public class OrderingScenarios : OrderingScenariosBase var cityExpected = $"city-{Guid.NewGuid()}"; var orderStatusExpected = "cancelled"; - var basketClient = basketServer.CreateIdempotentClient(); - var orderClient = orderServer.CreateIdempotentClient(); + var basketClient = basketServer.CreateClient(); + var orderClient = orderServer.CreateClient(); // GIVEN a basket is created - var contentBasket = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); + var contentBasket = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json") + { + Headers = { { "x-requestid", Guid.NewGuid().ToString() } } + }; await basketClient.PostAsync(BasketScenariosBase.Post.CreateBasket, contentBasket); // AND basket checkout is sent - await basketClient.PostAsync(BasketScenariosBase.Post.CheckoutOrder, new StringContent(BuildCheckout(cityExpected), UTF8Encoding.UTF8, "application/json")); + await basketClient.PostAsync( + BasketScenariosBase.Post.CheckoutOrder, + new StringContent(BuildCheckout(cityExpected), UTF8Encoding.UTF8, "application/json") + { + Headers = { { "x-requestid", Guid.NewGuid().ToString() } } + }); // WHEN Order is created in Ordering.api var newOrder = await TryGetNewOrderCreated(cityExpected, orderClient); // AND Order is cancelled in Ordering.api - await orderClient.PutAsync(OrderingScenariosBase.Put.CancelOrder, new StringContent(BuildCancelOrder(newOrder.OrderNumber), UTF8Encoding.UTF8, "application/json")); + await orderClient.PutAsync(OrderingScenariosBase.Put.CancelOrder, new StringContent(BuildCancelOrder(newOrder.OrderNumber), UTF8Encoding.UTF8, "application/json") + { + Headers = { { "x-requestid", Guid.NewGuid().ToString() } } + }); // AND the requested order is retrieved var order = await TryGetOrder(newOrder.OrderNumber, orderClient); diff --git a/src/Web/WebMVC/Services/BasketService.cs b/src/Web/WebMVC/Services/BasketService.cs index 4833b3408..952cdb187 100644 --- a/src/Web/WebMVC/Services/BasketService.cs +++ b/src/Web/WebMVC/Services/BasketService.cs @@ -53,7 +53,7 @@ public class BasketService : IBasketService var uri = API.Basket.CheckoutBasket(_basketByPassUrl); var basketContent = new StringContent(JsonSerializer.Serialize(basket), Encoding.UTF8, "application/json"); - _logger.LogInformation("Uri chechout {uri}", uri); + _logger.LogInformation("Uri checkout {uri}", uri); var response = await _apiClient.PostAsync(uri, basketContent);