Beispiel #1
0
        public void AddToCart_ValidItem_Succeeds()
        {
            // Get a product to load the details page for.
            var album = storeDB.Albums
                .Take(1)
                .First();

            var settings = new ExecutorSettings("ShoppingCart/AddToCart/" + album.AlbumId)
            {
                User = new GenericPrincipal(new GenericIdentity("GenghisKahn"), null)
            };

            using (var scope = new MvcExecutorScope(settings))
            {
                Assert.AreEqual(302, scope.HttpContext.Response.StatusCode);
                Assert.AreEqual("/ShoppingCart", scope.HttpContext.Response.RedirectLocation);

                // Now verify that the cart contains the item we just added.
                var cart = MvcMusicStore.Models.ShoppingCart.GetCart(scope.HttpContext);
                var cartItems = cart.GetCartItems();
                Assert.AreEqual(1, cartItems.Count);
                Assert.AreEqual(album.AlbumId, cartItems[0].AlbumId);

                // Finally clear the cart.
                cart.EmptyCart();
            }
        }
Beispiel #2
0
        public ExecutorScope(ExecutorSettings settings)
        {
            //Important that this line comes first so that ExecutorScope.Current does not return null.
            Current = this;

            HttpContextProxy.Current.OverrideContextState(settings);

            foreach (var handler in Setup.CurrentSettings.ExecutorScopeCreatedHandlers)
                handler(this, EventArgs.Empty);

            // Execute the request
            this.Execute();
        }
Beispiel #3
0
        public void OverrideContextState(ExecutorSettings settings)
        {
            this.User = (settings.User != null) ? settings.User : DefaultIdentity;
            _items = (settings.HttpContextItems != null) ? settings.HttpContextItems : new Hashtable();

            _request.OverrideRequestState(settings);
            if (_session.Value != null)
                _session.Value.OverrideSessionState(settings);

            _request.RequestContext.HttpContext = this;

            // Important that this line comes after we call OverrideReqeustState on the HttpRequestProxy
            _request.RequestContext.RouteData = RouteTable.Routes.GetRouteData(this);
        }
Beispiel #4
0
        public void Checkout_Success()
        {
            string userName = "******";
            int numItemsInCart = 2;

            var cart = TestUtil.AddItemsToCart(userName, storeDB.Albums.Take(numItemsInCart));

            var form = new HttpValueCollection
            {
             { "FirstName","Miles"},
             {"LastName","Davis"},
             {"Address","100 Broadway Ave."},
             {"City","New York"},
             {"State","NY"},
             {"PostalCode","12345"},
             {"Country","United States"},
             {"Phone","111-111-1111"},
             {"Email","*****@*****.**"},
             {"PromoCode","FREE"}
            };

            var settings = new ExecutorSettings("Checkout/AddressAndPayment") {
                Form=form,
                HttpMethod="POST",
                User=TestUtil.CreateUser(userName)
            };

            Order order;
            using (var scope = new MvcExecutorScope(settings))
            {
                Assert.AreEqual(302, scope.HttpContext.Response.StatusCode);

                var match = new Regex("/Checkout/Complete/(\\d+)").Match(scope.HttpContext.Response.RedirectLocation);
                Assert.IsTrue(match.Success);

                var orderId = Int32.Parse(match.Groups[1].Value);

                order = storeDB.Orders.Single(o=>o.OrderId==orderId);
                Assert.AreEqual(form["FirstName"], order.FirstName);
                Assert.AreEqual(form["Email"], order.Email);

                var orderDetails = storeDB.OrderDetails.Where(od => od.OrderId == orderId);
                Assert.AreEqual(numItemsInCart, orderDetails.Count());
            }

            // Cleanup
            storeDB.Orders.Remove(order);
            storeDB.SaveChanges();
        }
Beispiel #5
0
        public void RemoveFromCart_ValidJson()
        {
            // Add an item to the cart so we have something to remove.
            string userName = "******";
            MvcMusicStore.Models.ShoppingCart cart = TestUtil.AddItemsToCart(
                userName, storeDB.Albums.Take(1));
            var recordId = cart.GetCartItems().First().RecordId;

            var settings = new ExecutorSettings("ShoppingCart/RemoveFromCart/" + recordId)
            {
                User = TestUtil.CreateUser(userName),
                HttpMethod = "POST"
            };

            using (var scope = new MvcExecutorScope(settings))
            {
                Assert.AreEqual("application/json", scope.HttpContext.Response.ContentType, "Expected json to be returned.");

                var deserializedResponse = JsonConvert.DeserializeObject<ShoppingCartRemoveViewModel>(scope.ResponseText);
                Assert.AreEqual(0.0d, deserializedResponse.CartTotal, "The shopping cart total should be $0.00.");
                Assert.AreEqual(0, deserializedResponse.ItemCount, "The shopping cart should have 0 items left.");
                Assert.AreEqual(recordId, deserializedResponse.DeleteId);
            }
        }
Beispiel #6
0
        public void ViewCart_ExpectedHtml()
        {
            var albumsToCart = storeDB.Albums.Take(2);
            string userName = "******";

            var cart = TestUtil.AddItemsToCart(userName, albumsToCart);

            var settings = new ExecutorSettings("ShoppingCart/Index")
            {
                User = TestUtil.CreateUser(userName)
            };

            using (var scope = new MvcExecutorScope(settings))
            {
                var viewModel = scope.ViewData.Model as ShoppingCartViewModel;
                Assert.IsNotNull(viewModel);
                Assert.AreEqual(albumsToCart.Count(), viewModel.CartItems.Count);
                foreach (var album in albumsToCart)
                    Assert.IsTrue(viewModel.CartItems.Any(c => c.AlbumId == album.AlbumId));

                Assert.IsFalse(String.IsNullOrEmpty(scope.ResponseText));
            }

            // Cleanup
            cart.EmptyCart();
        }
Beispiel #7
0
 internal void OverrideSessionState(ExecutorSettings settings)
 {
     _sessionId = !String.IsNullOrEmpty(settings.SessionId) ? settings.SessionId : NewSessionId();
     _items = settings.SessionStateItems != null ? settings.SessionStateItems : new SessionStateItemCollection();
     _isNewSession = settings.IsNewSession;
 }
Beispiel #8
0
        internal void OverrideRequestState(ExecutorSettings settings)
        {
            if (settings.Url == null)
                throw new ArgumentException("The SimulatedRequestSettings must specifiy a Url.");

            _url = settings.Url;
            _cookies = (settings.Cookies != null) ? settings.Cookies : new HttpCookieCollection();
            _queryString = (settings.Url.Query.Length > 1) ? HttpValueCollection.Parse(settings.Url.Query.Substring(1)) : new HttpValueCollection();
            _form = (settings.Form  != null) ? settings.Form : new NameValueCollection();
            _headers = (settings.RequestHeaders != null) ? settings.RequestHeaders : new NameValueCollection();
            _httpMethod = !String.IsNullOrEmpty(settings.HttpMethod) ? settings.HttpMethod : (_form.Count > 0 ? "POST" : "GET");
            _isAuthenticated = settings.IsAuthenticated;
            _userAgent = settings.UserAgent;
            _contentType = !String.IsNullOrEmpty(settings.RequestContentType) ? settings.RequestContentType : "text/html";
            _referrer = settings.Referrer;

            //if (_settings.Files == null)
            //    _settings.Files = new ExecutorHttpFileCollection();
        }
Beispiel #9
0
 public MvcExecutorScope(ExecutorSettings settings)
     : base(settings)
 {
 }