"""Integration tests for reference_signing_entity nested endpoints.""" import pytest from abacus_contract.tests.integration.conftest import ows_abacus_contract_api_client from abacus_contract.tests.integration.utils import generic_helper @pytest.mark.jira('ACC-10456') def test_get_sap_profit_centers_for_signing_entity( basic_headers: dict[str, str], ) -> None: """GET /reference-signing-entity/:id/reference-sap-profit-centers/ returns the live PC list.""" client = ows_abacus_contract_api_client(basic_headers) se_id, pc_id = generic_helper.create_signing_entity_with_profit_center() res = client.get_sap_profit_centers_for_signing_entity(se_id) assert res.status_code == 200, res.text items = res.json()['items'] matched = next( ( item for item in items if item['sap_profit_center']['reference_sap_profit_center_id'] == pc_id ), None, ) assert matched is not None assert 'signing_entity_sap_profit_center_id' in matched assert 'created_at' in matched assert 'display_name' in matched['sap_profit_center'] assert 'profit_center' in matched['sap_profit_center'] @pytest.mark.jira('ACC-10456') def test_get_sap_profit_centers_for_signing_entity_unknown_id_returns_404( basic_headers: dict[str, str], ) -> None: """Unknown SE id -> 404.""" client = ows_abacus_contract_api_client(basic_headers) res = client.get_sap_profit_centers_for_signing_entity(99999999) assert res.status_code == 404, res.text # --------------------------------------------------------------------------- # POST /reference-signing-entity/reference-sap-profit-centers/dataloader # --------------------------------------------------------------------------- @pytest.mark.jira('ACC-10419') def test_post_profit_centers_dataloader_returns_live_pc_mappings( basic_headers: dict[str, str], ) -> None: """POST /reference-signing-entity/reference-sap-profit-centers/dataloader .""" client = ows_abacus_contract_api_client(basic_headers) se_id_1, pc_id_1 = generic_helper.create_signing_entity_with_profit_center() se_id_2, pc_id_2 = generic_helper.create_signing_entity_with_profit_center() res = client.post_profit_centers_by_signing_entity_dataloader([se_id_1, se_id_2]) assert res.status_code == 200, res.text body = res.json() assert isinstance(body, list) assert len(body) == 2 # Per-SE data lists contain the expected profit center IDs. pc_ids_1 = { m['sap_profit_center']['reference_sap_profit_center_id'] for m in body[0]['data'] } pc_ids_2 = { m['sap_profit_center']['reference_sap_profit_center_id'] for m in body[1]['data'] } assert pc_id_1 in pc_ids_1 assert pc_id_2 in pc_ids_2 @pytest.mark.jira('ACC-10419') def test_post_profit_centers_dataloader_unknown_se_ids_return_data_none( basic_headers: dict[str, str], ) -> None: """Non-existent SE IDs produce {data: None} — confirms the real DB zero-row path.""" client = ows_abacus_contract_api_client(basic_headers) res = client.post_profit_centers_by_signing_entity_dataloader([99999991, 99999992]) assert res.status_code == 200, res.text assert res.json() == [{'data': None}, {'data': None}] # --------------------------------------------------------------------------- # POST /reference-signing-entities/dataloader # --------------------------------------------------------------------------- @pytest.mark.jira('ACC-10497') def test_post_signing_entities_dataloader_returns_se_data( basic_headers: dict[str, str], ) -> None: """Happy path: real SE rows flow through the full stack and arrive correctly shaped. Verifies HTTP routing → auth middleware → query → real DB → serialization are wired together — logic edge-cases are covered by the functional tests. """ client = ows_abacus_contract_api_client(basic_headers) se_id_1, _ = ( generic_helper.create_signing_entity_and_profit_center_without_junction() ) se_id_2, _ = ( generic_helper.create_signing_entity_and_profit_center_without_junction() ) res = client.post_signing_entities_dataloader([se_id_1, se_id_2]) assert res.status_code == 200, res.text body = res.json() assert isinstance(body, list) assert len(body) == 2 returned_se_ids = { entry['data']['reference_signing_entity_id'] for entry in body if entry.get('data') } assert se_id_1 in returned_se_ids assert se_id_2 in returned_se_ids @pytest.mark.jira('ACC-10497') def test_post_signing_entities_dataloader_unknown_ids_return_data_none( basic_headers: dict[str, str], ) -> None: """Non-existent SE IDs produce {data: None} — confirms the real DB zero-row path.""" client = ows_abacus_contract_api_client(basic_headers) res = client.post_signing_entities_dataloader([99999981, 99999982]) assert res.status_code == 200, res.text assert res.json() == [{'data': None}, {'data': None}]