public async Task NullViewModelReturnsBadRequest()
        {
            QuestionGetRequestViewModel viewModel = null;
            var actionResponse = await AssessmentController.Index(viewModel).ConfigureAwait(false);

            Assert.IsType <BadRequestResult>(actionResponse);
        }
        public async Task QuestionsMustBeAnsweredInOrder()
        {
            var viewModel = new QuestionGetRequestViewModel()
            {
                QuestionNumber = 2, AssessmentType = "name1"
            };
            var expectedQuestion = new GetQuestionResponse()
            {
                MaxQuestionsCount = 3, QuestionNumber = 1
            };
            var expectedAssessment = new GetAssessmentResponse()
            {
                CurrentQuestionNumber = 1
            };

            A.CallTo(() => Session.HasValidSession()).Returns(true);
            A.CallTo(() => ApiService.GetQuestion(viewModel.AssessmentType, viewModel.QuestionNumber)).Returns(expectedQuestion);
            A.CallTo(() => ApiService.GetAssessment()).Returns(expectedAssessment);

            var actionResponse = await AssessmentController.Index(viewModel).ConfigureAwait(false);

            Assert.IsType <RedirectResult>(actionResponse);

            var redirectResult = actionResponse as RedirectResult;

            Assert.Equal($"~/{RouteName.Prefix}/assessment/name1/1", redirectResult.Url);
        }
        public async Task <IActionResult> Index(QuestionGetRequestViewModel requestViewModel)
        {
            this.logService.LogInformation($"{nameof(this.Index)} started");

            if (requestViewModel == null)
            {
                return(BadRequest());
            }

            var hasSessionId = await HasSessionId().ConfigureAwait(false);

            if (!hasSessionId)
            {
                return(RedirectToRoot());
            }

            var questionResponse = await GetQuestion(requestViewModel.AssessmentType, requestViewModel.QuestionNumber).ConfigureAwait(false);

            if (questionResponse == null)
            {
                return(BadRequest());
            }

            var assessment = await apiService.GetAssessment().ConfigureAwait(false);

            if (assessment == null)
            {
                return(BadRequest());
            }

            if (requestViewModel.QuestionNumber > questionResponse.MaxQuestionsCount)
            {
                return(BadRequest());
            }

            if (questionResponse.IsComplete)
            {
                return(RedirectTo("results"));
            }

            if (requestViewModel.QuestionNumber > assessment.CurrentQuestionNumber)
            {
                return(RedirectTo($"assessment/{requestViewModel.AssessmentType}/{assessment.CurrentQuestionNumber}"));
            }

            var responseViewModel = mapper.Map <QuestionGetResponseViewModel>(questionResponse);

            this.logService.LogInformation($"{nameof(this.Index)} generated the model and ready to pass to the view");

            return(View(responseViewModel));
        }
        public async Task IfQuestionDoesNotExistsReturnsBadRequest()
        {
            var viewModel = new QuestionGetRequestViewModel()
            {
                AssessmentType = "at1", QuestionNumber = 1
            };
            GetQuestionResponse expectedQuestion = null;

            A.CallTo(() => Session.HasValidSession()).Returns(true);
            A.CallTo(() => ApiService.GetQuestion(viewModel.AssessmentType, viewModel.QuestionNumber)).Returns(expectedQuestion);

            var actionResponse = await AssessmentController.Index(viewModel).ConfigureAwait(false);

            Assert.IsType <BadRequestResult>(actionResponse);
        }
        public async Task GetAssessmentWhenAssessmentDoesntExistReturnsBadResponse()
        {
            var viewModel = new QuestionGetRequestViewModel()
            {
                QuestionNumber = 2, AssessmentType = "name1"
            };
            var expectedQuestion = new GetQuestionResponse();
            GetAssessmentResponse expectedAssessment = null;

            A.CallTo(() => Session.HasValidSession()).Returns(true);
            A.CallTo(() => ApiService.GetQuestion(viewModel.AssessmentType, viewModel.QuestionNumber)).Returns(expectedQuestion);
            A.CallTo(() => ApiService.GetAssessment()).Returns(expectedAssessment);

            var actionResponse = await AssessmentController.Index(viewModel).ConfigureAwait(false);

            Assert.IsType <BadRequestResult>(actionResponse);
        }
        public async Task IfQuestionNumberIsGreaterThanMaxMaxQuestionsCountReturnsBadRequest()
        {
            var viewModel = new QuestionGetRequestViewModel()
            {
                QuestionNumber = 3, AssessmentType = "name1"
            };
            var expectedQuestion = new GetQuestionResponse()
            {
                MaxQuestionsCount = 2
            };

            A.CallTo(() => Session.HasValidSession()).Returns(true);
            A.CallTo(() => ApiService.GetQuestion(viewModel.AssessmentType, 1)).Returns(expectedQuestion);

            var actionResponse = await AssessmentController.Index(viewModel).ConfigureAwait(false);

            Assert.IsType <BadRequestResult>(actionResponse);
        }
        public async Task IfAssessmentIsCompleteRedirectsToResults()
        {
            var viewModel = new QuestionGetRequestViewModel()
            {
                QuestionNumber = 2, AssessmentType = "name1"
            };
            var expectedQuestion = new GetQuestionResponse()
            {
                MaxQuestionsCount = 2, IsComplete = true
            };

            A.CallTo(() => Session.HasValidSession()).Returns(true);
            A.CallTo(() => ApiService.GetQuestion(viewModel.AssessmentType, viewModel.QuestionNumber)).Returns(expectedQuestion);

            var actionResponse = await AssessmentController.Index(viewModel).ConfigureAwait(false);

            Assert.IsType <RedirectResult>(actionResponse);

            var redirectResult = actionResponse as RedirectResult;

            Assert.Equal($"~/{RouteName.Prefix}/results", redirectResult.Url);
        }