From 362f279869a24e6570563ba9f2d35b27f0bce689 Mon Sep 17 00:00:00 2001 From: Viswanatha Swamy Date: Fri, 15 Jan 2021 15:10:18 +0530 Subject: [PATCH 1/2] Small Refactoring inside Basket Service folder --- .../Controllers/BasketController.cs | 4 +-- .../Middlewares/ByPassAuthMiddleware.cs | 2 +- .../Middlewares/FailingMiddleware.cs | 6 ++-- .../Middlewares/FailingOptions.cs | 2 +- .../Events/OrderStartedIntegrationEvent.cs | 2 +- .../ProductPriceChangedIntegrationEvent.cs | 2 +- .../Basket/Basket.API/Model/BasketItem.cs | 2 +- src/Services/Basket/Basket.API/Program.cs | 5 +-- .../Basket.API/Services/IdentityService.cs | 2 +- src/Services/Basket/Basket.API/Startup.cs | 34 +++++++++---------- .../Base/BasketTestStartup.cs | 6 ++-- .../Basket.FunctionalTests/BasketScenarios.cs | 2 +- .../RedisBasketRepositoryTests.cs | 4 +-- 13 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index f51dbdb594..0a3b5879b3 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -46,7 +46,7 @@ public async Task> GetBasketByIdAsync(string id) [HttpPost] [ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)] - public async Task> UpdateBasketAsync([FromBody]CustomerBasket value) + public async Task> UpdateBasketAsync([FromBody] CustomerBasket value) { return Ok(await _repository.UpdateBasketAsync(value)); } @@ -55,7 +55,7 @@ public async Task> UpdateBasketAsync([FromBody]Cust [HttpPost] [ProducesResponseType((int)HttpStatusCode.Accepted)] [ProducesResponseType((int)HttpStatusCode.BadRequest)] - public async Task CheckoutAsync([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId) + public async Task CheckoutAsync([FromBody] BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId) { var userId = _identityService.GetUserIdentity(); diff --git a/src/Services/Basket/Basket.API/Infrastructure/Middlewares/ByPassAuthMiddleware.cs b/src/Services/Basket/Basket.API/Infrastructure/Middlewares/ByPassAuthMiddleware.cs index b3f42c14e4..164e896e71 100644 --- a/src/Services/Basket/Basket.API/Infrastructure/Middlewares/ByPassAuthMiddleware.cs +++ b/src/Services/Basket/Basket.API/Infrastructure/Middlewares/ByPassAuthMiddleware.cs @@ -62,7 +62,7 @@ public async Task Invoke(HttpContext context) new Claim("name", "Test user"), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "Test user"), new Claim("nonce", Guid.NewGuid().ToString()), - new Claim("http://schemas.microsoft.com/identity/claims/identityprovider", "ByPassAuthMiddleware"), + new Claim("http://schemas.microsoft.com/identity/claims/identityprovider", "ByPassAuthMiddleware"), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname","User"), new Claim("sub", currentUserId), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname","Microsoft")} diff --git a/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingMiddleware.cs b/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingMiddleware.cs index 108955bd4e..0019a20491 100644 --- a/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingMiddleware.cs +++ b/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingMiddleware.cs @@ -12,6 +12,7 @@ public class FailingMiddleware private bool _mustFail; private readonly FailingOptions _options; private readonly ILogger _logger; + public FailingMiddleware(RequestDelegate next, ILogger logger, FailingOptions options) { _next = next; @@ -19,6 +20,7 @@ public FailingMiddleware(RequestDelegate next, ILogger logger _mustFail = false; _logger = logger; } + public async Task Invoke(HttpContext context) { var path = context.Request.Path; @@ -44,7 +46,7 @@ public async Task Invoke(HttpContext context) private async Task ProcessConfigRequest(HttpContext context) { var enable = context.Request.Query.Keys.Any(k => k == "enable"); - var disable = context.Request.Query.Keys.Any(k => k == "disable"); + var disable = context.Request.Query.Keys.Any(k => k == "disable"); if (enable && disable) { @@ -86,7 +88,7 @@ private bool MustFail(HttpContext context) } return _mustFail && - (_options.EndpointPaths.Any(x => x == rpath) + (_options.EndpointPaths.Any(x => x == rpath) || _options.EndpointPaths.Count == 0); } } diff --git a/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingOptions.cs b/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingOptions.cs index 63e1e55256..e8a77f1fd6 100644 --- a/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingOptions.cs +++ b/src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingOptions.cs @@ -7,6 +7,6 @@ public class FailingOptions public string ConfigPath = "/Failing"; public List EndpointPaths { get; set; } = new List(); - public List NotFilteredPaths {get; set;} = new List(); + public List NotFilteredPaths { get; set; } = new List(); } } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs b/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs index b78d10c05b..c5c931d853 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs @@ -10,6 +10,6 @@ public class OrderStartedIntegrationEvent : IntegrationEvent public string UserId { get; set; } public OrderStartedIntegrationEvent(string userId) - => UserId = userId; + => UserId = userId; } } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs b/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs index 6f51010be7..a0e3ef3234 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs @@ -6,7 +6,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even // An Event is “something that has happened in the past”, therefore its name has to be // An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems. public class ProductPriceChangedIntegrationEvent : IntegrationEvent - { + { public int ProductId { get; private set; } public decimal NewPrice { get; private set; } diff --git a/src/Services/Basket/Basket.API/Model/BasketItem.cs b/src/Services/Basket/Basket.API/Model/BasketItem.cs index 4d2e4f331a..f781d5a604 100644 --- a/src/Services/Basket/Basket.API/Model/BasketItem.cs +++ b/src/Services/Basket/Basket.API/Model/BasketItem.cs @@ -18,7 +18,7 @@ public IEnumerable Validate(ValidationContext validationContex if (Quantity < 1) { - results.Add(new ValidationResult("Invalid number of units", new []{ "Quantity" })); + results.Add(new ValidationResult("Invalid number of units", new[] { "Quantity" })); } return results; diff --git a/src/Services/Basket/Basket.API/Program.cs b/src/Services/Basket/Basket.API/Program.cs index 1296256aae..1f450cc8ea 100644 --- a/src/Services/Basket/Basket.API/Program.cs +++ b/src/Services/Basket/Basket.API/Program.cs @@ -61,9 +61,10 @@ private static IWebHost BuildWebHost(IConfiguration configuration, string[] args }) .ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) - .UseFailing(options => { + .UseFailing(options => + { options.ConfigPath = "/Failing"; - options.NotFilteredPaths.AddRange(new[] {"/hc","/liveness"}); + options.NotFilteredPaths.AddRange(new[] { "/hc", "/liveness" }); }) .UseStartup() .UseContentRoot(Directory.GetCurrentDirectory()) diff --git a/src/Services/Basket/Basket.API/Services/IdentityService.cs b/src/Services/Basket/Basket.API/Services/IdentityService.cs index d187be8d6c..bc6b2faf44 100644 --- a/src/Services/Basket/Basket.API/Services/IdentityService.cs +++ b/src/Services/Basket/Basket.API/Services/IdentityService.cs @@ -6,7 +6,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Services { public class IdentityService : IIdentityService { - private IHttpContextAccessor _context; + private IHttpContextAccessor _context; public IdentityService(IHttpContextAccessor context) { diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index eb29c12ca5..2ca54988ad 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -211,7 +211,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF ConfigureAuth(app); app.UseStaticFiles(); - + app.UseEndpoints(endpoints => { endpoints.MapGrpcService(); @@ -253,22 +253,22 @@ private void RegisterAppInsights(IServiceCollection services) private void ConfigureAuthService(IServiceCollection services) { - // prevent from mapping "sub" claim to nameidentifier. - JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); - - var identityUrl = Configuration.GetValue("IdentityUrl"); - - services.AddAuthentication(options => - { - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - - }).AddJwtBearer(options => - { - options.Authority = identityUrl; - options.RequireHttpsMetadata = false; - options.Audience = "basket"; - }); + // prevent from mapping "sub" claim to nameidentifier. + JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); + + var identityUrl = Configuration.GetValue("IdentityUrl"); + + services.AddAuthentication(options => + { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + + }).AddJwtBearer(options => + { + options.Authority = identityUrl; + options.RequireHttpsMetadata = false; + options.Audience = "basket"; + }); } protected virtual void ConfigureAuth(IApplicationBuilder app) diff --git a/src/Services/Basket/Basket.FunctionalTests/Base/BasketTestStartup.cs b/src/Services/Basket/Basket.FunctionalTests/Base/BasketTestStartup.cs index cb8ec86418..d0237407d6 100644 --- a/src/Services/Basket/Basket.FunctionalTests/Base/BasketTestStartup.cs +++ b/src/Services/Basket/Basket.FunctionalTests/Base/BasketTestStartup.cs @@ -1,9 +1,9 @@ -using System; -using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Routing; using Microsoft.eShopOnContainers.Services.Basket.API; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System; namespace Basket.FunctionalTests.Base { @@ -20,7 +20,7 @@ public override IServiceProvider ConfigureServices(IServiceCollection services) services.Configure(Configuration); return base.ConfigureServices(services); } - + protected override void ConfigureAuth(IApplicationBuilder app) { if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) diff --git a/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs b/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs index c5eacfa4e7..6928d5cd20 100644 --- a/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs +++ b/src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs @@ -73,7 +73,7 @@ string BuildBasket() string BuildCheckout() { - var checkoutBasket = new + var checkoutBasket = new { City = "city", Street = "street", diff --git a/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs b/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs index 67931c443a..a13c1f6a24 100644 --- a/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs +++ b/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs @@ -33,7 +33,7 @@ public async Task UpdateBasket_return_and_add_basket() Assert.Single(basket.Items); } - + } [Fact] @@ -58,7 +58,7 @@ public async Task Delete_Basket_return_null() Assert.True(deleteResult); Assert.Null(result); - } + } } RedisBasketRepository BuildBasketRepository(ConnectionMultiplexer connMux) From b96df7ae00b36a3a7f6ab78c12e592e2ed1e43c2 Mon Sep 17 00:00:00 2001 From: Viswanatha Swamy Date: Fri, 15 Jan 2021 15:16:11 +0530 Subject: [PATCH 2/2] Small Refactoring --- src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs b/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs index 4fabfbeb4e..91270b5760 100644 --- a/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs +++ b/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs @@ -19,7 +19,7 @@ public class CatalogService : CatalogBase private readonly CatalogContext _catalogContext; private readonly CatalogSettings _settings; private readonly ILogger _logger; - + public CatalogService(CatalogContext dbContext, IOptions settings, ILogger logger) { _settings = settings.Value;