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 onto 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) { // Create the email object first, then add the properties. var myMessage = new SendGridMessage(); // Add the message properties. myMessage.From = new MailAddress("*****@*****.**"); // Add multiple addresses to the To field. List<String> recipients = new List<String> { shippingInfo.Email }; myMessage.AddTo(recipients); myMessage.Subject = "New order submitted!"; StringBuilder body = new StringBuilder() .AppendLine("<h3>A new order has been submitted<h3>") .AppendLine("<p>---</p>") .AppendLine("<p>Items:</p>"); foreach (var line in cart.Lines) { var subtotal = line.Product.Price * line.Quantity; body.AppendFormat("<p>{0} x {1} (subtotal: {2:c}</p>", line.Quantity, line.Product.Name, subtotal); } body.AppendFormat("<p>Total order value: {0:c}</p>", cart.ComputeTotalValue()) .AppendLine("<p>---</p>") .AppendLine("<p>Ship to:</p>") .AppendLine("<p>" + shippingInfo.Name + "</p>") .AppendLine("<p>" + shippingInfo.Line1 + "</p>") .AppendLine("<p>" + shippingInfo.Line2 ?? "" + "</p>") .AppendLine("<p>" + shippingInfo.Line3 ?? "" + "</p>") .AppendLine("<p>" + shippingInfo.City + "</p>") .AppendLine("<p>" + shippingInfo.State ?? "" + "</p>") .AppendLine("<p>" + shippingInfo.Country + "</p>") .AppendLine("<p>" + shippingInfo.Zip + "</p>") .AppendLine("<p>---</p>") .AppendFormat("<p>Gift wrap: {0}</p>", shippingInfo.GiftWrap ? "Yes" : "No"); //Add the HTML and Text bodies myMessage.Html = body.ToString(); // Create credentials, specifying your user name and password var credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password); // Create an Web transport for sending email. var transportWeb = new Web(credentials); // Send the email. // You can also use the **DeliverAsync** method, which returns an awaitable task. transportWeb.DeliverAsync(myMessage); }
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); } }
//[HttpPost] public ViewResult Checkout(Cart cart, ShippingDetails orderDetails) { if (cart.Lines.Count() == 0) { ModelState.AddModelError("", "Sorry, your cart is empty!"); } if (ModelState.IsValid) { orderProcessor.ProcessOrder(cart, orderDetails); cart.Clear(); return View("Completed"); } else { return View(orderDetails); } }
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); 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})\n", 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.Sate ?? "") .AppendLine(shippingInfo.Country) .AppendLine(shippingInfo.Zip) .AppendLine("---") .AppendFormat("Gift wrap: {0}", shippingInfo.GiftWrap ? "Yes" : "No"); MailMessage mailMessage = new MailMessage(new MailAddress(emailSettings.MailFromAddress).Address, new MailAddress(emailSettings.MailToAddress).Address, "New order submitted!", body.ToString()); smtpClient.Send(mailMessage); } }