Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 public ViewResult Index(Cart cart, string returnUrl)
 {
     return View(new CartIndexViewModel
     {
         Cart = cart,
         ReturnUrl = returnUrl
     });
 }
Esempio n. 3
0
 public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
 {
     Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
     if (product != null)
     {
         cart.RemoveLine(product);
     }
     return RedirectToAction("Index", new { returnUrl });
 }
Esempio n. 4
0
 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 });
 }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
            }
        }
Esempio n. 7
0
 //[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);
     }
 }
Esempio n. 8
0
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     Cart cart = null;
     if (controllerContext.HttpContext.Session != null)
     {
         cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
     }
     if (cart == null)
     {
         cart = new Cart();
         if (controllerContext.HttpContext.Session != null)
             controllerContext.HttpContext.Session[sessionKey] = cart;
     }
     return cart;
 }
Esempio n. 9
0
 public void Calculate_Cart_Total()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M};
     Product p2 = new Product { ProductID = 2, Name = "P2" , Price = 50M};
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     target.AddItem(p1, 3);
     decimal result = target.ComputeTotalValue();
     // Assert
     Assert.AreEqual(result, 450M);
 }
Esempio n. 10
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            // get the Cart from session
            Cart cart = (Cart)controllerContext.HttpContext.Session[sessionKey];

            // create the Cart if there wasn`t one in the session data
            if (cart == null)
            {
                cart = new Cart();
                controllerContext.HttpContext.Session[sessionKey] = cart;
            }

            //return the cart
            return cart;
        }
        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);
            }
        }
Esempio n. 12
0
 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");
 }
Esempio n. 13
0
 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 I am passing an invalid model to the view
     Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
 }
Esempio n. 14
0
 public PartialViewResult Summary(Cart cart)
 {
     return PartialView(cart);
 }
Esempio n. 15
0
 public void Can_Add_Quantity_For_Existing_Lines()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     target.AddItem(p1, 10);
     CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductID).ToArray();
     // Assert
     Assert.AreEqual(results.Length, 2);
     Assert.AreEqual(results[0].Quantity, 11);
     Assert.AreEqual(results[1].Quantity, 1);
 }
Esempio n. 16
0
        public void Can_View_Cart_Contents()
        {
            // Arrange - Create a cart
            Cart cart = new Cart();

            // Arrange - Create the controller
            CartController target = new CartController(null, null);

            //Act - Call the Index action method
            CartIndexViewModel result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;

            // Assert
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
Esempio n. 17
0
        public void Can_Remove_Line()
        {
            // Arrange - Create some test games
            Game p1 = new Game { GameID = 1, Name = "P1" };
            Game p2 = new Game { GameID = 2, Name = "P2" };
            Game p3 = new Game { GameID = 3, Name = "P3" };

            // Arrange - Create a new cart
            Cart target = new Cart();
            // Arrange - add some games to the cart
            target.AddItem(p1, 1);
            target.AddItem(p2, 3);
            target.AddItem(p3, 5);
            target.AddItem(p2, 1);

            // Act
            target.RemoveLine(p2);

            // Assert
            Assert.AreEqual(target.Lines.Where(c => c.Game == p2).Count(), 0);
            Assert.AreEqual(target.Lines.Count(), 2);
        }
Esempio n. 18
0
        public void Can_Clear_Contents()
        {
            // Arrange - Create some test games
            Game p1 = new Game { GameID = 1, Name = "P1", Price = 100M };
            Game p2 = new Game { GameID = 2, Name = "P2", Price = 50M };

            // Arrange - Create a new cart
            Cart target = new Cart();

            // Arrange - Add some items
            target.AddItem(p1, 1);
            target.AddItem(p2, 2);

            // Act - Reset the cart
            target.Clear();

            // Assert
            Assert.AreEqual(target.Lines.Count(), 0);
        }
Esempio n. 19
0
        public void Can_Checkout_And_Submit_Order()
        {
            // 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 Game(), 1);

            // Arrange - Create an instance of the controller
            CartController target = new CartController(null, mock.Object);

            // Act - Try to checkout
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            // Assert - Check that the order has been passed onto the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()),
                Times.Once());

            // Assert - Check that the method is returning the Completed view
            Assert.AreEqual("Completed", result.ViewName);

            // Assert - Check that we are passing a valid model to the view
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
Esempio n. 20
0
        public void Can_Add_To_Cart()
        {
            // Arrange - Create the mock repository
            Mock<IGameRepository> mock = new Mock<IGameRepository>();
            mock.Setup(m => m.Games).Returns(new Game[]
            {
                new Game {GameID = 1, Name = "P1", Category = "Apples"},
            }.AsQueryable());

            // Arrange - Create a new cart
            Cart cart = new Cart();

            // Arrange - Create the controller
            CartController target = new CartController(mock.Object, null);

            // Act - Add a game to the cart
            target.AddToCart(cart, 1, null);

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Game.GameID, 1);
        }
Esempio n. 21
0
        public void Can_Add_Quality_For_Existing_Lines()
        {
            //Arrange - Create some test games
            Game p1 = new Game { GameID = 1, Name = "P1" };
            Game p2 = new Game { GameID = 2, Name = "P2" };

            // Arrange - Create a new cart
            Cart target = new Cart();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 10);
            CartLine[] results = target.Lines.OrderBy(c => c.Game.GameID).ToArray();

            // Assert
            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, p1);
            Assert.AreEqual(results[1].Quantity, p2);
        }
Esempio n. 22
0
 public void Can_Add_New_Lines()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     CartLine[] results = target.Lines.ToArray();
     // Assert
     Assert.AreEqual(results.Length, 2);
     Assert.AreEqual(results[0].Product, p1);
     Assert.AreEqual(results[1].Product, p2);
 }
Esempio n. 23
0
 private Cart GetCart()
 {
     Cart cart = (Cart)Session["Cart"];
     if (cart == null)
     {
         cart = new Cart();
         Session["Cart"] = cart;
     }
     return cart;
 }
Esempio n. 24
0
 public void Can_Add_To_Cart()
 {
     // 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
     target.AddToCart(cart, 1, null);
     // Assert
     Assert.AreEqual(cart.Lines.Count(), 1);
     Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);
 }
Esempio n. 25
0
 public void Can_Clear_Contents()
 {
     // Arrange - create some test products
     Product p1 = new Product
     {
         ProductID = 1,
         Name = "P1",
         Price = 100M
     };
     Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Arrange - add some items
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     // Act - reset the cart
     target.Clear();
     // Assert
     Assert.AreEqual(target.Lines.Count(), 0);
 }
Esempio n. 26
0
        public void Can_Add_New_Lines()
        {
            // Arrange - Create some test games
            Game p1 = new Game { GameID = 1, Name = "P1" };
            Game p2 = new Game { GameID = 2, Name = "P2" };

            // Arrange - Create a new cart
            Cart target = new Cart();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            CartLine[] results = target.Lines.ToArray();

            // Assert
            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Game, p1);
            Assert.AreEqual(results[1].Game, p2);
        }
Esempio n. 27
0
 public void Can_Remove_Line()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     Product p3 = new Product { ProductID = 3, Name = "P3" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Arrange - add some products to the cart
     target.AddItem(p1, 1);
     target.AddItem(p2, 3);
     target.AddItem(p3, 5);
     target.AddItem(p2, 1);
     // Act
     target.RemoveLine(p2);
     // Assert
     Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0);
     Assert.AreEqual(target.Lines.Count(), 2);
 }