public void AddQuestionReplacesCarriageReturnsInPossibleAnswersWhenModelIsValid()
        {
            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                var question = new Question { PossibleAnswers = "possible answers\r\n" };
                controller.TempData[SurveysController.CachedSurvey] = new Survey();

                controller.AddQuestion(question);

                var cachedSurvey = controller.TempData[SurveysController.CachedSurvey] as Survey;

                var actualQuestion = cachedSurvey.Questions.Single(q => q == question);
                Assert.AreEqual("possible answers\n", actualQuestion.PossibleAnswers);
            }
        }
        public void AddQuestionCopiesCachedSurveyToTempDataWhenModelIsNotValid()
        {
            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");
                var cachedSurvey = new Survey();
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;

                var result = controller.AddQuestion(null) as ViewResult;

                var cachedSurveyReturnedInTempData = result.TempData[SurveysController.CachedSurvey] as Survey;

                Assert.AreSame(cachedSurvey, cachedSurveyReturnedInTempData);
            }
        }
        public void AddQuestionAddsTheNewQuestionToTheCachedSurveyWhenModelIsValid()
        {
            var cachedSurvey = new Survey();
            cachedSurvey.Questions.Add(new Question());
            var question = new Question();

            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = cachedSurvey;
                controller.AddQuestion(question);

                var actualQuestions = (controller.TempData[SurveysController.CachedSurvey] as Survey).Questions;

                Assert.AreEqual(2, actualQuestions.Count);
                Assert.IsTrue(actualQuestions.Contains(question));
            }
        }
        public void ExportPostRedirectsToBrowseAction()
        {
            var mockSurveyTransferStore = new Mock<ISurveyTransferStore>();

            using (var controller = new SurveysController(null, null, null, null, mockSurveyTransferStore.Object, null))
            {
                var result = controller.ExportResponses("tenant", "slugName") as RedirectToRouteResult;

                Assert.AreEqual(result.RouteValues["action"], "BrowseResponses");
            }
        }
        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 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 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 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 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 NewQuestionReturnsEmptyViewName()
        {
            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = new Survey();

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

                Assert.AreEqual(string.Empty, result.ViewName);
            }
        }
        public void NewQuestionCopiesSurveyTitleToCachedSurveyThatIsReturnedInViewData()
        {
            var survey = new Survey { Title = "title" };

            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = new Survey();

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

                var cachedSurvey = result.TempData[SurveysController.CachedSurvey] as Survey;
                Assert.AreSame(survey.Title, cachedSurvey.Title);
            }
        }
        public void IndexReturnsTheSurveysForTheTenantInTheModel()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveysByTenant(It.IsAny<string>()))
                .Returns(new List<Survey>() { new Survey() { Title = "title" } });

            using (var controller = new SurveysController(mockSurveyStore.Object, null, null, null, null, null))
            {
                controller.Tenant = new Tenant() { SubscriptionKind = SubscriptionKind.Standard };

                var result = controller.Index() as ViewResult;

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

                Assert.AreEqual(1, model.ContentModel.Count());
                Assert.AreEqual("title", model.ContentModel.First().Title);
            }
        }
        public void IndexReturnsMySurveysAsTitleInTheModel()
        {
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                var result = controller.Index() as ViewResult;

                var model = result.ViewData.Model as TenantMasterPageViewData;
                Assert.AreSame("My Surveys", model.Title);
            }
        }
        public void IndexReturnsEmptyViewName()
        {
            using (var controller = new SurveysController(new Mock<ISurveyStore>().Object, null, null, null, null, null))
            {
                var result = controller.Index() as ViewResult;

                Assert.AreEqual(string.Empty, result.ViewName);
            }
        }
        public void IndexCallsGetAllSurveysByTenantFromSurveyStore()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();

            using (var controller = new SurveysController(mockSurveyStore.Object, null, null, null, null, null))
            {
                controller.TenantName = "tenant";

                controller.Index();
            }

            mockSurveyStore.Verify(r => r.GetSurveysByTenant(It.Is<string>(actual => "tenant" == actual)), Times.Once());
        }
        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 NewQuestionReturnsNewQuestionAsTitleInTheModel()
        {
            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = new Survey();

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

                var model = result.ViewData.Model as TenantMasterPageViewData;
                Assert.AreSame("New Question", 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 NewQuestionReturnsNewQuestionInTheModel()
        {
            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = new Survey();

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

                var model = result.ViewData.Model as TenantPageViewData<Question>;
                Assert.IsInstanceOfType(model.ContentModel, typeof(Question));
            }
        }
        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 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));
            }
        }
        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 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 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 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 AddQuestionReturnsNewQuestionAsModelWhenModelIsNotValidAndQuestionIsNull()
        {
            using (var controller = new SurveysController(null, null, null, null, null, null))
            {
                controller.TempData[SurveysController.CachedSurvey] = new Survey();
                controller.ModelState.AddModelError("error for test", "invalid model state");

                var result = controller.AddQuestion(null) as ViewResult;

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

                Assert.IsInstanceOfType(model.ContentModel, typeof(Question));
            }
        }
        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 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 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());
        }