public async Task OrdersController_GetById_Success() { var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, _fixture.Users[0].UserName), new Claim(ClaimTypes.NameIdentifier, _fixture.Users[0].Id) }, "mock")); var userManager = _fixture.ServiceProvider.GetRequiredService <UserManager <User> >(); var controller = new OrdersController(_fixture.OrderService, userManager) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() { User = user } } }; var request = new OrderCreateRequest { Address = "ABC Street, XYZ City", PhoneNumber = "123 456 789", Lines = _fixture.Products.Select(x => new CartLineCreateRequest { ProductId = x.Id, Quantity = 10 }).ToList() }; var createResult = await controller.Checkout(request); var createResponse = GetResponse <OrderCreateResponse>(createResult); Assert.True(createResponse.Id > 0); var getResult = await controller.GetById(createResponse.Id); var getResponse = GetResponse <OrderVm>(getResult); Assert.Equal(createResponse.Id, getResponse.OrderID); }
public void Checkout_WithShoppingCartWithSupplements_ShouldReturnValidViewModel() { //Arrange ShoppingCart shoppingCart = new ShoppingCart() { Supplements = new List <SupplementInCartServiceModel>() { new SupplementInCartServiceModel() } }; byte[] shoppingCartAsByteArray = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(shoppingCart)); Mock <ISession> session = new Mock <ISession>(); session .Setup(s => s.TryGetValue(UserSessionShoppingCartKey, out shoppingCartAsByteArray)) .Returns(true); Mock <HttpContext> httpContext = new Mock <HttpContext>(); httpContext .SetupGet(h => h.Session) .Returns(session.Object); OrdersController ordersController = new OrdersController(null, null, null) { ControllerContext = new ControllerContext { HttpContext = httpContext.Object } }; var result = ordersController.Checkout(); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <ShoppingCart>(); }
public async Task OrdersController_Checkout_Order_Missing_Address_Or_Phonenumber() { var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, _fixture.Users[0].UserName), new Claim(ClaimTypes.NameIdentifier, _fixture.Users[0].Id) }, "mock")); var userManager = _fixture.ServiceProvider.GetRequiredService <UserManager <User> >(); var controller = new OrdersController(_fixture.OrderService, userManager) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() { User = user } } }; controller.ModelState.AddModelError("fakeError", "fakeError"); var request = new OrderCreateRequest { Address = null, PhoneNumber = null, Lines = _fixture.Products.Select(x => new CartLineCreateRequest { ProductId = x.Id, Quantity = 10 }).ToList() }; var result = await controller.Checkout(request); var badResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal(badResult.Value.ToString(), ErrorCodes.ORDER_MISSING_ADDRESS_OR_PHONENUMBER); }
public async Task OrdersController_Checkout_Invalid_Request() { var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "User"), new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()) }, "mock")); var userManager = _fixture.ServiceProvider.GetRequiredService <UserManager <User> >(); var controller = new OrdersController(_fixture.OrderService, userManager) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() { User = user } } }; var request = new OrderCreateRequest { Address = "ABC Street, XYZ City", PhoneNumber = "123 456 789", Lines = _fixture.Products.Select(x => new CartLineCreateRequest { ProductId = x.Id, Quantity = 10 }).ToList() }; var result = await controller.Checkout(request); var badResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal(badResult.Value.ToString(), ErrorCodes.INVALID_REQUEST); }
public async Task OrdersController_GetAllOrders_Success() { var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, _fixture.Users[0].UserName), new Claim(ClaimTypes.NameIdentifier, _fixture.Users[0].Id) }, "mock")); var userManager = _fixture.ServiceProvider.GetRequiredService <UserManager <User> >(); var controller = new OrdersController(_fixture.OrderService, userManager) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() { User = user } } }; var createRequests = new List <OrderCreateRequest> { new OrderCreateRequest { Address = "ABC Street, XYZ City", PhoneNumber = "123 456 789", Lines = _fixture.Products.Skip(0).Take(3).Select(x => new CartLineCreateRequest { ProductId = x.Id, Quantity = 10 }).ToList() }, new OrderCreateRequest { Address = "DEF Street, OPR City", PhoneNumber = "+84 111 222 333", Lines = _fixture.Products.Skip(3).Take(1).Select(x => new CartLineCreateRequest { ProductId = x.Id, Quantity = 4 }).ToList() }, }; foreach (var request in createRequests) { var createResult = await controller.Checkout(request); var createResponse = GetResponse <OrderCreateResponse>(createResult); Assert.True(createResponse.Id > 0); } var getResult = await controller.GetAll(); var getRespone = GetCollectionResponse <OrderVm>(getResult); Assert.Equal(2, getRespone.Count()); foreach (var request in createRequests) { var actualOrder = getRespone.FirstOrDefault(x => x.Address == request.Address && x.PhoneNumber == request.PhoneNumber && x.Lines.Count() == request.Lines.Count()); Assert.NotNull(actualOrder); foreach (var line in request.Lines) { var actualCartLine = actualOrder.Lines .FirstOrDefault(x => x.Product.Id == line.ProductId && x.Quantity == line.Quantity); Assert.NotNull(actualCartLine); } } }