"""Integration test fixtures — mock GraphQL + API server.""" import itertools import json import pytest from pytest_httpserver import HTTPServer MOCK_SIGNING_ENTITIES_GQL = { 'items': [ { 'referenceSigningEntityId': '1', 'legalName': 'Acme', 'companyCode': '0001', 'address': None, 'companyRegistrationNumber': None, 'vatNumber': None, }, { 'referenceSigningEntityId': '2', 'legalName': 'Globex', 'companyCode': '0002', 'address': None, 'companyRegistrationNumber': None, 'vatNumber': None, }, ] } MOCK_RUN_CONTROLLERS_GQL = { 'items': [ { 'runControllerId': 10, 'runControllerName': 'ctrl-a', 'contractType': 'distribution', }, ] } def _make_graphql_handler(): """Create a GraphQL handler with its own contract ID counter.""" counter = itertools.count(42) def _handle_graphql(request): body = request.get_json() query = body.get('query', '') if 'abacusReferenceSigningEntities' in query: return json.dumps( {'data': {'abacusReferenceSigningEntities': MOCK_SIGNING_ENTITIES_GQL}} ) if 'abacusRunControllers' in query: return json.dumps( {'data': {'abacusRunControllers': MOCK_RUN_CONTROLLERS_GQL}} ) if 'abacusCreateContractWithLifecycles' in query: contract_id = next(counter) variables = body.get('variables', {}) contract_input = variables.get('input', {}).get('contract', {}) return json.dumps( { 'data': { 'abacusCreateContractWithLifecycles': { 'contractId': contract_id, 'contractName': contract_input.get('contractName', ''), 'contractType': contract_input.get('contractType', ''), } } } ) if 'abacusUpdateRunControllerContracts' in query: variables = body.get('variables', {}) inp = variables.get('input', {}) return json.dumps( { 'data': { 'abacusUpdateRunControllerContracts': { 'contractId': inp.get('contractId'), 'runControllerContractId': 1, 'runControllerId': inp.get('runControllerId'), } } } ) return json.dumps({'errors': [{'message': 'Unknown query'}]}) return _handle_graphql def mock_graphql(server: HTTPServer) -> None: """Register persistent GraphQL handler for all operations.""" server.expect_request('/graphql', method='POST').respond_with_handler( _make_graphql_handler() ) @pytest.fixture() def api_server(httpserver: HTTPServer): """Raw HTTPServer — tests register their own endpoints.""" return httpserver @pytest.fixture() def api_url(api_server: HTTPServer) -> str: """Base URL of the mock API server.""" return api_server.url_for('').rstrip('/') @pytest.fixture() def config_file(tmp_path, api_url): """Write a valid config.json pointing at the mock server.""" cfg = { 'bearer_token': 'test-token', 'graphql_url': f'{api_url}/graphql', 'profile_uuid': 'test-uuid', } path = tmp_path / 'config.json' path.write_text(json.dumps(cfg)) return str(path) @pytest.fixture() def csv_file(tmp_path): """Write a sample CSV with two valid rows.""" content = ( 'Account ID,Contract Name,Contract Type,' 'Current Period Start Date,Signing Entity,Run Controller\n' '100,Contract A,Distribution,01/01/2026,acme,ctrl-a\n' '200,Contract B,Neighbouring Rights,02/01/2026,globex,\n' ) path = tmp_path / 'input.csv' path.write_text(content) return str(path)