"""Integration tests for SE-PC junction admin 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-10416') def test_post_signing_entity_profit_center_lifecycle( basic_headers: dict[str, str], ) -> None: """POST creates -> POST duplicate 409 -> DELETE -> POST restores -> DELETE.""" client = ows_abacus_contract_api_client(basic_headers) se_id, pc_id = ( generic_helper.create_signing_entity_and_profit_center_without_junction() ) payload = { 'reference_signing_entity_id': se_id, 'reference_sap_profit_center_id': pc_id, } create_res = client.post_signing_entity_profit_center(payload) assert create_res.status_code == 201, create_res.text junction_id = create_res.json()['signing_entity_sap_profit_center_id'] assert create_res.json()['reference_signing_entity_id'] == se_id assert create_res.json()['reference_sap_profit_center_id'] == pc_id assert create_res.json()['deleted_at'] is None duplicate_res = client.post_signing_entity_profit_center(payload) assert duplicate_res.status_code == 409, duplicate_res.text delete_res = client.delete_signing_entity_profit_center(junction_id) assert delete_res.status_code == 204, delete_res.text restore_res = client.post_signing_entity_profit_center(payload) assert restore_res.status_code == 201, restore_res.text assert restore_res.json()['signing_entity_sap_profit_center_id'] == junction_id assert restore_res.json()['deleted_at'] is None cleanup_res = client.delete_signing_entity_profit_center(junction_id) assert cleanup_res.status_code == 204, cleanup_res.text @pytest.mark.jira('ACC-10417') def test_delete_signing_entity_profit_center_with_active_contract_returns_409( basic_headers: dict[str, str], ) -> None: """DELETE is blocked with 409 if any contract still references the (SE, PC) pair.""" client = ows_abacus_contract_api_client(basic_headers) _contract_id, _body, _account_id = generic_helper.create_account_and_contract() # The helper seeds (PE, PC, SE, junction) and creates a contract pointing at them. # Pull the junction id by reading the contract's SE+PC back out. from abacus_common_logic.connectors.database import db row = db.engine.execute( f'SELECT reference_signing_entity_id, reference_sap_profit_center_id ' f'FROM contract WHERE contract_id = {_contract_id}' ).fetchone() junction_id = generic_helper.get_signing_entity_profit_center_id(row[0], row[1]) assert junction_id is not None, 'helper should have seeded a junction row' delete_res = client.delete_signing_entity_profit_center(junction_id) assert delete_res.status_code == 409, delete_res.text @pytest.mark.jira('ACC-10417') def test_delete_signing_entity_profit_center_unknown_id_returns_404( basic_headers: dict[str, str], ) -> None: """DELETE on a non-existent junction id -> 404.""" client = ows_abacus_contract_api_client(basic_headers) res = client.delete_signing_entity_profit_center(99999999) assert res.status_code == 404, res.text @pytest.mark.jira('ACC-10416') def test_post_signing_entity_profit_center_unknown_se_returns_400( basic_headers: dict[str, str], ) -> None: """POST with a non-existent SE -> 400.""" client = ows_abacus_contract_api_client(basic_headers) _se_id, pc_id = ( generic_helper.create_signing_entity_and_profit_center_without_junction() ) res = client.post_signing_entity_profit_center( { 'reference_signing_entity_id': 99999999, 'reference_sap_profit_center_id': pc_id, } ) assert res.status_code == 400, res.text