def execute_complete_payment(client, payment, action): """ Complete a payment. :param client: :param payment :param action :return: """ headers = get_test_headers() # PATCH /api/v1/payments/<int:nr_id>/payment/<int:payment_id>/<string:payment_action> request_uri = API_BASE_URI + str(payment.get('nrId')) + '/payment/' + str( payment.get('id')) + '/' + action test_params = [{}] query = build_test_query(test_params) path = build_request_uri(request_uri, query) log_request_path(path) response = client.patch(path, data={}, headers=headers) assert response.status_code == 200 payload = json.loads(response.data) return payload
def patch_nr(client, action, nr_id, nr_data): try: request_uri = API_BASE_URI + str(nr_id) + '/' + action test_params = [{}] headers = get_test_headers() query = build_test_query(test_params) path = build_request_uri(request_uri, query) print('Patch (' + action + ') Name Request [' + str(nr_id) + ']: \n' + json.dumps( nr_data, sort_keys=True, indent=4, separators=(',', ': '))) log_request_path(path) patch_response = client.patch(path, data=json.dumps(nr_data), headers=headers) if not patch_response or patch_response.status_code != 200: # raise Exception('NR PATCH operation failed') pass return patch_response except Exception as err: print(repr(err)) raise
def execute_refund_payment(client, payment): """ Refund a payment. :param client: :param payment :return: """ headers = get_test_headers() request_uri = API_BASE_URI + \ str(payment.get('nrId')) + '/payment/' + str(payment.get('id')) + '/' + \ NameRequestPaymentActions.REQUEST_REFUND.value test_params = [{}] query = build_test_query(test_params) path = build_request_uri(request_uri, query) log_request_path(path) response = client.patch(path, data={}, headers=headers) assert response.status_code == 200 payload = json.loads(response.data) return payload
def execute_calculate_upgrade_fees(client): """ 1) Get the current the fees. :param client: :return: """ headers = get_test_headers() request_uri = API_BASE_URI + 'fees' # Test regular submission path = request_uri body = json.dumps({ 'corp_type': 'NRO', 'filing_type_code': 'NM606', 'jurisdiction': 'BC', 'date': '', 'priority': '' }) log_request_path(path) response = client.post(path, data=body, headers=headers) assert response.status_code == 200 payload = json.loads(response.data) verify_fees_payload(payload) assert payload.get('filingTypeCode') == 'NM606' return payload
def execute_create_payment(client, create_payment_request): """ Create a payment. Automatically creates an NR for use. :param client: :param create_payment_request: :return: """ headers = get_test_headers() draft_nr = setup_draft_nr(client) nr_id = draft_nr.get('id') payment_action = 'COMPLETE' # POST /api/v1/payments/<int:nr_id>/<string:payment_action> request_uri = API_BASE_URI + str(nr_id) + '/' + payment_action path = request_uri body = json.dumps(create_payment_request) log_request_path(path) response = client.post(path, data=body, headers=headers) assert response.status_code == 201 payload = json.loads(response.data) verify_payment_payload(payload) assert payload.get('statusCode') == 'CREATED' return payload
def test_cancel_payment(client): """ Assert that a payment record gets cancelled correctly. """ try: payment = test_create_payment(client) with patch.object(SBCPaymentClient, 'cancel_payment', return_value={}): headers = get_test_headers() request_uri = API_BASE_URI + str(payment.get('nrId')) + '/payment/' + str(payment.get('id')) + '/' + \ NameRequestPaymentActions.CANCEL.value test_params = [{}] query = build_test_query(test_params) path = build_request_uri(request_uri, query) log_request_path(path) response = client.patch(path, data={}, headers=headers) assert response.status_code == 200 # Get namex payment and ensure that it is in a cancelled state payments = execute_get_payments(client, payment['nrId']) assert payments and isinstance(payments, list) and len(payments) == 1 assert payments[0]['statusCode'] == State.CANCELLED except Exception as err: print(repr(err)) raise err
def test_temp_nr(client, test_name, request_action_cd, entity_type_cd): """ Test temp NRs """ draft_input_fields['request_action_cd'] = request_action_cd draft_input_fields['entity_type_cd'] = entity_type_cd add_test_user_to_db() path = build_request_uri(API_BASE_URI, '') headers = get_test_headers() post_response = client.post(path, data=json.dumps(draft_input_fields), headers=headers) draft_nr = json.loads(post_response.data) assert draft_nr['id'] > 0 assert draft_nr['nrNum'].startswith('NR L') assert draft_nr['request_action_cd'] == request_action_cd assert draft_nr['entity_type_cd'] == entity_type_cd assert draft_nr['applicants']['firstName'] == 'John'
def get_nr(client, nr_id): try: request_uri = API_BASE_URI + str(nr_id) test_params = [{}] headers = get_test_headers() query = build_test_query(test_params) path = build_request_uri(request_uri, query) print('Get Name Request [' + str(nr_id) + ']') log_request_path(path) get_response = client.get(path, headers=headers) if not get_response or get_response.status_code != 200: # raise Exception('NR PATCH operation failed') pass return get_response except Exception as err: print(repr(err)) raise
def post_test_nr_json(client, nr_data=None): """ Create a temp NR with nr data passed. """ try: add_states_to_db(state_data) add_test_user_to_db() request_uri = API_BASE_URI path = build_request_uri(request_uri, '') headers = get_test_headers() post_response = client.post(path, data=json.dumps(nr_data), headers=headers) if not post_response or post_response.status_code != 201: raise Exception( 'Temp NR POST operation failed, cannot continue with test') return post_response except Exception as err: print(repr(err))
def execute_cancel_and_refund_all_payments(client, nr_id): """ Cancel NR and request refund for all NR payments. :param client :param nr_id :return: """ headers = get_test_headers() request_uri = API_BASE_NAMEREQUEST_URI + str(nr_id) + '/' + NameRequestPatchActions.REQUEST_REFUND.value test_params = [{}] query = build_test_query(test_params) path = build_request_uri(request_uri, query) log_request_path(path) response = client.patch(path, json={}, headers=headers) assert response.status_code == 200 payload = json.loads(response.data) return payload
def post_test_nr(client, nr_data=None, nr_state=State.DRAFT): """ Create a draft NR, using the API, to use as the initial state for each test. :param client: :param nr_data: :param nr_state: :return: """ try: # Configure auth # token, headers = setup_test_token(jwt, claims, token_header) headers = get_test_headers() # Set up our test data add_states_to_db(state_data) add_test_user_to_db() # Optionally supply the field data custom_names = nr_data.get('names', None) if not custom_names: custom_names = [{ 'name': 'BLUE HERON TOURS LTD.', 'choice': 1, 'designation': 'LTD.', 'name_type_cd': 'CO', 'consent_words': '', 'conflict1': 'BLUE HERON TOURS LTD.', 'conflict1_num': '0515211' }] nr = build_nr(nr_state, nr_data, custom_names, False) nr_data = nr.json() nr_data['applicants'] = [{ 'addrLine1': '1796 KINGS RD', 'addrLine2': '', 'addrLine3': '', 'city': 'VICTORIA', 'clientFirstName': '', 'clientLastName': '', 'contact': '', 'countryTypeCd': 'CA', # 'declineNotificationInd': None, 'emailAddress': '*****@*****.**', 'faxNumber': '', 'firstName': 'BOB', 'lastName': 'JOHNSON', 'middleName': '', # 'partyId': None, 'phoneNumber': '2505320083', 'postalCd': 'V8R 2P1', 'stateProvinceCd': 'BC' }] # Create a new DRAFT NR using the NR we just created request_uri = API_BASE_URI test_params = [{}] query = build_test_query(test_params) path = build_request_uri(request_uri, query) log_request_path(path) post_response = client.post(path, data=json.dumps(nr_data), headers=headers) if not post_response or post_response.status_code != 201: raise Exception( 'NR POST operation failed, cannot continue with PATCH test') return post_response except Exception as err: print(repr(err))
def test_create_payment(client, jwt, test_name, action, complete_payment, do_refund, cancel_payment, request_receipt): payment = execute_payment(client, jwt, create_payment_request, action) assert payment['action'] == action if complete_payment: test_payment_fees(client) payment_id = payment['id'] nr_id = payment['nrId'] # Fire off the request to complete the payment, just to test that the endpoint is there and runs, # we will not actually be able to complete the payment without a browser (at this time anyway) execute_complete_payment(client, payment, NameRequestPaymentActions.COMPLETE.value) # Manually update the Payment, setting the stateCd to COMPLETE payment_model = Payment.query.get(payment_id) payment_model.payment_status_code = PaymentState.COMPLETED.value payment_model.save_to_db() # Get the 'completed' payment completed_payment = execute_get_payment(client, nr_id, payment_id) assert payment_id == completed_payment[0]['id'] assert completed_payment[0][ 'statusCode'] == PaymentState.COMPLETED.value if do_refund: with patch.object(SBCPaymentClient, 'refund_payment', return_value={}): execute_refund_payment(client, payment) # Get any payments and make sure they payments = execute_get_payments(client, completed_payment[0]['nrId']) assert payments and isinstance(payments, list) and len(payments) == 1 assert payments[0]['statusCode'] == State.REFUND_REQUESTED if cancel_payment: with patch.object(SBCPaymentClient, 'cancel_payment', return_value={}): headers = get_test_headers() request_uri = API_BASE_URI + str(payment.get('nrId')) + '/payment/' + str(payment.get('id')) + '/' + \ NameRequestPaymentActions.CANCEL.value test_params = [{}] query = build_test_query(test_params) path = build_request_uri(request_uri, query) log_request_path(path) response = client.patch(path, data={}, headers=headers) assert response.status_code == 200 # Get namex payment and ensure that it is in a cancelled state payments = execute_get_payments(client, payment['nrId']) assert payments and isinstance(payments, list) and len(payments) == 1 assert payments[0]['statusCode'] == State.CANCELLED if request_receipt: with patch.object(SBCPaymentClient, 'get_receipt', return_value=mock_receipt_response): request_uri = API_BASE_URI + str(payment.get('id')) + '/receipt' query = build_test_query([{}]) path = build_request_uri(request_uri, query) log_request_path(path) response = client.get(path) assert response.status_code == 200