public async Task Then_Updates_Existing_Reservation_If_Supplied(ReservationsRouteModel routeModel,
                                                                        ConfirmLegalEntityViewModel viewModel,
                                                                        GetLegalEntitiesResponse getLegalEntitiesResponse,
                                                                        long decodedAccountId,
                                                                        [Frozen] Mock <IMediator> mockMediator,
                                                                        [Frozen] Mock <IEncodingService> mockEncodingService,
                                                                        EmployerReservationsController controller)
        {
            var firstLegalEntity = getLegalEntitiesResponse.AccountLegalEntities.First();

            firstLegalEntity.AgreementSigned = true;
            viewModel.LegalEntity            = firstLegalEntity.AccountLegalEntityPublicHashedId;
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.Is <GetLegalEntitiesQuery>(query => query.AccountId == decodedAccountId),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(getLegalEntitiesResponse);
            mockEncodingService
            .Setup(service => service.Decode(routeModel.EmployerAccountId, EncodingType.AccountId))
            .Returns(decodedAccountId);

            await controller.PostSelectLegalEntity(routeModel, viewModel);

            mockMediator.Verify(mediator => mediator.Send(
                                    It.Is <CacheReservationEmployerCommand>(command =>
                                                                            command.Id == routeModel.Id),
                                    It.IsAny <CancellationToken>()),
                                Times.Once);
        }
        And_User_Has_Transactor_Role_And_Chosen_Legal_Entity_Has_Not_Signed_Agreement_Then_Redirect_To_Transactor_Sign_Route(
            ReservationsRouteModel routeModel,
            ConfirmLegalEntityViewModel viewModel,
            GetLegalEntitiesResponse getLegalEntitiesResponse,
            [Frozen] Mock <IMediator> mockMediator,
            [Frozen] Mock <IUserClaimsService> mockClaimsService,
            EmployerReservationsController controller)
        {
            var firstLegalEntity = getLegalEntitiesResponse.AccountLegalEntities.First();

            firstLegalEntity.AgreementSigned = false;
            viewModel.LegalEntity            = firstLegalEntity.AccountLegalEntityPublicHashedId;
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.IsAny <GetLegalEntitiesQuery>(),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(getLegalEntitiesResponse);
            mockClaimsService
            .Setup(service => service.UserIsInRole(
                       routeModel.EmployerAccountId,
                       EmployerUserRole.Owner,
                       It.IsAny <IEnumerable <Claim> >()))
            .Returns(false);

            var result = await controller.PostSelectLegalEntity(routeModel, viewModel) as RedirectToRouteResult;

            result.RouteName.Should().Be(RouteNames.EmployerTransactorSignAgreement);
            result.RouteValues[nameof(ReservationsRouteModel.PreviousPage)].Should().Be(RouteNames.EmployerSelectLegalEntity);
        }
        public async Task And_ValidationException_Then_Redirects_To_SelectLegalEntity(
            ReservationsRouteModel routeModel,
            ConfirmLegalEntityViewModel viewModel,
            GetLegalEntitiesResponse getLegalEntitiesResponse,
            [Frozen] Mock <IMediator> mockMediator,
            EmployerReservationsController controller)
        {
            var firstLegalEntity = getLegalEntitiesResponse.AccountLegalEntities.First();

            firstLegalEntity.AgreementSigned = true;
            viewModel.LegalEntity            = firstLegalEntity.AccountLegalEntityPublicHashedId;
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.IsAny <GetLegalEntitiesQuery>(),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(getLegalEntitiesResponse);
            mockMediator.Setup(x => x.Send(It.IsAny <CacheReservationEmployerCommand>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new ValidationException(
                             new ValidationResult("Failed",
                                                  new List <string> {
                "AccountId| Account reservation limit has been reached."
            }), null, null));

            var actual = await controller.PostSelectLegalEntity(routeModel, viewModel);

            actual.Should().NotBeNull();
            var actualViewResult = actual as ViewResult;

            actualViewResult.Should().NotBeNull();
            actualViewResult?.ViewName.Should().Be("SelectLegalEntity");
            controller.ModelState.IsValid.Should().BeFalse();
            controller.ModelState.Should().Contain(pair => pair.Key == "AccountId");
        }
Exemple #4
0
        public async Task Then_Caches_New_Reservation(
            ReservationsRouteModel routeModel,
            ConfirmLegalEntityViewModel viewModel,
            GetLegalEntitiesResponse getLegalEntitiesResponse,
            long decodedAccountId,
            [Frozen] Mock <IMediator> mockMediator,
            [Frozen] Mock <IEncodingService> mockEncodingService,
            EmployerReservationsController controller)
        {
            var firstLegalEntity = getLegalEntitiesResponse.AccountLegalEntities.First();

            viewModel.LegalEntity = firstLegalEntity.AccountLegalEntityPublicHashedId;
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.Is <GetLegalEntitiesQuery>(query => query.AccountId == decodedAccountId),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(getLegalEntitiesResponse);
            mockEncodingService
            .Setup(service => service.Decode(routeModel.EmployerAccountId, EncodingType.AccountId))
            .Returns(decodedAccountId);

            await controller.PostSelectLegalEntity(routeModel, viewModel);

            mockMediator.Verify(mediator => mediator.Send(
                                    It.Is <CacheReservationEmployerCommand>(command =>
                                                                            command.Id != Guid.Empty &&
                                                                            command.AccountId == firstLegalEntity.AccountId &&
                                                                            command.AccountLegalEntityId == firstLegalEntity.AccountLegalEntityId &&
                                                                            command.AccountLegalEntityName == firstLegalEntity.AccountLegalEntityName &&
                                                                            command.AccountLegalEntityPublicHashedId == firstLegalEntity.AccountLegalEntityPublicHashedId &&
                                                                            !command.EmployerHasSingleLegalEntity),
                                    It.IsAny <CancellationToken>()),
                                Times.Once);
        }
        public async Task And_Reservation_Limit_Has_Been_Exceeded_Then_Shows_Reservation_Limit_Reached_Page(
            ReservationsRouteModel routeModel,
            ConfirmLegalEntityViewModel viewModel,
            [Frozen] Mock <IMediator> mockMediator,
            EmployerReservationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetLegalEntitiesQuery>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new ReservationLimitReachedException(10));

            var result = await controller.PostSelectLegalEntity(routeModel, viewModel) as ViewResult;

            Assert.AreEqual("ReservationLimitReached", result?.ViewName);
        }
        public async Task And_Global_Rule_Exists_Then_Shows_Funding_Paused_Page(
            ReservationsRouteModel routeModel,
            ConfirmLegalEntityViewModel viewModel,
            [Frozen] Mock <IMediator> mockMediator,
            EmployerReservationsController controller)
        {
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetLegalEntitiesQuery>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new GlobalReservationRuleException(10));

            var result = await controller.PostSelectLegalEntity(routeModel, viewModel) as ViewResult;

            Assert.AreEqual("EmployerFundingPaused", result?.ViewName);
        }
        public async Task And_Model_Invalid_Then_Shows_View_Again(
            ReservationsRouteModel routeModel,
            ConfirmLegalEntityViewModel viewModel,
            [Frozen] Mock <IMediator> mockMediator,
            EmployerReservationsController controller)
        {
            controller.ModelState.AddModelError("test", "test");

            var result = await controller.PostSelectLegalEntity(routeModel, viewModel) as ViewResult;

            result.Should().NotBeNull();
            result.ViewName.Should().Be("SelectLegalEntity");

            mockMediator.Verify(mediator => mediator.Send(
                                    It.IsAny <CacheReservationEmployerCommand>(),
                                    It.IsAny <CancellationToken>()),
                                Times.Never);
        }
        public async Task Then_Redirects_To_Select_Course(
            ReservationsRouteModel routeModel,
            ConfirmLegalEntityViewModel viewModel,
            GetLegalEntitiesResponse getLegalEntitiesResponse,
            [Frozen] Mock <IMediator> mockMediator,
            EmployerReservationsController controller)
        {
            var firstLegalEntity = getLegalEntitiesResponse.AccountLegalEntities.First();

            firstLegalEntity.AgreementSigned = true;
            viewModel.LegalEntity            = firstLegalEntity.AccountLegalEntityPublicHashedId;
            mockMediator
            .Setup(mediator => mediator.Send(
                       It.IsAny <GetLegalEntitiesQuery>(),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(getLegalEntitiesResponse);

            var result = await controller.PostSelectLegalEntity(routeModel, viewModel) as RedirectToRouteResult;

            result.Should().NotBeNull();
            result.RouteName.Should().Be(RouteNames.EmployerSelectCourse);
        }
        public void GivenIHaveChosenALegalEntity()
        {
            var controller = Services.GetService <EmployerReservationsController>();
            var urlHelper  = Services.GetService <IUrlHelper>();
            var mock       = Mock.Get(urlHelper);

            controller.Url = mock.Object;
            var confirmLegalEntityViewModel = new ConfirmLegalEntityViewModel
            {
                LegalEntity = TestData.AccountLegalEntity.AccountLegalEntityPublicHashedId
            };

            TestData.ActionResult = controller.PostSelectLegalEntity(TestData.ReservationRouteModel, confirmLegalEntityViewModel)
                                    .Result;

            if (typeof(RedirectToRouteResult) == TestData.ActionResult.GetType())
            {
                var result = TestData.ActionResult as RedirectToRouteResult;

                Assert.IsNotNull(result);
                Assert.AreEqual(RouteNames.EmployerSelectCourse, result.RouteName);
            }
        }
        public async Task <IActionResult> PostSelectLegalEntity(ReservationsRouteModel routeModel, ConfirmLegalEntityViewModel viewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var legalEntitiesResponse = await _mediator.Send(new GetLegalEntitiesQuery { AccountId = _encodingService.Decode(routeModel.EmployerAccountId, EncodingType.AccountId) });

                    var requestViewModel = new SelectLegalEntityViewModel(routeModel, legalEntitiesResponse.AccountLegalEntities, viewModel.LegalEntity);
                    return(View("SelectLegalEntity", requestViewModel));
                }

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

                var selectedAccountLegalEntity = response.AccountLegalEntities.Single(model =>
                                                                                      model.AccountLegalEntityPublicHashedId == viewModel.LegalEntity);

                if (!selectedAccountLegalEntity.AgreementSigned)
                {
                    return(RedirectToSignAgreement(routeModel, RouteNames.EmployerSelectLegalEntity));
                }

                await CacheReservation(routeModel, selectedAccountLegalEntity);

                return(RedirectToRoute(RouteNames.EmployerSelectCourse, routeModel));
            }
            catch (ValidationException e)
            {
                foreach (var member in e.ValidationResult.MemberNames)
                {
                    ModelState.AddModelError(member.Split('|')[0], member.Split('|')[1]);
                }

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

                var requestViewModel = new SelectLegalEntityViewModel(routeModel, legalEntitiesResponse.AccountLegalEntities, viewModel.LegalEntity);
                return(View("SelectLegalEntity", requestViewModel));
            }
            catch (ReservationLimitReachedException)
            {
                return(View("ReservationLimitReached", GenerateLimitReachedBackLink(routeModel)));
            }
            catch (GlobalReservationRuleException)
            {
                return(View("EmployerFundingPaused"));
            }
        }