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: SearchClient.validate_query(json_data) # check assert bad_request_err assert bad_request_err.value.status_code == HTTPStatus.BAD_REQUEST
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: SearchClient.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)
def post(): """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 is_staff(jwt) and account_id is None: 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 SearchClient.validate_query(request_json) query = SearchClient.create_from_json(request_json, account_id) # Charge a search fee. if account_id: payment = Payment(jwt=jwt.get_token_auth_header(), account_id=account_id) 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() # if not query.search_response or query.returned_results_size == 0: # return resource_utils.unprocessable_error_response('search query') # 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( f'Search {account_id} db save failed: ' + repr(db_exception)) current_app.logger.error( f'Search {account_id} rolling back payment for invoice {invoice_id}.' ) try: payment.cancel_payment(invoice_id) except Exception as cancel_exception: # noqa: B902; log exception current_app.logger.error( f'Search {account_id} payment refund failed for invoice {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)