Included file-scoped namespaces Basket.FunctionalTests

This commit is contained in:
Sumit Ghosh 2021-10-14 15:29:39 +05:30
parent 886c6070c6
commit 4e6700d3c6
6 changed files with 128 additions and 168 deletions

View File

@ -1,31 +1,26 @@
using Microsoft.AspNetCore.Http; namespace Basket.FunctionalTests.Base;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Basket.FunctionalTests.Base class AutoAuthorizeMiddleware
{ {
class AutoAuthorizeMiddleware public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00";
private readonly RequestDelegate _next;
public AutoAuthorizeMiddleware(RequestDelegate rd)
{ {
public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00"; _next = rd;
}
private readonly RequestDelegate _next; public async Task Invoke(HttpContext httpContext)
{
var identity = new ClaimsIdentity("cookies");
public AutoAuthorizeMiddleware(RequestDelegate rd) identity.AddClaim(new Claim("sub", IDENTITY_ID));
{ identity.AddClaim(new Claim("unique_name", IDENTITY_ID));
_next = rd; identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID));
}
public async Task Invoke(HttpContext httpContext) httpContext.User.AddIdentity(identity);
{
var identity = new ClaimsIdentity("cookies");
identity.AddClaim(new Claim("sub", IDENTITY_ID)); await _next.Invoke(httpContext);
identity.AddClaim(new Claim("unique_name", IDENTITY_ID));
identity.AddClaim(new Claim(ClaimTypes.Name, IDENTITY_ID));
httpContext.User.AddIdentity(identity);
await _next.Invoke(httpContext);
}
} }
} }

View File

@ -1,43 +1,36 @@
using Microsoft.AspNetCore.Hosting; namespace Basket.FunctionalTests.Base;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Reflection;
namespace Basket.FunctionalTests.Base public class BasketScenarioBase
{ {
public class BasketScenarioBase private const string ApiUrlBase = "api/v1/basket";
public TestServer CreateServer()
{ {
private const string ApiUrlBase = "api/v1/basket"; var path = Assembly.GetAssembly(typeof(BasketScenarioBase))
.Location;
public TestServer CreateServer() var hostBuilder = new WebHostBuilder()
{ .UseContentRoot(Path.GetDirectoryName(path))
var path = Assembly.GetAssembly(typeof(BasketScenarioBase)) .ConfigureAppConfiguration(cb =>
.Location;
var hostBuilder = new WebHostBuilder()
.UseContentRoot(Path.GetDirectoryName(path))
.ConfigureAppConfiguration(cb =>
{
cb.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables();
}).UseStartup<BasketTestsStartup>();
return new TestServer(hostBuilder);
}
public static class Get
{
public static string GetBasket(int id)
{ {
return $"{ApiUrlBase}/{id}"; cb.AddJsonFile("appsettings.json", optional: false)
} .AddEnvironmentVariables();
} }).UseStartup<BasketTestsStartup>();
public static class Post return new TestServer(hostBuilder);
}
public static class Get
{
public static string GetBasket(int id)
{ {
public static string Basket = $"{ApiUrlBase}/"; return $"{ApiUrlBase}/{id}";
public static string CheckoutOrder = $"{ApiUrlBase}/checkout";
} }
} }
public static class Post
{
public static string Basket = $"{ApiUrlBase}/";
public static string CheckoutOrder = $"{ApiUrlBase}/checkout";
}
} }

View File

@ -1,9 +1,4 @@
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 namespace Basket.FunctionalTests.Base
{ {

View File

@ -1,18 +1,13 @@
using Microsoft.AspNetCore.TestHost; namespace Basket.FunctionalTests.Base;
using System;
using System.Net.Http;
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;
}
} }
} }

View File

@ -1,95 +1,85 @@
using Basket.FunctionalTests.Base; namespace Basket.FunctionalTests;
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 public class BasketScenarios
: BasketScenarioBase
{ {
public class BasketScenarios [Fact]
: BasketScenarioBase public async Task Post_basket_and_response_ok_status_code()
{ {
[Fact] using (var server = CreateServer())
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()
var content = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json"); .PostAsync(Post.Basket, content);
var response = await server.CreateClient()
.PostAsync(Post.Basket, content);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
}
}
[Fact]
public async Task Get_basket_and_response_ok_status_code()
{
using (var server = CreateServer())
{
var response = await server.CreateClient()
.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()
.PostAsync(Post.Basket, contentBasket);
var contentCheckout = new StringContent(BuildCheckout(), UTF8Encoding.UTF8, "application/json");
var response = await server.CreateIdempotentClient()
.PostAsync(Post.CheckoutOrder, contentCheckout);
response.EnsureSuccessStatusCode();
}
}
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
});
return JsonSerializer.Serialize(order);
}
string BuildCheckout()
{
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()
};
return JsonSerializer.Serialize(checkoutBasket);
} }
} }
[Fact]
public async Task Get_basket_and_response_ok_status_code()
{
using (var server = CreateServer())
{
var response = await server.CreateClient()
.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()
.PostAsync(Post.Basket, contentBasket);
var contentCheckout = new StringContent(BuildCheckout(), UTF8Encoding.UTF8, "application/json");
var response = await server.CreateIdempotentClient()
.PostAsync(Post.CheckoutOrder, contentCheckout);
response.EnsureSuccessStatusCode();
}
}
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
});
return JsonSerializer.Serialize(order);
}
string BuildCheckout()
{
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()
};
return JsonSerializer.Serialize(checkoutBasket);
}
} }

View File

@ -1,12 +1,4 @@
using Basket.FunctionalTests.Base; 
using Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Repositories;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
namespace Basket.FunctionalTests namespace Basket.FunctionalTests
{ {