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
|
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)
|
public AutoAuthorizeMiddleware(RequestDelegate rd)
|
||||||
{
|
{
|
||||||
_next = rd;
|
_next = rd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Invoke(HttpContext httpContext)
|
public async Task Invoke(HttpContext httpContext)
|
||||||
{
|
{
|
||||||
var identity = new ClaimsIdentity("cookies");
|
var identity = new ClaimsIdentity("cookies");
|
||||||
|
|
||||||
identity.AddClaim(new Claim("sub", IDENTITY_ID));
|
identity.AddClaim(new Claim("sub", IDENTITY_ID));
|
||||||
identity.AddClaim(new Claim("unique_name", 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
|
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()
|
public TestServer CreateServer()
|
||||||
{
|
{
|
||||||
var path = Assembly.GetAssembly(typeof(BasketScenarioBase))
|
var path = Assembly.GetAssembly(typeof(BasketScenarioBase))
|
||||||
.Location;
|
.Location;
|
||||||
|
|
||||||
var hostBuilder = new WebHostBuilder()
|
var hostBuilder = new WebHostBuilder()
|
||||||
.UseContentRoot(Path.GetDirectoryName(path))
|
.UseContentRoot(Path.GetDirectoryName(path))
|
||||||
.ConfigureAppConfiguration(cb =>
|
.ConfigureAppConfiguration(cb =>
|
||||||
{
|
{
|
||||||
cb.AddJsonFile("appsettings.json", optional: false)
|
cb.AddJsonFile("appsettings.json", optional: false)
|
||||||
.AddEnvironmentVariables();
|
.AddEnvironmentVariables();
|
||||||
}).UseStartup<BasketTestsStartup>();
|
}).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 class Post
|
||||||
{
|
{
|
||||||
public static string Basket = $"{ApiUrlBase}/";
|
public static string Basket = $"{ApiUrlBase}/";
|
||||||
public static string CheckoutOrder = $"{ApiUrlBase}/checkout";
|
public static string CheckoutOrder = $"{ApiUrlBase}/checkout";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,22 +4,22 @@ using Microsoft.Extensions.Configuration;
|
|||||||
|
|
||||||
namespace Basket.FunctionalTests.Base
|
namespace Basket.FunctionalTests.Base
|
||||||
{
|
{
|
||||||
class BasketTestsStartup : Startup
|
class BasketTestsStartup : Startup
|
||||||
{
|
{
|
||||||
public BasketTestsStartup(IConfiguration env) : base(env)
|
public BasketTestsStartup(IConfiguration env) : base(env)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ConfigureAuth(IApplicationBuilder app)
|
protected override void ConfigureAuth(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
|
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
|
||||||
{
|
{
|
||||||
app.UseMiddleware<AutoAuthorizeMiddleware>();
|
app.UseMiddleware<AutoAuthorizeMiddleware>();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
base.ConfigureAuth(app);
|
base.ConfigureAuth(app);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,15 +4,15 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,87 +10,87 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Basket.FunctionalTests
|
namespace Basket.FunctionalTests
|
||||||
{
|
{
|
||||||
public class BasketScenarios
|
public class BasketScenarios
|
||||||
: BasketScenarioBase
|
: BasketScenarioBase
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_basket_and_response_ok_status_code()
|
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 content = new StringContent(BuildBasket(), UTF8Encoding.UTF8, "application/json");
|
||||||
var response = await server.CreateClient()
|
var response = await server.CreateClient()
|
||||||
.PostAsync(Post.Basket, content);
|
.PostAsync(Post.Basket, content);
|
||||||
|
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Get_basket_and_response_ok_status_code()
|
public async Task Get_basket_and_response_ok_status_code()
|
||||||
{
|
{
|
||||||
using (var server = CreateServer())
|
using (var server = CreateServer())
|
||||||
{
|
{
|
||||||
var response = await server.CreateClient()
|
var response = await server.CreateClient()
|
||||||
.GetAsync(Get.GetBasket(1));
|
.GetAsync(Get.GetBasket(1));
|
||||||
|
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Send_Checkout_basket_and_response_ok_status_code()
|
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()
|
await server.CreateClient()
|
||||||
.PostAsync(Post.Basket, contentBasket);
|
.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()
|
var response = await server.CreateIdempotentClient()
|
||||||
.PostAsync(Post.CheckoutOrder, contentCheckout);
|
.PostAsync(Post.CheckoutOrder, contentCheckout);
|
||||||
|
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string BuildBasket()
|
string BuildBasket()
|
||||||
{
|
{
|
||||||
var order = new CustomerBasket(AutoAuthorizeMiddleware.IDENTITY_ID);
|
var order = new CustomerBasket(AutoAuthorizeMiddleware.IDENTITY_ID);
|
||||||
|
|
||||||
order.Items.Add(new BasketItem
|
order.Items.Add(new BasketItem
|
||||||
{
|
{
|
||||||
ProductId = "1",
|
ProductId = "1",
|
||||||
ProductName = ".NET Bot Black Hoodie",
|
ProductName = ".NET Bot Black Hoodie",
|
||||||
UnitPrice = 10,
|
UnitPrice = 10,
|
||||||
Quantity = 1
|
Quantity = 1
|
||||||
});
|
});
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(order);
|
return JsonConvert.SerializeObject(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
string BuildCheckout()
|
string BuildCheckout()
|
||||||
{
|
{
|
||||||
var checkoutBasket = new BasketDTO()
|
var checkoutBasket = new BasketDTO()
|
||||||
{
|
{
|
||||||
City = "city",
|
City = "city",
|
||||||
Street = "street",
|
Street = "street",
|
||||||
State = "state",
|
State = "state",
|
||||||
Country = "coutry",
|
Country = "coutry",
|
||||||
ZipCode = "zipcode",
|
ZipCode = "zipcode",
|
||||||
CardNumber = "1234567890123456",
|
CardNumber = "1234567890123456",
|
||||||
CardHolderName = "CardHolderName",
|
CardHolderName = "CardHolderName",
|
||||||
CardExpiration = DateTime.UtcNow.AddDays(1),
|
CardExpiration = DateTime.UtcNow.AddDays(1),
|
||||||
CardSecurityNumber = "123",
|
CardSecurityNumber = "123",
|
||||||
CardTypeId = 1,
|
CardTypeId = 1,
|
||||||
Buyer = "Buyer",
|
Buyer = "Buyer",
|
||||||
RequestId = Guid.NewGuid()
|
RequestId = Guid.NewGuid()
|
||||||
};
|
};
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(checkoutBasket);
|
return JsonConvert.SerializeObject(checkoutBasket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,79 +4,77 @@ using Microsoft.Extensions.Logging;
|
|||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Moq;
|
using Moq;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Basket.FunctionalTests
|
namespace Basket.FunctionalTests
|
||||||
{
|
{
|
||||||
public class RedisBasketRepositoryTests
|
public class RedisBasketRepositoryTests
|
||||||
{
|
{
|
||||||
private Mock<IOptionsSnapshot<BasketSettings>> _optionsMock;
|
private readonly Mock<IOptionsSnapshot<BasketSettings>> _optionsMock;
|
||||||
|
|
||||||
public RedisBasketRepositoryTests()
|
public RedisBasketRepositoryTests()
|
||||||
{
|
{
|
||||||
_optionsMock = new Mock<IOptionsSnapshot<BasketSettings>>();
|
_optionsMock = new Mock<IOptionsSnapshot<BasketSettings>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task UpdateBasket_return_and_add_basket()
|
public async Task UpdateBasket_return_and_add_basket()
|
||||||
{
|
{
|
||||||
var redisBasketRepository = BuildBasketRepository();
|
var redisBasketRepository = BuildBasketRepository();
|
||||||
|
|
||||||
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
||||||
{
|
{
|
||||||
BuyerId = "buyerId",
|
BuyerId = "buyerId",
|
||||||
Items = BuildBasketItems()
|
Items = BuildBasketItems()
|
||||||
});
|
});
|
||||||
|
|
||||||
Assert.NotNull(basket);
|
Assert.NotNull(basket);
|
||||||
Assert.Single(basket.Items);
|
Assert.Single(basket.Items);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Delete_Basket_return_null()
|
public async Task Delete_Basket_return_null()
|
||||||
{
|
{
|
||||||
var redisBasketRepository = BuildBasketRepository();
|
var redisBasketRepository = BuildBasketRepository();
|
||||||
|
|
||||||
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
var basket = await redisBasketRepository.UpdateBasketAsync(new CustomerBasket("customerId")
|
||||||
{
|
{
|
||||||
BuyerId = "buyerId",
|
BuyerId = "buyerId",
|
||||||
Items = BuildBasketItems()
|
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.True(deleteResult);
|
||||||
Assert.Null(result);
|
Assert.Null(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
RedisBasketRepository BuildBasketRepository()
|
RedisBasketRepository BuildBasketRepository()
|
||||||
{
|
{
|
||||||
var loggerFactory = new LoggerFactory();
|
var loggerFactory = new LoggerFactory();
|
||||||
var configuration = ConfigurationOptions.Parse("127.0.0.1", true);
|
var configuration = ConfigurationOptions.Parse("127.0.0.1", true);
|
||||||
configuration.ResolveDns = true;
|
configuration.ResolveDns = true;
|
||||||
return new RedisBasketRepository(loggerFactory, ConnectionMultiplexer.Connect(configuration));
|
return new RedisBasketRepository(loggerFactory, ConnectionMultiplexer.Connect(configuration));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<BasketItem> BuildBasketItems()
|
List<BasketItem> BuildBasketItems()
|
||||||
{
|
{
|
||||||
return new List<BasketItem>()
|
return new List<BasketItem>()
|
||||||
{
|
{
|
||||||
new BasketItem()
|
new BasketItem()
|
||||||
{
|
{
|
||||||
Id = "basketId",
|
Id = "basketId",
|
||||||
PictureUrl = "pictureurl",
|
PictureUrl = "pictureurl",
|
||||||
ProductId = "productId",
|
ProductId = "productId",
|
||||||
ProductName = "productName",
|
ProductName = "productName",
|
||||||
Quantity = 1,
|
Quantity = 1,
|
||||||
UnitPrice = 1
|
UnitPrice = 1
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
{
|
{
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"IncludeScopes": false,
|
"IncludeScopes": false,
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Debug",
|
"Default": "Debug",
|
||||||
"System": "Information",
|
"System": "Information",
|
||||||
"Microsoft": "Information"
|
"Microsoft": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"IdentityUrl": "http://localhost:5105",
|
"IdentityUrl": "http://localhost:5105",
|
||||||
"ConnectionString": "127.0.0.1",
|
"ConnectionString": "127.0.0.1",
|
||||||
"isTest": "true",
|
"isTest": "true",
|
||||||
"EventBusConnection": "localhost",
|
"EventBusConnection": "localhost",
|
||||||
"SubscriptionClientName": "Basket"
|
"SubscriptionClientName": "Basket"
|
||||||
}
|
}
|
||||||
|
@ -15,121 +15,121 @@ using IBasketIdentityService = Microsoft.eShopOnContainers.Services.Basket.API.S
|
|||||||
|
|
||||||
namespace UnitTest.Basket.Application
|
namespace UnitTest.Basket.Application
|
||||||
{
|
{
|
||||||
public class BasketWebApiTest
|
public class BasketWebApiTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
||||||
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
||||||
private readonly Mock<IEventBus> _serviceBusMock;
|
private readonly Mock<IEventBus> _serviceBusMock;
|
||||||
|
|
||||||
public BasketWebApiTest()
|
public BasketWebApiTest()
|
||||||
{
|
{
|
||||||
_basketRepositoryMock = new Mock<IBasketRepository>();
|
_basketRepositoryMock = new Mock<IBasketRepository>();
|
||||||
_identityServiceMock = new Mock<IBasketIdentityService>();
|
_identityServiceMock = new Mock<IBasketIdentityService>();
|
||||||
_serviceBusMock = new Mock<IEventBus>();
|
_serviceBusMock = new Mock<IEventBus>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Get_customer_basket_success()
|
public async Task Get_customer_basket_success()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var fakeCustomerId = "1";
|
var fakeCustomerId = "1";
|
||||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
|
|
||||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
|
|
||||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var basketController = new BasketController(
|
var basketController = new BasketController(
|
||||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||||
var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
|
var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_customer_basket_success()
|
public async Task Post_customer_basket_success()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var fakeCustomerId = "1";
|
var fakeCustomerId = "1";
|
||||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
|
|
||||||
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
|
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
|
||||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var basketController = new BasketController(
|
var basketController = new BasketController(
|
||||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||||
|
|
||||||
var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
|
var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
|
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
|
||||||
{
|
{
|
||||||
var fakeCustomerId = "2";
|
var fakeCustomerId = "2";
|
||||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||||
.Returns(Task.FromResult((CustomerBasket)null));
|
.Returns(Task.FromResult((CustomerBasket)null));
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var basketController = new BasketController(
|
var basketController = new BasketController(
|
||||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||||
|
|
||||||
var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
||||||
Assert.NotNull(result);
|
Assert.NotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
|
public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
|
||||||
{
|
{
|
||||||
var fakeCustomerId = "1";
|
var fakeCustomerId = "1";
|
||||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
|
|
||||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
|
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
|
|
||||||
var basketController = new BasketController(
|
var basketController = new BasketController(
|
||||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||||
|
|
||||||
basketController.ControllerContext = new ControllerContext()
|
basketController.ControllerContext = new ControllerContext()
|
||||||
{
|
{
|
||||||
HttpContext = new DefaultHttpContext()
|
HttpContext = new DefaultHttpContext()
|
||||||
{
|
{
|
||||||
User = new ClaimsPrincipal(
|
User = new ClaimsPrincipal(
|
||||||
new ClaimsIdentity(new Claim[] { new Claim("unique_name", "testuser") }))
|
new ClaimsIdentity(new Claim[] { new Claim("unique_name", "testuser") }))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult;
|
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)
|
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
|
||||||
{
|
{
|
||||||
return new CustomerBasket(fakeCustomerId)
|
return new CustomerBasket(fakeCustomerId)
|
||||||
{
|
{
|
||||||
Items = new List<BasketItem>()
|
Items = new List<BasketItem>()
|
||||||
{
|
{
|
||||||
new BasketItem()
|
new BasketItem()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ using Microsoft.eShopOnContainers.WebMVC.Controllers;
|
|||||||
using Microsoft.eShopOnContainers.WebMVC.Services;
|
using Microsoft.eShopOnContainers.WebMVC.Services;
|
||||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
@ -12,119 +11,119 @@ using BasketModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Basket;
|
|||||||
|
|
||||||
namespace UnitTest.Basket.Application
|
namespace UnitTest.Basket.Application
|
||||||
{
|
{
|
||||||
public class CartControllerTest
|
public class CartControllerTest
|
||||||
{
|
{
|
||||||
private readonly Mock<ICatalogService> _catalogServiceMock;
|
private readonly Mock<ICatalogService> _catalogServiceMock;
|
||||||
private readonly Mock<IBasketService> _basketServiceMock;
|
private readonly Mock<IBasketService> _basketServiceMock;
|
||||||
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
||||||
private readonly Mock<HttpContext> _contextMock;
|
private readonly Mock<HttpContext> _contextMock;
|
||||||
|
|
||||||
public CartControllerTest()
|
public CartControllerTest()
|
||||||
{
|
{
|
||||||
_catalogServiceMock = new Mock<ICatalogService>();
|
_catalogServiceMock = new Mock<ICatalogService>();
|
||||||
_basketServiceMock = new Mock<IBasketService>();
|
_basketServiceMock = new Mock<IBasketService>();
|
||||||
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
||||||
_contextMock = new Mock<HttpContext>();
|
_contextMock = new Mock<HttpContext>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_cart_success()
|
public async Task Post_cart_success()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var fakeBuyerId = "1";
|
var fakeBuyerId = "1";
|
||||||
var action = string.Empty;
|
var action = string.Empty;
|
||||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||||
var fakeQuantities = new Dictionary<string, int>()
|
var fakeQuantities = new Dictionary<string, int>()
|
||||||
{
|
{
|
||||||
["fakeProdA"] = 1,
|
["fakeProdA"] = 1,
|
||||||
["fakeProdB"] = 2
|
["fakeProdB"] = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||||
cartController.ControllerContext.HttpContext = _contextMock.Object;
|
cartController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
var actionResult = await cartController.Index(fakeQuantities, action);
|
var actionResult = await cartController.Index(fakeQuantities, action);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_cart_checkout_success()
|
public async Task Post_cart_checkout_success()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var fakeBuyerId = "1";
|
var fakeBuyerId = "1";
|
||||||
var action = "[ Checkout ]";
|
var action = "[ Checkout ]";
|
||||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||||
var fakeQuantities = new Dictionary<string, int>()
|
var fakeQuantities = new Dictionary<string, int>()
|
||||||
{
|
{
|
||||||
["fakeProdA"] = 1,
|
["fakeProdA"] = 1,
|
||||||
["fakeProdB"] = 2
|
["fakeProdB"] = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
var actionResult = await orderController.Index(fakeQuantities, action);
|
var actionResult = await orderController.Index(fakeQuantities, action);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||||
Assert.Equal("Order", redirectToActionResult.ControllerName);
|
Assert.Equal("Order", redirectToActionResult.ControllerName);
|
||||||
Assert.Equal("Create", redirectToActionResult.ActionName);
|
Assert.Equal("Create", redirectToActionResult.ActionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Add_to_cart_success()
|
public async Task Add_to_cart_success()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var fakeCatalogItem = GetFakeCatalogItem();
|
var fakeCatalogItem = GetFakeCatalogItem();
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<Int32>()))
|
_basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<int>()))
|
||||||
.Returns(Task.FromResult(1));
|
.Returns(Task.FromResult(1));
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
var actionResult = await orderController.AddToCart(fakeCatalogItem);
|
var actionResult = await orderController.AddToCart(fakeCatalogItem);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||||
Assert.Equal("Catalog", redirectToActionResult.ControllerName);
|
Assert.Equal("Catalog", redirectToActionResult.ControllerName);
|
||||||
Assert.Equal("Index", redirectToActionResult.ActionName);
|
Assert.Equal("Index", redirectToActionResult.ActionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BasketModel GetFakeBasket(string buyerId)
|
private BasketModel GetFakeBasket(string buyerId)
|
||||||
{
|
{
|
||||||
return new BasketModel()
|
return new BasketModel()
|
||||||
{
|
{
|
||||||
BuyerId = buyerId
|
BuyerId = buyerId
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private CatalogItem GetFakeCatalogItem()
|
private CatalogItem GetFakeCatalogItem()
|
||||||
{
|
{
|
||||||
return new CatalogItem()
|
return new CatalogItem()
|
||||||
{
|
{
|
||||||
Id = 1,
|
Id = 1,
|
||||||
Name = "fakeName",
|
Name = "fakeName",
|
||||||
CatalogBrand = "fakeBrand",
|
CatalogBrand = "fakeBrand",
|
||||||
CatalogType = "fakeType",
|
CatalogType = "fakeType",
|
||||||
CatalogBrandId = 2,
|
CatalogBrandId = 2,
|
||||||
CatalogTypeId = 5,
|
CatalogTypeId = 5,
|
||||||
Price = 20
|
Price = 20
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user