public void Index_DayHigherThanNumberOfDaysInMonth_RedisplaysViewWithSameViewModel()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 31, // Only 28 or 29 days
                Month = 2 // February
            };

            AgeGateController controller = new AgeGateController();

            var result = controller.Index(model) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.ViewName, Is.Empty.Or.EqualTo(nameof(AgeGateController.Index)));
            Assert.That(result.Model, Is.SameAs(model));
        }
        public void Index_YearOutsideOfValidRange_RedisplaysViewWithSameViewModel(int year)
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 1,
                Month = 1,
                Year = year
            };

            AgeGateController controller = new AgeGateController();

            var result = controller.Index(model) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.ViewName, Is.Empty.Or.EqualTo(nameof(AgeGateController.Index)));
            Assert.That(result.Model, Is.SameAs(model));
        }
        public void Index_LocalUrl_RedirectsToReturnUrl()
        {
            string returnUrl = "/Games/1";

            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015,
                ReturnUrl = returnUrl
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(true);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            var result = controller.Index(model) as RedirectResult;

            Assert.That(result != null);
            Assert.That(result.Url, Is.SameAs(returnUrl));
        }
        public void Index_NonLocalUrl_RedirectsToHomeIndex()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(false);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            var result = controller.Index(model) as RedirectToRouteResult;

            Assert.That(result != null);
            Assert.That(result.RouteValues["Action"], Is.EqualTo(nameof(HomeController.Index)));
            Assert.That(result.RouteValues["Controller"], Is.EqualTo("Home"));
        }
        public void Index_ValidDate_NewCookieExpiresInADay()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(false);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            controller.Index(model);

            HttpCookie cookie = cookieCollection[AgeGateController.DATE_OF_BIRTH_COOKIE];

            Assert.That(cookie != null);
            Assert.That(cookie.Expires, Is.EqualTo(DateTime.UtcNow.AddDays(1)).Within(1).Minutes);
        }
        public void Index_ValidDate_AddsCookieWithCorrectKeyAndValue()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(false);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            controller.Index(model);

            HttpCookie cookie = cookieCollection[AgeGateController.DATE_OF_BIRTH_COOKIE];

            Assert.That(cookie != null);
            Assert.That(cookie.Name, Is.SameAs(AgeGateController.DATE_OF_BIRTH_COOKIE));
            Assert.That(cookie.Value, Is.EqualTo("12/4/2015"));
        }