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
def test_registration_types(session, desc, reg_num): """Assert that a reg num searches on different registations returns the expected result.""" # setup json_data = {'type': 'REGISTRATION_NUMBER', 'criteria': {'value': reg_num}} query = SearchRequest.create_from_json(json_data, 'PS12345', 'UNIT_TEST') query.search() result = query.json # print(result) assert query.id assert query.search_response assert query.account_id == 'PS12345' assert query.user_id == 'UNIT_TEST' assert result['searchId'] assert result['searchQuery'] assert result['searchDateTime'] assert result['totalResultsSize'] == 1 assert result['maxResultsSize'] assert result['returnedResultsSize'] == 1 assert len(result['results']) == 1 assert result['results'][0]['baseRegistrationNumber'] assert result['results'][0]['createDateTime'] assert result['results'][0]['matchType'] == 'EXACT' assert result['results'][0]['registrationType']
def test_valid_callback_search_report(session, client, jwt): """Assert that a valid callback request returns a 200 status.""" # setup json_data = { 'type': 'SERIAL_NUMBER', 'criteria': { 'value': 'JU622994' }, 'clientReferenceId': 'UT-SS-1001' } search_query = SearchRequest.create_from_json(json_data, 'PS12345') search_query.search() query_json = search_query.json search_detail = SearchResult.create_from_search_query(search_query) search_detail.save() select_json = query_json['results'] search_detail.update_selection(select_json, 'UNIT TEST INC.', 'CALLBACK_URL') # test rv = client.post('/api/v1/search-results/callback/' + str(search_detail.search_id), headers=None) # check print(rv.json) assert rv.status_code == HTTPStatus.OK response = rv.json assert response['name'] assert response['selfLink'] GoogleStorageService.delete_document(response['name'])
def test_search_valid(session, search_type, json_data): """Assert that a valid search returns the expected search type result.""" query = SearchRequest.create_from_json(json_data, 'PS12345', 'UNIT_TEST') query.search() assert not query.updated_selection result = query.json # print(result) assert query.id assert query.search_response assert query.account_id == 'PS12345' assert query.user_id == 'UNIT_TEST' assert result['searchId'] assert result['searchQuery'] assert result['searchDateTime'] assert result['totalResultsSize'] assert result['maxResultsSize'] assert result['returnedResultsSize'] assert len(result['results']) >= 1 assert result['results'][0]['baseRegistrationNumber'] assert result['results'][0]['createDateTime'] assert result['results'][0]['matchType'] == 'EXACT' assert result['results'][0]['registrationType'] if search_type == 'BS': assert result['results'][0]['debtor'] assert result['results'][0]['debtor'][ 'businessName'] == 'TEST BUS 2 DEBTOR' elif search_type == 'IS': assert result['results'][0]['debtor'] assert result['results'][0]['debtor']['personName'] assert result['results'][0]['debtor']['personName']['last'] == 'DEBTOR' assert result['results'][0]['debtor']['personName'][ 'first'] == 'TEST IND' if result['results'][0]['baseRegistrationNumber'] == 'TEST0004': assert result['results'][0]['debtor']['birthDate'] elif search_type == 'AM': assert result['results'][0]['baseRegistrationNumber'] == 'TEST0001' assert result['results'][0]['registrationNumber'] == 'TEST0007' elif search_type == 'CH': assert result['results'][0]['baseRegistrationNumber'] == 'TEST0001' assert result['results'][0]['registrationNumber'] == 'TEST0008' elif search_type == 'RG': assert result['results'][0]['baseRegistrationNumber'] == 'TEST0001' else: assert result['results'][0]['vehicleCollateral'] assert result['results'][0]['vehicleCollateral']['year'] assert result['results'][0]['vehicleCollateral']['make'] assert result['results'][0]['vehicleCollateral']['serialNumber'] if search_type != 'MH': assert result['results'][0]['vehicleCollateral']['model'] if search_type == 'AF': assert result['results'][0]['vehicleCollateral']['type'] == 'AF' assert result['results'][0]['vehicleCollateral'][ 'serialNumber'] == 'AF16031' elif search_type == 'AC': assert result['results'][0]['vehicleCollateral']['type'] == 'AC' assert result['results'][0]['vehicleCollateral'][ 'serialNumber'] == 'CFYXW' elif search_type == 'MH': assert result['results'][0]['vehicleCollateral'][ 'manufacturedHomeRegistrationNumber'] == '220000'
def test_get_search_report(session): """Assert that config to get a google storage token works as expected.""" # setup json_data = { 'type': 'SERIAL_NUMBER', 'criteria': { 'value': 'JU622994' }, 'clientReferenceId': 'UT-SS-1001' } search_query = SearchRequest.create_from_json(json_data, 'PS12345') search_query.search() query_json = search_query.json search_detail = SearchResult.create_from_search_query(search_query) search_detail.save() select_json = query_json['results'] search_detail.update_selection(select_json, 'UNIT TEST INC.') # test raw_data, status_code, headers = get_search_report( str(search_detail.search_id)) print(status_code) # check assert raw_data assert status_code assert headers assert len(raw_data) > 0 with open(TEST_SEARCH_REPORT_FILE, "wb") as pdf_file: pdf_file.write(raw_data) pdf_file.close()
def test_search_no_results(session, search_type, json_data): """Assert that a search query with no results returns the expected result.""" query = SearchRequest.create_from_json(json_data, None) query.search() assert query.id assert not query.search_response assert query.returned_results_size == 0
def test_get_payment_details(session, client, jwt): """Assert that a valid search request payment details setup works as expected.""" # setup json_data = copy.deepcopy(SERIAL_NUMBER_JSON) query = SearchRequest.create_from_json(json_data) # test details = get_payment_details(query, json_data['type']) # check assert details assert details['label'] == 'Serial/VIN Number:' assert details['value'] == 'JU622994'
def test_find_by_account_id(session): """Assert that the account search history list first item contains all expected elements.""" history = SearchRequest.find_all_by_account_id('PS12345') # print(history) assert history assert history[0]['searchId'] assert history[0]['searchDateTime'] assert history[0]['totalResultsSize'] assert history[0]['returnedResultsSize'] assert history[0]['exactResultsSize'] assert history[0]['selectedResultsSize'] assert history[0]['searchQuery'] assert len(history) >= 1
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)
def staff_search(req: request, request_json, account_id: str): """Execute a staff search with special payment validation and methods.""" payment_info = build_staff_payment(req, account_id) # bcol help is no fee; reg staff can be no fee. # FAS is routing slip only. # BCOL is dat number (optional) and BCOL account number (mandatory). # All staff roles including SBC can submit no fee searches. if ROUTING_SLIP_PARAM in payment_info and BCOL_NUMBER_PARAM in payment_info: return resource_utils.staff_payment_bcol_fas() if CERTIFIED_PARAM in payment_info: request_json['certified'] = True query: SearchRequest = SearchRequest.create_from_json( request_json, account_id, g.jwt_oidc_token_info.get('username', None)) # Always create a payment transaction. invoice_id = None payment = Payment(jwt=jwt.get_token_auth_header(), account_id=account_id, details=get_payment_details(query, request_json['type'])) # staff payment pay_ref = payment.create_payment_staff_search(payment_info, 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
def test_search_no_account(session): """Assert that a search query with no account id returns the expected result.""" json_data = { 'type': 'REGISTRATION_NUMBER', 'criteria': { 'value': 'TEST0001' }, 'clientReferenceId': 'T-SQ-RG-4' } query = SearchRequest.create_from_json(json_data, None) query.search() assert query.id assert query.search_response
def test_search_autosave(session): """Assert that a valid search query selection update works as expected.""" query = SearchRequest.find_by_id(200000000) assert query.search_response update_data = copy.deepcopy( query.search_response) # json.loads(query.search_response) if update_data[0]['matchType'] == 'EXACT': update_data[0]['matchType'] = 'SIMILAR' else: update_data[0]['matchType'] = 'EXACT' assert not query.updated_selection query.update_search_selection(update_data) assert query.updated_selection json_data = query.json assert json_data['results'][0]['matchType'] == update_data[0]['matchType']
def test_search_discharged(session, search_type, json_data, excluded_match): """Assert that a discharged financing statement is excluded from the search results.""" query = SearchRequest.create_from_json(json_data, None) query.search() result = query.json assert result['searchId'] if search_type == 'RG': assert not query.search_response assert query.returned_results_size == 0 elif 'results' in result: for r in result['results']: if search_type == 'BS': assert r['debtor']['businessName'] != excluded_match elif search_type == 'IS': assert r['debtor']['personName']['first'] != excluded_match else: assert r['vehicleCollateral']['serialNumber'] != excluded_match
def test_create_from_json(session): """Assert that the search_client creates from a json format correctly.""" json_data = { 'type': 'SERIAL_NUMBER', 'criteria': { 'value': 'JU622994' }, 'clientReferenceId': 'T-SQ-SS-1' } search_client = SearchRequest.create_from_json(json_data, 'PS12345', 'USERID') assert search_client.account_id == 'PS12345' assert search_client.search_type == 'SS' assert search_client.client_reference_id == 'T-SQ-SS-1' assert search_client.search_ts assert search_client.search_criteria assert search_client.user_id == 'USERID'
def test_debtor_middle_name(session): """Assert that a individual debtor name searches with a middle name returns the expected result.""" # setup json_data = { 'type': 'INDIVIDUAL_DEBTOR', 'criteria': { 'debtorName': { 'last': 'Debtor', 'second': '4', 'first': 'Test Ind' } } } query = SearchRequest.create_from_json(json_data, 'PS12345', 'UNIT_TEST') query.search() result = query.json print(result) assert query.id assert query.search_response assert query.account_id == 'PS12345' assert query.user_id == 'UNIT_TEST' assert result['searchId'] assert result['searchQuery'] assert result['searchDateTime'] assert result['totalResultsSize'] >= 1 assert result['maxResultsSize'] assert result['returnedResultsSize'] >= 1 assert len(result['results']) >= 1 for match in result['results']: if 'middle' in match['debtor']['personName'] and match['debtor'][ 'personName']['middle'] == '4': assert match['matchType'] == 'EXACT' else: assert match['matchType'] == 'SIMILAR' assert 'middle' not in match['debtor'][ 'personName'] or match['debtor']['personName']['middle'] != 'None'
def get(): """Get account search history.""" try: # Quick check: must have an account ID. account_id = resource_utils.get_account_id(request) if 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) # Try to fetch search history by account id. # No results throws a not found business exception. current_app.logger.info( f'Fetching search history for {account_id}.') history = SearchRequest.find_all_by_account_id(account_id) return jsonify(history), HTTPStatus.OK 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)
def put(search_id): """Execute a search selection update request replacing the current value with the request body contents.""" try: if search_id is None: return resource_utils.path_param_error_response('search ID') # Quick check: must be staff or provide an account ID. account_id = resource_utils.get_account_id(request) if 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 schema. valid_format, errors = schema_utils.validate( request_json, 'searchSummary', 'ppr') if not valid_format: return resource_utils.validation_error_response( errors, VAL_ERROR) search_request = SearchRequest.find_by_id(search_id) if not search_request: return resource_utils.not_found_error_response( 'searchId', search_id) # Save the updated search selection. search_request.update_search_selection(request_json) return jsonify( search_request.updated_selection), HTTPStatus.ACCEPTED 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)
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)
def test_find_by_account_id_no_result(session): """Assert that the find search history by invalid account ID returns the expected result.""" history = SearchRequest.find_all_by_account_id('XXXX345') # check assert len(history) == 0
} } if len(name) > 2: criteria['debtorName']['second'] = name[2] elif search_type == SearchRequest.SearchTypes.BUSINESS_DEBTOR.value: criteria = { 'debtorName': { 'business': legacy_search['criteria'] } } request_json = { 'criteria': criteria, 'type': TO_API_SEARCH_TYPE[search_type] } query = SearchRequest.create_from_json( request_json, '0', 'search-tester') # run search on api fn start = datetime.utcnow() query.search() end = datetime.utcnow() interval = end - start search.run_time = interval.total_seconds() # save results results = query.search_response or [] add_api_results(search, results) # add search to batch batch.searches.append(search) # save batch to db batch.save() completed += 1
def test_get_total_count(session, search_type, json_data, result_size): """Assert that the get total count function works as expected.""" search_client = SearchRequest.create_from_json(json_data, 'PS12345') search_client.get_total_count() # print('test_total_count ' + search_type + ' actual results size=' + str(search_client.total_results_size)) assert search_client.total_results_size >= result_size