Browse Source

Swamy/15jan2021 small refactoring (#1580)

* Small Refactoring inside Basket Service folder

* Small Refactoring
pull/1583/head
Viswanatha Swamy 4 years ago
committed by GitHub
parent
commit
130e46ccdf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 39 additions and 36 deletions
  1. +2
    -2
      src/Services/Basket/Basket.API/Controllers/BasketController.cs
  2. +1
    -1
      src/Services/Basket/Basket.API/Infrastructure/Middlewares/ByPassAuthMiddleware.cs
  3. +4
    -2
      src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingMiddleware.cs
  4. +1
    -1
      src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingOptions.cs
  5. +1
    -1
      src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs
  6. +1
    -1
      src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs
  7. +1
    -1
      src/Services/Basket/Basket.API/Model/BasketItem.cs
  8. +3
    -2
      src/Services/Basket/Basket.API/Program.cs
  9. +1
    -1
      src/Services/Basket/Basket.API/Services/IdentityService.cs
  10. +17
    -17
      src/Services/Basket/Basket.API/Startup.cs
  11. +3
    -3
      src/Services/Basket/Basket.FunctionalTests/Base/BasketTestStartup.cs
  12. +1
    -1
      src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs
  13. +2
    -2
      src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs
  14. +1
    -1
      src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs

+ 2
- 2
src/Services/Basket/Basket.API/Controllers/BasketController.cs View File

@ -46,7 +46,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
[HttpPost] [HttpPost]
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)] [ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
public async Task<ActionResult<CustomerBasket>> UpdateBasketAsync([FromBody]CustomerBasket value)
public async Task<ActionResult<CustomerBasket>> UpdateBasketAsync([FromBody] CustomerBasket value)
{ {
return Ok(await _repository.UpdateBasketAsync(value)); return Ok(await _repository.UpdateBasketAsync(value));
} }
@ -55,7 +55,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
[HttpPost] [HttpPost]
[ProducesResponseType((int)HttpStatusCode.Accepted)] [ProducesResponseType((int)HttpStatusCode.Accepted)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)] [ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task<ActionResult> CheckoutAsync([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId)
public async Task<ActionResult> CheckoutAsync([FromBody] BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId)
{ {
var userId = _identityService.GetUserIdentity(); var userId = _identityService.GetUserIdentity();


+ 1
- 1
src/Services/Basket/Basket.API/Infrastructure/Middlewares/ByPassAuthMiddleware.cs View File

@ -62,7 +62,7 @@ namespace Basket.API.Infrastructure.Middlewares
new Claim("name", "Test user"), new Claim("name", "Test user"),
new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/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("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("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname","User"),
new Claim("sub", currentUserId), new Claim("sub", currentUserId),
new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname","Microsoft")} new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname","Microsoft")}


+ 4
- 2
src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingMiddleware.cs View File

@ -12,6 +12,7 @@ namespace Basket.API.Infrastructure.Middlewares
private bool _mustFail; private bool _mustFail;
private readonly FailingOptions _options; private readonly FailingOptions _options;
private readonly ILogger _logger; private readonly ILogger _logger;
public FailingMiddleware(RequestDelegate next, ILogger<FailingMiddleware> logger, FailingOptions options) public FailingMiddleware(RequestDelegate next, ILogger<FailingMiddleware> logger, FailingOptions options)
{ {
_next = next; _next = next;
@ -19,6 +20,7 @@ namespace Basket.API.Infrastructure.Middlewares
_mustFail = false; _mustFail = false;
_logger = logger; _logger = logger;
} }
public async Task Invoke(HttpContext context) public async Task Invoke(HttpContext context)
{ {
var path = context.Request.Path; var path = context.Request.Path;
@ -44,7 +46,7 @@ namespace Basket.API.Infrastructure.Middlewares
private async Task ProcessConfigRequest(HttpContext context) private async Task ProcessConfigRequest(HttpContext context)
{ {
var enable = context.Request.Query.Keys.Any(k => k == "enable"); 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) if (enable && disable)
{ {
@ -86,7 +88,7 @@ namespace Basket.API.Infrastructure.Middlewares
} }
return _mustFail && return _mustFail &&
(_options.EndpointPaths.Any(x => x == rpath)
(_options.EndpointPaths.Any(x => x == rpath)
|| _options.EndpointPaths.Count == 0); || _options.EndpointPaths.Count == 0);
} }
} }


+ 1
- 1
src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingOptions.cs View File

@ -7,6 +7,6 @@ namespace Basket.API.Infrastructure.Middlewares
public string ConfigPath = "/Failing"; public string ConfigPath = "/Failing";
public List<string> EndpointPaths { get; set; } = new List<string>(); public List<string> EndpointPaths { get; set; } = new List<string>();
public List<string> NotFilteredPaths {get; set;} = new List<string>();
public List<string> NotFilteredPaths { get; set; } = new List<string>();
} }
} }

+ 1
- 1
src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs View File

@ -10,6 +10,6 @@ namespace Basket.API.IntegrationEvents.Events
public string UserId { get; set; } public string UserId { get; set; }
public OrderStartedIntegrationEvent(string userId) public OrderStartedIntegrationEvent(string userId)
=> UserId = userId;
=> UserId = userId;
} }
} }

+ 1
- 1
src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs View File

@ -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 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. // An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
public class ProductPriceChangedIntegrationEvent : IntegrationEvent public class ProductPriceChangedIntegrationEvent : IntegrationEvent
{
{
public int ProductId { get; private set; } public int ProductId { get; private set; }
public decimal NewPrice { get; private set; } public decimal NewPrice { get; private set; }


+ 1
- 1
src/Services/Basket/Basket.API/Model/BasketItem.cs View File

@ -18,7 +18,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
if (Quantity < 1) 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; return results;


+ 3
- 2
src/Services/Basket/Basket.API/Program.cs View File

@ -61,9 +61,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
}) })
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) .ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
.UseFailing(options => {
.UseFailing(options =>
{
options.ConfigPath = "/Failing"; options.ConfigPath = "/Failing";
options.NotFilteredPaths.AddRange(new[] {"/hc","/liveness"});
options.NotFilteredPaths.AddRange(new[] { "/hc", "/liveness" });
}) })
.UseStartup<Startup>() .UseStartup<Startup>()
.UseContentRoot(Directory.GetCurrentDirectory()) .UseContentRoot(Directory.GetCurrentDirectory())


+ 1
- 1
src/Services/Basket/Basket.API/Services/IdentityService.cs View File

@ -6,7 +6,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Services
{ {
public class IdentityService : IIdentityService public class IdentityService : IIdentityService
{ {
private IHttpContextAccessor _context;
private IHttpContextAccessor _context;
public IdentityService(IHttpContextAccessor context) public IdentityService(IHttpContextAccessor context)
{ {


+ 17
- 17
src/Services/Basket/Basket.API/Startup.cs View File

@ -211,7 +211,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
ConfigureAuth(app); ConfigureAuth(app);
app.UseStaticFiles(); app.UseStaticFiles();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapGrpcService<BasketService>(); endpoints.MapGrpcService<BasketService>();
@ -253,22 +253,22 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
private void ConfigureAuthService(IServiceCollection services) private void ConfigureAuthService(IServiceCollection services)
{ {
// prevent from mapping "sub" claim to nameidentifier.
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
var identityUrl = Configuration.GetValue<string>("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<string>("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) protected virtual void ConfigureAuth(IApplicationBuilder app)


+ 3
- 3
src/Services/Basket/Basket.FunctionalTests/Base/BasketTestStartup.cs View File

@ -1,9 +1,9 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.eShopOnContainers.Services.Basket.API; using Microsoft.eShopOnContainers.Services.Basket.API;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System;
namespace Basket.FunctionalTests.Base namespace Basket.FunctionalTests.Base
{ {
@ -20,7 +20,7 @@ namespace Basket.FunctionalTests.Base
services.Configure<RouteOptions>(Configuration); services.Configure<RouteOptions>(Configuration);
return base.ConfigureServices(services); return base.ConfigureServices(services);
} }
protected override void ConfigureAuth(IApplicationBuilder app) protected override void ConfigureAuth(IApplicationBuilder app)
{ {
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())


+ 1
- 1
src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs View File

@ -73,7 +73,7 @@ namespace Basket.FunctionalTests
string BuildCheckout() string BuildCheckout()
{ {
var checkoutBasket = new
var checkoutBasket = new
{ {
City = "city", City = "city",
Street = "street", Street = "street",


+ 2
- 2
src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs View File

@ -33,7 +33,7 @@ namespace Basket.FunctionalTests
Assert.Single(basket.Items); Assert.Single(basket.Items);
} }
} }
[Fact] [Fact]
@ -58,7 +58,7 @@ namespace Basket.FunctionalTests
Assert.True(deleteResult); Assert.True(deleteResult);
Assert.Null(result); Assert.Null(result);
}
}
} }
RedisBasketRepository BuildBasketRepository(ConnectionMultiplexer connMux) RedisBasketRepository BuildBasketRepository(ConnectionMultiplexer connMux)


+ 1
- 1
src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs View File

@ -19,7 +19,7 @@ namespace Catalog.API.Grpc
private readonly CatalogContext _catalogContext; private readonly CatalogContext _catalogContext;
private readonly CatalogSettings _settings; private readonly CatalogSettings _settings;
private readonly ILogger _logger; private readonly ILogger _logger;
public CatalogService(CatalogContext dbContext, IOptions<CatalogSettings> settings, ILogger<CatalogService> logger) public CatalogService(CatalogContext dbContext, IOptions<CatalogSettings> settings, ILogger<CatalogService> logger)
{ {
_settings = settings.Value; _settings = settings.Value;


Loading…
Cancel
Save