"""Integration test configuration.""" import os import pytest from abacus_common_logic.connectors.database import db from abacus_schedule.api import create_app from abacus_schedule.config import Config from tests.integration.consts import headers from tests.integration.utils import auth from tests.integration.utils.ows_abacus_schedule_api_client import ( AbacusScheduleAPIClient, ) QA_BASE_URL = os.environ.get( 'QA_BASE_URL', 'http://abacus-schedule-ows-abacus-schedule:8080' ) 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', 'localhost') MYSQL_DB_PORT = os.environ.get('MYSQL_DB_PORT', '6057') MYSQL_DB_PASS = os.environ.get('MYSQL_DB_PASS', '1234') @pytest.fixture(autouse=True) def clear_db(): """Clean tables between runs.""" tables = ['schedule', 'schedule_attachment'] 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 create_schedule_fixture(): """Create a DB fixture in schedule table.""" query = """ INSERT INTO schedule ( schedule_id, schedule_name, target_type, target_id, conditions, created_by, created_at, last_modified_by, last_modified ) VALUES ( 1, 'test', 'contributor', '1', 'null', 'vz', '2023-02-28 04:13:32', 'vz', '2023-02-28 04:13:34' ) """ db.engine.execute(query) def create_schedule_attachment_fixture(): """Create a DB fixture in schedule_attachment table.""" query = """ INSERT INTO schedule_attachment ( schedule_attachment_id, schedule_id, target_type, target_id, created_by, created_at, last_modified_by, last_modified, deleted_by, deleted_at ) VALUES ( 1, 1, 'contribution', '1', 'vz', '2023-02-28 07:11:03', 'vz', '2023-02-28 07:11:09', null, null ) """ db.engine.execute(query) @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 def basic_headers(): """Return basic headers.""" return {'Content-Type': 'application/json'} @pytest.fixture(scope='session') def bearer_token_read_only_user() -> str: """Fetch Auth0 secrets from AWS Secret Manager.""" login_secrets = 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, ) token = auth.generate_auth_token( auth.LoginInfo( auth_client_id=login_secrets.auth_client_id, auth_client_secret=login_secrets.auth_client_secret, password=login_secrets.password, username=auth.READONLY_USER, ) ) return token @pytest.fixture(scope='session') def admin_headers() -> dict[str, str]: """Return profile headers.""" return headers.ADMIN_HEADERS @pytest.fixture(params=['read_only', 'admin'], scope='session') def auth_headers( request: pytest.FixtureRequest, bearer_token_read_only_user: str, admin_headers: dict[str, str], ) -> dict[str, str]: """Return appropriate headers based on the parameter.""" if request.param == 'read_only': return { 'Authorization': f'Bearer {bearer_token_read_only_user}', 'Content-Type': 'application/json', 'Orchard-Identity-Id': 'ff3737b4-7734-4339-9798-5ddd1c9998b0', 'Orchard-Profile-Id': '1005', 'Orchard-Profile-Type': 'Account360Profile', 'Orchard-Requestor-Service': 'graphql-abacus', 'Orchard-Roles': 'account360', } else: return admin_headers @pytest.fixture() def unauthorized_headers(bearer_token_read_only_user_unauthorized) -> dict[str, str]: """Return headers for unauthorized user.""" return { 'Authorization': f'Bearer {bearer_token_read_only_user_unauthorized}', 'Orchard-Requestor-Service': 'graphql-abacus', } @pytest.fixture(scope='session') def bearer_token_read_only_user_unauthorized() -> str: """Fetch Auth0 secrets from AWS Secret Manager.""" login_secrets = 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, ) token = auth.generate_auth_token( auth.LoginInfo( auth_client_id=login_secrets.auth_client_id, auth_client_secret=login_secrets.auth_client_secret, password=login_secrets.password, username=auth.READONLY_USER_UNAUTHORIZED, ) ) return token def ows_abacus_schedule_api_client(basic_headers): """Create ows-abacus-schedule client.""" return AbacusScheduleAPIClient(QA_BASE_URL, basic_headers)