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 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 void ProcessOrder(Cart cart, ShippingDetails shippingInfo)
        {
            using (var smtpClient = new SmtpClient())
            {

            }
        }
        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 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.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);
            }
        }
        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();
        }
Exemple #9
0
        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 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);
            }
        }
 public ViewResult CheckOut(Cart cart, ShippingDetails Details)
 {
     if (!cart.Lines.Any())
     {
         ModelState.AddModelError("", "Cart is empty");
     }
     return View();
 }
 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 void SubmitOrder(Cart cart, ShippingDetails shippingDetails)
 {
     using (var smtpClient = new SmtpClient())
     using (var mailMessage = BuildMailMessage(cart, shippingDetails))
     {
         smtpClient.Send(mailMessage);
     }
 }
        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 void ProcessOrder(Cart cart, ShippingDetails shippingDetails)
        {
            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 being 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.Zip ?? "")
                    .AppendLine(shippingDetails.Country)
                    .AppendLine("---")
                    .AppendFormat("Gift wrap: {0}", shippingDetails.GiftWrap ? "Yes" : "No");

                MailMessage message = new MailMessage();
                AlternateView plainView = AlternateView.CreateAlternateViewFromString(body.ToString(), null, MediaTypeNames.Text.Plain);
                message.From = new MailAddress(emailSettings.MailFromAddress);
                message.To.Add(emailSettings.MailToAddress);
                message.Subject = "New order submitted";
                message.AlternateViews.Add(plainView);

                if (emailSettings.WriteAsFile)
                {
                    message.BodyEncoding = Encoding.ASCII;
                }

                smtpClient.Send(message);
            }
        }
Exemple #16
0
        public void Cannot_Check_Out_If_Cart_Is_Empty()
        {
            // Arrange/act: When a user tries to check out with an empty cart
            var emptyCart = new Cart();
            var shippingDetails = new ShippingDetails();
            var result = new CartController(null, null).CheckOut(emptyCart, shippingDetails);

            // Assert
            result.ShouldBeDefaultView();
        }
Exemple #17
0
        public void ProcessOrder(Cart cart, SportsStore.Domain.Entities.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("---")
                                     .Append("Items");

                foreach (var line in cart.Lines)
                {
                    var subtotal = line.Product.Price * line.Quantity;
                    body.AppendFormat("{0} x {1} (subtotl: {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 ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
 {
     if (ModelState.IsValid)
     {
         return View();
     }
     else
     {
         return View(new ShippingDetails());
     }
 }
 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");
     }
     return View(new ShippingDetails());
 }
        public ViewResult CheckOut(CartRepository cart, ShippingDetails shippingDetails)
        {
            if (cart.CartLineCollections.Count == 0)
                ModelState.AddModelError("", "Koszyk jest pusty");

            if (ModelState.IsValid)
            {
                _proc.ProcessOrder(cart, shippingDetails);
                cart.Clear();
                return View("Completed");
            }
            return View(shippingDetails);
        }
        public void ProcessOrder(Cart cart, ShippingDetails shippingDetails)
        {
            using (SmtpClient smtpClient = new SmtpClient())
            {
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                smtpClient.PickupDirectoryLocation = emailSettings.FileLocation;

                MailMessage mailMessage = new MailMessage(emailSettings.From, emailSettings.To, "New Order", "Body");
                mailMessage.BodyEncoding = Encoding.ASCII;

                smtpClient.Send(mailMessage);
            }
        }
Exemple #22
0
        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);

            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 void ProcessOrder(Cart cart, ShippingDetails shippingDetails)
        {
            using (var smtp = new SmtpClient())
            {
                smtp.EnableSsl = this.emailSettings.UseSsl;
                smtp.Host = this.emailSettings.ServerName;
                smtp.Port = this.emailSettings.ServerPort;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(this.emailSettings.Username, this.emailSettings.Password);

                if (this.emailSettings.WriteAsFile)
                {
                    smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtp.PickupDirectoryLocation = this.emailSettings.FileLocation;
                    smtp.EnableSsl = false;
                }

                var body = new StringBuilder()
                    .AppendLine("A new order has been submited")
                    .AppendLine("---")
                    .AppendLine("Items:");

                foreach (var item in cart.Lines)
                {
                    body.AppendFormat("{0} x {1}, unitary price: {2:c} (subtotal: {3:c})", item.Product.Name, item.Product.Price, item.Quantity, item.Product.Price * item.Quantity);
                }

                body.AppendFormat("Total order value {0:c}", cart.ComputeTotalCost())
                    .AppendLine("---")
                    .AppendLine("Ship to:")
                    .AppendLine(shippingDetails.Name)
                    .AppendLine(shippingDetails.Line1)
                    .AppendLine(shippingDetails.Line2 ?? string.Empty)
                    .AppendLine(shippingDetails.Line3 ?? string.Empty)
                    .AppendLine(shippingDetails.City)
                    .AppendLine(shippingDetails.State ?? string.Empty)
                    .AppendLine(shippingDetails.Country)
                    .AppendLine(shippingDetails.Zip ?? string.Empty)
                    .AppendLine("---")
                    .AppendFormat("Gift wrap: {0}", shippingDetails.Wrap ? "yes" : "no");

                var mailMessage = new MailMessage(this.emailSettings.MailFromAddress, this.emailSettings.MailToAddress, "New order submitted!", body.ToString());

                if (this.emailSettings.WriteAsFile)
                {
                    mailMessage.BodyEncoding = Encoding.ASCII;
                }

                smtp.Send(mailMessage);
            }
        }
        public ActionResult CheckOut(Cart cart, ShippingDetails shippingDetails)
        {
            // Empty carts can't be checked out
            if (cart.Lines.Count == 0)
                ModelState.AddModelError("Cart", "Sorry, your cart is empty!");

            if (ModelState.IsValid) {
                orderSubmitter.SubmitOrder(cart, shippingDetails);
                cart.Clear();
                return View("Completed");
            }
            else // Something was invalid
                return View(shippingDetails);
        }
        public void ProcessOrder(Cart cart, ShippingDetails shippingDetails)
        {
            using (var smtpClient = new SmtpClient {
                EnableSsl = emailSettings.UseSsl,
                Host = emailSettings.ServerName,
                Port = emailSettings.ServerPort,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password),
            })
            {
                if (emailSettings.WriteAsFile)
                {
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtpClient.PickupDirectoryLocation = HttpContext.Current.Server.MapPath(emailSettings.FileLocation);
                    smtpClient.EnableSsl = false;
                }

                var 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.TotalValue())
                    .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");

                var mailMessage = new MailMessage(emailSettings.MailFromAddress, emailSettings.MailtoAddress, "New order submitted!", body.ToString());

                if (emailSettings.WriteAsFile)
                    mailMessage.BodyEncoding = Encoding.ASCII;

                smtpClient.Send(mailMessage);
            }
        }
        public void Cannot_Checkout_empty_cart()
        {
            var mock = new Mock<IOrderProcessor>();
            var cart = new Cart();
            var shipping = new ShippingDetails();

            var controller = new CartController(null, mock.Object);

            var result = controller.Checkout(cart, shipping);

            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("", "Koszyk jest pusty");
     if (ModelState.IsValid)
     {
         _orderProcessor.ProcessOrder(cart, shippingDetails);
         cart.Clear();
         return View("Completed");
     }
     else
     {
         return View(shippingDetails);
     }
 }
Exemple #28
0
        public void Cannot_Checkout_Empty_Cart()
        {
            Mock<IOrderProcessor> mockProc = new Mock<IOrderProcessor>();
            Cart cart = new Cart();
            ShippingDetails shippingDetails = new ShippingDetails();
            CartController target = new CartController(null, mockProc.Object);

            ViewResult result = target.Checkout(cart, shippingDetails);

            //Assert - check that the order hasn't been passed on to the processor, means that ProcessorOrder was never invoked
            mockProc.Verify(mock=> mock.ProcessorOrder(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);
        }
Exemple #29
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (!cart.Lines.Any())
                ModelState.AddModelError("", "Sorry, your cart is empty");

            if (ModelState.IsValid)
            {
                orderProcessor.ProcessOrder(cart, shippingDetails);
                cart.Clear();
                return View("Completed");
            }
            else
            {
                return View(shippingDetails);
            }
        }
        public void Can_checkout_and_submit_order()
        {
            var mock = new Mock<IOrderProcessor>();
            var cart = new Cart();
            cart.AddItem(new Product(), 1);
            var shipping = new ShippingDetails();

            var controller = new CartController(null, mock.Object);

            var result = controller.Checkout(cart, shipping);

            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Once);

            Assert.AreEqual("Completed", result.ViewName);
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
Exemple #31
0
        public void Cannot_Checkout_EmptyCart()
        {
            //arrange - Create a mock order process

            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            Cart cart = new Cart();

            ShippingDetails shippingDetails = new ShippingDetails();
            CartController controller = new CartController(null, mock.Object );

            ViewResult result = controller.Checkout(cart, shippingDetails);

            mock.Verify(m=>m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.AtLeastOnce());
            Assert.AreEqual("", result.ViewName);
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Exemple #32
0
 public ViewResult Checkout(Cart cart, SportsStore.Domain.Entities.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));
     }
 }