You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
6.1 KiB

  1. using Basket.API.IntegrationEvents.Events;
  2. using Basket.API.Model;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
  5. using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
  6. using Microsoft.eShopOnContainers.Services.Basket.API.Model;
  7. using Moq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Threading.Tasks;
  11. using Xunit;
  12. using IBasketIdentityService = Microsoft.eShopOnContainers.Services.Basket.API.Services.IIdentityService;
  13. namespace UnitTest.Basket.Application
  14. {
  15. public class BasketWebApiTest
  16. {
  17. private readonly Mock<IBasketRepository> _basketRepositoryMock;
  18. private readonly Mock<IBasketIdentityService> _identityServiceMock;
  19. private readonly Mock<IEventBus> _serviceBusMock;
  20. public BasketWebApiTest()
  21. {
  22. _basketRepositoryMock = new Mock<IBasketRepository>();
  23. _identityServiceMock = new Mock<IBasketIdentityService>();
  24. _serviceBusMock = new Mock<IEventBus>();
  25. }
  26. [Fact]
  27. public async Task Get_customer_basket_success()
  28. {
  29. //Arrange
  30. var fakeCustomerId = "1";
  31. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  32. _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
  33. .Returns(Task.FromResult(fakeCustomerBasket));
  34. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  35. _serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
  36. //Act
  37. var basketController = new BasketController(
  38. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  39. var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
  40. //Assert
  41. Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
  42. Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
  43. }
  44. [Fact]
  45. public async Task Post_customer_basket_success()
  46. {
  47. //Arrange
  48. var fakeCustomerId = "1";
  49. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  50. _basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
  51. .Returns(Task.FromResult(fakeCustomerBasket));
  52. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  53. _serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
  54. //Act
  55. var basketController = new BasketController(
  56. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  57. var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
  58. //Assert
  59. Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
  60. Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
  61. }
  62. [Fact]
  63. public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
  64. {
  65. var fakeCustomerId = "2";
  66. _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
  67. .Returns(Task.FromResult((CustomerBasket)null));
  68. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  69. //Act
  70. var basketController = new BasketController(
  71. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  72. // Fix to init user claim
  73. basketController.ControllerContext = new ControllerContext
  74. {
  75. HttpContext = new DefaultHttpContext
  76. {
  77. User = new ClaimsPrincipal(
  78. new ClaimsIdentity(GetTestingUserClaim()))
  79. }
  80. };
  81. var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
  82. Assert.NotNull(result);
  83. }
  84. [Fact]
  85. public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
  86. {
  87. var fakeCustomerId = "1";
  88. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  89. _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
  90. .Returns(Task.FromResult(fakeCustomerBasket));
  91. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  92. //Act
  93. var basketController = new BasketController(
  94. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  95. // Fix to init user claim
  96. basketController.ControllerContext = new ControllerContext
  97. {
  98. HttpContext = new DefaultHttpContext
  99. {
  100. User = new ClaimsPrincipal(
  101. new ClaimsIdentity(GetTestingUserClaim()))
  102. }
  103. };
  104. var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult;
  105. _serviceBusMock.Verify(mock => mock.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()), Times.Once);
  106. Assert.NotNull(result);
  107. }
  108. private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
  109. {
  110. return new CustomerBasket(fakeCustomerId)
  111. {
  112. Items = new List<BasketItem>()
  113. {
  114. new BasketItem()
  115. }
  116. };
  117. }
  118. /// <summary>
  119. /// Provide Test User Claims
  120. /// </summary>
  121. /// <returns>
  122. /// List of claims
  123. /// </returns>
  124. private IEnumerable<Claim> GetTestingUserClaim()
  125. {
  126. return new List<Claim>
  127. {
  128. new Claim("unique_name", "test")
  129. };
  130. }
  131. }
  132. }