public async Task And_Single_Legal_Entity_Then_The_BackLink_Is_Set_To_Return_To_Start(
            ReservationsRouteModel routeModel,
            ICollection <Course> courses,
            GetCachedReservationResult cachedReservationResult,
            [Frozen] Mock <IMediator> mockMediator,
            EmployerReservationsController controller)
        {
            //Arrange
            routeModel.FromReview             = false;
            cachedReservationResult.CohortRef = string.Empty;
            cachedReservationResult.IsEmptyCohortFromSelect      = false;
            cachedReservationResult.EmployerHasSingleLegalEntity = true;
            mockMediator
            .Setup(m => m.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetCoursesResult {
                Courses = courses
            });
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);

            //Act
            var result = await controller.SelectCourse(routeModel) as ViewResult;

            //Assert
            var viewModel = result?.Model as EmployerSelectCourseViewModel;

            Assert.IsNotNull(viewModel);
            Assert.AreEqual(RouteNames.EmployerStart, viewModel.BackLink);
        }
        public void Arrange()
        {
            var fixture = new Fixture().Customize(new AutoMoqCustomization {
                ConfigureMembers = true
            });

            _course = new Course("1-4-5", "test", 1);
            _cachedReservationResult = fixture.Create <GetCachedReservationResult>();
            _externalUrlHelper       = fixture.Freeze <Mock <IExternalUrlHelper> >();
            _mediator = fixture.Freeze <Mock <IMediator> >();

            _controller = fixture.Create <EmployerReservationsController>();

            _mediator.Setup(mediator => mediator.Send(
                                It.IsAny <GetCachedReservationQuery>(),
                                It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _cachedReservationResult);

            _mediator.Setup(mediator => mediator.Send(
                                It.IsAny <CacheReservationCourseCommand>(),
                                It.IsAny <CancellationToken>()))
            .ReturnsAsync(Unit.Value);

            _mediator.Setup(m => m.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetCoursesResult
            {
                Courses = new List <Course>
                {
                    _course
                }
            });
        }
        public async Task Then_It_Sets_The_Back_Link_Correctly_Coming_From_The_SelectReservation_Screen(
            ReservationsRouteModel routeModel,
            GetCoursesResult getCoursesResult,
            GetCachedReservationResult cachedReservationResult,
            string cohortDetailsUrl,
            [Frozen] Mock <IExternalUrlHelper> mockUrlHelper,
            [Frozen] Mock <IMediator> mockMediator,
            ReservationsController controller)
        {
            cachedReservationResult.CohortRef = "ABC123";
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(getCoursesResult);
            routeModel.FromReview = true;
            mockUrlHelper
            .Setup(helper => helper.GenerateCohortDetailsUrl(routeModel.UkPrn, routeModel.EmployerAccountId,
                                                             cachedReservationResult.CohortRef, false, It.IsAny <string>()))
            .Returns(cohortDetailsUrl);

            var result = await controller.ApprenticeshipTraining(routeModel);

            var viewModel = result.Should().BeOfType <ViewResult>()
                            .Which.Model.Should().BeOfType <ApprenticeshipTrainingViewModel>()
                            .Subject;

            viewModel.CohortRef.Should().Be(cachedReservationResult.CohortRef);
            viewModel.BackLink.Should().Be(cohortDetailsUrl);
        }
Пример #4
0
        public async Task Then_If_The_ViewModel_Is_Null_It_Is_Read_From_The_Cache_And_Account_Id_Is_Hashed(
            string hashedAccountId,
            [Frozen] Mock <IMediator> mediator,
            [Frozen] Mock <IEncodingService> encodingService,
            ProviderReservationsController controller,
            ConfirmEmployerViewModel viewModel,
            GetCachedReservationResult cachedResult)
        {
            //Arrange
            mediator
            .Setup(x => x.Send(
                       It.Is <GetCachedReservationQuery>(c => c.Id.Equals(viewModel.Id) && c.UkPrn.Equals(viewModel.UkPrn)),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedResult);
            encodingService.Setup(x => x.Encode(cachedResult.AccountId, EncodingType.AccountId)).Returns(hashedAccountId);

            //Act
            var actual = await controller.ConfirmEmployer(new ConfirmEmployerViewModel { Id = viewModel.Id, UkPrn = viewModel.UkPrn });

            //Assert
            Assert.IsNotNull(actual);
            var viewResult = actual as ViewResult;

            Assert.IsNotNull(viewResult);
            var model = viewResult.Model as ConfirmEmployerViewModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(cachedResult.AccountLegalEntityName, model.AccountLegalEntityName);
            Assert.AreEqual(hashedAccountId, model.AccountPublicHashedId);
            Assert.AreEqual(cachedResult.AccountLegalEntityPublicHashedId, model.AccountLegalEntityPublicHashedId);
            Assert.AreEqual(viewModel.UkPrn, model.UkPrn);
        }
        public async Task Then_It_Returns_The_Apprenticeship_Training_View_With_Mapped_Values(
            ReservationsRouteModel routeModel,
            IEnumerable <TrainingDateModel> expectedStartDates,
            GetCoursesResult getCoursesResult,
            GetCachedReservationResult cachedReservationResult,
            long accountLegalEntityId,
            [Frozen] Mock <IEncodingService> mockEncodingService,
            [Frozen] Mock <ITrainingDateService> mockStartDateService,
            [Frozen] Mock <IMediator> mockMediator,
            ReservationsController controller)
        {
            cachedReservationResult.CohortRef = string.Empty;
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(getCoursesResult);
            mockEncodingService
            .Setup(service => service.Decode(
                       cachedReservationResult.AccountLegalEntityPublicHashedId,
                       EncodingType.PublicAccountLegalEntityId))
            .Returns(accountLegalEntityId);
            mockStartDateService
            .Setup(service => service.GetTrainingDates(accountLegalEntityId))
            .ReturnsAsync(expectedStartDates);
            var mappedDates   = expectedStartDates.Select(startDateModel => new TrainingDateViewModel(startDateModel)).OrderBy(model => model.StartDate);
            var mappedCourses = getCoursesResult.Courses.Select(course => new CourseViewModel(course));

            routeModel.FromReview = false;

            var result = await controller.ApprenticeshipTraining(routeModel);

            var viewModel = result.Should().BeOfType <ViewResult>()
                            .Which.Model.Should().BeOfType <ApprenticeshipTrainingViewModel>()
                            .Subject;

            viewModel.PossibleStartDates.Should().BeEquivalentTo(mappedDates);
            viewModel.Courses.Should().BeEquivalentTo(mappedCourses);
            viewModel.CourseId.Should().Be(cachedReservationResult.CourseId);
            viewModel.IsProvider.Should().BeTrue();
            viewModel.RouteName.Should().Be(RouteNames.ProviderCreateApprenticeshipTraining);
            viewModel.BackLink.Should().Be(RouteNames.ProviderConfirmEmployer);
            viewModel.AccountLegalEntityPublicHashedId.Should()
            .Be(cachedReservationResult.AccountLegalEntityPublicHashedId);
        }
        private string GenerateBackLink(ReservationsRouteModel routeModel, GetCachedReservationResult result)
        {
            if (!string.IsNullOrEmpty(result.CohortRef) || result.IsEmptyCohortFromSelect)
            {
                return(_urlHelper.GenerateCohortDetailsUrl(result.UkPrn, routeModel.EmployerAccountId, result.CohortRef, result.IsEmptyCohortFromSelect));
            }

            if (routeModel.FromReview.HasValue && routeModel.FromReview.Value)
            {
                return(RouteNames.EmployerReview);
            }

            if (result.EmployerHasSingleLegalEntity)
            {
                return(RouteNames.EmployerStart);
            }

            return(RouteNames.EmployerSelectLegalEntity);
        }
        public async Task <IActionResult> SelectLegalEntity(ReservationsRouteModel routeModel)
        {
            try
            {
                GetCachedReservationResult cachedResponse = null;
                if (routeModel.Id.HasValue)
                {
                    cachedResponse = await _mediator.Send(new GetCachedReservationQuery { Id = routeModel.Id.Value });
                }

                var legalEntitiesResponse = await _mediator.Send(new GetLegalEntitiesQuery { AccountId = _encodingService.Decode(routeModel.EmployerAccountId, EncodingType.AccountId) });

                if (legalEntitiesResponse.AccountLegalEntities.Count() == 1)
                {
                    var accountLegalEntity = legalEntitiesResponse.AccountLegalEntities.First();

                    if (!accountLegalEntity.AgreementSigned)
                    {
                        return(RedirectToSignAgreement(routeModel, RouteNames.EmployerIndex));
                    }

                    await CacheReservation(routeModel, accountLegalEntity, true);

                    return(RedirectToRoute(RouteNames.EmployerSelectCourse, routeModel));
                }

                var viewModel = new SelectLegalEntityViewModel(routeModel, legalEntitiesResponse.AccountLegalEntities, cachedResponse?.AccountLegalEntityPublicHashedId);
                return(View("SelectLegalEntity", viewModel));
            }
            catch (ReservationLimitReachedException)
            {
                return(View("ReservationLimitReached", GenerateLimitReachedBackLink(routeModel)));
            }
            catch (GlobalReservationRuleException)
            {
                return(View("EmployerFundingPaused", GenerateLimitReachedBackLink(routeModel)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(RedirectToRoute(RouteNames.Error500));
            }
        }
        public void Arrange()
        {
            _fixture = new Fixture().Customize(new AutoMoqCustomization());

            _course = new Course("1-4-5", "test", 1);
            _cachedReservationResult = _fixture.Create <GetCachedReservationResult>();

            _mediator   = new Mock <IMediator>();
            _urlHelper  = new Mock <IExternalUrlHelper>();
            _controller = new ReservationsController(
                _mediator.Object,
                Mock.Of <ITrainingDateService>(),
                Mock.Of <IOptions <ReservationsWebConfiguration> >(),
                Mock.Of <ILogger <ReservationsController> >(),
                Mock.Of <IEncodingService>(),
                _urlHelper.Object);

            _mediator.Setup(mediator => mediator.Send(
                                It.IsAny <GetCachedReservationQuery>(),
                                It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _cachedReservationResult);

            _mediator.Setup(mediator => mediator.Send(
                                It.IsAny <CacheReservationCourseCommand>(),
                                It.IsAny <CancellationToken>()))
            .ReturnsAsync(Unit.Value);

            _mediator.Setup(mediator => mediator.Send(
                                It.IsAny <CacheReservationStartDateCommand>(),
                                It.IsAny <CancellationToken>()))
            .ReturnsAsync(Unit.Value);

            _mediator.Setup(m => m.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetCoursesResult
            {
                Courses = new List <Course>
                {
                    _course
                }
            });
        }
        public async Task Then_The_BackLink_Is_Set_To_Return_To_CohortDetails_If_There_Is_A_Cohort_Ref(
            ICollection <Course> courses,
            [Frozen] Mock <IExternalUrlHelper> externalUrlHelper,
            [Frozen] Mock <IMediator> mockMediator,
            ReservationsRouteModel routeModel,
            GetCachedReservationResult cachedReservationResult,
            string cohortUrl,
            EmployerReservationsController controller
            )
        {
            //Arrange
            cachedReservationResult.CohortRef = "ABC123";
            cachedReservationResult.IsEmptyCohortFromSelect = false;
            cachedReservationResult.UkPrn = null;
            mockMediator.Setup(m => m.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetCoursesResult
            {
                Courses = courses
            });
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);
            externalUrlHelper.Setup(x =>
                                    x.GenerateCohortDetailsUrl(
                                        null, routeModel.EmployerAccountId, cachedReservationResult.CohortRef, false,
                                        It.IsAny <string>()
                                        ))
            .Returns(cohortUrl);

            //Act
            var result = await controller.SelectCourse(routeModel) as ViewResult;

            //Assert
            var viewModel = result?.Model as EmployerSelectCourseViewModel;

            Assert.IsNotNull(viewModel);
            Assert.AreEqual(cohortUrl, viewModel.BackLink);
            Assert.AreEqual(cachedReservationResult.CohortRef, viewModel.CohortReference);
            Assert.AreEqual(cachedReservationResult.IsEmptyCohortFromSelect, viewModel.IsEmptyCohortFromSelect);
        }
        public async Task Then_It_Calls_Start_Date_Service_To_Get_Start_Dates(
            ReservationsRouteModel routeModel,
            GetCachedReservationResult cachedReservationResult,
            long accountLegalEntityId,
            [Frozen] Mock <IMediator> mockMediator,
            [Frozen] Mock <IEncodingService> mockEncodingService,
            [Frozen] Mock <ITrainingDateService> mockStartDateService,
            ReservationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);
            mockEncodingService
            .Setup(service => service.Decode(
                       cachedReservationResult.AccountLegalEntityPublicHashedId,
                       EncodingType.PublicAccountLegalEntityId))
            .Returns(accountLegalEntityId);

            await controller.ApprenticeshipTraining(routeModel);

            mockStartDateService.Verify(provider => provider.GetTrainingDates(accountLegalEntityId), Times.Once);
        }
Пример #11
0
        public async Task <IActionResult> ApprenticeshipTraining(ReservationsRouteModel routeModel)
        {
            GetCachedReservationResult cachedReservation = null;

            if (routeModel.Id.HasValue)
            {
                cachedReservation = await _mediator.Send(new GetCachedReservationQuery { Id = routeModel.Id.GetValueOrDefault() });

                //todo: error handling if fails validation e.g. id not found, redirect to index.
            }

            var viewModel = await BuildApprenticeshipTrainingViewModel(
                routeModel.UkPrn != null,
                cachedReservation?.AccountLegalEntityPublicHashedId,
                cachedReservation?.CourseId,
                cachedReservation?.TrainingDate,
                routeModel.FromReview ?? false,
                cachedReservation?.CohortRef,
                routeModel.UkPrn,
                routeModel.EmployerAccountId);

            return(View(viewModel));
        }
        public async Task And_Previously_Selected_Course_Then_Sets_Selected_Course(
            ReservationsRouteModel routeModel,
            GetCoursesResult coursesResult,
            GetCachedReservationResult cachedReservationResult,
            [Frozen] Mock <IMediator> mockMediator,
            EmployerReservationsController controller)
        {
            cachedReservationResult.CourseId = coursesResult.Courses.First().Id;
            mockMediator
            .Setup(m => m.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(coursesResult);
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);

            var result = await controller.SelectCourse(routeModel) as ViewResult;

            result.Should().NotBeNull();
            var viewModel = result.Model as EmployerSelectCourseViewModel;

            viewModel.Courses.Single(model => model.Selected == "selected").Id
            .Should().Be(cachedReservationResult.CourseId);
        }
        public async Task Then_It_Returns_The_ViewModel(
            ReservationsRouteModel routeModel,
            GetCachedReservationResult cachedReservationResult,
            [Frozen] Mock <IMediator> mockMediator,
            ReservationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);
            routeModel.FromReview = false;

            var result = await controller.Review(routeModel);

            var viewModel = result.Should().BeOfType <ViewResult>()
                            .Which.Model.Should().BeOfType <ReviewViewModel>().Subject;

            viewModel.RouteModel.Should().BeEquivalentTo(routeModel);
            viewModel.TrainingDate.Should().Be(cachedReservationResult.TrainingDate);
            viewModel.CourseDescription.Should().Be(cachedReservationResult.CourseDescription);
            viewModel.AccountLegalEntityName.Should().Be(cachedReservationResult.AccountLegalEntityName);
            viewModel.AccountLegalEntityPublicHashedId.Should().Be(cachedReservationResult.AccountLegalEntityPublicHashedId);
            viewModel.RouteModel.FromReview.Should().BeTrue();
        }
        public async Task Then_It_Sets_The_Back_Link_Correctly_Coming_From_The_Review_Screen(
            ReservationsRouteModel routeModel,
            GetCoursesResult getCoursesResult,
            GetCachedReservationResult cachedReservationResult,
            [Frozen] Mock <IMediator> mockMediator,
            ReservationsController controller)
        {
            cachedReservationResult.CohortRef = string.Empty;
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cachedReservationResult);
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCoursesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(getCoursesResult);
            routeModel.FromReview = true;

            var result = await controller.ApprenticeshipTraining(routeModel);

            var viewModel = result.Should().BeOfType <ViewResult>()
                            .Which.Model.Should().BeOfType <ApprenticeshipTrainingViewModel>()
                            .Subject;

            viewModel.BackLink.Should().Be(RouteNames.ProviderReview);
        }