@ -1,31 +1,26 @@ | |||||
using Microsoft.AspNetCore.Http; | |||||
using System.Security.Claims; | |||||
using System.Threading.Tasks; | |||||
namespace Basket.FunctionalTests.Base; | |||||
namespace Basket.FunctionalTests.Base | |||||
class AutoAuthorizeMiddleware | |||||
{ | { | ||||
class AutoAuthorizeMiddleware | |||||
{ | |||||
public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00"; | |||||
public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00"; | |||||
private readonly RequestDelegate _next; | |||||
private readonly RequestDelegate _next; | |||||
public AutoAuthorizeMiddleware(RequestDelegate rd) | |||||
{ | |||||
_next = rd; | |||||
} | |||||
public AutoAuthorizeMiddleware(RequestDelegate rd) | |||||
{ | |||||
_next = rd; | |||||
} | |||||
public async Task Invoke(HttpContext httpContext) | |||||
{ | |||||
var identity = new ClaimsIdentity("cookies"); | |||||
public async Task Invoke(HttpContext httpContext) | |||||
{ | |||||
var identity = new ClaimsIdentity("cookies"); | |||||
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("sub", IDENTITY_ID)); | |||||
identity.AddClaim(new Claim("unique_name", IDENTITY_ID)); | |||||
identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID)); | |||||
httpContext.User.AddIdentity(identity); | |||||
httpContext.User.AddIdentity(identity); | |||||
await _next.Invoke(httpContext); | |||||
} | |||||
await _next.Invoke(httpContext); | |||||
} | } | ||||
} | } |
@ -1,43 +1,36 @@ | |||||
using Microsoft.AspNetCore.Hosting; | |||||
using Microsoft.AspNetCore.TestHost; | |||||
using Microsoft.Extensions.Configuration; | |||||
using System.IO; | |||||
using System.Reflection; | |||||
namespace Basket.FunctionalTests.Base; | |||||
namespace Basket.FunctionalTests.Base | |||||
public class BasketScenarioBase | |||||
{ | { | ||||
public class BasketScenarioBase | |||||
{ | |||||
private const string ApiUrlBase = "api/v1/basket"; | |||||
private const string ApiUrlBase = "api/v1/basket"; | |||||
public TestServer CreateServer() | |||||
{ | |||||
var path = Assembly.GetAssembly(typeof(BasketScenarioBase)) | |||||
.Location; | |||||
public TestServer CreateServer() | |||||
{ | |||||
var path = Assembly.GetAssembly(typeof(BasketScenarioBase)) | |||||
.Location; | |||||
var hostBuilder = new WebHostBuilder() | |||||
.UseContentRoot(Path.GetDirectoryName(path)) | |||||
.ConfigureAppConfiguration(cb => | |||||
{ | |||||
cb.AddJsonFile("appsettings.json", optional: false) | |||||
.AddEnvironmentVariables(); | |||||
}).UseStartup<BasketTestsStartup>(); | |||||
var hostBuilder = new WebHostBuilder() | |||||
.UseContentRoot(Path.GetDirectoryName(path)) | |||||
.ConfigureAppConfiguration(cb => | |||||
{ | |||||
cb.AddJsonFile("appsettings.json", optional: false) | |||||
.AddEnvironmentVariables(); | |||||
}).UseStartup<BasketTestsStartup>(); | |||||
return new TestServer(hostBuilder); | |||||
} | |||||
return new TestServer(hostBuilder); | |||||
} | |||||
public static class Get | |||||
public static class Get | |||||
{ | |||||
public static string GetBasket(int id) | |||||
{ | { | ||||
public static string GetBasket(int id) | |||||
{ | |||||
return $"{ApiUrlBase}/{id}"; | |||||
} | |||||
return $"{ApiUrlBase}/{id}"; | |||||
} | } | ||||
} | |||||
public static class Post | |||||
{ | |||||
public static string Basket = $"{ApiUrlBase}/"; | |||||
public static string CheckoutOrder = $"{ApiUrlBase}/checkout"; | |||||
} | |||||
public static class Post | |||||
{ | |||||
public static string Basket = $"{ApiUrlBase}/"; | |||||
public static string CheckoutOrder = $"{ApiUrlBase}/checkout"; | |||||
} | } | ||||
} | } |
@ -1,18 +1,13 @@ | |||||
using Microsoft.AspNetCore.TestHost; | |||||
using System; | |||||
using System.Net.Http; | |||||
namespace Basket.FunctionalTests.Base; | |||||
namespace Basket.FunctionalTests.Base | |||||
static class HttpClientExtensions | |||||
{ | { | ||||
static class HttpClientExtensions | |||||
public static HttpClient CreateIdempotentClient(this TestServer server) | |||||
{ | { | ||||
public static HttpClient CreateIdempotentClient(this TestServer server) | |||||
{ | |||||
var client = server.CreateClient(); | |||||
var client = server.CreateClient(); | |||||
client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString()); | |||||
client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString()); | |||||
return client; | |||||
} | |||||
return client; | |||||
} | } | ||||
} | } |
@ -1,95 +1,85 @@ | |||||
using Basket.FunctionalTests.Base; | |||||
using Microsoft.eShopOnContainers.Services.Basket.API.Model; | |||||
using System; | |||||
using System.Net.Http; | |||||
using System.Text; | |||||
using System.Text.Json; | |||||
using System.Threading.Tasks; | |||||
using Xunit; | |||||
namespace Basket.FunctionalTests; | |||||
namespace Basket.FunctionalTests | |||||
public class BasketScenarios | |||||
: BasketScenarioBase | |||||
{ | { | ||||
public class BasketScenarios | |||||
: BasketScenarioBase | |||||
[Fact] | |||||
public async Task Post_basket_and_response_ok_status_code() | |||||
{ | { | ||||
[Fact] | |||||
public async Task Post_basket_and_response_ok_status_code() | |||||
using (var server = CreateServer()) | |||||
{ | { | ||||
using (var server = CreateServer()) | |||||
{ | |||||
var content = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); | |||||
var response = await server.CreateClient() | |||||
.PostAsync(Post.Basket, content); | |||||
var content = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); | |||||
var response = await server.CreateClient() | |||||
.PostAsync(Post.Basket, content); | |||||
response.EnsureSuccessStatusCode(); | |||||
} | |||||
response.EnsureSuccessStatusCode(); | |||||
} | } | ||||
} | |||||
[Fact] | |||||
public async Task Get_basket_and_response_ok_status_code() | |||||
[Fact] | |||||
public async Task Get_basket_and_response_ok_status_code() | |||||
{ | |||||
using (var server = CreateServer()) | |||||
{ | { | ||||
using (var server = CreateServer()) | |||||
{ | |||||
var response = await server.CreateClient() | |||||
.GetAsync(Get.GetBasket(1)); | |||||
var response = await server.CreateClient() | |||||
.GetAsync(Get.GetBasket(1)); | |||||
response.EnsureSuccessStatusCode(); | |||||
} | |||||
response.EnsureSuccessStatusCode(); | |||||
} | } | ||||
} | |||||
[Fact] | |||||
public async Task Send_Checkout_basket_and_response_ok_status_code() | |||||
[Fact] | |||||
public async Task Send_Checkout_basket_and_response_ok_status_code() | |||||
{ | |||||
using (var server = CreateServer()) | |||||
{ | { | ||||
using (var server = CreateServer()) | |||||
{ | |||||
var contentBasket = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); | |||||
var contentBasket = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); | |||||
await server.CreateClient() | |||||
.PostAsync(Post.Basket, contentBasket); | |||||
await server.CreateClient() | |||||
.PostAsync(Post.Basket, contentBasket); | |||||
var contentCheckout = new StringContent(BuildCheckout(), UTF8Encoding.UTF8, "application/json"); | |||||
var contentCheckout = new StringContent(BuildCheckout(), UTF8Encoding.UTF8, "application/json"); | |||||
var response = await server.CreateIdempotentClient() | |||||
.PostAsync(Post.CheckoutOrder, contentCheckout); | |||||
var response = await server.CreateIdempotentClient() | |||||
.PostAsync(Post.CheckoutOrder, contentCheckout); | |||||
response.EnsureSuccessStatusCode(); | |||||
} | |||||
response.EnsureSuccessStatusCode(); | |||||
} | } | ||||
} | |||||
string BuildBasket() | |||||
{ | |||||
var order = new CustomerBasket(AutoAuthorizeMiddleware.IDENTITY_ID); | |||||
string BuildBasket() | |||||
{ | |||||
var order = new CustomerBasket(AutoAuthorizeMiddleware.IDENTITY_ID); | |||||
order.Items.Add(new BasketItem | |||||
{ | |||||
ProductId = 1, | |||||
ProductName = ".NET Bot Black Hoodie", | |||||
UnitPrice = 10, | |||||
Quantity = 1 | |||||
}); | |||||
order.Items.Add(new BasketItem | |||||
{ | |||||
ProductId = 1, | |||||
ProductName = ".NET Bot Black Hoodie", | |||||
UnitPrice = 10, | |||||
Quantity = 1 | |||||
}); | |||||
return JsonSerializer.Serialize(order); | |||||
} | |||||
return JsonSerializer.Serialize(order); | |||||
} | |||||
string BuildCheckout() | |||||
string BuildCheckout() | |||||
{ | |||||
var checkoutBasket = new | |||||
{ | { | ||||
var checkoutBasket = new | |||||
{ | |||||
City = "city", | |||||
Street = "street", | |||||
State = "state", | |||||
Country = "coutry", | |||||
ZipCode = "zipcode", | |||||
CardNumber = "1234567890123456", | |||||
CardHolderName = "CardHolderName", | |||||
CardExpiration = DateTime.UtcNow.AddDays(1), | |||||
CardSecurityNumber = "123", | |||||
CardTypeId = 1, | |||||
Buyer = "Buyer", | |||||
RequestId = Guid.NewGuid() | |||||
}; | |||||
City = "city", | |||||
Street = "street", | |||||
State = "state", | |||||
Country = "coutry", | |||||
ZipCode = "zipcode", | |||||
CardNumber = "1234567890123456", | |||||
CardHolderName = "CardHolderName", | |||||
CardExpiration = DateTime.UtcNow.AddDays(1), | |||||
CardSecurityNumber = "123", | |||||
CardTypeId = 1, | |||||
Buyer = "Buyer", | |||||
RequestId = Guid.NewGuid() | |||||
}; | |||||
return JsonSerializer.Serialize(checkoutBasket); | |||||
} | |||||
return JsonSerializer.Serialize(checkoutBasket); | |||||
} | } | ||||
} | } |