"""Integration tests suite.""" from datetime import datetime from abacus_common_logic.connectors.database import db from abacus_common_logic.utils.dates import safe_format_datetime import pytest import requests from tests.integration.conftest import ows_abacus_account_api_client, QA_BASE_URL from tests.integration.consts import api @pytest.mark.jira('ACC-3715') def test_get_hello(): """Get Hello Endpoint.""" response = requests.get(api.HEALTH_CHECK) assert response.status_code == 200, 'Reason: {}\nURL: {}'.format( response.reason, response.url ) response_body = response.json() assert 'status' in response_body assert 'ok' in response_body['status'] @pytest.mark.jira('ACC-3717', 'ACC-3700') def test_post_get_account(basic_headers, clean_db): """POST GET /account, GET /accounts .""" account_id = 123455 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account = { 'account_name': 'Test Account', 'account_id': account_id, } response_post = ows_account_client.post_account(post_account) assert response_post.status_code == 201 response_get = ows_account_client.get_account(account_id) assert response_get.status_code == 200 assert response_get.json()['account_name'] == post_account['account_name'] assert response_get.json()['account_id'] == post_account['account_id'] assert 'account_payment_term_id' in response_get.json() assert 'account_payee_id' in response_get.json() response_get = ows_account_client.get_accounts() assert response_get.status_code == 200 assert response_get.json()['items'][0]['account_id'] == account_id assert ( response_get.json()['items'][0]['account_name'] == post_account['account_name'] ) @pytest.mark.jira('ACC-3943') def test_get_eligible_accounts( clean_db, basic_headers, payment_eligibility_fixtures_integrational ): """GET /eligible-accounts .""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account = { 'account_name': 'Test Account', 'account_id': account_id, } response_post = ows_account_client.post_account(post_account) assert response_post.status_code == 201 # defined in payment_eligibility_fixtures_integrational fixture payment_group_id = 123 response_get = ows_account_client.get_eligible_accounts( {'payment_group_id': payment_group_id} ) assert response_get.status_code == 200 assert len(response_get.json()) > 0 assert response_get.json()[0]['account_id'] == 123456 @pytest.mark.jira('ACC-4086') @pytest.mark.parametrize('hold_status', [True, False]) def test_post_get_payment_hold(clean_db, basic_headers, hold_status): """POST and GET /payment_hold endpoint.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account = { 'account_name': 'Test Account', 'account_id': account_id, } response_post_account = ows_account_client.post_account(post_account) assert response_post_account.status_code == 201 from datetime import datetime today = datetime.utcnow().date() post_hold_body = { 'is_on_hold': hold_status, 'reason': 'orcd_payment_hold_testing_reason', 'start_date': str(today), } response_post_payment_hold = ows_account_client.post_payment_hold( account_id, post_hold_body ) assert response_post_payment_hold.status_code == 201, ( response_post_payment_hold.__dict__ ) response_get_payment_hold = ows_account_client.get_payment_hold(account_id) assert response_get_payment_hold.status_code == 200 response_body = response_get_payment_hold.json() assert response_body['is_on_hold'] == post_hold_body['is_on_hold'] assert response_body['reason'] == post_hold_body['reason'] assert response_body['start_date'] == post_hold_body['start_date'] assert response_body['account_id'] == account_id assert 'payment_hold_id' in response_body def test_post_payment_term(basic_headers, clean_db): """POST /account-payment-term endpoint.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account = { 'account_name': 'Test Account', 'account_id': account_id, } response_post_account = ows_account_client.post_account(post_account) post_payment_term_body = { 'account_id': account_id, 'currency_code': 'USD', 'payment_entity_id': 2, 'payment_schedule': '30_days_after_month_end', 'payment_minimum': '555.00', } assert response_post_account.status_code == 201 response_post_payment_term = ows_account_client.post_account_payment_term( post_payment_term_body ) assert response_post_payment_term.status_code == 201 response_get = ows_account_client.get_payment_payment_eligibility_status(account_id) assert response_get.status_code == 200 assert response_get.json()['eligibility_status'] == 'active' def test_get_payment_eligibility_status_by_account_id(auth_headers, clean_db): """GET /payment_eligibility_status/{account_id} endpoint.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(auth_headers) payload_data = { 'account_id': account_id, 'account_name': 'Test Account', 'created_by': 'default_user', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) response_get = ows_account_client.get_payment_payment_eligibility_status(account_id) assert response_get.status_code == 200 assert response_get.json()['eligibility_status'] == 'active' def test_get_payment_eligibility_status_unauthorized(clean_db, unauthorized_headers): """GET /account//payment-eligibility-status unauthorized headers.""" account_id = 123456 response_get = requests.get( f'{QA_BASE_URL}/account/{account_id}/payment-eligibility-status', headers=unauthorized_headers, ) assert response_get.status_code == 403 expected = {'code': 'authorization_error', 'message': 'Unauthorized'} assert response_get.json() == expected @pytest.mark.jira('ACC-4231') def test_post_get_account_payee(basic_headers, clean_db): """POST and GET for /account-payee endpoints.""" ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': 123456, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) post_body = { 'account_id': 123456, 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'Joe Payoneer', 'payoneer_iframe_url': 'payoneer.com/iframe', 'payoneer_iframe_url_date': '2022-02-01', 'payoneer_session_id': 'T-800', 'sap_vendor_id': None, } response_post = ows_account_client.post_account_payee(post_body) assert response_post.status_code == 201 account_payee_id = response_post.json()['account_payee_id'] response_get = ows_account_client.get_account_payee(account_payee_id) assert response_get.status_code == 200 post_body.update(account_payee_id=account_payee_id) post_body.update(payoneer_program_id=None) post_body.update(reference_payment_type_id=None) post_body.update(payment_description='') response_body = response_get.json() response_body.pop('last_modified') assert response_body == post_body @pytest.mark.jira('ACC-4231') def test_get_account_payee_by_account_id(basic_headers, clean_db): """GET for /account/{account_id}/account-payee.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': account_id, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) post_body = { 'account_id': account_id, 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'Joe Payoneer', 'payoneer_iframe_url': 'payoneer.com/iframe', 'payoneer_iframe_url_date': '2022-02-01', 'payoneer_session_id': 'T-800', 'sap_vendor_id': None, } response_post = ows_account_client.post_account_payee(post_body) assert response_post.status_code == 201 account_payee_id = response_post.json()['account_payee_id'] post_body.update(account_payee_id=account_payee_id) post_body.update(payoneer_program_id=None) post_body.update(reference_payment_type_id=None) post_body.update(payment_description='') response_get = ows_account_client.get_account_payee_by_account_id(account_id) assert response_get.status_code == 200 response_body = response_get.json() response_body.pop('last_modified') assert response_body == post_body @pytest.mark.jira('ACC-4231') def test_put_account_payee(basic_headers, clean_db): """PUT for /account-payee endpoints.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': account_id, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) post_body = { 'account_id': account_id, 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'Joe Payoneer', 'payoneer_iframe_url': 'payoneer.com/iframe', 'payoneer_iframe_url_date': '2022-02-01', 'payoneer_session_id': 'T-800', 'sap_vendor_id': None, } response_post = ows_account_client.post_account_payee(post_body) assert response_post.status_code == 201 account_payee_id = response_post.json()['account_payee_id'] response_get = ows_account_client.get_account_payee(account_payee_id) assert response_get.status_code == 200 post_body.update(account_payee_id=account_payee_id) post_body.update(payoneer_program_id=None) post_body.update(reference_payment_type_id=None) post_body.update(payment_description='') response_body = response_get.json() response_body.pop('last_modified') assert response_body == post_body put_body = { 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'NEW Joe Payoneer NEW', 'payoneer_iframe_url': 'payoneer.com/iframe', 'payoneer_iframe_url_date': '2022-02-01', 'payoneer_session_id': 'T-1000', 'sap_vendor_id': None, } response_put = ows_account_client.put_account_payee(account_payee_id, put_body) assert response_put.status_code == 200 put_body.update(account_payee_id=account_payee_id) put_body.update(account_id=account_id) put_body.update(payoneer_program_id=None) put_body.update(reference_payment_type_id=None) put_body.update(payment_description='') response_get = ows_account_client.get_account_payee(account_payee_id) assert response_get.status_code == 200 response_body = response_get.json() response_body.pop('last_modified') assert response_body == put_body @pytest.mark.jira('ACC-4468') def test_put_get_sap(basic_headers, clean_db): """GET & PUT /SAP endpoints.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': account_id, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) body = {'sap_created_at': safe_format_datetime(datetime.date(datetime.now()))} resp_put = ows_account_client.put_account(account_id, body) assert resp_put.status_code == 200 assert resp_put.json()['account_name'] == payload_data['account_name'] assert resp_put.json()['account_id'] == payload_data['account_id'] assert resp_put.json()['sap_created_at'] == body['sap_created_at'] resp_get = ows_account_client.get_sap_by_account_id(account_id) assert resp_get.status_code == 200 assert resp_get.json()['AcctName'] == payload_data['account_name'] assert int(resp_get.json()['AccountId']) == payload_data['account_id'] assert 'Kunnr' in resp_get.json() assert 'Zzfield1' in resp_get.json() assert 'Zzfield2' in resp_get.json() assert 'Lifnr' in resp_get.json() @pytest.mark.jira('ACC-3970') def test_get_account_tax_info_snapshot(basic_headers, clean_db): """POST /account-tax-info/snapshot .""" account_id = 11 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account = { 'account_name': 'Test account 11', 'account_id': account_id, 'country_of_tax_residence': 'USA', } response_post_account = ows_account_client.post_account(post_account) assert response_post_account.status_code == 201 data = ( 'account_tax_info_id\taccount_id\tcountry_of_tax_residence\t' 'is_sba_signed\tis_vat_exempt\tis_tax_treaty_claimed\ttax_employment_type\t' 'certificate_of_residence_expiration_date\tis_wht_applicable\t' 'is_resident_of_spanish_islands\twht_rate_override' '\n1\t11\tUSA\t0\t1\t0\t\t\t1\t\t\n' ) response_get = ows_account_client.get_account_tax_info_snapshot([]) assert response_get.status_code == 200 assert response_get.text == data @pytest.mark.jira('ACC-5680') def test_get_accounts_by_search_term(basic_headers, clean_db): """Get accounts by search term.""" account_id_1 = 11 account_id_2 = 12 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account_1 = { 'account_name': 'test search by term', 'account_id': account_id_1, } response_post_account = ows_account_client.post_account(post_account_1) assert response_post_account.status_code == 201 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account_2 = { 'account_name': 'abcdefg', 'account_id': account_id_2, } response_post_account = ows_account_client.post_account(post_account_2) assert response_post_account.status_code == 201 body = {'search_term': 'search'} response = ows_account_client.get_accounts_by_search_term(body) assert response.status_code == 200 assert len(response.json()['items']) == 1 assert response.json()['items'][0]['account_name'] == post_account_1['account_name'] body = {'search_term': account_id_1} response = ows_account_client.get_accounts_by_search_term(body) assert response.status_code == 200 assert len(response.json()['items']) == 1 assert response.json()['items'][0]['account_id'] == post_account_1['account_id'] @pytest.mark.jira('TAP-1421') def test_get_tax_info_by_accont_ids_bulk(basic_headers, clean_db): """GET /accounts/account-tax-info.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) post_account = { 'account_name': 'Test Account', 'account_id': account_id, 'country_of_tax_residence': 'USA', } response_post = ows_account_client.post_account(post_account) assert response_post.status_code == 201 res_get = ows_account_client.get_tax_info_by_account_ids([account_id]) assert res_get.status_code == 200 @pytest.mark.jira('TAP-2841') def test_post_account_payee_with_description(basic_headers, clean_db): """POST /account-payee with description field.""" account_id = 123456 ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': account_id, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) post_body = { 'account_id': account_id, 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'Joe Payoneer', 'payoneer_iframe_url': 'payoneer.com/iframe', 'payoneer_iframe_url_date': '2022-02-01', 'payoneer_session_id': 'T-800', 'sap_vendor_id': None, 'payment_description': 'Test description for integration', } response_post = ows_account_client.post_account_payee(post_body) assert response_post.status_code == 201 account_payee_id = response_post.json()['account_payee_id'] assert response_post.json()['payment_description'] == ( 'Test description for integration' ) response_get = ows_account_client.get_account_payee(account_payee_id) assert response_get.status_code == 200 assert response_get.json()['payment_description'] == ( 'Test description for integration' ) @pytest.mark.jira('TAP-2841') def test_post_account_payee_with_description_trimmed(basic_headers, clean_db): """POST /account-payee with description that should be trimmed.""" account_id = 123457 ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': account_id, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) post_body = { 'account_id': account_id, 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'Joe Payoneer', 'payment_description': ' Test description with spaces ', } response_post = ows_account_client.post_account_payee(post_body) assert response_post.status_code == 201 assert response_post.json()['payment_description'] == 'Test description with spaces' @pytest.mark.jira('TAP-2841') def test_put_account_payee_with_description(basic_headers, clean_db): """PUT /account-payee with description field.""" account_id = 123458 ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': account_id, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) post_body = { 'account_id': account_id, 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'Joe Payoneer', 'payment_description': 'Original description', } response_post = ows_account_client.post_account_payee(post_body) assert response_post.status_code == 201 account_payee_id = response_post.json()['account_payee_id'] put_body = {'payment_description': 'Updated description'} response_put = ows_account_client.put_account_payee(account_payee_id, put_body) assert response_put.status_code == 200 assert response_put.json()['payment_description'] == 'Updated description' response_get = ows_account_client.get_account_payee(account_payee_id) assert response_get.status_code == 200 assert response_get.json()['payment_description'] == 'Updated description' @pytest.mark.jira('TAP-2841') def test_put_account_payee_without_description_field(basic_headers, clean_db): """PUT /account-payee without description field should not change it.""" account_id = 123459 ows_account_client = ows_abacus_account_api_client(basic_headers) payload_data = { 'account_id': account_id, 'account_name': 'test', 'created_by': 'sax_ingestion', } query = """INSERT INTO account (account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES ({account_id}, '{account_name}', '{created_by}', '2022-03-29 01:38:06', 'default_user_id', '2022-03-29 01:38:11')""" db.engine.execute(query.format(**payload_data)) post_body = { 'account_id': account_id, 'payoneer_payee_id': 123123, 'payoneer_payee_name': 'Joe Payoneer', 'payment_description': 'Original description', } response_post = ows_account_client.post_account_payee(post_body) assert response_post.status_code == 201 account_payee_id = response_post.json()['account_payee_id'] put_body = {'payoneer_payee_name': 'Updated Joe Payoneer'} response_put = ows_account_client.put_account_payee(account_payee_id, put_body) assert response_put.status_code == 200 assert response_put.json()['payment_description'] == 'Original description'