"""Fixtures for integration tests.""" import os from os import getenv from os.path import dirname, join from dotenv import load_dotenv import pytest from tests.integration.splitio import SplitIoClient from tests.testutils.api_client.ows_accounting_api_client \ import AccountingAPIClient from tests.testutils.api_client.ows_features_api_client \ import FeaturesAPIClient from tests.testutils.dynamodb import dynamodb_helper # Load environment variables from a .env file if present dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) @pytest.fixture() def clean_temp_files(): yield temp_dir = '{}/tests/integration/data/tmp'.format( os.getcwd()) files_to_delete = os.listdir(temp_dir) for file in files_to_delete: os.remove('{}/{}'.format(temp_dir, file)) def template_files(): return [ 'template_doc.doc', 'template_jpg.jpg', 'template_png.png', 'template_xlsx.xlsx', 'template_docx.docx', 'template_pdf.pdf', 'template_xls.xls' ] def label_18805(): """Return data for label_18805.""" return {'id': 18805, 'vend_contact_id': 26122, 'user_type': 'vendor'} def label_18810(): """Return data for label_18805.""" return {'id': 18810, 'vend_contact_id': 26154, 'user_type': 'vendor'} def label_7482(): """Return data for label_7482.""" return {'id': 7482, 'vend_contact_id': 7412, 'user_type': 'vendor'} def subaccount_13823(): """Return data for subaccount_13823.""" return {'id': 13823, 'vend_contact_id': 24241, 'user_type': 'subaccount'} def basic_headers(): """Return headers.""" return {'Content-Type': 'application/json'} def user_headers(user_type, account_id, vend_contact_id): """Return headers for test user.""" return {'Grass-Account-Type': str(user_type), 'Grass-Account-Id': str(account_id), 'Orchard-User-Id': 'alw:{}'.format(vend_contact_id), 'Content-Type': 'application/json'} def ows_accounting_api_client(headers): """Create ows-accounting APIClient object.""" return AccountingAPIClient(getenv('BASE_QA_OWS_ACCOUNTING_URL'), headers) @pytest.fixture def current_accounting_period(): """Obtain current accounting period for testing.""" headers = {} ows_accounting_client = ows_accounting_api_client(headers) get_current_period = ows_accounting_client.get_current_accounting_period() assert get_current_period.status_code == 200, \ 'Result of call was {}, expected 200.'.format( get_current_period.status_code) return int(get_current_period.json()['accounting_period']) def ows_features_api_client(): """Create ows-features APIClient object""" return FeaturesAPIClient(getenv('BASE_QA_OWS_FEATURES_URL')) def update_gross_accounting_logic(variant_name, vend_contact_id): """Update gross_accounting and vendor_gross_net flags for a test user.""" clt = SplitIoClient() if variant_name == 'enabled': clt.remove_rule_from_split('gross_accounting') clt.remove_rule_from_split('vendor_gross_net') clt.add_user_to_split( 'alw:{}'.format(vend_contact_id), 'gross_accounting') clt.add_user_to_split( 'alw:{}'.format(vend_contact_id), 'vendor_gross_net') else: clt.remove_rule_from_split('gross_accounting') clt.remove_rule_from_split('vendor_gross_net') clt.remove_user_from_split( 'alw:{}'.format(vend_contact_id), 'gross_accounting') clt.remove_user_from_split( 'alw:{}'.format(vend_contact_id), 'vendor_gross_net') def d3_user_transaction_types_expectations(): """Return transaction_types expectations for a D3 user""" return [ 'Ad-Supported Audio Streams', 'Ad-Supported Video Streams', 'Cloud Match Units', 'Download Albums', 'Download Tracks', 'Mid-Tier Subscription Audio Streams', 'Mobile Videos', 'Non-interactive Radio', 'Performance Rights', 'Subscription Audio Streams', 'Subscription Video Streams', 'Tethered Downloads' ] def subaccount_transaction_types_expectations(): """Return transaction_types expectations for a subaccount""" return [ 'Ad-Supported Audio Streams', 'Cloud Match Units', 'Download Albums', 'Download Tracks', 'Mobile Videos', 'Performance Rights', 'Subscription Audio Streams' ] def quarterly_user_transaction_types_expectations(): """Return transaction_types expectations for a quarterly user""" return [ 'Ad-Supported Audio Streams', 'Ad-Supported Video Streams', 'Cloud Match Units', 'Download Albums', 'Download Tracks', 'Mid-Tier Subscription Audio Streams', 'Non-interactive Radio', 'Performance Rights', 'Ringbacks', 'Subscription Audio Streams', 'Subscription Video Streams', 'Tethered Downloads' ] @pytest.fixture(scope="module") def clean_physical_reports(): quarterly_id = label_7482()['id'] monthly_id = label_18805()['id'] subaccount_id = subaccount_13823()['id'] periods = get_periods() period = get_period() tr_types = get_transaction_types_physical() file_type = get_file_type_txt() format = get_format_es_es() dynamodb_helper.clean_up( '{}L'.format(quarterly_id), '{periods}__{format}__{tr_types}__{file_type}'.format( periods=periods, format=format, tr_types=tr_types, file_type=file_type)) dynamodb_helper.clean_up( '{}L'.format(monthly_id), '{period}__{format}__{tr_types}__{file_type}'.format( period=period, format=format, tr_types=tr_types, file_type=file_type)) dynamodb_helper.clean_up( '{}S'.format(subaccount_id), '{period}__{format}__{tr_types}__{file_type}'.format( period=period, format=format, tr_types=tr_types, file_type=file_type)) @pytest.fixture(scope="module") def clean_full_reports(): quarterly_id = label_7482()['id'] monthly_id = label_18805()['id'] subaccount_id = subaccount_13823()['id'] periods = get_periods() period = get_period() tr_types = get_transaction_types_all() file_type = get_file_type_xls() format = get_format_es_es() dynamodb_helper.clean_up( '{}L'.format(quarterly_id), '{periods}__{tr_types}__{format}__{file_type}'.format( periods=periods, format=format, tr_types=tr_types, file_type=file_type)) dynamodb_helper.clean_up( '{}L'.format(monthly_id), '{period}__{tr_types}__{format}__{file_type}'.format( period=period, format=format, tr_types=tr_types, file_type=file_type)) dynamodb_helper.clean_up( '{}S'.format(subaccount_id), '{period}__{tr_types}__{format}__{file_type}'.format( period=period, format=format, tr_types=tr_types, file_type=file_type)) @pytest.fixture(scope="module") def clean_custom_reports(): quarterly_id = label_7482()['id'] monthly_id = label_18805()['id'] subaccount_id = subaccount_13823()['id'] periods = get_periods() period = get_period() tr_types = get_transaction_types_clmv() file_type = get_file_type_xls() format = get_format_en_us() dynamodb_helper.clean_up( '{}L'.format(quarterly_id), '{periods}__{tr_types}__{format}__{file_type}'.format( periods=periods, format=format, tr_types=tr_types, file_type=file_type)) dynamodb_helper.clean_up( '{}L'.format(monthly_id), '{period}__{tr_types}__{format}__{file_type}'.format( period=period, format=format, tr_types=tr_types, file_type=file_type)) dynamodb_helper.clean_up( '{}S'.format(subaccount_id), '{period}__{tr_types}__{format}__{file_type}'.format( period=period, format=format, tr_types=tr_types, file_type=file_type)) def get_periods(): """Return list of periods for reports testing.""" return '241,242,243' def get_period(): """Return single period for reports testing.""" return '241' def get_transaction_types_physical(): """Return transaction_types for physical reports testing.""" return 'physical' def get_transaction_types_all(): """Return transaction_types for full reports testing.""" return 'all' def get_transaction_types_clmv(): """Return transaction_types for full reports testing.""" return 'CL,MV' def get_file_type_txt(): """Return file_type for physical reports testing.""" return 'txt' def get_file_type_xls(): """Return file_type for full reports testing.""" return 'xls' def get_format_es_es(): """Return format for reports testing.""" return 'es_ES' def get_format_en_us(): """Return format for reports testing.""" return 'en_US'