"""Integration test configuration.""" import os from typing import Any import pytest from abacus_common_logic.connectors.database import db from abacus_state.api import create_app from abacus_state.config import Config from tests.integration.utils import auth from tests.integration.utils.ows_abacus_state_api_client import AbacusstateAPIClient QA_BASE_URL = os.environ.get('QA_BASE_URL', 'http://localhost:6400') TOP_LEVEL_TABLES = ( 'abacus_state', 'accounting_period', 'account', 'contract', 'account_contract', 'account_payee', 'payment_group', 'payment_group_payment', 'payment_group_payment_account', 'statement_period', ) 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(scope='session', 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 def basic_headers(): """Return basic headers.""" return {'Content-Type': 'application/json'} def ows_abacus_state_api_client(basic_headers): """Create ows-abacus-state client.""" return AbacusstateAPIClient(QA_BASE_URL, basic_headers) @pytest.fixture(autouse=True) def fresh_db(): """Refresh the test database.""" db.engine.execute('SET FOREIGN_KEY_CHECKS=0;') for table in TOP_LEVEL_TABLES: db.engine.execute(f'TRUNCATE {table}') db.engine.execute('SET FOREIGN_KEY_CHECKS=1;') @pytest.fixture def drop_fks(): """Drop FKs in DB.""" try: db.engine.execute("""ALTER TABLE accounting_period DROP FOREIGN KEY fk_statement_period_acct_period;""") db.engine.execute("""ALTER TABLE contract DROP FOREIGN KEY fk_contract_reference_sap_profit_center;""") except Exception as e: print('FK was already deleted: {}'.format(e)) @pytest.fixture def create_payment_group_payment_account_fixture(drop_fks): """Create the rest of the fixtures.""" statement_period_query = """ INSERT INTO royalty_accounting.statement_period ( statement_period_id, statement_period_name, statement_period_status ) VALUE ( 256, 'Test statement period', 'current' ); """ db.engine.execute(statement_period_query) account_query = """ INSERT INTO royalty_accounting.account ( account_id, account_name, created_by, created_at, last_modified_by, last_modified ) VALUES ( 25153, 'TEST ACCOUNT', 1472, '2021-04-12 02:38:45.000000', 1472, '2021-04-12 02:38:45.000000' ) """ db.engine.execute(account_query) contract_query = """ INSERT INTO royalty_accounting.contract ( contract_id, reference_signing_entity_id, contract_name, contract_type, term_start, term_end, created_by, created_at, last_modified_by, last_modified ) VALUES ( 1, 1, 'test', 'distribution', '2022-06-06', '2022-06-09', 'vz', '2022-06-06 03:43:02', 'vz', '2022-06-06 03:43:05' ) """ db.engine.execute(contract_query) account_contract_query = """INSERT INTO royalty_accounting.account_contract ( account_contract_id, account_id, contract_id ) VALUES (1, 25153, 1); """ db.engine.execute(account_contract_query) account_payee_query = """ INSERT INTO royalty_accounting.account_payee (account_id, payoneer_payee_id, payoneer_payee_name, payoneer_iframe_url, payoneer_iframe_url_date, payoneer_session_id, created_by, created_at, last_modified_by, last_modified) VALUES (25153, 1, 'test', 'test', '2022-03-24', 'test', 'vz', '2022-03-24 07:27:26', 'vz', '2022-03-24 07:27:30')""" db.engine.execute(account_payee_query) payment_group_query = """INSERT INTO payment_group ( payment_group_id, group_name, group_criteria, is_reusable, created_at, created_by, last_modified, last_modified_by, deleted_at, deleted_by) VALUES (1, 'test', '{"account_id": 25153}', 0, '2020-08-28 07:11:39', 'test', '2020-08-28 07:11:42', 'test', null, null)""" db.engine.execute(payment_group_query) payment_group_payment_query = """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, deleted_at, deleted_by) VALUES (1, 1, 256, 'test', '2020-08-28 07:26:44', 'test', '2020-08-28 07:26:49', 'test', null, null)""" db.engine.execute(payment_group_payment_query) payment_group_payment_account_query = """INSERT INTO payment_group_payment_account ( payment_group_payment_account_id, current_statement_period_id, payoneer_program_id, payment_group_payment_id, prior_payment_group_payment_id, account_id, currency_code, last_payment, contracts_payable, current_balance, tax_withholding, balance_after_tax, note, created_at, created_by, last_modified, last_modified_by, deleted_at, deleted_by) VALUES (1, 256, 100176930, 1, null, 25153, 'USD', 100.00, '[{"contract_id": 1, "currency_code": "USD", "current_balance": "763.31"}]', 200.00, 0.00, 200.00, null, '2020-09-02 04:55:20', 'test', '2020-09-02 04:55:28', 'test', null, null)""" db.engine.execute(payment_group_payment_account_query) @pytest.fixture(autouse=True) def create_accounting_period_fixture(drop_fks): """Create accounting_period fixture.""" acc_period_query = """ INSERT INTO accounting_period ( accounting_period_id, statement_period_id, accounting_period_name, accounting_period_status, contract_type, closed_date, created_by, created_at, last_modified_by, last_modified ) VALUES ( 1, 1, 'test', 'open', 'distribution', null, 'vz', '2021-12-10 03:40:45', 'vz', '2021-12-10 03:40:48' ); """ try: db.engine.execute(acc_period_query) except Exception as e: print('Fixture was already present: {}'.format(e)) @pytest.fixture(scope='session') def read_only_user_login_secrets() -> auth.LoginSecrets: """Get LoginSecrets for read only identity.""" return auth.login_from_secrets_manager( password_secret_name=auth.READONLY_USER_PASSWORD_SECRET_NAME, auth_client_id_secret_name=auth.AUTH0_CLIENT_ID_SECRET_NAME, auth_client_secret_secret_name=auth.AUTH0_CLIENT_SECRET_SECRET_NAME, ) @pytest.fixture(scope='session') def bearer_token_read_only_user(read_only_user_login_secrets) -> str: """Fetch bearer token for a read only identity.""" return auth.generate_auth_token( auth.LoginInfo( auth_client_id=read_only_user_login_secrets.auth_client_id, auth_client_secret=read_only_user_login_secrets.auth_client_secret, password=read_only_user_login_secrets.password, username=auth.READONLY_USER, ) ) @pytest.fixture(scope='session') def bearer_token_read_only_user_unauthorized(read_only_user_login_secrets) -> str: """Fetch bearer token for an unauthorized read only identity.""" return auth.generate_auth_token( auth.LoginInfo( auth_client_id=read_only_user_login_secrets.auth_client_id, auth_client_secret=read_only_user_login_secrets.auth_client_secret, password=read_only_user_login_secrets.password, username=auth.READONLY_USER_UNAUTHORIZED, ) ) @pytest.fixture def no_authorization_headers() -> dict[str, Any]: """Return headers that have no authorization headers present. The orchard-requestor-service is set because this triggers checking on authorization headers. """ return { 'Content-Type': 'application/json', 'orchard-requestor-service': 'graphql-abacus', } @pytest.fixture def some_other_app_profile_headers() -> dict[str, Any]: """Return some other profile headers.""" return { 'Content-Type': 'application/json', 'orchard-requestor-service': 'graphql-abacus', 'Orchard-Profile-Type': 'ContentProfile', 'Orchard-Profile-Id': '123456', 'Orchard-Roles': 'content-reviewer', } @pytest.fixture def abacus_headers() -> dict[str, Any]: """Return abacus profile headers.""" return { 'Content-Type': 'application/json', 'orchard-requestor-service': 'graphql-abacus', 'Orchard-Profile-Type': 'AbacusProfile', 'Orchard-Profile-Id': '123456', 'Orchard-Roles': 'administrator', } @pytest.fixture def a360_headers() -> dict[str, Any]: """Return a360 profile headers.""" return { 'Content-Type': 'application/json', 'orchard-requestor-service': 'graphql-abacus', 'Orchard-Profile-Type': 'Account360Profile', 'Orchard-Profile-Id': '123456', 'Orchard-Roles': 'account360', }