"""Test GET /contracts endpoint.""" import pytest import requests from tests.integration.conftest import ( ows_abacus_contract_api_client, ) from tests.integration.consts.api import QA_BASE_URL from tests.integration.consts.constants import BAD_SEED_ACCOUNT_ID, TEST_ACCOUNT_ID from tests.integration.utils.generic_helper import ( create_account, create_account_and_contract, create_runcontroller, create_runcontroller_contract, generate_random_string ) @pytest.mark.jira('ACC-6184') def test_get_contracts_by_contract_type( basic_headers, clear_db, create_schedule_fixtures ): """GET /contracts by contract_type .""" account_fixture = create_account() runcontroller_fixture = create_runcontroller() ows_abacus_contract_client = ows_abacus_contract_api_client(basic_headers) contract_name = 'orcd_autotest_{}'.format(generate_random_string(16)) contract_body = ows_abacus_contract_client.generate_contract_body( contract_name, account_fixture['account_id'], 'distribution' ) response_post = ows_abacus_contract_client.post_contract(contract_body) assert response_post.status_code == 201 contract_id_1 = response_post.json()['contract_id'] contract_name = 'orcd_autotest_{}'.format(generate_random_string(16)) contract_body = ows_abacus_contract_client.generate_contract_body( contract_name, account_fixture['account_id'], 'neighbouring_rights' ) response_post = ows_abacus_contract_client.post_contract(contract_body) assert response_post.status_code == 201 contract_id_2 = response_post.json()['contract_id'] create_runcontroller_contract( runcontroller_fixture['runcontroller_id'], contract_id_1 ) create_runcontroller_contract( runcontroller_fixture['runcontroller_id'], contract_id_2 ) response_get = ows_abacus_contract_client.get_contracts() assert response_get.status_code == 200 assert len(response_get.json()['items']) == 2 response_get = ows_abacus_contract_client.get_contracts( {'contract_type': 'neighbouring_rights'} ) assert response_get.status_code == 200 assert len(response_get.json()['items']) == 1 assert response_get.json()['items'][0]['contract_type'] == 'neighbouring_rights' @pytest.mark.jira('PLATFORM-4471') def test_get_contracts_with_default_parameters(auth_headers: dict[str, str]) -> None: """GET /contracts with default parameters.""" # create an account and contract using the admin headers. runcontroller_fixture = create_runcontroller() contract_name_1 = 'orcd_autotest_{}'.format(generate_random_string(16)) contract_name_2 = 'orcd_autotest_{}'.format(generate_random_string(16)) contract_id_1, _, _ = create_account_and_contract( account_id=1, contract_name=contract_name_1 ) contract_id_2, _, _ = create_account_and_contract( account_id=2, contract_name=contract_name_2 ) create_runcontroller_contract( runcontroller_fixture['runcontroller_id'], contract_id_1 ) create_runcontroller_contract( runcontroller_fixture['runcontroller_id'], contract_id_2 ) endpoint = '{}/contracts?account_ids=&limit=20&offset=0&search_term='.format( QA_BASE_URL) result = requests.get(endpoint, headers=auth_headers) assert result.status_code == 200 result_data = result.json() assert result_data['total_count'] == 2 assert len(result_data['items']) == 2 assert ({ 'contract_type': 'distribution', 'execution_date': None, 'reference_signing_entity_id': 2, 'term_end': '2020-06-01', 'term_start': '2019-06-01', 'summary_note': 'testing', 'general_note': 'testing', 'contract_id': 1, 'contract_name': contract_name_1, 'oa_contract_id': None, 'account_id': 1, 'sap_created_at': None, 'initial_start_date': None, 'is_excluded_from_accounting_run': False }) in result_data['items'] assert ({ 'contract_type': 'distribution', 'execution_date': None, 'reference_signing_entity_id': 2, 'term_end': '2020-06-01', 'term_start': '2019-06-01', 'summary_note': 'testing', 'general_note': 'testing', 'contract_id': 2, 'contract_name': contract_name_2, 'oa_contract_id': None, 'account_id': 2, 'sap_created_at': None, 'initial_start_date': None, 'is_excluded_from_accounting_run': False }) in result_data['items'] @pytest.mark.jira('PLATFORM-4471') def test_get_contracts_by_account_ids( auth_headers: dict[str, str], ) -> None: """Test POST /contracts/accounts.""" contract_name_1 = 'orcd_autotest_{}'.format(generate_random_string(16)) contract_name_2 = 'orcd_autotest_{}'.format(generate_random_string(16)) create_account_and_contract( account_id=TEST_ACCOUNT_ID, contract_name=contract_name_1), create_account_and_contract( account_id=BAD_SEED_ACCOUNT_ID, contract_name=contract_name_2) result = requests.post( f'{QA_BASE_URL}/contracts/accounts', headers=auth_headers, json=[TEST_ACCOUNT_ID, BAD_SEED_ACCOUNT_ID], ) assert result.status_code == 200 result_data = result.json() assert result_data['total_count'] == 2 assert len(result_data['items']) == 2 assert ({ 'contract_type': 'distribution', 'execution_date': None, 'reference_signing_entity_id': 2, 'term_end': '2020-06-01', 'term_start': '2019-06-01', 'summary_note': 'testing', 'general_note': 'testing', 'contract_id': 1, 'contract_name': contract_name_1, 'oa_contract_id': None, 'account_id': 7123, 'sap_created_at': None, 'initial_start_date': None, 'is_excluded_from_accounting_run': False }) in result_data['items'] assert ({ 'contract_type': 'distribution', 'execution_date': None, 'reference_signing_entity_id': 2, 'term_end': '2020-06-01', 'term_start': '2019-06-01', 'summary_note': 'testing', 'general_note': 'testing', 'contract_id': 2, 'contract_name': contract_name_2, 'oa_contract_id': None, 'account_id': 57608, 'sap_created_at': None, 'initial_start_date': None, 'is_excluded_from_accounting_run': False }) in result_data['items'] @pytest.mark.jira('PLATFORM-4471') def test_get_contracts_not_found_and_no_auth(auth_headers: dict[str, str]) -> None: """GET /contracts with search term that skips authorization checks.""" endpoint = ( '{}/contracts?account_ids=&limit=20&offset=0&search_term=notsuchcontractever' .format(QA_BASE_URL)) result = requests.get(endpoint, headers=auth_headers) assert result.status_code == 200 result_data = result.json() assert result_data['total_count'] == 0 assert len(result_data['items']) == 0 assert result_data['items'] == []