예제 #1
0
def test_search_invalid_criteria_400(session, client, jwt, search_type,
                                     json_data):
    """Assert that validation of a search request with invalid criteria throws a BusinessException."""
    # test
    with pytest.raises(BusinessException) as bad_request_err:
        SearchRequest.validate_query(json_data)

    # check
    assert bad_request_err
    assert bad_request_err.value.status_code == HTTPStatus.BAD_REQUEST
예제 #2
0
def test_search_enddatatetime_invalid(session, client, jwt):
    """Assert that validation of a search with an invalid endDateTime throws a BusinessException."""
    # setup
    json_data = {
        'type': 'REGISTRATION_NUMBER',
        'criteria': {
            'value': 'TEST0001'
        },
        'clientReferenceId': 'T-API-SQ-RG-8',
        'startDateTime': '2021-01-20T19:38:43+00:00'
    }
    ts_end = now_ts_offset(1, True)
    json_data['endDateTime'] = format_ts(ts_end)

    # test
    with pytest.raises(BusinessException) as bad_request_err:
        SearchRequest.validate_query(json_data)

    # check
    assert bad_request_err
    assert bad_request_err.value.status_code == HTTPStatus.BAD_REQUEST
    print(bad_request_err.value.error)
예제 #3
0
    def post():  # pylint: disable=too-many-branches
        """Execute a new search request using criteria in the request body."""
        try:
            # Quick check: must be staff or provide an account ID.
            account_id = resource_utils.get_account_id(request)
            if not account_id:
                return resource_utils.account_required_response()

            # Verify request JWT and account ID
            if not authorized(account_id, jwt):
                return resource_utils.unauthorized_error_response(account_id)

            request_json = request.get_json(silent=True)
            # Validate request against the schema.
            valid_format, errors = schema_utils.validate(
                request_json, 'searchQuery', 'ppr')
            if not valid_format:
                return resource_utils.validation_error_response(
                    errors, VAL_ERROR)
            # Perform any extra data validation such as start and end dates here
            SearchRequest.validate_query(request_json)
            # Staff has special payment rules and setup.
            if is_staff_account(account_id) or is_bcol_help(account_id):
                return staff_search(request, request_json, account_id)

            query = SearchRequest.create_from_json(
                request_json, account_id,
                g.jwt_oidc_token_info.get('username', None))

            # Charge a search fee.
            invoice_id = None
            payment = Payment(jwt=jwt.get_token_auth_header(),
                              account_id=account_id,
                              details=get_payment_details(
                                  query, request_json['type']))
            pay_ref = payment.create_payment(TransactionTypes.SEARCH.value, 1,
                                             None, query.client_reference_id)
            invoice_id = pay_ref['invoiceId']
            query.pay_invoice_id = int(invoice_id)
            query.pay_path = pay_ref['receipt']

            # Execute the search query: treat no results as a success.
            try:
                query.search()

                # Now save the initial detail results in the search_result table with no
                # search selection criteria (the absence indicates an incomplete search).
                search_result = SearchResult.create_from_search_query(query)
                search_result.save()

            except Exception as db_exception:  # noqa: B902; handle all db related errors.
                current_app.logger.error(
                    SAVE_ERROR_MESSAGE.format(account_id, repr(db_exception)))
                if invoice_id is not None:
                    current_app.logger.info(
                        PAY_REFUND_MESSAGE.format(account_id, invoice_id))
                    try:
                        payment.cancel_payment(invoice_id)
                    except Exception as cancel_exception:  # noqa: B902; log exception
                        current_app.logger.error(
                            PAY_REFUND_ERROR.format(account_id, invoice_id,
                                                    repr(cancel_exception)))

                raise db_exception

            return query.json, HTTPStatus.CREATED

        except SBCPaymentException as pay_exception:
            return resource_utils.pay_exception_response(pay_exception)
        except BusinessException as exception:
            return resource_utils.business_exception_response(exception)
        except Exception as default_exception:  # noqa: B902; return nicer default error
            return resource_utils.default_exception_response(default_exception)