public void NewWhenHttpVerbIsPostReturnsTitleInTheModelWhenModelStateIsNotValid()
        {
            var survey = new Survey();

            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");
                controller.TempData[SurveysController.CachedSurvey] = new Survey();

                var result = controller.New(survey) as ViewResult;

                var model = result.ViewData.Model as TenantMasterPageViewData;
                Assert.AreSame("New Survey", model.Title);
            }
        }
        public void NewWhenHttpVerbIsPostSavedTenantNameIsSameAsControllerWhenModelStateIsValid()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();

            var cachedSurvey = new Survey();
            cachedSurvey.Questions.Add(new Question());

            using (var controller = new SurveysController(mockSurveyStore.Object, null, null, null, null, null))
            {
                controller.TenantName = "Tenant";
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                controller.New(new Survey());
            }

            mockSurveyStore.Verify(r => r.SaveSurvey(It.Is<Survey>(s => s.Tenant == "Tenant")), Times.Once());
        }
        public void NewWhenHttpVerbIsPostReturnsRedirectToMySurveysWhenModelStateIsValid()
        {
            var cachedSurvey = new Survey();
            cachedSurvey.Questions.Add(new Question());

            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.TenantName = "Tenant";
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                var result = controller.New(new Survey()) as RedirectToRouteResult;

                Assert.AreEqual("Index", result.RouteValues["action"]);
                Assert.AreEqual(null, result.RouteValues["controller"]);
            }
        }
        public void NewWhenHttpVerbIsPostReturnsRedirectToTheNewActionWhenCachedSurveyIsNull()
        {
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = null;

                var result = controller.New(new Survey()) as RedirectToRouteResult;

                Assert.AreEqual("New", result.RouteValues["action"]);
                Assert.AreEqual(null, result.RouteValues["controller"]);
            }
        }
        public void NewWhenHttpVerbIsPostReturnsErrorInModelStateWhenCachedSurveyHasNoQuestions()
        {
            var cachedSurvey = new Survey { Questions = new List<Question>() };

            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                var result = controller.New(new Survey()) as ViewResult;

                Assert.IsTrue(controller.ModelState.Keys.Contains("ContentModel.Questions"));
            }
        }
        public void NewWhenHttpVerbIsPostReturnsNewSurveyAsTitleInTheModelWhenCachedSurveyHasNoQuestions()
        {
            var cachedSurvey = new Survey { Questions = new List<Question>() };

            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                var result = controller.New(new Survey()) as ViewResult;
                var model = result.ViewData.Model as TenantMasterPageViewData;

                Assert.AreSame("New Survey", model.Title);
            }
        }
        public void NewWhenHttpVerbIsPostCopiesQuestionsFromCachedSurveyToSurveyWhenModelStateIsNotValid()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();

            var questionsToBeCopied = new List<Question>();
            questionsToBeCopied.Add(new Question());
            var cachedSurvey = new Survey { Questions = questionsToBeCopied };

            using (var controller = new SurveysController(mockSurveyStore.Object, null, null, null, null, null))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                var result = controller.New(new Survey()) as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<Survey>;

                Assert.AreSame(model.ContentModel.Questions, questionsToBeCopied);
            }
        }
        public void NewWhenHttpVerbIsGetReturnsEmptyViewName()
        {
            var mockDictionary = new Mock<IUDFDictionary>();
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, mockDictionary.Object))
            {
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };

                var result = controller.New() as ViewResult;

                Assert.AreEqual(string.Empty, result.ViewName);
            }
        }
        public void NewWhenHttpVerbIsPostCleansCachedSurveyWhenModelStateIsValid()
        {
            var cachedSurvey = new Survey();
            cachedSurvey.Questions.Add(new Question());

            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.TenantName = "Tenant";
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                controller.New(new Survey());

                Assert.IsNull(controller.TempData[SurveysController.CachedSurvey]);
            }
        }
        public void NewWhenHttpVerbIsPostCopiesQuestionsFromCachedSurveyToSurveyWhenCallingSaveFromSurveyStoreWhenModelStateIsValid()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();

            var survey = new Survey("slug-name");
            var questionsToBeCopied = new List<Question>();
            questionsToBeCopied.Add(new Question());
            var cachedSurvey = new Survey { Questions = questionsToBeCopied };

            using (var controller = new SurveysController(mockSurveyStore.Object, null, null, null, null, null))
            {
                controller.TenantName = "Tenant";
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                controller.New(survey);
            }

            mockSurveyStore.Verify(r => r.SaveSurvey(It.Is<Survey>(actual => questionsToBeCopied == actual.Questions)));
        }
        public void NewWhenHttpVerbIsPostCallsSaveFromSurveyStoreWithSurveyParameterWhenModelStateIsValid()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();

            var survey = new Survey("slug-name");
            var cachedSurvey = new Survey();
            cachedSurvey.Questions.Add(new Question());

            using (var controller = new SurveysController(mockSurveyStore.Object, null, null, null, null, null))
            {
                controller.TenantName = "Tenant";
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                controller.New(survey);
            }

            mockSurveyStore.Verify(r => r.SaveSurvey(It.IsAny<Survey>()), Times.Once());
        }
        public void NewWhenHttpVerbIsGetReturnsTheCachedSurveyInTheModelWhenCachedSurveyExistsInTempData()
        {
            var mockDictionary = new Mock<IUDFDictionary>();
            mockDictionary.Setup(d => d.AreValidFor<SurveyRow>(It.IsAny<string>(), null)).Returns(true);
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, mockDictionary.Object))
            {
                var survey = new Survey();
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };
                controller.TempData[SurveysController.CachedSurvey] = survey;

                var result = controller.New() as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<Survey>;

                Assert.AreSame(survey, model.ContentModel);
            }
        }
        public void NewWhenHttpVerbIsGetReturnsNewSurveyAsTitleInTheModel()
        {
            var mockDictionary = new Mock<IUDFDictionary>();
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, mockDictionary.Object))
            {
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };

                var result = controller.New() as ViewResult;

                var model = result.ViewData.Model as TenantMasterPageViewData;
                Assert.AreSame("New Survey", model.Title);
            }
        }
        public void NewWhenHttpVerbIsPostSavesCachedSurveyInTempDataWhenCachedSurveyHasNoQuestions()
        {
            var cachedSurvey = new Survey { Questions = new List<Question>() };

            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                var result = controller.New(new Survey()) as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<Survey>;
                Assert.AreSame(cachedSurvey, controller.TempData[SurveysController.CachedSurvey]);
            }
        }
        public void NewWhenHttpVerbIsPostReturnsEmptyViewNameWhenModelStateIsNotValid()
        {
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");
                controller.TempData[SurveysController.CachedSurvey] = new Survey();

                var result = controller.New(new Survey()) as ViewResult;

                Assert.AreEqual(string.Empty, result.ViewName);
            }
        }
        public void NewWhenHttpVerbIsPostSavesCachedSurveyInTempDataWhenModelStateIsNotValid()
        {
            var cachedSurvey = new Survey();

            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                var result = controller.New(new Survey()) as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<Survey>;
                Assert.AreSame(cachedSurvey, controller.TempData[SurveysController.CachedSurvey]);
            }
        }
        public void NewWhenHttpVerbIsGetReturnsANewSurveyInTheModelWhenCachedSurveyDoesNotExistInTempData()
        {
            var mockDictionary = new Mock<IUDFDictionary>();
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, mockDictionary.Object))
            {
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };
                controller.TempData[SurveysController.CachedSurvey] = null;

                var result = controller.New() as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<Survey>;

                Assert.IsInstanceOfType(model.ContentModel, typeof(Survey));
            }
        }