"""Set up unit test environment.""" from unittest.mock import MagicMock import pytest from owsresponse import response from account import api, config from account.constants import header config.ENVIRONMENT = config.TEST_ENVIRONMENT @pytest.fixture def fixture_valid_vendor(): """Fixture response for a vendor.""" return response.Response( { 'vendor_id': 1, 'is_distributor': 'Y', 'name': 'Funky Vendor', 'country_id': 1, 'owner': 'odd', } ) @pytest.fixture def fixture_valid_update_vendor(): """Fixture response for a vendor.""" return response.Response( { 'assigned_to_id': '1', 'assigned_reviewer_id': '2', 'company_brand': 'awal', 'contact_email': 'test2903@test.com', 'contact_name': 'Blacktop 2', 'country_id': 1, 'is_owned': 'No', 'label_identifier': 'Frontline', 'name': 'Blacktop 2', 'owner': 'odd', 'primary_genre_id': '2', 'priority': 3, 'product_manager_id': '1', 'quarterback_label_manager_id': '12', 'region_id': '2', 'service_tier_uuid': '5f2bd4fc-df94-4f35-97d3-ef23f8573279', 'show_release_builder': 'N', 'support_contact_email': 'test_support2903@test.com', } ) @pytest.fixture def fixture_valid_update_vendor_with_last_updated_by(): """Fixture response for a vendor.""" return response.Response( { 'assigned_to_id': '1', 'assigned_reviewer_id': '2', 'company_brand': 'awal', 'contact_email': 'test2903@test.com', 'contact_name': 'Blacktop 2', 'country_id': 1, 'is_owned': 'No', 'label_identifier': 'Frontline', 'name': 'Blacktop 2', 'owner': 'odd', 'primary_genre_id': '2', 'priority': 3, 'product_manager_id': '1', 'quarterback_label_manager_id': '12', 'region_id': '2', 'service_tier_uuid': '5f2bd4fc-df94-4f35-97d3-ef23f8573279', 'show_release_builder': 'N', 'support_contact_email': 'test_support2903@test.com', 'last_updated_by': 1111, } ) MINIMUM_VENDOR_ATTRS = { 'name': 'Full name of the Vendor', 'owner': 'Owner of this Vendor', 'company_brand': 'awal', 'service_tier_uuid': '7410e51c-90da-4092-99da-b5489f364fa8', 'payment_currency': 'AUD', } @pytest.fixture def minimum_vendor_attrs(): """dict containing the minimum required fields to create a vendor.""" return MINIMUM_VENDOR_ATTRS @pytest.fixture def fixture_subaccount_document(): """Fixture response for subaccount document.""" return { 'label_id': 2, 'account_name': 'Demo', 'label_owner': 'Test', 'label_status': 'signed', 'label_type': 'Subaccount', 'label_country': 'USA', 'is_owned': 'No', 'client_manager': 'User', 'parent_label': 'Demo Inc', 'parent_label_id': 1, 'is_active': 1, } @pytest.fixture def fixture_vendor_document(): """Fixture response for vendor document.""" return { 'label_type': 'D3', 'label_status': 'signed', 'label_contact_name': 'Test', 'label_contact_login': 'test@test', 'client_manager': 'User', 'is_owned': 'No', 'account_name': 'Test Label', 'label_country': 'USA', 'label_id': 2, 'label_owner': 'Test Owner', 'label_contact_id': 1234, 'vendor_uuid': None, } @pytest.fixture def fixture_vendor_currency_codes(): """Fixture response for vendor currency codes.""" return [[123, 'USD'], [456, 'EUR']] @pytest.fixture def fixture_vendor_currency_codes_response(): """Fixture response for vendor currency codes response.""" return { 'items': [ {'vendor_id': 123, 'currency_code': 'USD'}, {'vendor_id': 456, 'currency_code': 'EUR'}, ] } @pytest.fixture def fixture_account_type(): """Fixture for vendor Grass-Account-Type.""" return header.GRASS_ACCOUNT_TYPE_VENDOR @pytest.fixture def fixture_account_id(): """Fixture for Grass-Account-Id.""" return 1 @pytest.fixture def fixture_subaccount_id(): """Fixture for subaccount-id.""" return 2 @pytest.fixture def fixture_subaccount_data_to_activate(): """Fixture data to update subaccount.""" return {'active': True} @pytest.fixture def fixture_subaccount_data_to_deactivate(): """Fixture data to update subaccount.""" return {'active': False} @pytest.fixture def headers(): """Return valid headers with content type and correlation id. Returns: dict: header dict """ return {header.CORRELATION_ID: 'abc-123', 'Content-Type': 'application/json'} @pytest.fixture def valid_headers_for_vendor(headers, fixture_account_id, fixture_account_type): """Return valid headers with vendor type and account id. Returns: dict: header dict """ headers.update( { header.GRASS_ACCOUNT_TYPE: fixture_account_type, header.GRASS_ACCOUNT_ID: fixture_account_id, } ) return headers @pytest.fixture def invalid_headers_for_vendor(headers, fixture_account_id, fixture_account_type): """Return valid headers with vendor type and account id. Returns: dict: header dict """ headers.update({header.GRASS_ACCOUNT_ID: fixture_account_id}) return headers @pytest.fixture def fixture_subaccount(): """Fixture response for single subaccount.""" return response.Response( { 'subaccount_id': 1, 'vendor_id': 1, 'subaccount_name': 'Subaccount 1', 'description': 'Subaccount Description 1', 'date_deleted': None, 'commission_override': 0.5, } ) def get_transactional_session_mock(side_effect=[]): """Mock for session from @Neo4jSession(transaction=True).""" enter_mock = MagicMock() result_mock = MagicMock() result_mock.data.side_effect = side_effect result_mock.single.side_effect = side_effect result_mock.peek.side_effect = side_effect transaction_mock = MagicMock() transaction_mock.run.return_value = result_mock enter_mock.begin_transaction.return_value = transaction_mock enter_mock.run.return_value = result_mock session_mock = MagicMock() session_mock.run.return_value = result_mock session_mock.__enter__.return_value = enter_mock return session_mock def get_session_mock(return_data=[]): """GET get a mock for db_session() which returns return_data list.""" enter_mock = MagicMock() session_mock = MagicMock() result_mock = MagicMock() session_mock.__enter__.return_value = enter_mock enter_mock.read_transaction.return_value = return_data enter_mock.write_transaction.return_value = return_data enter_mock.run.return_value = result_mock enter_mock.execute.return_value = result_mock result_mock.single.return_value = return_data and return_data[0] or None result_mock.fetchall.return_value = return_data result_mock.__iter__.return_value = return_data return session_mock @pytest.fixture def context(): """Return flask app context, including a mock logger. Returns: AppContext: flask app context, including a mock logger. """ import application class MockLog: """Mock Logging Class.""" def error(self): pass def info(self): pass def warning(self): pass context_instance = application.app.app_context() return context_instance @pytest.fixture def app_context(): """Create an api test app fixture.""" with api.app.app_context(): yield