Наследование: Microsoft.AspNet.Mvc.Controller
Пример #1
0
        public async Task AddressAndPayment_ReturnsOrderIfRequestCanceled()
        {
            // Arrange
            var context = new DefaultHttpContext();

            context.Request.Form =
                new FormCollection(new Dictionary <string, StringValues>());
            var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>();

            var controller = new CheckoutController();

            controller.ControllerContext.HttpContext = context;

            var order = new Order();

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, new CancellationToken(true));

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Same(order, viewResult.ViewData.Model);
        }
Пример #2
0
        public async Task AddressAndPayment_RedirectToCompleteWhenSuccessful()
        {
            // Arrange
            var httpContext = new DefaultHttpContext();

            var orderId = 10;
            var order   = new Order()
            {
                OrderId = orderId,
            };

            // Session initialization
            var cartId = "CartId_A";

            httpContext.Session = new TestSession();
            httpContext.Session.SetString("Session", cartId);

            // FormCollection initialization
            httpContext.Request.Form =
                new FormCollection(
                    new Dictionary <string, StringValues>()
            {
                { "PromoCode", new[] { "FREE" } }
            }
                    );

            // UserName initialization
            var claims = new List <Claim> {
                new Claim(ClaimTypes.Name, "TestUserName")
            };

            httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims));

            // DbContext initialization
            var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>();
            var cartItems = CreateTestCartItems(
                cartId,
                itemPrice: 10,
                numberOfItem: 1);

            dbContext.AddRange(cartItems.Select(n => n.Album).Distinct());
            dbContext.AddRange(cartItems);
            dbContext.SaveChanges();

            var controller = new CheckoutController();

            controller.ControllerContext.HttpContext = httpContext;

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None);

            // Assert
            var redirectResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal("Complete", redirectResult.ActionName);
            Assert.Null(redirectResult.ControllerName);
            Assert.NotNull(redirectResult.RouteValues);

            Assert.Equal(orderId, redirectResult.RouteValues["Id"]);
        }
Пример #3
0
        public async Task AddressAndPayment_ReturnsOrderIfInvalidPromoCode()
        {
            // Arrange
            var context   = new DefaultHttpContext();
            var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>();

            // AddressAndPayment action reads the Promo code from FormCollection.
            context.Request.Form =
                new FormCollection(new Dictionary <string, StringValues>());

            var controller = new CheckoutController();

            controller.ControllerContext.HttpContext = context;

            // Do not need actual data for Order; the Order object will be checked for the reference equality.
            var order = new Order();

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Same(order, viewResult.ViewData.Model);
        }
        public async Task Complete_ReturnsOrderIdIfValid()
        {
            // Arrange
            var orderId  = 100;
            var userName = "******";
            var claims   = new List <Claim> {
                new Claim(ClaimTypes.Name, userName)
            };

            var httpContext = new DefaultHttpContext()
            {
                User = new ClaimsPrincipal(new ClaimsIdentity(claims)),
            };

            var dbContext = _fixture.Context;

            dbContext.Add(CreateOrder(orderId, userName));
            dbContext.SaveChanges();

            var controller = new CheckoutController(_fixture.ServiceProvider.GetService <ILogger <CheckoutController> >());

            controller.ControllerContext.HttpContext = httpContext;

            // Act
            var result = await controller.Complete(dbContext, orderId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Equal(orderId, viewResult.ViewData.Model);
        }
Пример #5
0
        public void AddressAndPayment_ReturnsDefaultView()
        {
            // Arrange
            var controller = new CheckoutController();

            // Act
            var result = controller.AddressAndPayment();

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);
        }
        public void AddressAndPayment_ReturnsDefaultView()
        {
            // Arrange
            var controller = new CheckoutController(_fixture.ServiceProvider.GetService <ILogger <CheckoutController> >());

            // Act
            var result = controller.AddressAndPayment();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);
        }
Пример #7
0
        public void AddressAndPayment_ReturnsDefaultView()
        {
            // Arrange
            var controller = new CheckoutController();

            // Act
            var result = controller.AddressAndPayment();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);
        }
Пример #8
0
        public async Task AddressAndPayment_RedirectToCompleteWhenSuccessful()
        {
            // Arrange
            var httpContext = new DefaultHttpContext();

            var orderId = 10;
            var order = new Order()
            {
                OrderId = orderId,
            };

            // Session initialization
            var cartId = "CartId_A";
            httpContext.Session = new TestSession();
            httpContext.Session.SetString("Session", cartId);

            // FormCollection initialization
            httpContext.Request.Form =
                new FormCollection(
                    new Dictionary<string, StringValues>()
                        { { "PromoCode", new[] { "FREE" } } }
                    );

            // UserName initialization
            var claims = new List<Claim> { new Claim(ClaimTypes.Name, "TestUserName") };
            httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims));

            // DbContext initialization
            var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
            var cartItems = CreateTestCartItems(
                cartId,
                itemPrice: 10,
                numberOfItem: 1);
            dbContext.AddRange(cartItems.Select(n => n.Album).Distinct());
            dbContext.AddRange(cartItems);
            dbContext.SaveChanges();

            var controller = new CheckoutController();
            controller.ControllerContext.HttpContext = httpContext;

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None);

            // Assert
            var redirectResult = Assert.IsType<RedirectToActionResult>(result);
            Assert.Equal("Complete", redirectResult.ActionName);
            Assert.Null(redirectResult.ControllerName);
            Assert.NotNull(redirectResult.RouteValues);

            Assert.Equal(orderId, redirectResult.RouteValues["Id"]);
        }
        public async Task Complete_ReturnsErrorIfInvalidOrder()
        {
            // Arrange
            var invalidOrderId = 100;
            var dbContext      = _fixture.Context;

            var controller = new CheckoutController(_fixture.ServiceProvider.GetService <ILogger <CheckoutController> >());

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            // Act
            var result = await controller.Complete(dbContext, invalidOrderId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Equal("Error", viewResult.ViewName);
        }
        public async Task Complete_ReturnsOrderIdIfValid()
        {
            // Arrange
            var orderId  = 100;
            var userName = "******";
            var claims   = new List <Claim> {
                new Claim(ClaimTypes.Name, userName)
            };

            var httpContext = new DefaultHttpContext()
            {
                User = new ClaimsPrincipal(new ClaimsIdentity(claims)),
            };

            var dbContext =
                _serviceProvider.GetRequiredService <MusicStoreContext>();

            dbContext.Add(new Order()
            {
                OrderId  = orderId,
                Username = userName
            });
            dbContext.SaveChanges();

            var controller = new CheckoutController()
            {
                DbContext = dbContext,
            };

            controller.ActionContext.HttpContext = httpContext;

            // Act
            var result = await controller.Complete(orderId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Equal(orderId, viewResult.ViewData.Model);
        }
        public async Task AddressAndPayment_ReturnsOrderIfInvalidOrderModel()
        {
            // Arrange
            var controller = new CheckoutController();

            controller.ModelState.AddModelError("a", "ModelErrorA");

            var order = new Order();

            // Act
            var result = await controller.AddressAndPayment(order, CancellationToken.None);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Same(order, viewResult.ViewData.Model);
        }
        public async Task AddressAndPayment_ReturnsOrderIfInvalidOrderModel()
        {
            // Arrange
            var controller = new CheckoutController(_serviceProvider.GetService <ILogger <CheckoutController> >());

            controller.ModelState.AddModelError("a", "ModelErrorA");
            var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>();

            var order = new Order();

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Same(order, viewResult.ViewData.Model);
        }
        public async Task Complete_ReturnsErrorIfInvalidOrder()
        {
            // Arrange
            var invalidOrderId = 100;
            var dbContext      =
                _serviceProvider.GetRequiredService <MusicStoreContext>();

            var controller = new CheckoutController()
            {
                DbContext = dbContext,
            };

            controller.ActionContext.HttpContext = new DefaultHttpContext();

            // Act
            var result = await controller.Complete(invalidOrderId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Equal("Error", viewResult.ViewName);
        }
Пример #14
0
        public async Task AddressAndPayment_ReturnsOrderIfInvalidPromoCode()
        {
            // Arrange
            var context = new DefaultHttpContext();
            var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();

            // AddressAndPayment action reads the Promo code from FormCollection.
            context.Request.Form =
                new FormCollection(new Dictionary<string, StringValues>());

            var controller = new CheckoutController();
            controller.ControllerContext.HttpContext = context;

            // Do not need actual data for Order; the Order object will be checked for the reference equality.
            var order = new Order();

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None);

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Same(order, viewResult.ViewData.Model);
        }
Пример #15
0
        public async Task Complete_ReturnsErrorIfInvalidOrder()
        {
            // Arrange
            var invalidOrderId = 100;
            var dbContext =
                _serviceProvider.GetRequiredService<MusicStoreContext>();

            var controller = new CheckoutController();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            // Act
            var result = await controller.Complete(dbContext, invalidOrderId);

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);

            Assert.Equal("Error", viewResult.ViewName);
        }
Пример #16
0
        public async Task Complete_ReturnsOrderIdIfValid()
        {
            // Arrange
            var orderId = 100;
            var userName = "******";
            var claims = new List<Claim> { new Claim(ClaimTypes.Name, userName) };

            var httpContext = new DefaultHttpContext()
            {
                User = new ClaimsPrincipal(new ClaimsIdentity(claims)),
            };

            var dbContext =
                _serviceProvider.GetRequiredService<MusicStoreContext>();
            dbContext.Add(new Order()
            {
                OrderId = orderId,
                Username = userName
            });
            dbContext.SaveChanges();

            var controller = new CheckoutController();
            controller.ControllerContext.HttpContext = httpContext;

            // Act
            var result = await controller.Complete(dbContext, orderId);

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Equal(orderId, viewResult.ViewData.Model);
        }
Пример #17
0
        public async Task AddressAndPayment_ReturnsOrderIfInvalidOrderModel()
        {
            // Arrange
            var controller = new CheckoutController();
            controller.ModelState.AddModelError("a", "ModelErrorA");
            var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();

            var order = new Order();

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None);

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Same(order, viewResult.ViewData.Model);
        }
Пример #18
0
        public async Task AddressAndPayment_ReturnsOrderIfRequestCanceled()
        {
            // Arrange
            var context = new DefaultHttpContext();
            context.Request.Form =
                new FormCollection(new Dictionary<string, StringValues>());
            var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();

            var controller = new CheckoutController();
            controller.ControllerContext.HttpContext = context;

            var order = new Order();

            // Act
            var result = await controller.AddressAndPayment(dbContext, order, new CancellationToken(true));

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            Assert.Same(order, viewResult.ViewData.Model);
        }