"""Integration test configuration.""" import os from abacus_common_logic.connectors.database import db from jwtauth.testing import ( get_bearer_token_identity_uuid, JwtAuthSecretsManager, SecretLookupInfo, ) import pytest from abacus_account.api import create_app from abacus_account.config import Config from tests.integration.utils import auth from tests.integration.utils.ows_abacus_account_api_client import \ AbacusAccountAPIClient pytest_plugins = ['jwtauth.testing.pytest_plugin'] QA_BASE_URL = os.environ.get( 'QA_BASE_URL', 'http://localhost:6452') class TestConfig(Config): """Test configuration.""" MYSQL_DB_NAME = os.environ.get('MYSQL_DB_NAME', Config.MYSQL_DB_NAME) MYSQL_DB_USER = os.environ.get('MYSQL_DB_USER', 'royalties') MYSQL_DB_HOST = os.environ.get('MYSQL_DB_HOST', 'mysql-royalties-container') MYSQL_DB_PORT = os.environ.get('MYSQL_DB_PORT', '3306') MYSQL_DB_PASS = os.environ.get('MYSQL_DB_PASS', '1234') @pytest.fixture(scope='session', autouse=True) def test_app(): """Create a test application.""" return create_app(TestConfig) @pytest.fixture(autouse=True) def test_app_in_context(test_app): """Push the test app onto the context.""" with test_app.app_context(): yield test_app @pytest.fixture(scope='session') def basic_headers(): """Return basic headers.""" profile_id = '35109' # profile_id for Joe User, as set in QA cypher refresh script return { 'Content-Type': 'application/json', 'Orchard-Identity-Id': 'd5ca8ac3-7e51-4793-8775-50d11282504c', 'Orchard-Profile-Id': profile_id, 'Orchard-Profile-Type': 'AbacusProfile', 'Orchard-Roles': 'administrator', 'Orchard-Requestor-Service': 'graphql-abacus' } @pytest.fixture(params=['read_only', 'admin']) def auth_headers(request, bearer_token_account_readonly_authorized, basic_headers): """Return appropriate headers based on the parameter.""" if request.param == 'read_only': return { 'Authorization': f'Bearer {bearer_token_account_readonly_authorized}', 'Orchard-Requestor-Service': 'graphql-abacus' } elif request.param == 'admin': return basic_headers @pytest.fixture() def unauthorized_headers(bearer_token_account_readonly_unauthorized): """Return headers for unauthorized user.""" return { 'Authorization': f'Bearer {bearer_token_account_readonly_unauthorized}', 'Orchard-Requestor-Service': 'graphql-abacus' } def ows_abacus_account_api_client(headers): """Create ows-abacus-account client.""" return AbacusAccountAPIClient(QA_BASE_URL, headers) @pytest.fixture def clean_db(): """Clean tables between runs.""" tables = [ 'account', 'account_payee', 'account_payee_history', 'account_payment_term', 'account_payment_term_history', 'account_payment_term_template', 'account_tax_info', 'account_tax_info_history', 'ledger_account_contract', 'ledger_account_contract_current_balance', 'payment_group', 'payment_hold', 'payment_hold_history', 'payment_minimum', 'payment_group_payment', 'payment_group_payment_account', 'abacus_state', 'reference_payoneer_program', 'payment_entity_payoneer_program', 'statement_period', ] con = db.engine.connect() con.execute('SET FOREIGN_KEY_CHECKS = 0;') trans = con.begin() for table in tables: con.execute(f'TRUNCATE TABLE `{table}`') trans.commit() con.execute('SET FOREIGN_KEY_CHECKS = 1;') @pytest.fixture def payment_eligibility_fixtures_integrational(): """Create required fixtures for /eligible-accounts endpoint.""" statement_period_insert = """ INSERT INTO statement_period( statement_period_id, statement_period_name, statement_period_status, closed_date, closed_by ) VALUES (1, 'Month 1', 'closed', '2022-01-01', 'default_user_id'), (2, 'Month 2', 'current', NULL, NULL), (3, 'Month 3', 'open', NULL, NULL); """ db.engine.execute(statement_period_insert) account_payee_insert = """ INSERT INTO account_payee( account_id, payoneer_program_id, payoneer_payee_id, created_by, created_at, last_modified_by, last_modified ) VALUES (123456, 1, 1, 'user_id', NOW(), 'user_id', NOW()); """ db.engine.execute(account_payee_insert) abacus_state_insert = """ INSERT INTO abacus_state( parent_table_id, parent_table_name, action_name, action_status, message, created_by, created_at, last_modified_by, last_modified) VALUES (1, 'payment_group_payment_account', 'send_payments', 'complete', null, 'vz', '2022-04-08 04:25:08', 'vz', '2022-04-08 04:25:13') """ db.engine.execute(abacus_state_insert) abacus_state_insert = """ INSERT INTO abacus_state( parent_table_id, parent_table_name, action_name, action_status, message, created_by, created_at, last_modified_by, last_modified) VALUES (1, 'account_payee', 'payment_eligibility', 'approved', null, 'vz', '2022-04-08 04:25:08', 'vz', '2022-04-08 04:25:13') """ db.engine.execute(abacus_state_insert) abacus_state_insert = """ INSERT INTO abacus_state( parent_table_id, parent_table_name, action_name, action_status, message, created_by, created_at, last_modified_by, last_modified) VALUES (1, 'account_payee', 'tax_eligibility', 'complete', null, 'vz', '2022-04-08 04:25:08', 'vz', '2022-04-08 04:25:13') """ db.engine.execute(abacus_state_insert) payment_group_payment_account_insert = """ INSERT INTO payment_group_payment_account( payment_group_payment_account_id, payment_group_payment_id, account_id, payoneer_program_id, last_statement_period_id, current_statement_period_id, currency_code, last_payment, current_balance, tax_withholding, balance_after_tax, created_at, created_by, last_modified, last_modified_by ) VALUES ( 1, 1, 123456, 1, 1, 2, 'USD', 1000.00, 2000.00, null, 900.00, '2022-04-08 04:22:57', 'vz', '2022-04-08 04:23:01', 'vz' ); """ db.engine.execute(payment_group_payment_account_insert) payment_group_payment_insert = """ INSERT INTO payment_group_payment( payment_group_payment_id, payment_group_id, statement_period_id, payment_name, created_at, created_by, last_modified, last_modified_by ) VALUES ( 1, 123, 2, 'test', '2022-04-08 04:22:17', 'vz', '2022-04-08 04:22:20', 'vz' ); """ db.engine.execute(payment_group_payment_insert) payment_hold_insert = """ INSERT INTO payment_hold( payment_hold_id, account_id, is_on_hold, start_date, reason, created_by, created_at, last_modified_by, last_modified) VALUES (1, 123456, 0, NOW(), 'test', 'vz', '2022-04-08 04:48:59', 'vz', '2022-04-08 04:49:01') """ db.engine.execute(payment_hold_insert) account_tax_info_insert = """ INSERT INTO account_tax_info( account_id, country_of_tax_residence, is_sba_signed, is_vat_exempt, created_by, created_at, last_modified_by, last_modified ) VALUES (123456, 'USA', 0, 1, 'user_id', NOW(), 'user_id', NOW()); """ db.engine.execute(account_tax_info_insert) account_payment_term_insert = """ INSERT INTO account_payment_term( account_id, currency_code, payment_entity_id, payment_minimum, payment_schedule, created_by, created_at, last_modified_by, last_modified ) VALUES ( 123456, 'USD', 2, '35.00', '30_days_after_month_end', 'user_id', NOW(), 'user_id', NOW() ); """ db.engine.execute(account_payment_term_insert) ledger_account_contract_insert = """ INSERT INTO ledger_account_contract ( abacus_event_id, account_id, contract_id, currency_code, currency_amount, previous_balance, current_balance, created_by, created_at, last_modified_by, last_modified ) VALUES ( 123, 123456, 111, 'USD', 5000.00, 0.00, 5000.00, 'user_id', NOW(), 'user_id', NOW() ); """ db.engine.execute(ledger_account_contract_insert) payment_group_insert = """ INSERT INTO payment_group( payment_group_id, group_name, group_criteria, is_reusable, created_at, created_by, last_modified, last_modified_by ) VALUES ( 123, 'Group', '{"currency_codes":["USD"],"reference_payment_entities":[2],"payment_schedules":["30_days_after_month_end"]}', 0, NOW(), 'user_id', NOW(), 'user_id' ); """ db.engine.execute(payment_group_insert) payment_minimum_insert = """ INSERT INTO payment_minimum( currency_code, check_amount, wire_transfer_amount, western_union_amount, created_by, created_at, last_modified_by, last_modified ) VALUES ('USD', 30.00, 30.00, 30.00, 'user_id', NOW(), 'user_id', NOW()); """ db.engine.execute(payment_minimum_insert) @pytest.fixture def insert_payoneer_programs() -> None: """Insert reference_payoneer_program into the database.""" query = """ INSERT IGNORE INTO `reference_payoneer_program` ( `payoneer_program_id`, `payoneer_program_name`, `funding_currency`, `reference_payment_type_id` ) VALUES ('0', 'SAP Vendor Payments', 'TBD', '1'), ('100176610', 'Orchard UK - GBP', 'GBP', '1'), ('100176630', 'Orchard US - USD', 'USD', '1'), ('100176650', 'Orchard US - USD MC', 'USD', '1'), ('100176770', 'AWAL Core - EUR', 'EUR', '2'), ('100176830', 'AWAL Core - GBP', 'GBP', '2'), ('100176850', 'Orchard NO - NOK', 'NOK', '2'), ('100176870', 'Orchard EU - EUR', 'EUR', '2'), ('100176890', 'Orchard UK - EUR', 'EUR', '2'), ('100176910', 'Orchard NO - NOK MC', 'NOK', '2'), ('100176920', 'Orchard EU - EUR MC', 'EUR', '2'), ('100176930', 'AWAL Core - USD', 'USD', '3'), ('100176950', 'Orchard UK - USD MC', 'USD', '3'), ('100176970', 'Orchard UK - USD', 'USD', '3'), ('100176990', 'AWAL Core - USD MC', 'USD', '3'), ('100184360', 'AWAL US USD', 'USD', '4'), ('100184450', 'AWAL UK EUR', 'EUR', '4'), ('100184470', 'AWAL UK GBP', 'GBP', '4'), ('100184490', 'AWAL UK USD', 'USD', '4'), ('100184600', 'AWAL UK USD MC', 'USD', '5'), ('100184620', 'AWAL US USD MC', 'USD', '6'); """ db.engine.execute(query) @pytest.fixture def insert_payoneer_program() -> None: """Insert reference_payoneer_program into the database.""" query = """ INSERT IGNORE INTO `reference_payoneer_program` ( `payoneer_program_id`, `payoneer_program_name`, `funding_currency`, `reference_payment_type_id` ) VALUES ('1', 'test', 'USD', '5'); """ db.engine.execute(query) @pytest.fixture def insert_payees(insert_payoneer_programs) -> None: """Insert account_payees into the database.""" query = """ INSERT IGNORE INTO `account_payee` ( `account_payee_id`, `account_id`, `payoneer_program_id`, `payoneer_payee_id`, `payoneer_payee_name`, `payoneer_iframe_url`, `payoneer_iframe_url_date`, `payoneer_session_id`, `sap_vendor_id`, `reference_payment_type_id`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES ('75320', '83418', '100176770', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '0a22b48b-765f-4583-9d71-6ad5cc9c60a9', '2024-01-22 12:14:38', '0888a766-2ac6-4944-ae94-90f306126ec8', '2024-03-11 21:26:20'), ('30895', '66289', '100176930', '65399290', '559b679c1f4609700891e8679f6e818d', NULL, NULL, NULL, NULL, NULL, 'f0730ba4-e995-451f-8fba-a883d53660a2', '2022-09-07 20:07:04', '0888a766-2ac6-4944-ae94-90f306126ec8', '2024-03-11 21:26:20'), ('9841', '45233', '100176930', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'f0730ba4-e995-451f-8fba-a883d53660a2', '2022-09-07 20:07:04', '0888a766-2ac6-4944-ae94-90f306126ec8', '2024-03-11 21:26:20'); """ db.engine.execute(query) @pytest.fixture def insert_accounts(insert_payees) -> None: """Insert accounts into the database.""" query = """ INSERT IGNORE INTO `account` ( `account_id`, `account_name`, `sap_created_at`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES ('83418', '!nertia', '2024-01-22 12:14:40', '', '2024-01-22 12:14:38', 'default_user_id', '2024-01-22 12:14:40'), ('66289', '#ERROR!', '2022-12-07 18:41:35', 'sax_ingestion', '2022-09-07 15:27:58', 'default_user_id', '2022-12-07 18:41:35'), ('45233', '#NAME?', '2022-12-05 18:26:42', 'sax_ingestion', '2022-09-07 15:27:58', 'default_user_id', '2022-12-05 18:26:42'); """ db.engine.execute(query) @pytest.fixture def insert_account_payment_terms(insert_accounts) -> None: """Insert account_payment_term into the database.""" query = """ INSERT IGNORE INTO `account_payment_term` ( `account_payment_term_id`, `account_id`, `currency_code`, `payment_minimum`, `payment_entity_id`, `agreement_type_id`, `payment_schedule`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES ('108137', '83418', 'EUR', '25.00', '1', '1', '45_days_after_month_end', '0a22b48b-765f-4583-9d71-6ad5cc9c60a9', '2024-01-22 12:14:38', '0a22b48b-765f-4583-9d71-6ad5cc9c60a9', '2024-01-22 12:14:38'), ('30895', '66289', 'USD', '0.00', '1', NULL, '45_days_after_month_end', 'f0730ba4-e995-451f-8fba-a883d53660a2', '2022-09-07 20:07:18', 'sax_ingestion', '2022-09-30 08:05:30'), ('9841', '45233', 'USD', '0.00', '1', NULL, '45_days_after_month_end', 'f0730ba4-e995-451f-8fba-a883d53660a2', '2022-09-07 20:07:18', 'sax_ingestion', '2022-09-30 08:05:56'); """ db.engine.execute(query) @pytest.fixture(scope='session') def bearer_token_account_readonly_authorized( generate_bearer_token, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """Return a JWT for the authorized readonly account user.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment='qa', service_name=auth.APPLICATION, secret_name=auth.OWS_ABACUS_ACCOUNT_READONLY_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment='qa', service_name=auth.APPLICATION, secret_name=auth.ABACUS_APP_AUTH0_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def bearer_token_account_readonly_unauthorized( generate_bearer_token, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """Return a JWT for the unauthorized readonly account user.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment='qa', service_name=auth.APPLICATION, secret_name=auth.OWS_ABACUS_ACCOUNT_READONLY_USER_UNAUTHORIZED_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment='qa', service_name=auth.APPLICATION, secret_name=auth.ABACUS_APP_AUTH0_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def bearer_token_account_readonly_authorized_identity_uuid( bearer_token_account_readonly_authorized: str, ) -> str: """Extract identity_uuid from the authorized readonly token.""" return get_bearer_token_identity_uuid( bearer_token_account_readonly_authorized, environment='qa', ) @pytest.fixture(scope='session') def bearer_token_account_readonly_unauthorized_identity_uuid( bearer_token_account_readonly_unauthorized: str, ) -> str: """Extract identity_uuid from the unauthorized readonly token.""" return get_bearer_token_identity_uuid( bearer_token_account_readonly_unauthorized, environment='qa', )