"""Integration test configuration.""" from os import getenv from os.path import dirname, join import pytest from auth.auth import GrassAuth, UserType from dotenv import load_dotenv from tests.utils.api_client.api_client import APIClient from tests.utils.api_client.ows_contracts_api_client import ContractsAPIClient dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) def generate_product_split(upc, split_rate, oa_user_id, vend_contract_id, release_name): """Generate a product split for ows-contracts integration tests.""" return [ { 'upc': upc, 'product_split_rate': split_rate, 'oa_user_id': oa_user_id, 'vend_contract_id': vend_contract_id, 'release_name': release_name, } ] def generate_track_split( isrc, track_split, track_unique_id, oa_user_id, vend_contract_id ): """Generate a track split for ows-contracts integration tests.""" return [ { 'isrc': isrc, 'track_split': track_split, 'track_unique_id': track_unique_id, 'oa_user_id': oa_user_id, 'vend_contract_id': vend_contract_id, } ] def find_product_split_by_upc(splits_json, upc): """Find a split entry within the response by UPC.""" for el in splits_json: if str(el['upc']) == str(upc): return el def find_track_split_by_isrc(splits_json, isrc): """Find a split entry within the response by ISRC.""" for el in splits_json: if str(el['isrc']) == str(isrc): return el @pytest.fixture def vendor_id(): """Return vendor_id for a test user.""" return '7123' @pytest.fixture def product_split_data(): """Return product split data for testing.""" return { 'upc': 886788064409, 'oa_user_id': 1241, 'vend_contract_id': 51643, 'release_name': "I'm going to try and make it quick!", } @pytest.fixture def territory_product_split_data(): """Return product split data for testing.""" return [ { 'oa_user_id': 1241, 'vend_contract_id': 51643, 'country_id': 4, 'split_rate': 0.56, 'upc': 886788064409, 'country_name': 'India', } ] @pytest.fixture def track_split_data(): """Return track split data for testing.""" return { 'isrc': 'QM7281541881', 'track_unique_id': 21702471, 'oa_user_id': 1241, 'vend_contract_id': 86496, } @pytest.fixture def user_headers(): """Return headers for test user.""" return { 'Grass-Account-Type': 'vendor', 'Grass-Account-Id': '7123', 'Content-Type': 'application/json', } @pytest.fixture def unauthorized_user_headers(): """Return headers for test user.""" return {'Grass-Account-Type': 'vendor', 'Grass-Account-Id': '7124'} @pytest.fixture(scope='function') def cleanup_product_splits(vendor_id, product_split_data, user_headers): """Clean up product splits before test scenario to avoid conflict errors.""" data = { 'vend_contract_id': product_split_data['vend_contract_id'], 'oa_user_id': product_split_data['oa_user_id'], 'vendor_id': vendor_id, } ows_contracts_client = ows_contracts_api_client(user_headers) splits = ows_contracts_client.get_product_split(vendor_id).json() for split in splits['items']: response = ows_contracts_client.delete_product_splits(str(split['upc']), data) assert response.status_code == 200, 'failed to cleanup splits' @pytest.fixture(scope='function') def cleanup_track_splits(vendor_id, user_headers, track_split_data): """Clean up track splits before test scenario to avoid conflict errors.""" ows_contracts_client = ows_contracts_api_client(user_headers) splits = ows_contracts_client.get_track_split(vendor_id).json() for split in splits['items']: data = { 'isrc': str(split['isrc']), 'oa_user_id': track_split_data['oa_user_id'], 'vend_contract_id': track_split_data['vend_contract_id'], } response = ows_contracts_client.delete_track_splits(vendor_id, data) assert response.status_code == 200, 'failed to cleanup splits' @pytest.fixture(scope='function') def cleanup_product_territory_splits( vendor_id, territory_product_split_data, user_headers ): """Clean up product territory splits before test scenario to avoid conflict errors.""" ows_contracts_client = ows_contracts_api_client(user_headers) splits = ows_contracts_client.get_product_territory_split(vendor_id).json() for split in splits['items']: data = { 'country_id': split['country_details']['id'], 'oa_user_id': territory_product_split_data[0]['oa_user_id'], 'vend_contract_id': territory_product_split_data[0]['vend_contract_id'], 'country_name': split['country_details']['name'], } upc = str(split['upc']) response = ows_contracts_client.delete_product_territory_split(upc, data) assert response.status_code == 200, 'failed to cleanup splits' def regular_workstation_user(): """Return data for workstation user that is NOT a mech-admin.""" return {'user_id': 38271, 'vendor_id': 7123} def mechadmin_workstation_user(): """Return data for workstation user that is a mech-admin.""" return {'user_id': 17409, 'vendor_id': 16155} def workstation_session_token(user_id): """Initialize workstation grass auth token at start of test session.""" return GrassAuth(UserType.WORKSTATION, user_id).grass_token def workstation_api_client(workstation_user): """Create Track APIClient object with Workstation session token.""" return APIClient( getenv('BASE_QA_WORKSTATION_URL'), workstation_session_token(workstation_user['user_id']), ) def ows_contracts_api_client(headers): """Create ows-contracts APIClient object.""" return ContractsAPIClient(getenv('BASE_QA_OWS_CONTRACTS_URL'), headers)