public void GetListFromCookies_IfEmptyReturnNull() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { List <OrderedBook> emptyOrderedBookList = new List <OrderedBook>(); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; homeController.HttpContext.Session.Set(WebConst.SessionCart, emptyOrderedBookList); var result = homeController.GetListFromCookies(); Assert.Null(result); } }
public void RedirectToBooksByPublishers_CheckRedirectToBooksByPublisher() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var formCollection = new FormCollection(new Dictionary <string, StringValues>() { { "Publishers", "1" } }); var controllerContext = new ControllerContext() { HttpContext = Mock.Of <HttpContext>(ctx => ctx.Request.Form == formCollection) }; homeController.ControllerContext = controllerContext; var action = homeController.RedirectToBooksByPublishers() as RedirectToActionResult; Assert.Equal("BooksByPublisher", action.ActionName); Assert.Equal("Home", action.ControllerName); Assert.Equal("1", action.RouteValues["publisherId"].ToString()); } }
public void GetListFromCookies_IfNotEmptyReturnList() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { OrderedBook orderedBook1 = new OrderedBook { BookId = 1, Quantity = 3 }; OrderedBook orderedBook2 = new OrderedBook { BookId = 2, Quantity = 4 }; OrderedBook orderedBook3 = new OrderedBook { BookId = 3, Quantity = 1 }; List <OrderedBook> mockOrderedBookList = new List <OrderedBook>(); mockOrderedBookList.Add(orderedBook1); mockOrderedBookList.Add(orderedBook2); mockOrderedBookList.Add(orderedBook3); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; homeController.HttpContext.Session.Set(WebConst.SessionCart, mockOrderedBookList); var result = homeController.GetListFromCookies(); Assert.Equal(3, result.Count); Assert.Equal(4, result.FirstOrDefault(x => x.BookId == 2)?.Quantity ?? 0); } }
public void CheckOut_InvalidModelOfShoppingCartViewModel() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var user = new User { UserName = "******", Email = "*****@*****.**", Phone = "+48 605 337 334" }; var billingAddress = new Address { Country = "Poland", City = "Warsaw", ZipCode = "01-111", Street = "Marszałkowska 123A" }; var scvm = new ShoppingCartViewModel() { Order = new Order { UserId = 2, Payment = true, Draft = false, Date = new DateTime(2021, 2, 27, 9, 10, 23), Status = null, TotalPrice = 0.00, BillingAddress = billingAddress, ShippingAddress = billingAddress, User = user }, Basket = MockData.GetRandomMoqOrderedBooksList().ToList(), ShippingEqualBilling = true }; homeController.ModelState.AddModelError(string.Empty, "Test Error"); Assert.False(homeController.ModelState.IsValid, "Model state has remained valid."); // Test for BadRequest being returned var result = homeController.CheckOut(scvm) as ViewResult; var model = result?.Model; Assert.Equal("ShoppingCart", result?.ViewName); Assert.True(model.IsSameOrEqualTo(scvm)); } }
public void AddToAddressDBOrGetID_AddressNotExists() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var address = new Address { Street = "Testing 23", City = "Warsaw", Country = "Poland", ZipCode = "12-678" }; var result = homeController.AddToAddressDBOrGetID(address); Assert.Equal(6, result); } }
public void AddToAddressDBOrGetID_AddressExists() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var address1 = new Address { Street = "Spacerowa 23", City = "Warsaw", Country = "Poland", ZipCode = "12-678" }; var address2 = new Address { Street = "Pl. Zwyciestwa 12", City = "Cracow", Country = "Poland", ZipCode = "12-345" }; var result1 = homeController.AddToAddressDBOrGetID(address1); var result2 = homeController.AddToAddressDBOrGetID(address2); Assert.Equal(4, result1); Assert.Equal(2, result2); } }
public void PaymentPost_ValidModelOfPaymentViewModel() { var context = MockData.MoqShopContext(); using (var homeController = new HomeController(MockData.MoqLogger(), context, MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var pvm1 = MockData.GetMoqPaymentViewModel(3, 499.99); var pvm2 = MockData.GetMoqPaymentViewModel(1, 500.00);; // Order does not exists var pvm3 = MockData.GetMoqPaymentViewModel(25, 230.45); var result1 = homeController.PaymentPost(pvm1) as RedirectToActionResult; var result2 = homeController.PaymentPost(pvm2) as RedirectToActionResult; var result3 = homeController.PaymentPost(pvm3) as RedirectToActionResult; var orderPayment1 = context?.Orders?.FirstOrDefault(o => o.OrderId == pvm1.OrderId)?.Payment; var orderPayment2 = context?.Orders?.FirstOrDefault(o => o.OrderId == pvm2.OrderId)?.Payment; var orderPayment3 = context?.Orders?.FirstOrDefault(o => o.OrderId == pvm3.OrderId)?.Payment; Assert.Equal("OrderConfirmation", result1?.ActionName); Assert.Equal("True", result1?.RouteValues["success"]?.ToString()); Assert.Equal("499.99", result1?.RouteValues["price"]?.ToString()); Assert.Equal("3", result1?.RouteValues["id"]?.ToString()); Assert.Equal(orderPayment1, pvm1.SuccessfulPayment); Assert.Equal("OrderConfirmation", result2?.ActionName); Assert.Equal("False", result2?.RouteValues["success"]?.ToString()); Assert.Equal("500", result2?.RouteValues["price"]?.ToString()); Assert.Equal("1", result2?.RouteValues["id"]?.ToString()); Assert.Equal(orderPayment2, pvm2.SuccessfulPayment); Assert.Equal("OrderConfirmation", result3?.ActionName); Assert.Equal("True", result3?.RouteValues["success"]?.ToString()); Assert.Equal("230.45", result3?.RouteValues["price"]?.ToString()); Assert.Equal("25", result3?.RouteValues["id"]?.ToString()); Assert.Null(orderPayment3); Assert.Equal("OrderConfirmation", result1?.ActionName); Assert.Equal("OrderConfirmation", result2?.ActionName); Assert.Equal("OrderConfirmation", result3?.ActionName); } }
public void BooksByGenre_ShouldReturnValidModel(int genreId) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var result = homeController.BooksByGenre(genreId) as ViewResult; var model = result?.Model as IEnumerable <Book>; Assert.NotNull(model); Assert.Equal("Computing", model.FirstOrDefault(x => x.Genre.GenreId == genreId)?.Genre.GenreName); Assert.Equal(2, model.Count()); } }
public void Payment_CheckPaymentViewModel(int orderId, double totalPrice) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var result = homeController.Payment(orderId, totalPrice) as ViewResult; var model = result?.Model as PaymentViewModel; Assert.NotNull(model); Assert.NotNull(result); Assert.IsAssignableFrom <ViewResult>(result); Assert.Equal(orderId, model.OrderId); Assert.Equal(totalPrice, model.TotalPrice); } }
public void Index_ShouldReturnDefaultView() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var result = homeController.Index() as ViewResult; Assert.NotNull(result); Assert.True(string.IsNullOrEmpty(result.ViewName) || result.ViewName == "Index"); Assert.IsAssignableFrom <ViewResult>(result); } }
public void BooksByPublisherPost_CheckRedirectionAndSession(int id, int quantity, int pId) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var formCollection = new FormCollection(new Dictionary <string, StringValues>() { { "Publishers", pId.ToString() } }); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext { Session = new MockHttpSession(), Request = { Form = formCollection } }, }; homeController.ControllerContext = controllerContext; var action = homeController.BooksByPublisherPost(id, quantity, pId) as RedirectToActionResult; Assert.Equal("BooksByPublisher", action.ActionName); Assert.Equal(pId.ToString(), action.RouteValues["publisherId"].ToString()); } }
public void BookDetailsPost_CheckRedirectionAndSession(int id, int quantity) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; var action = homeController.BookDetailsPost(id, quantity) as RedirectToActionResult; Assert.Equal("BookDetails", action.ActionName); } }
public void AddBooksToSessionBasket_CheckAddItemToNonEmptyBasket(int id, int quantity) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { OrderedBook orderedBook1 = new OrderedBook { BookId = 1, Quantity = 3 }; OrderedBook orderedBook2 = new OrderedBook { BookId = 2, Quantity = 4 }; OrderedBook orderedBook3 = new OrderedBook { BookId = 3, Quantity = 1 }; List <OrderedBook> mockOrderedBookList = new List <OrderedBook>(); mockOrderedBookList.Add(orderedBook1); mockOrderedBookList.Add(orderedBook2); mockOrderedBookList.Add(orderedBook3); int quantityFromSession = mockOrderedBookList.FirstOrDefault(x => x.BookId == id)?.Quantity ?? 0; var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; homeController.HttpContext.Session.Set(WebConst.SessionCart, mockOrderedBookList); var result = homeController.AddBooksToSessionBasket(id, quantity).ToList(); int actualQuantity = result.FirstOrDefault(x => x.BookId == id)?.Quantity ?? 0; int actualId = result.FirstOrDefault(x => x.BookId == id)?.BookId ?? 0; if (actualId == 0 && actualQuantity == 0) { Assert.True(mockOrderedBookList.Count == result.Count + 1); } else { Assert.Equal(id, actualId); Assert.Equal(quantity + quantityFromSession, actualQuantity); } } }
public void AddBooksToSessionBasket_CheckAddItemToEmptyBasket(int id, int quantity) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { OrderedBook orderedBook = new OrderedBook { BookId = id, Quantity = quantity }; List <OrderedBook> expectedOrderedBookList = new List <OrderedBook>(); expectedOrderedBookList.Add(orderedBook); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; var result = homeController.AddBooksToSessionBasket(id, quantity).ToList(); int actualQuantity = result.FirstOrDefault(x => x.BookId == id)?.Quantity ?? 0; int actualId = result.FirstOrDefault(x => x.BookId == id)?.BookId ?? 0; Assert.Equal(id, actualId); Assert.Equal(quantity, actualQuantity); } }
public void BookDetails_ShouldReturnValidModel(int bookId) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var result = homeController.BookDetails(bookId) as ViewResult; var model = result?.Model as Book; Assert.NotNull(model); Assert.Equal(1, model.BookId); Assert.Equal("C# (C Sharp Programming) : A Step by Step Guide for Beginners", model.Title); } }
public void ShoppingCart_IfNullRedirectToIndex() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; homeController.HttpContext.Session.Set(WebConst.SessionCart, (List <OrderedBook>)null); var action = homeController.ShoppingCart() as RedirectToActionResult; Assert.Equal("Index", action.ActionName); } }
public void CheckOut_ValidModelOfShoppingCartViewModelDifferentAddresses() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var user = new User { UserName = "******", Email = "*****@*****.**", Phone = "+48 605 337 334" }; var billingAddress = new Address { Country = "Poland", City = "Warsaw", ZipCode = "01-111", Street = "Marszałkowska 123A" }; var shippingAddress = new Address { Country = "Poland", City = "Cracow", ZipCode = "03-456", Street = "Spacerowa 31" }; var scvm = new ShoppingCartViewModel() { Order = new Order { UserId = 2, Payment = true, Draft = false, Date = new DateTime(2021, 2, 27, 9, 10, 23), Status = null, TotalPrice = 0.00, BillingAddress = billingAddress, ShippingAddress = shippingAddress, User = user }, Basket = MockData.GetRandomMoqOrderedBooksList().ToList(), ShippingEqualBilling = false }; double totalPrice = 0; foreach (var book in scvm.Basket) { totalPrice += book.Price * book.Quantity; } totalPrice = Math.Round(totalPrice, 2, MidpointRounding.AwayFromZero); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; homeController.HttpContext.Session.Set("userId", "8befe3cc-024b-4084-ab38-8f5c7663cd1d"); var action = homeController.CheckOut(scvm) as RedirectToActionResult; Assert.Equal("Payment", action?.ActionName); Assert.Equal("5", action?.RouteValues["orderId"]?.ToString()); Assert.Equal(totalPrice.ToString(CultureInfo.InvariantCulture), action?.RouteValues["totalPrice"]?.ToString()); } }
public void Index_ShouldReturnValidModel() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var result = homeController.Index() as ViewResult; var model = result?.Model as BookGenrePublisher; Assert.NotNull(model); Assert.NotNull(model.Books); Assert.NotNull(model.Genres); Assert.NotNull(model.Publishers); Assert.Equal(6, model.Genres.Count()); Assert.Equal(6, model.Publishers.Count()); Assert.Equal(11, model.Books.Count()); Assert.Equal("Little Brown Book Group", model.Books.Single(x => x.BookId == 11).Publisher.PublisherName); } }
public void OrderConfirmation_CheckStatusOfOrder(bool success, double price, int id) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var culture = CultureInfo.CreateSpecificCulture("pl-PL"); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; var compareContext = MockData.MoqShopContext(); var userId = compareContext? .Orders.Include(u => u.User)? .SingleOrDefault(o => o.OrderId == id)?.User.UserId; if (userId == 1) { homeController.HttpContext.Session.Set("userId", "6040582f-0c20-44dc-8acc-b684479454b0"); } else if (userId == 2) { homeController.HttpContext.Session.Set("userId", "8befe3cc-024b-4084-ab38-8f5c7663cd1d"); } var result = homeController.OrderConfirmation(success, price, id) as ViewResult; (int id, double price, bool success)model = ((int id, double price, bool success))result.Model; ViewDataDictionary viewData = result?.ViewData; if (success) { Assert.Equal($"Thank You for your order! {price.ToString("0.00", culture)} zł was successfully charged from your bank account.", viewData?["Message"]); } else { Assert.Equal($"We couldn't charge your account...", viewData?["Message"]); } Assert.NotNull(result); Assert.NotNull(viewData); Assert.IsAssignableFrom <ViewResult>(result); Assert.Equal("OrderConfirmation", result.ViewName); Assert.Equal(id, model.id); Assert.Equal(price, model.price); Assert.Equal(success, model.success); } }
public void ShoppingCart_ShouldReturnValidModel() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { OrderedBook orderedBook1 = new OrderedBook { BookId = 1, Quantity = 3 }; OrderedBook orderedBook2 = new OrderedBook { BookId = 2, Quantity = 4 }; OrderedBook orderedBook3 = new OrderedBook { BookId = 3, Quantity = 2 }; List <OrderedBook> mockOrderedBookList = new List <OrderedBook>(); mockOrderedBookList.Add(orderedBook1); mockOrderedBookList.Add(orderedBook2); mockOrderedBookList.Add(orderedBook3); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; homeController.HttpContext.Session.Set(WebConst.SessionCart, mockOrderedBookList); var result = homeController.ShoppingCart() as ViewResult; var model = result?.Model as ShoppingCartViewModel; Assert.NotNull(model); Assert.NotNull(model.Basket); Assert.Equal("The House in the Cerulean Sea", model.Basket.FirstOrDefault(x => x.BookId == 3)?.Title); Assert.Equal(2, model.Basket.FirstOrDefault(x => x.BookId == 3)?.Quantity); Assert.Equal(70.1, model.Basket.FirstOrDefault(x => x.BookId == 3)?.Price); } }
public void PaymentPost_InvalidModelOfPaymentViewModel() { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var pvm = new PaymentViewModel { SuccessfulPayment = false, CardName = "Jan Nowak", CardNumber = "3714 4963 539 8431", CardExp = "12.2023", CardCVV = 123, OrderId = 3, TotalPrice = 0.00 }; homeController.ModelState.AddModelError(string.Empty, "Test Error"); Assert.False(homeController.ModelState.IsValid, "Model state has remained valid."); // Test for BadRequest being returned var result = homeController.PaymentPost(pvm) as ViewResult; var model = result?.Model; Assert.Equal("Payment", result?.ViewName); Assert.True(model.IsSameOrEqualTo(pvm)); } }
public void ShoppingCartUpdate_CheckRedirection(int id, int value) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { OrderedBook orderedBook1 = new OrderedBook { BookId = 1, Quantity = 3 }; OrderedBook orderedBook2 = new OrderedBook { BookId = 2, Quantity = 4 }; OrderedBook orderedBook3 = new OrderedBook { BookId = 4, Quantity = 2 }; OrderedBook orderedBook4 = new OrderedBook { BookId = 3, Quantity = 3 }; List <OrderedBook> mockOrderedBookList = new List <OrderedBook>(); mockOrderedBookList.Add(orderedBook1); mockOrderedBookList.Add(orderedBook2); mockOrderedBookList.Add(orderedBook3); mockOrderedBookList.Add(orderedBook4); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { Session = new MockHttpSession() } }; homeController.ControllerContext = controllerContext; homeController.HttpContext.Session.Set(WebConst.SessionCart, mockOrderedBookList); var action = homeController.ShoppingCartUpdate(id, value) as RedirectToActionResult; Assert.Equal("ShoppingCart", action.ActionName); } }
public static shopContext MoqShopContext() { var moqDatabase = new MockData(); return(moqDatabase.GetTestShopContext()); }
public void BooksByPublisher_ShouldReturnValidModel(int publisherId) { using (var homeController = new HomeController(MockData.MoqLogger(), MockData.MoqShopContext(), MockData.MoqEmailSender(), MockData.MoqMyLogger())) { var result = homeController.BooksByPublisher(publisherId) as ViewResult; var model = result?.Model as IEnumerable <Book>; Assert.NotNull(model); Assert.Equal("Neustar, Inc.", model.FirstOrDefault(x => x.Publisher.PublisherId == publisherId)?.Publisher.PublisherName); Assert.Equal(3, model.Count()); } }