public void Years_Contains120Years()
        {
            AgeGateViewModel model = new AgeGateViewModel();

            Assert.That(model.Years, Is.Not.Empty);
            Assert.That(model.Years.Count(), Is.EqualTo(120));
        }
        public void Years_ContainsCurrentYear()
        {
            AgeGateViewModel model = new AgeGateViewModel();

            Assert.That(model.Years, Is.Not.Empty);
            Assert.That(model.Years, Has.Exactly(1).Matches<SelectListItem>(i => i.Text == DateTime.Today.Year.ToString()));
        }
        public ActionResult Index(AgeGateViewModel model)
        {
            if (DateTime.MinValue.Year > model.Year || DateTime.UtcNow.Year < model.Year)
            {
                ModelState.AddModelError(nameof(model.Year), "Invalid");
                return View(model);
            }

            if (DateTime.MinValue.Month > model.Month || DateTime.MaxValue.Month < model.Month)
            {
                ModelState.AddModelError(nameof(model.Month), "Invalid");
                return View(model);
            }

            if (DateTime.DaysInMonth(model.Year, model.Month) < model.Day)
            {
                ModelState.AddModelError(nameof(model.Day), "Invalid");
                return View(model);
            }

            DateTime dateOfBirth = new DateTime(model.Year, model.Month, model.Day);

            HttpCookie cookie = new HttpCookie(DATE_OF_BIRTH_COOKIE, dateOfBirth.ToShortDateString())
            {
                Expires = DateTime.UtcNow.AddDays(1)
            };

            Response.Cookies.Add(cookie);

            return RedirectToLocal(model.ReturnUrl);
        }
        public void Days_Contains31Days()
        {
            AgeGateViewModel model = new AgeGateViewModel();

            Assert.That(model.Days, Is.Not.Empty);
            Assert.That(model.Days.Count(), Is.EqualTo(31));
            Assert.That(model.Days, Has.Exactly(1).Matches<SelectListItem>(i => i.Text == "1"));
            Assert.That(model.Days, Has.Exactly(1).Matches<SelectListItem>(i => i.Text == "31"));
        }
        public void Index_MonthOutsideOfValidRange_RedisplaysViewWithSameViewModel([Values(-1, 13)]int month)
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 1,
                Month = month
            };

            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_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 Months_ContainsAllMonths()
        {
            AgeGateViewModel model = new AgeGateViewModel();

            Assert.That(model.Months, Is.Not.Empty);
            Assert.That(model.Months.Count(), Is.EqualTo(12));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("January") && i.Text.Contains("01")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("February") && i.Text.Contains("02")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("March") && i.Text.Contains("03")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("April") && i.Text.Contains("04")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("May") && i.Text.Contains("05")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("June") && i.Text.Contains("06")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("July") && i.Text.Contains("07")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("August") && i.Text.Contains("08")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("September") && i.Text.Contains("09")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("October") && i.Text.Contains("10")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("November") && i.Text.Contains("11")));
            Assert.That(model.Months, Has.Exactly(1).Matches<SelectListItem>(i => i.Text.Contains("December") && i.Text.Contains("12")));
        }
        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"));
        }
        public void Year_IsSetToCurrentYearByDefault()
        {
            AgeGateViewModel model = new AgeGateViewModel();

            Assert.That(model.Year, Is.EqualTo(DateTime.Today.Year));
        }