예제 #1
0
def build_cache(app):
    """Build cache."""
    cache.init_app(app)
    with app.app_context():
        cache.clear()
        if not app.config.get('TESTING', False):
            try:
                from pay_api.services.code import Code as CodeService  # pylint: disable=import-outside-toplevel
                CodeService.build_all_codes_cache()
            except Exception as e:  # NOQA pylint:disable=broad-except
                app.logger.error('Error on caching ')
                app.logger.error(e)
예제 #2
0
    def response(self, error: Error = Error.SERVICE_UNAVAILABLE):  # pylint: disable=no-self-use
        """Response attributes."""
        from pay_api.services.code import Code as CodeService  # pylint: disable=import-outside-toplevel

        error_details = CodeService.find_code_value_by_type_and_code(
            Code.ERROR.value, error.code)
        return convert_to_response(body=error_details, status=error.status)
예제 #3
0
def test_find_code_value_by_type_and_code(session):
    """Assert that code values are returned."""
    codes = CodeService.find_code_values_by_type(Code.ERROR.value)
    first_code = codes.get('codes')[0].get('type')
    cache.clear()
    code = CodeService.find_code_value_by_type_and_code(
        Code.ERROR.value, first_code)
    assert code is not None
    assert code.get('type') == first_code

    codes = CodeService.find_code_values_by_type(Code.PAYMENT_STATUS.value)
    first_code = codes.get('codes')[0].get('type')
    cache.clear()
    code = CodeService.find_code_value_by_type_and_code(
        Code.PAYMENT_STATUS.value, first_code)
    assert code is not None
    assert code.get('type') == first_code
예제 #4
0
 def as_problem_json(self):
     """Return problem+json of error message."""
     from pay_api.services.code import Code as CodeService  # pylint: disable=import-outside-toplevel
     problem_json = CodeService.find_code_value_by_type_and_code(
         Code.ERROR.value, self.code)
     if not problem_json:  # If the error is not configured in DB, return details from Error object
         problem_json = dict(type=self.code)
         if self.detail:
             problem_json['detail'] = self.detail
     return problem_json
예제 #5
0
 def as_problem_json(self):
     """Return problem+json of error message."""
     from pay_api.services.code import Code as CodeService  # pylint: disable=import-outside-toplevel
     return CodeService.find_code_value_by_type_and_code(
         Code.ERROR.value, self.code)
예제 #6
0
def test_find_payment_types_code_values(session):
    """Assert that code values are returned."""
    codes = CodeService.find_code_values_by_type(Code.PAYMENT_STATUS.value)
    assert codes is not None
    assert len(codes) > 0
예제 #7
0
def test_find_code_values_by_type(session):
    """Assert that code values are returned."""
    codes = CodeService.find_code_values_by_type(Code.ERROR.value)
    assert codes is not None
    assert len(codes) > 0
예제 #8
0
def test_build_cache(session):
    """Assert that code cache is built."""
    CodeService.build_all_codes_cache()
    assert cache is not None
    assert cache.get(Code.ERROR.value) is not None
예제 #9
0
파일: code.py 프로젝트: shabeeb-aot/sbc-pay
 def get(code_type, code):
     """Return all codes based on code_type."""
     return CodeService.find_code_value_by_type_and_code(
         code_type, code), HTTPStatus.OK
예제 #10
0
def check_auth(business_identifier: str,
               account_id: str = None,
               corp_type_code: str = None,
               **kwargs):  # pylint: disable=unused-argument, too-many-branches, too-many-statements
    """Authorize the user for the business entity and return authorization response."""
    user: UserContext = kwargs['user']
    is_authorized: bool = False
    auth_response = None
    product_code = CodeService.find_code_value_by_type_and_code(
        Code.CORP_TYPE.value, corp_type_code).get('product')
    account_id = account_id or user.account_id

    call_auth_svc: bool = True

    if user.is_system():
        is_authorized = bool(Role.EDITOR.value in user.roles)
        if product_code:
            is_authorized = is_authorized and product_code == user.product_code

        # Call auth only if it's business (entities), as affiliation is the auhtorization
        if user.product_code != 'BUSINESS':
            call_auth_svc = False
            # Add account name as the service client name
            auth_response = {
                'account': {
                    'id': user.account_id or user.user_name
                }
            }

    if call_auth_svc:
        bearer_token = user.bearer_token
        current_app.logger.info(
            f'Checking auth for Account : {account_id}, Business : {business_identifier}, '
            f'Is Staff : {user.is_staff()}')
        roles: list = []
        auth_response = {}

        if account_id:
            auth_url = current_app.config.get('AUTH_API_ENDPOINT') + f'orgs/{account_id}' \
                                                                     f'/authorizations?expanded=true'
            additional_headers = None
            if corp_type_code:
                additional_headers = {'Product-Code': product_code}
            auth_response = RestService.get(
                auth_url,
                bearer_token,
                AuthHeaderType.BEARER,
                ContentType.JSON,
                additional_headers=additional_headers).json()
            roles: list = auth_response.get('roles', [])
            g.account_id = account_id
        elif business_identifier:
            auth_url = current_app.config.get(
                'AUTH_API_ENDPOINT'
            ) + f'entities/{business_identifier}/authorizations?expanded=true'
            auth_response = RestService.get(auth_url, bearer_token,
                                            AuthHeaderType.BEARER,
                                            ContentType.JSON).json()

            roles: list = auth_response.get('roles', [])
            g.account_id = auth_response.get('account').get(
                'id') if auth_response.get('account', None) else None
        elif user.is_staff():
            roles: list = user.roles
            auth_response = {}
        else:
            current_app.logger.info('No Auth Information Found')

        g.user_permission = auth_response.get('roles')
        if kwargs.get('one_of_roles', None):
            is_authorized = len(
                list(set(kwargs.get('one_of_roles')) & set(roles))) > 0
        if kwargs.get('contains_role', None):
            is_authorized = kwargs.get('contains_role') in roles
        # Check if premium flag is required
        if kwargs.get('is_premium', False) and auth_response['account'][
                'accountType'] != AccountType.PREMIUM.value:
            is_authorized = False
        # For staff users, if the account is coming as empty add stub data
        # (businesses which are not affiliated won't have account)
        if user.is_staff() and not auth_response.get('account', None):
            auth_response['account'] = {
                'id': f'PASSCODE_ACCOUNT_{business_identifier}'
            }

        if user.is_system() and bool(Role.EDITOR.value in user.roles):
            is_authorized = True

    if not is_authorized:
        abort(403)

    # IF auth response is empty (means a service account or a business with no account by staff)
    if not auth_response:
        if Role.SYSTEM.value in user.roles:  # Call auth only if it's business (entities)
            # Add account name as the service client name
            auth_response = {
                'account': {
                    'id': user.user_name,
                    'paymentInfo': {
                        'methodOfPayment': PaymentMethod.DIRECT_PAY.value
                    }
                }
            }
    return auth_response