Ejemplo n.º 1
0
        public async Task DisplayCopiesTheSurveyQuestionTextToTheSurveyAnswerReturnedInTheContentModel()
        {
            var survey = new ClientModels.Survey()
            {
                Questions = new List <ClientModels.Question>()
                {
                    new ClientModels.Question()
                    {
                        Text = "question text to copy"
                    }
                }
            };

            var mockSurveyManagementService = new Mock <ISurveyManagementService>();

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(survey);

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       new Mock <ISurveyAnswerService>().Object,
                       new Mock <ISurveyResponseService>().Object))
            {
                var result = await controller.Display(string.Empty) as ViewResult;

                var model = result.ViewData.Model as PageViewData <SurveyAnswer>;
                Assert.AreEqual("question text to copy", model.ContentModel.QuestionAnswers.First().QuestionText);
            }
        }
Ejemplo n.º 2
0
        public async Task DisplayWhenPostReturnsEmptyViewNameWhenModelIsNotValid()
        {
            var mockSurveyManagementService = new Mock <ISurveyManagementService>();
            var survey = new ClientModels.Survey()
            {
                Questions = new List <ClientModels.Question>()
                {
                    new ClientModels.Question()
                }
            };

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(survey);
            var surveyAnswer = new SurveyAnswer()
            {
                QuestionAnswers = new List <QuestionAnswer>()
                {
                    new QuestionAnswer()
                }
            };

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       new Mock <ISurveyAnswerService>().Object,
                       new Mock <ISurveyResponseService>().Object))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");

                var result = await controller.Display(string.Empty, surveyAnswer) as ViewResult;

                Assert.IsNull(result.ViewName);
            }
        }
Ejemplo n.º 3
0
        public async Task DisplayWhenPostThrowsIfSaveSurveyAnswersReadFromSurveyStoreHaveDifferentCountToTheSurveyAnswerParameter()
        {
            var surveyWith1Question = new ClientModels.Survey()
            {
                Questions = new List <ClientModels.Question>()
                {
                    new ClientModels.Question()
                }
            };

            var mockSurveyManagementService = new Mock <ISurveyManagementService>();

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(surveyWith1Question);
            var surveyAnswerWithoutQuestions = new SurveyAnswer();

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       new Mock <ISurveyAnswerService>().Object,
                       new Mock <ISurveyResponseService>().Object))
            {
                await AssertEx.ThrowsExceptionAsync <ArgumentException>(
                    async() => await controller.Display(string.Empty, surveyAnswerWithoutQuestions),
                    string.Empty, null);
            }
        }
Ejemplo n.º 4
0
        public async Task DisplayWhenPostCallsSaveSurveyAnswerFromSurveyAnswerStoreWithTheSlugParameterWhenModelIsValid()
        {
            var survey = new ClientModels.Survey()
            {
                SlugName = "slug"
            };

            var mockSurveyManagementService = new Mock <ISurveyManagementService>();

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(survey);
            var mockSurveyAnswerService   = new Mock <ISurveyAnswerService>();
            var mockSurveyResponseService = new Mock <ISurveyResponseService>();

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       mockSurveyAnswerService.Object,
                       mockSurveyResponseService.Object))
            {
                await controller.Display("slug", new SurveyAnswer());
            }

            mockSurveyResponseService.Verify(r => r.SaveSurveyResponseAsync(
                                                 It.Is <ClientModels.SurveyAnswer>(
                                                     sa => "slug" == sa.SlugName)),
                                             Times.Once);
        }
Ejemplo n.º 5
0
        public async Task <ClientModels.Survey> GetSurveyAsync(string slugName)
        {
            if (string.IsNullOrWhiteSpace(slugName))
            {
                throw new ArgumentException($"Required {nameof(slugName)} parameter is empty.");
            }

            try
            {
                var container = _surveyContainerFactory(SurveyListPartitionKeyAndContainerName);
                var survey    = await container.GetAsync(slugName);

                ClientModels.Survey result = null;
                if (survey != null)
                {
                    result = survey.ToSurvey();
                }

                return(result);
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceRequestFailed(ex.ToString());
                throw;
            }
        }
Ejemplo n.º 6
0
        public async Task <ClientModels.SurveyInformation> PublishSurveyAsync([FromBody] ClientModels.Survey survey)
        {
            try
            {
                if (survey == null)
                {
                    throw new ArgumentNullException(nameof(survey));
                }

                if (string.IsNullOrEmpty(survey.SlugName) && string.IsNullOrEmpty(survey.Title))
                {
                    throw new ArgumentException($"{nameof(survey)} must have a slug or title");
                }
                var slugName = string.IsNullOrEmpty(survey.SlugName) ? GenerateSlug(survey.Title, 100) : survey.SlugName;
                survey.SlugName  = slugName;
                survey.CreatedOn = DateTime.UtcNow;
                var table     = _surveyInformationTableFactory(SurveyListPartitionKeyAndContainerName);
                var container = _surveyContainerFactory(SurveyListPartitionKeyAndContainerName);

                await table.EnsureExistsAsync();

                await container.EnsureExistsAsync();

                var row         = survey.ToSurveyRow(SurveyListPartitionKeyAndContainerName);
                var surveyModel = survey.ToSurvey();

                var existingRows = await table.GetByStringPropertiesAsync(new[]
                {
                    new KeyValuePair <string, string>(nameof(SurveyInformationRow.PartitionKey), row.PartitionKey),
                    new KeyValuePair <string, string>(nameof(SurveyInformationRow.SlugName), row.SlugName)
                });

                if (existingRows.Count > 0)
                {
                    throw new InvalidOperationException(
                              $"Survey with PartitionKey: {row.PartitionKey} and SlugName: {row.SlugName} is already published");
                }

                // The survey is not published, so save to blob storage first, then table.
                await container.SaveAsync(surveyModel.SlugName, surveyModel);

                await table.AddAsync(row);

                return(row.ToSurveyInformation());
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceRequestFailed(ex.ToString());
                throw;
            }
        }
Ejemplo n.º 7
0
        internal static Tailspin.Web.Shared.Models.SurveyAnswer ToSurveyAnswer(this ClientModels.Survey survey)
        {
            if (survey == null)
            {
                throw new ArgumentNullException(nameof(survey));
            }

            return(new Shared.Models.SurveyAnswer()
            {
                QuestionAnswers = survey.Questions.Select(q => q.ToQuestionAnswer()).ToList(),
                SlugName = survey.SlugName,
                Title = survey.Title
            });
        }
Ejemplo n.º 8
0
        internal static Models.Survey ToSurvey(this ClientModels.Survey survey)
        {
            if (survey == null)
            {
                throw new ArgumentNullException(nameof(survey));
            }

            return(new Models.Survey()
            {
                CreatedOn = survey.CreatedOn,
                Questions = survey.Questions.Select(q => q.ToQuestion()).ToList(),
                SlugName = survey.SlugName,
                Title = survey.Title
            });
        }
Ejemplo n.º 9
0
        internal static Tailspin.Web.Shared.Models.Survey ToSurvey(this ClientModels.Survey survey)
        {
            if (survey == null)
            {
                throw new ArgumentNullException(nameof(survey));
            }

            return(new Shared.Models.Survey()
            {
                CreatedOn = survey.CreatedOn,
                Questions = survey.Questions.Select(q => q.ToQuestion()).ToList(),
                TenantId = "",
                Title = survey.Title
            });
        }
Ejemplo n.º 10
0
        public async Task DisplayWhenPostCallsSaveSurveyAnswerFromSurveyAnswerStoreWithQuestionTypeReadFromTheSurveyWhenModelIsValid()
        {
            var survey = new ClientModels.Survey
            {
                Questions = new List <ClientModels.Question>
                {
                    new ClientModels.Question
                    {
                        Type = ClientModels.QuestionType.SimpleText
                    },
                }
            };

            var mockSurveyManagementService = new Mock <ISurveyManagementService>();

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(survey);
            var mockSurveyAnswerService   = new Mock <ISurveyAnswerService>();
            var mockSurveyResponseService = new Mock <ISurveyResponseService>();
            var surveyAnswer = new SurveyAnswer()
            {
                QuestionAnswers = new List <QuestionAnswer>()
                {
                    new QuestionAnswer()
                }
            };

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       mockSurveyAnswerService.Object,
                       mockSurveyResponseService.Object))
            {
                await controller.Display(string.Empty, surveyAnswer);
            }

            mockSurveyResponseService.Verify(r => r.SaveSurveyResponseAsync(
                                                 It.Is <ClientModels.SurveyAnswer>(
                                                     sa => ClientModels.QuestionType.SimpleText == sa.QuestionAnswers[0].QuestionType)),
                                             Times.Once);
        }
Ejemplo n.º 11
0
        public async Task DisplayReturnsSurveyTitleAsTitleInTheModel()
        {
            var survey = new ClientModels.Survey {
                Title = "title"
            };

            var mockSurveyManagementService = new Mock <ISurveyManagementService>();

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(survey);

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       new Mock <ISurveyAnswerService>().Object,
                       new Mock <ISurveyResponseService>().Object))
            {
                var result = await controller.Display(string.Empty) as ViewResult;

                var model = result.ViewData.Model as MasterPageViewData;
                Assert.AreSame("title", model.Title);
            }
        }
Ejemplo n.º 12
0
        internal static SurveyInformationRow ToSurveyRow(this ClientModels.Survey survey, string partitionKey)
        {
            if (survey == null)
            {
                throw new ArgumentNullException(nameof(survey));
            }

            if (string.IsNullOrWhiteSpace(partitionKey))
            {
                throw new ArgumentException($"{nameof(partitionKey)} cannot be null, empty, or only whitespace");
            }

            return(new SurveyInformationRow()
            {
                CreatedOn = survey.CreatedOn,
                PartitionKey = partitionKey,
                // Store the rows in reverse DateTime order
                RowKey = $"{DateTime.MaxValue.Ticks - survey.CreatedOn.Ticks:D19}",
                SlugName = survey.SlugName,
                Title = survey.Title
            });
        }
Ejemplo n.º 13
0
        public async Task DisplayCopiesTheSurveyTitleToTheSurveyAnswerReturnedInTheContentModel()
        {
            var survey = new ClientModels.Survey()
            {
                Title = "title to be copied"
            };

            var mockSurveyManagementService = new Mock <ISurveyManagementService>();

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(survey);

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       new Mock <ISurveyAnswerService>().Object,
                       new Mock <ISurveyResponseService>().Object))
            {
                var result = await controller.Display(string.Empty) as ViewResult;

                var model = result.ViewData.Model as PageViewData <SurveyAnswer>;
                Assert.AreEqual("title to be copied", model.ContentModel.Title);
            }
        }
Ejemplo n.º 14
0
        public async Task DisplayWhenPostReturnsModelWithTheAnswersReadFromTheParameterWhenModelIsNotValid()
        {
            var mockSurveyManagementService = new Mock <ISurveyManagementService>();
            var survey = new ClientModels.Survey()
            {
                Questions = new List <ClientModels.Question>()
                {
                    new ClientModels.Question()
                }
            };

            mockSurveyManagementService.Setup(r => r.GetSurveyAsync(It.IsAny <string>()))
            .ReturnsAsync(survey);
            var surveyAnswer = new SurveyAnswer
            {
                QuestionAnswers = new List <QuestionAnswer>()
                {
                    new QuestionAnswer
                    {
                        Answer = "answer"
                    }
                }
            };

            using (var controller = new SurveysController(
                       mockSurveyManagementService.Object,
                       new Mock <ISurveyAnswerService>().Object,
                       new Mock <ISurveyResponseService>().Object))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");

                var result = await controller.Display(string.Empty, surveyAnswer) as ViewResult;

                var model = result.ViewData.Model as PageViewData <SurveyAnswer>;
                Assert.AreEqual("answer", model.ContentModel.QuestionAnswers.First().Answer);
            }
        }