Exemplo n.º 1
0
        private async Task <string> GetEmployerAccountType(ActionExecutingContext context, string userId, string employerAccountId)
        {
            string result;

            if (!GetCookieValueForUserAndEmployer(context.HttpContext,
                                                  userId: userId,
                                                  employerAccountId: employerAccountId,
                                                  readValue: _employerAccountTypeCookieWriter.GetCookieFromRequest,
                                                  result: out result))
            {
                EmployerAccountDetails accountDetails = await _employerAccountProvider.GetEmployerAccountDetailsAsync(employerAccountId);

                result = accountDetails.ApprenticeshipEmployerType;

                if (!_allowedAccountTypes.Any(x => string.Compare(accountDetails.ApprenticeshipEmployerType, x, true) == 0))
                {
                    throw new BlockedEmployerException($"Unknown account type {accountDetails.ApprenticeshipEmployerType}");
                }

                _employerAccountTypeCookieWriter.WriteCookie(context.HttpContext.Response, userId, employerAccountId, result);
            }
            return(result);
        }
        public EmployerAccessCheckFilterTests()
        {
            _modelState = new ModelStateDictionary();
            _routeData  = new RouteData();
            _routeData.Values[RouteValues.EmployerAccountId] = "EMPLOYERID";

            var identity = new ClaimsIdentity();

            identity.AddClaim(new Claim(EmployerRecruitClaims.IdamsUserIdClaimTypeIdentifier, "USERID"));
            _user = new ClaimsPrincipal(identity);

            _httpContext = new Mock <HttpContext>();
            _httpContext.SetupGet(x => x.User).Returns(_user);

            _controllerActionDescriptor = new ControllerActionDescriptor();
            _controllerActionDescriptor.ControllerTypeInfo = typeof(DashboardController).GetTypeInfo();

            _actionContext = new ActionContext(
                _httpContext.Object,
                _routeData,
                _controllerActionDescriptor,
                _modelState
                );

            _actionExecutingContext = new ActionExecutingContext(
                _actionContext,
                new List <IFilterMetadata>(),
                new Dictionary <string, object>(),
                _controllerActionDescriptor
                );

            _levyDeclarationCookieWriter = new Mock <ILevyDeclarationCookieWriter>();
            _recruitVacancyClient        = new Mock <IRecruitVacancyClient>();
            _employerAccountProvider     = new Mock <IEmployerAccountProvider>();
            _eoiAgreementCookieWriter    = new Mock <IEoiAgreementCookieWriter>();

            _employerAccountTypeCookieWriter = new Mock <IEmployerAccountTypeCookieWriter>();
            _employerAccountTypeCookieWriter
            .Setup(x => x.GetCookieFromRequest(It.IsAny <HttpContext>()))
            .Returns(() => _employerAccountTypeCookieValue);

            _next = new Mock <ActionExecutionDelegate>();

            _domainUser = new DomainUser();
            _recruitVacancyClient
            .Setup(x => x.GetUsersDetailsAsync("USERID"))
            .ReturnsAsync(_domainUser);

            _account = new EmployerAccountDetails
            {
                AccountAgreementType = AccountAgreementType.Inconsistent
            };
            _employerAccountProvider
            .Setup(x => x.GetEmployerAccountDetailsAsync("EMPLOYERID"))
            .ReturnsAsync(_account);

            _sut = new EmployerAccessCheckFilter(
                Mock.Of <ILogger <EmployerAccessCheckFilter> >(),
                _levyDeclarationCookieWriter.Object,
                _recruitVacancyClient.Object,
                _employerAccountProvider.Object,
                _eoiAgreementCookieWriter.Object,
                _employerAccountTypeCookieWriter.Object);
        }