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("Новый заказ обработан") .AppendLine("---") .AppendLine("Товары:"); foreach (var line in cart.Lines) { var subtotal = line.Game.Price * line.Quantity; body.AppendFormat("{0} x {1} (итого: {2:c}", line.Quantity, line.Game.Name, subtotal); } body.AppendFormat("Общая стоимость: {0:c}", cart.ComputeTotalValue()) .AppendLine("---") .AppendLine("Доставка:") .AppendLine(shippingInfo.Name) .AppendLine(shippingInfo.Line1) .AppendLine(shippingInfo.Line2 ?? "") .AppendLine(shippingInfo.Line3 ?? "") .AppendLine(shippingInfo.City) .AppendLine(shippingInfo.Country) .AppendLine("---") .AppendFormat("Подарочная упаковка: {0}", shippingInfo.GiftWrap ? "Да" : "Нет"); MailMessage mailMessage = new MailMessage( emailSettings.MailFromAddress, // От кого emailSettings.MailToAddress, // Кому "Новый заказ отправлен!", // Тема body.ToString()); // Тело письма if (emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.UTF8; } smtpClient.Send(mailMessage); } }
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 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("Nowe zamowienie") .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.Price, subtotal); } body.AppendFormat("Wartość całkowita : {0:c}", cart.ComputeTotalValue()) .AppendLine("---") .AppendLine("Wysyłka dla: ") .AppendLine(shippingInfo.Name) .AppendLine(shippingInfo.Line1) .AppendLine(shippingInfo.Line2 ?? "") .AppendLine(shippingInfo.Line3 ?? "") .AppendLine(shippingInfo.City) .AppendLine(shippingInfo.State ?? "") .AppendLine(shippingInfo.Zip) .AppendLine("---") .AppendFormat("Pakowanie prezentu: {0}", shippingInfo.GiftWrap ? "Tak" : "Nie"); MailMessage mailMessage = new MailMessage( emailSettings.MailFromAddress, //od kogo emailSettings.MailToAddress, //do kogo "Otrzymano nowe zamówienie!", //temat body.ToString()); // zawartosc generowana automatycznie przez string buildera if(emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.ASCII; } smtpClient.Send(mailMessage); } }
public void Cannot_Checkout_Empty_Cart() { Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>(); Cart cart = new Cart(); ShippingDetails shippingDetails = new ShippingDetails(); CartController target = new CartController(null, mock.Object); ViewResult result = target.Checkout(cart, shippingDetails); //Assert mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never()); Assert.AreEqual("", result.ViewName); Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }
public ViewResult Checkout(Cart cart,ShippingDetails shippingDetails) { if (cart.Lines.Count() == 0) { ModelState.AddModelError("", "Sorry, your cart is empty!"); } if (ModelState.IsValid) { orderprocessor.ProcessOrder(cart, shippingDetails); cart.Clear(); return View("Completed"); } else { return View(shippingDetails); } }
public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails) { if (cart.Lines.Count() == 0) { ModelState.AddModelError("", "Извините, ваша корзина пуста!"); } if (ModelState.IsValid) { orderProcessor.ProcessOrder(cart, shippingDetails); cart.Clear(); return View("Completed"); } else { return View(shippingDetails); } }
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 I am passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }