Code re-factorings and formatting for Basket.API Test projects
This commit is contained in:
parent
d44c12e718
commit
d6ad2b4a09
@ -4,27 +4,27 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Basket.FunctionalTests.Base
|
||||
{
|
||||
class AutoAuthorizeMiddleware
|
||||
{
|
||||
public const string IDENTITY_ID = "9e3163b9-1ae6-4652-9dc6-7898ab7b7a00";
|
||||
class AutoAuthorizeMiddleware
|
||||
{
|
||||
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("sub", IDENTITY_ID));
|
||||
identity.AddClaim(new Claim("unique_name", IDENTITY_ID));
|
||||
|
||||
httpContext.User.AddIdentity(identity);
|
||||
httpContext.User.AddIdentity(identity);
|
||||
|
||||
await _next.Invoke(httpContext);
|
||||
}
|
||||
}
|
||||
await _next.Invoke(httpContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,38 +6,38 @@ using System.Reflection;
|
||||
|
||||
namespace Basket.FunctionalTests.Base
|
||||
{
|
||||
public class BasketScenarioBase
|
||||
{
|
||||
private const string ApiUrlBase = "api/v1/basket";
|
||||
public class BasketScenarioBase
|
||||
{
|
||||
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 string GetBasket(int id)
|
||||
{
|
||||
return $"{ApiUrlBase}/{id}";
|
||||
}
|
||||
}
|
||||
public static class Get
|
||||
{
|
||||
public static string GetBasket(int 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,22 +4,22 @@ using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Basket.FunctionalTests.Base
|
||||
{
|
||||
class BasketTestsStartup : Startup
|
||||
{
|
||||
public BasketTestsStartup(IConfiguration env) : base(env)
|
||||
{
|
||||
}
|
||||
class BasketTestsStartup : Startup
|
||||
{
|
||||
public BasketTestsStartup(IConfiguration env) : base(env)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void ConfigureAuth(IApplicationBuilder app)
|
||||
{
|
||||
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
|
||||
{
|
||||
app.UseMiddleware<AutoAuthorizeMiddleware>();
|
||||
}
|
||||
else
|
||||
{
|
||||
base.ConfigureAuth(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected override void ConfigureAuth(IApplicationBuilder app)
|
||||
{
|
||||
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
|
||||
{
|
||||
app.UseMiddleware<AutoAuthorizeMiddleware>();
|
||||
}
|
||||
else
|
||||
{
|
||||
base.ConfigureAuth(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,15 +4,15 @@ using System.Net.Http;
|
||||
|
||||
namespace Basket.FunctionalTests.Base
|
||||
{
|
||||
static class HttpClientExtensions
|
||||
{
|
||||
public static HttpClient CreateIdempotentClient(this TestServer server)
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
static class HttpClientExtensions
|
||||
{
|
||||
public static HttpClient CreateIdempotentClient(this TestServer server)
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString());
|
||||
client.DefaultRequestHeaders.Add("x-requestid", Guid.NewGuid().ToString());
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,87 +10,87 @@ using Xunit;
|
||||
|
||||
namespace Basket.FunctionalTests
|
||||
{
|
||||
public class BasketScenarios
|
||||
: BasketScenarioBase
|
||||
{
|
||||
[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);
|
||||
public class BasketScenarios
|
||||
: BasketScenarioBase
|
||||
{
|
||||
[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);
|
||||
|
||||
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));
|
||||
[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();
|
||||
}
|
||||
}
|
||||
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");
|
||||
[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);
|
||||
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 JsonConvert.SerializeObject(order);
|
||||
}
|
||||
return JsonConvert.SerializeObject(order);
|
||||
}
|
||||
|
||||
string BuildCheckout()
|
||||
{
|
||||
var checkoutBasket = new BasketDTO()
|
||||
{
|
||||
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()
|
||||
};
|
||||
string BuildCheckout()
|
||||
{
|
||||
var checkoutBasket = new BasketDTO()
|
||||
{
|
||||
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 JsonConvert.SerializeObject(checkoutBasket);
|
||||
}
|
||||
}
|
||||
return JsonConvert.SerializeObject(checkoutBasket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,79 +4,77 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Basket.FunctionalTests
|
||||
{
|
||||
public class RedisBasketRepositoryTests
|
||||
{
|
||||
private Mock<IOptionsSnapshot<BasketSettings>> _optionsMock;
|
||||
public class RedisBasketRepositoryTests
|
||||
{
|
||||
private readonly Mock<IOptionsSnapshot<BasketSettings>> _optionsMock;
|
||||
|
||||
public RedisBasketRepositoryTests()
|
||||
{
|
||||
_optionsMock = new Mock<IOptionsSnapshot<BasketSettings>>();
|
||||
}
|
||||
public RedisBasketRepositoryTests()
|
||||
{
|
||||
_optionsMock = new Mock<IOptionsSnapshot<BasketSettings>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateBasket_return_and_add_basket()
|
||||
{
|
||||
var redisBasketRepository = BuildBasketRepository();
|
||||
[Fact]
|
||||
public async Task UpdateBasket_return_and_add_basket()
|
||||
{
|
||||
var redisBasketRepository = BuildBasketRepository();
|
||||
|
||||
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
||||
{
|
||||
BuyerId = "buyerId",
|
||||
Items = BuildBasketItems()
|
||||
});
|
||||
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
||||
{
|
||||
BuyerId = "buyerId",
|
||||
Items = BuildBasketItems()
|
||||
});
|
||||
|
||||
Assert.NotNull(basket);
|
||||
Assert.Single(basket.Items);
|
||||
}
|
||||
Assert.NotNull(basket);
|
||||
Assert.Single(basket.Items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Delete_Basket_return_null()
|
||||
{
|
||||
var redisBasketRepository = BuildBasketRepository();
|
||||
[Fact]
|
||||
public async Task Delete_Basket_return_null()
|
||||
{
|
||||
var redisBasketRepository = BuildBasketRepository();
|
||||
|
||||
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
||||
{
|
||||
BuyerId = "buyerId",
|
||||
Items = BuildBasketItems()
|
||||
});
|
||||
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
||||
{
|
||||
BuyerId = "buyerId",
|
||||
Items = BuildBasketItems()
|
||||
});
|
||||
|
||||
var deleteResult = await redisBasketRepository.DeleteBasketAsync("buyerId");
|
||||
var deleteResult = await redisBasketRepository.DeleteBasketAsync("buyerId");
|
||||
|
||||
var result = await redisBasketRepository.GetBasketAsync(basket.BuyerId);
|
||||
var result = await redisBasketRepository.GetBasketAsync(basket.BuyerId);
|
||||
|
||||
Assert.True(deleteResult);
|
||||
Assert.Null(result);
|
||||
}
|
||||
Assert.True(deleteResult);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
RedisBasketRepository BuildBasketRepository()
|
||||
{
|
||||
var loggerFactory = new LoggerFactory();
|
||||
var configuration = ConfigurationOptions.Parse("127.0.0.1", true);
|
||||
configuration.ResolveDns = true;
|
||||
return new RedisBasketRepository(loggerFactory, ConnectionMultiplexer.Connect(configuration));
|
||||
}
|
||||
RedisBasketRepository BuildBasketRepository()
|
||||
{
|
||||
var loggerFactory = new LoggerFactory();
|
||||
var configuration = ConfigurationOptions.Parse("127.0.0.1", true);
|
||||
configuration.ResolveDns = true;
|
||||
return new RedisBasketRepository(loggerFactory, ConnectionMultiplexer.Connect(configuration));
|
||||
}
|
||||
|
||||
List<BasketItem> BuildBasketItems()
|
||||
{
|
||||
return new List<BasketItem>()
|
||||
{
|
||||
new BasketItem()
|
||||
{
|
||||
Id = "basketId",
|
||||
PictureUrl = "pictureurl",
|
||||
ProductId = "productId",
|
||||
ProductName = "productName",
|
||||
Quantity = 1,
|
||||
UnitPrice = 1
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
List<BasketItem> BuildBasketItems()
|
||||
{
|
||||
return new List<BasketItem>()
|
||||
{
|
||||
new BasketItem()
|
||||
{
|
||||
Id = "basketId",
|
||||
PictureUrl = "pictureurl",
|
||||
ProductId = "productId",
|
||||
ProductName = "productName",
|
||||
Quantity = 1,
|
||||
UnitPrice = 1
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
},
|
||||
"IdentityUrl": "http://localhost:5105",
|
||||
"ConnectionString": "127.0.0.1",
|
||||
"isTest": "true",
|
||||
"EventBusConnection": "localhost",
|
||||
"SubscriptionClientName": "Basket"
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
},
|
||||
"IdentityUrl": "http://localhost:5105",
|
||||
"ConnectionString": "127.0.0.1",
|
||||
"isTest": "true",
|
||||
"EventBusConnection": "localhost",
|
||||
"SubscriptionClientName": "Basket"
|
||||
}
|
||||
|
@ -15,121 +15,121 @@ using IBasketIdentityService = Microsoft.eShopOnContainers.Services.Basket.API.S
|
||||
|
||||
namespace UnitTest.Basket.Application
|
||||
{
|
||||
public class BasketWebApiTest
|
||||
{
|
||||
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
||||
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
||||
private readonly Mock<IEventBus> _serviceBusMock;
|
||||
public class BasketWebApiTest
|
||||
{
|
||||
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
||||
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
||||
private readonly Mock<IEventBus> _serviceBusMock;
|
||||
|
||||
public BasketWebApiTest()
|
||||
{
|
||||
_basketRepositoryMock = new Mock<IBasketRepository>();
|
||||
_identityServiceMock = new Mock<IBasketIdentityService>();
|
||||
_serviceBusMock = new Mock<IEventBus>();
|
||||
}
|
||||
public BasketWebApiTest()
|
||||
{
|
||||
_basketRepositoryMock = new Mock<IBasketRepository>();
|
||||
_identityServiceMock = new Mock<IBasketIdentityService>();
|
||||
_serviceBusMock = new Mock<IEventBus>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Get_customer_basket_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeCustomerId = "1";
|
||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||
[Fact]
|
||||
public async Task Get_customer_basket_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeCustomerId = "1";
|
||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||
|
||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
|
||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
|
||||
|
||||
//Assert
|
||||
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||
}
|
||||
//Assert
|
||||
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Post_customer_basket_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeCustomerId = "1";
|
||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||
[Fact]
|
||||
public async Task Post_customer_basket_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeCustomerId = "1";
|
||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||
|
||||
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
|
||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
|
||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
|
||||
var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
|
||||
var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
|
||||
|
||||
//Assert
|
||||
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||
}
|
||||
//Assert
|
||||
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
|
||||
{
|
||||
var fakeCustomerId = "2";
|
||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult((CustomerBasket)null));
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
[Fact]
|
||||
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
|
||||
{
|
||||
var fakeCustomerId = "2";
|
||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult((CustomerBasket)null));
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
|
||||
var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
|
||||
{
|
||||
var fakeCustomerId = "1";
|
||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||
[Fact]
|
||||
public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
|
||||
{
|
||||
var fakeCustomerId = "1";
|
||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||
|
||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
|
||||
basketController.ControllerContext = new ControllerContext()
|
||||
{
|
||||
HttpContext = new DefaultHttpContext()
|
||||
{
|
||||
User = new ClaimsPrincipal(
|
||||
new ClaimsIdentity(new Claim[] { new Claim("unique_name", "testuser") }))
|
||||
}
|
||||
};
|
||||
basketController.ControllerContext = new ControllerContext()
|
||||
{
|
||||
HttpContext = new DefaultHttpContext()
|
||||
{
|
||||
User = new ClaimsPrincipal(
|
||||
new ClaimsIdentity(new Claim[] { new Claim("unique_name", "testuser") }))
|
||||
}
|
||||
};
|
||||
|
||||
//Act
|
||||
var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult;
|
||||
//Act
|
||||
var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult;
|
||||
|
||||
_serviceBusMock.Verify(mock => mock.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()), Times.Once);
|
||||
_serviceBusMock.Verify(mock => mock.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()), Times.Once);
|
||||
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
|
||||
{
|
||||
return new CustomerBasket(fakeCustomerId)
|
||||
{
|
||||
Items = new List<BasketItem>()
|
||||
{
|
||||
new BasketItem()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
|
||||
{
|
||||
return new CustomerBasket(fakeCustomerId)
|
||||
{
|
||||
Items = new List<BasketItem>()
|
||||
{
|
||||
new BasketItem()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ using Microsoft.eShopOnContainers.WebMVC.Controllers;
|
||||
using Microsoft.eShopOnContainers.WebMVC.Services;
|
||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
@ -12,119 +11,119 @@ using BasketModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Basket;
|
||||
|
||||
namespace UnitTest.Basket.Application
|
||||
{
|
||||
public class CartControllerTest
|
||||
{
|
||||
private readonly Mock<ICatalogService> _catalogServiceMock;
|
||||
private readonly Mock<IBasketService> _basketServiceMock;
|
||||
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
||||
private readonly Mock<HttpContext> _contextMock;
|
||||
public class CartControllerTest
|
||||
{
|
||||
private readonly Mock<ICatalogService> _catalogServiceMock;
|
||||
private readonly Mock<IBasketService> _basketServiceMock;
|
||||
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
||||
private readonly Mock<HttpContext> _contextMock;
|
||||
|
||||
public CartControllerTest()
|
||||
{
|
||||
_catalogServiceMock = new Mock<ICatalogService>();
|
||||
_basketServiceMock = new Mock<IBasketService>();
|
||||
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
||||
_contextMock = new Mock<HttpContext>();
|
||||
}
|
||||
public CartControllerTest()
|
||||
{
|
||||
_catalogServiceMock = new Mock<ICatalogService>();
|
||||
_basketServiceMock = new Mock<IBasketService>();
|
||||
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
||||
_contextMock = new Mock<HttpContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Post_cart_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeBuyerId = "1";
|
||||
var action = string.Empty;
|
||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||
var fakeQuantities = new Dictionary<string, int>()
|
||||
{
|
||||
["fakeProdA"] = 1,
|
||||
["fakeProdB"] = 2
|
||||
};
|
||||
[Fact]
|
||||
public async Task Post_cart_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeBuyerId = "1";
|
||||
var action = string.Empty;
|
||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||
var fakeQuantities = new Dictionary<string, int>()
|
||||
{
|
||||
["fakeProdA"] = 1,
|
||||
["fakeProdB"] = 2
|
||||
};
|
||||
|
||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
|
||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
|
||||
//Act
|
||||
var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||
cartController.ControllerContext.HttpContext = _contextMock.Object;
|
||||
var actionResult = await cartController.Index(fakeQuantities, action);
|
||||
//Act
|
||||
var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||
cartController.ControllerContext.HttpContext = _contextMock.Object;
|
||||
var actionResult = await cartController.Index(fakeQuantities, action);
|
||||
|
||||
//Assert
|
||||
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||
}
|
||||
//Assert
|
||||
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Post_cart_checkout_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeBuyerId = "1";
|
||||
var action = "[ Checkout ]";
|
||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||
var fakeQuantities = new Dictionary<string, int>()
|
||||
{
|
||||
["fakeProdA"] = 1,
|
||||
["fakeProdB"] = 2
|
||||
};
|
||||
[Fact]
|
||||
public async Task Post_cart_checkout_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeBuyerId = "1";
|
||||
var action = "[ Checkout ]";
|
||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||
var fakeQuantities = new Dictionary<string, int>()
|
||||
{
|
||||
["fakeProdA"] = 1,
|
||||
["fakeProdB"] = 2
|
||||
};
|
||||
|
||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
|
||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||
.Returns(Task.FromResult(fakeBasket));
|
||||
|
||||
//Act
|
||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||
var actionResult = await orderController.Index(fakeQuantities, action);
|
||||
//Act
|
||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||
var actionResult = await orderController.Index(fakeQuantities, action);
|
||||
|
||||
//Assert
|
||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||
Assert.Equal("Order", redirectToActionResult.ControllerName);
|
||||
Assert.Equal("Create", redirectToActionResult.ActionName);
|
||||
}
|
||||
//Assert
|
||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||
Assert.Equal("Order", redirectToActionResult.ControllerName);
|
||||
Assert.Equal("Create", redirectToActionResult.ActionName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Add_to_cart_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeCatalogItem = GetFakeCatalogItem();
|
||||
[Fact]
|
||||
public async Task Add_to_cart_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeCatalogItem = GetFakeCatalogItem();
|
||||
|
||||
_basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<Int32>()))
|
||||
.Returns(Task.FromResult(1));
|
||||
_basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<int>()))
|
||||
.Returns(Task.FromResult(1));
|
||||
|
||||
//Act
|
||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||
var actionResult = await orderController.AddToCart(fakeCatalogItem);
|
||||
//Act
|
||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||
var actionResult = await orderController.AddToCart(fakeCatalogItem);
|
||||
|
||||
//Assert
|
||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||
Assert.Equal("Catalog", redirectToActionResult.ControllerName);
|
||||
Assert.Equal("Index", redirectToActionResult.ActionName);
|
||||
}
|
||||
//Assert
|
||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||
Assert.Equal("Catalog", redirectToActionResult.ControllerName);
|
||||
Assert.Equal("Index", redirectToActionResult.ActionName);
|
||||
}
|
||||
|
||||
private BasketModel GetFakeBasket(string buyerId)
|
||||
{
|
||||
return new BasketModel()
|
||||
{
|
||||
BuyerId = buyerId
|
||||
};
|
||||
}
|
||||
private BasketModel GetFakeBasket(string buyerId)
|
||||
{
|
||||
return new BasketModel()
|
||||
{
|
||||
BuyerId = buyerId
|
||||
};
|
||||
}
|
||||
|
||||
private CatalogItem GetFakeCatalogItem()
|
||||
{
|
||||
return new CatalogItem()
|
||||
{
|
||||
Id = 1,
|
||||
Name = "fakeName",
|
||||
CatalogBrand = "fakeBrand",
|
||||
CatalogType = "fakeType",
|
||||
CatalogBrandId = 2,
|
||||
CatalogTypeId = 5,
|
||||
Price = 20
|
||||
};
|
||||
}
|
||||
}
|
||||
private CatalogItem GetFakeCatalogItem()
|
||||
{
|
||||
return new CatalogItem()
|
||||
{
|
||||
Id = 1,
|
||||
Name = "fakeName",
|
||||
CatalogBrand = "fakeBrand",
|
||||
CatalogType = "fakeType",
|
||||
CatalogBrandId = 2,
|
||||
CatalogTypeId = 5,
|
||||
Price = 20
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user