public void ProcessOrder(Cart cart, ShippingDetails shippingInfo) { using (var smtpClient = new SmtpClient()) { } }
public void Can_Send_Email() { // Arrange - create and populate a cart Cart cart = new Cart(); cart.AddItem(new Product { ProductID = 1, Name = "Banana", Price = 10M }, 2); cart.AddItem(new Product { ProductID = 2, Name = "Apple", Price = 5M }, 2); // Arrange - create and populate some shipping details ShippingDetails shipDetails = new ShippingDetails { Name = "Joe Smith", Line1 = "Apartment 4a", Line2 = "123 West Street", City = "Northtown", State = "GA", Country = "USA", Zip = "12345" }; // Arrange - create the test-specific email settings EmailSettings settings = new EmailSettings { // put test specific settings here WriteAsFile = true }; // Arrange - create the EmailOrderProcessor class EmailOrderProcessor proc = new EmailOrderProcessor(settings); // Act - process the order proc.ProcessOrder(cart, shipDetails); // NOTE - there is assert in this test }
public RedirectToRouteResult RemoveFromCart(Cart cart, int id, string returnUrl) { Product product = _repository.Products.FirstOrDefault(p => p.Id == id); if(product != null) cart.RemoveLine(product); return RedirectToAction("Index", new {returnUrl}); }
private MailMessage BuildMailMessage(Cart cart, ShippingDetails shippingDetails) { StringBuilder body = new StringBuilder(); body.AppendLine("A new order has been submitted"); body.AppendLine("---"); body.AppendLine("Items:"); foreach (var line in cart.Lines) { var subtotal = line.Product.Price * line.Quantity; body.AppendFormat("{0} x {1} (subtotal: {2:c}", line.Quantity, line.Product.Name, subtotal); } body.AppendFormat("Total order value: {0:c}", cart.ComputeTotalValue()); body.AppendLine("---"); body.AppendLine("Ship to:"); body.AppendLine(shippingDetails.Name); body.AppendLine(shippingDetails.Line1); body.AppendLine(shippingDetails.Line2 ?? ""); body.AppendLine(shippingDetails.Line3 ?? ""); body.AppendLine(shippingDetails.City); body.AppendLine(shippingDetails.State ?? ""); body.AppendLine(shippingDetails.Country); body.AppendLine(shippingDetails.Zip); body.AppendLine("---"); body.AppendFormat("Gift wrap: {0}", shippingDetails.GiftWrap ? "Yes" : "No"); return new MailMessage("*****@*****.**", // From mailTo, // To "New order submitted!", // Subject body.ToString()); // Body }
public void Cannot_Checkout_Empty_Cart() { // Arragne - Mock 주문 처리기 생성 Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>(); // Arrange - 빈 Cart 개체 생성 Cart cart = new Cart(); // Arragne - 배송 정보 생성 ShippingDetails shippingDetails = new ShippingDetails(); // Arrange - 컨트롤러의 인스턴스 생성 CartController target = new CartController(null, mock.Object); // Act ViewResult result = target.Checkout(cart, shippingDetails); // Assert - 주문이 주문 처리기에 전달되지 않은 것을 확인한다. mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never()); // Assert - 메서드가 기본 뷰를 반환했는지 확인한다. Assert.AreEqual("", result.ViewName); // Assert - 유효하지 않은 모델을 뷰에 전달하는지 확인한다 Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }
public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) { Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId); if (product != null) { cart.AddItem(product, 1); } return RedirectToAction("Index", new { returnUrl }); }
public void ProcessOrder(Cart cart, ShippingDetails shippingInfo) { using (var smtpClient = new SmtpClient()) { smtpClient.EnableSsl = emailSettings.UseSsl; smtpClient.Host = emailSettings.ServerName; smtpClient.Port = emailSettings.ServerPort; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password); if (emailSettings.WriteAsFile) { smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; smtpClient.PickupDirectoryLocation = emailSettings.FileLocation; smtpClient.EnableSsl = false; } var body = buildMessageBody(cart, shippingInfo); MailMessage mailMessage = new MailMessage( emailSettings.MailFromAddress, // From emailSettings.MailToAddress, // To "New order submitted!", // Subject body.ToString()); // Body if (emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.ASCII; } smtpClient.Send(mailMessage); } }
public void Cannot_Checkout_Invalid_ShippingDetails() { // Arrange - create a mock order processor Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>(); // Arrange - create a cart with an item Cart cart = new Cart(); cart.AddItem(new Product(), 1); // Arrange - create an instance of the controller CartController target = new CartController(null, mock.Object); // Arrange - add an error to the model target.ModelState.AddModelError("error", "error"); // Act - try to checkout ViewResult result = target.Checkout(cart, new ShippingDetails()); // Assert - check that the order hasn't been passed on to the processor mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never()); // Assert - check that the method is returning the default view Assert.AreEqual("", result.ViewName); // Assert - check that we are passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }
public void ProcessOrder(Cart cart, ShippingDetails shippingInfo) { using (var smtpClient = new SmtpClient()) { smtpClient.EnableSsl = emailSettings.UseSsl; smtpClient.Host = emailSettings.ServerName; smtpClient.Port = emailSettings.ServerPort; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password); if (emailSettings.WriteAsFile) { smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; smtpClient.PickupDirectoryLocation = emailSettings.FileLocation; smtpClient.EnableSsl = false; } StringBuilder body = new StringBuilder() .AppendLine("A new order has been submitted") .AppendLine("---") .AppendLine("Items:"); foreach (var line in cart.Lines) { var subtotal = line.Product.Price * line.Quantity; body.AppendFormat("{0} x {1} (subtotal: {2:c}", line.Quantity, line.Product.Name, subtotal); } body.AppendFormat("Total order value: {0:c}", cart.ComputeTotalValue()) .AppendLine("---") .AppendLine("Ship to:") .AppendLine(shippingInfo.Name) .AppendLine(shippingInfo.Line1) .AppendLine(shippingInfo.Line2 ?? "") .AppendLine(shippingInfo.Line3 ?? "") .AppendLine(shippingInfo.City) .AppendLine(shippingInfo.State ?? "") .AppendLine(shippingInfo.Country) .AppendLine(shippingInfo.Zip) .AppendLine("---") .AppendFormat("Gift wrap: {0}", shippingInfo.GiftWrap ? "Yes" : "No"); MailMessage mailMessage = new MailMessage( emailSettings.MailFromAddress, // From emailSettings.MailToAddress, // To "New order submitted!", // Subject body.ToString()); // Body if (emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.ASCII; } //smtpClient.Send(mailMessage); } }
public void ProcessorOrder(Cart cart, ShippingDetails shipping) { using(var smtpClient = new SmtpClient(emailSettings.Servername, emailSettings.Serverport)) { smtpClient.EnableSsl = emailSettings.UseSsl; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password); if(emailSettings.WriteAsFile) { smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; smtpClient.PickupDirectoryLocation = emailSettings.FileLocation; smtpClient.EnableSsl = false; } StringBuilder body = new StringBuilder(); body.AppendLine("A new order has been submitted"); body.AppendLine("---"); body.AppendLine("Items: "); foreach (var item in cart.Lines) { var subtotal = item.Product.Price*item.Quantity; body.AppendFormat("{0} x {1} (subtotal: {2})", item.Quantity, item.Product.Price, subtotal.ToString("c")); } body.AppendFormat("Total order value: {0}", cart.ComputeTotalValue().ToString("c")) .AppendLine("---") .AppendLine("Ship to: ") .AppendLine(shipping.Name) .AppendLine(shipping.Line1) .AppendLine(shipping.Line2 ?? "") .AppendLine(shipping.Line3 ?? "") .AppendLine(shipping.City) .AppendLine(shipping.State ?? "") .AppendLine(shipping.Country) .AppendLine(shipping.Zip) .AppendLine("---") .AppendFormat("Gift wrap: {0}", shipping.GiftWrap ? "Yes" : "No") ; MailMessage mailMessage = new MailMessage(emailSettings.MailFromAddress, emailSettings.MailToAddress, "New order submitted!", body.ToString()); if(emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.ASCII; } try { smtpClient.Send(mailMessage); } catch (Exception exception) { Debug.WriteLine("Couldn't send mail: " + exception.Message); } } }
public RedirectToRouteResult AddToCard(Cart cart, int productId, string returnUrl) { var product = _productRepository.All.FirstOrDefault(p => p.ProductId == productId); if (product != null) cart.Add(product, 1); return RedirectToAction("Index", new {returnUrl}); }
public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl) { var product = _productRepository.All.FirstOrDefault(p => p.ProductId == productId); if(product != null) cart.RemoveLine(product); return RedirectToAction("Index",new {returnUrl}); }
public ViewResult Index(Cart cart, string returnUrl) { CartIndexViewModel cartIndexVM = new CartIndexViewModel(); cartIndexVM.Cart = cart; cartIndexVM.ReturnUrl = returnUrl; return View(cartIndexVM); }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // get the cart from the session Cart cart = null; if (controllerContext.HttpContext.Session != null) { cart = (Cart)controllerContext.HttpContext.Session[sessionKey]; } // create the cart if there wasn't one in the session data if (cart == null) { cart = new Cart(); if (controllerContext.HttpContext.Session != null) { controllerContext.HttpContext.Session[sessionKey] = cart; } } // return silly cart return cart; }
public ViewResult Index(Cart cart, string returnUrl) { return View(new CartIndexViewModel { Cart = cart, ReturnUrl = returnUrl }); }
public void Adding_Product_To_Cart_Goes_To_Cart_Screen() { // Arrange // - Create the mock repository Mock<IProductRepository> mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1", Category = "Apples" } }.AsQueryable()); // Arrange // - Create a Cart Cart cart = new Cart(); // Arrange // - Create the controller CartController target = new CartController(mock.Object, null); // Act // - Add a product to the cart RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl"); // Assert Assert.AreEqual(result.RouteValues["action"], "Index"); Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl"); }
private string CombineMessage(Cart cart, ShippingDetails shippingDetails) { StringBuilder body = new StringBuilder() .AppendLine("A new order has been submitted") .AppendLine("---") .AppendLine("Items:"); foreach (var line in cart.Lines) { var subtotal = line.Product.Price * line.Quantity; body.AppendFormat("{0} x {1} (subtotal: {2:c})", line.Quantity, line.Product.Name, subtotal); } body.AppendFormat("Total order value: {0:c}", cart.ComputeTotalValue()) .AppendLine("---") .AppendLine("Ship to:") .AppendLine(shippingDetails.Name) .AppendLine(shippingDetails.Line1) .AppendLine(shippingDetails.Line2 ?? "") .AppendLine(shippingDetails.Line3 ?? "") .AppendLine(shippingDetails.City) .AppendLine(shippingDetails.State ?? "") .AppendLine(shippingDetails.Country) .AppendLine(shippingDetails.Zip) .AppendLine("---") .AppendFormat("Gift wrap: {0}", shippingDetails.GiftWrap ? "Yes" : "No"); return body.ToString(); }
public ViewResult List(Cart cart, string category, int page = 1) { var products = _productRepository.Products .Where(p => category == null || p.Category == category) .OrderBy(p => p.ProductID) .Skip((page - 1)*PageSize) .Take(PageSize); string carttotal = cart.ComputeTotalValue().ToString("c"); int quantity = cart.Lines.Sum(x => x.Quantity); ProductListViewModel viewModel = new ProductListViewModel { Products = products, PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null ? _productRepository.Products.Count() : _productRepository.Products.Count(x => x.Category == category) }, CurrentCategory = category, CartTotal = carttotal, CartQuantity = quantity }; return View(viewModel); }
public void Cannot_Checkout_Empty_Cart() { // Arrange - create a mock order processor Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>(); // Arrange - create an empty cart Cart cart = new Cart(); // Arrange - create shipping details ShippingDetails shippingDetails = new ShippingDetails(); // Arrange - create an instance of the controller CartController target = new CartController(null, mock.Object); // Act ViewResult result = target.Checkout(cart, shippingDetails); // Assert - check that the order hasn't been passed on to the processor mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never()); // Assert - check that the method is returning the default view Assert.AreEqual("", result.ViewName); // Assert - check that we are passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }
public void ProcessOrder(Cart cart, ShippingDetails shippingInfo) { using (var smtpClient = new SmtpClient()) { smtpClient.EnableSsl = emailSettings.UseSsl; smtpClient.Host = emailSettings.ServerName; smtpClient.Port = emailSettings.ServerPort; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(emailSettings.UserName, emailSettings.Password); smtpClient.Timeout = 10000; if (emailSettings.WriteAsFile) { smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; smtpClient.PickupDirectoryLocation = emailSettings.FileLocation; smtpClient.EnableSsl = false; } StringBuilder body = new StringBuilder() .AppendLine("Nowe zamówienie") .AppendLine("---") .AppendLine("Produkty:"); foreach (var line in cart.Lines) { var subtotal = line.Product.Price*line.Quantity; body.AppendFormat("{0} x {1} (wartość: {2:c})", line.Quantity, line.Product.Name, subtotal); } body.AppendFormat("Wartosc calkowita:{0:c}", cart.ComputeTotalValue()) .AppendLine("---") .AppendLine("Wysylka dla:") .AppendLine(shippingInfo.Name) .AppendLine(shippingInfo.Line1) .AppendLine(shippingInfo.Line2 ?? "") .AppendLine(shippingInfo.Line3 ?? "") .AppendLine(shippingInfo.City) .AppendLine(shippingInfo.State ?? "") .AppendLine(shippingInfo.Country) .AppendLine(shippingInfo.Zip) .AppendLine("---") .AppendFormat("Pakowanie prezentu: {0}", shippingInfo.GiftWrap ? "Tak" : "Nie"); MailMessage mailMessage = new MailMessage( emailSettings.MailFromAddress, emailSettings.MailToAddress, "Otrzymano nowe zamówienie!", body.ToString()); mailMessage.Priority = MailPriority.High; if (emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.ASCII; } smtpClient.Send(mailMessage); } }
private Cart GetCart() { Cart cart = (Cart) Session["Cart"]; if (cart == null) { cart = new Cart(); Session["Cart"] = cart; } return cart; }
public void SubmitOrder(Cart cart, ShippingDetails shippingDetails) { // If you're using .NET 4, you need to dispose the objects, so write this: using (var smtpClient = new SmtpClient()) using (var mailMessage = BuildMailMessage(cart, shippingDetails)) { smtpClient.Send(mailMessage); } }
public ViewResult Index(Cart cart, string returnUrl) { return View(new Models.CartIndexViewModel { ReturnUrl = returnUrl, Cart = cart }); }
public RedirectToRouteResult RemoveFromCart(Cart cart,int productId, string returnUrl) { Product prod = repository.Products.FirstOrDefault(p=>p.ProductID==productId); if (prod!=null){ cart.RemoveLine(prod); } return RedirectToAction("Index", new { returnUrl }); }
public void SubmitOrder(Cart cart, ShippingDetails shippingDetails) { using (var smtpClient = new SmtpClient()) using (var mailMessage = BuildMailMessage(cart, shippingDetails)) { smtpClient.Send(mailMessage); } }
public ViewResult CheckOut(Cart cart, ShippingDetails Details) { if (!cart.Lines.Any()) { ModelState.AddModelError("", "Cart is empty"); } return View(); }
public void After_Checking_Out_Cart_Is_Emptied() { var cart = new Cart(); cart.AddItem(new Product(), 1); new CartController(null, new Mock<IOrderSubmitter>().Object).CheckOut(cart, new ShippingDetails()); ; }
public void Cannot_Check_Out_If_Cart_Is_Empty() { var emptyCart = new Cart(); var shippingDetails = new ShippingDetails(); var result = new CartController(null, null).CheckOut(emptyCart, shippingDetails); result.ShouldBeDefaultView(); }
public ActionResult AjaxRemove(Cart cart, int productId) { Product product = _productRepository.Products.FirstOrDefault(p => p.ProductID == productId); if (product != null) cart.RemoveLine(product); return Json(new { success = true, cart }); }
public ViewResult Index(Cart cart, string returnUrl) { return View(new CartIndexViewModel { //Cart = GetCart(), ReturnUrl = returnUrl, Cart = cart }); }