"""Render synthetic API responses for the generated Orval/Zod contract gate.""" import json import os from importlib import import_module from io import StringIO from pathlib import Path from unittest.mock import patch from uuid import uuid4 import pytest from apps.core.models import BackgroundJob from apps.parsers.budget_registry import normalize_budget_record from apps.parsers.checko_sections import collect_checko_section from apps.parsers.fssp_csv import FSSP_SOURCE, GENERAL_DATASET, iter_fssp_records from apps.parsers.models import ParserSourceArtifact from apps.parsers.sme_support import support_payload from apps.parsers.sro_membership import normalize_sro_membership from django.urls import reverse from django.utils import timezone from lxml import etree from organizations.models import Organization, OrganizationSourceRecord from organizations.source_groups import get_source_group_descriptor from rest_framework.test import APIClient from tests.apps.organizations.test_exchange_contract import exchange_payload from tests.apps.parsers.test_checko_sections import client_with, fixture from tests.apps.parsers.test_registry_payload_contract import full_budget_fixture from tests.apps.parsers.test_registry_snapshots import _xml from tests.apps.user.factories import UserFactory def _create_record(organization, source, normalized): descriptor = get_source_group_descriptor(source) extension = descriptor.extension_model.objects.create(organization=organization) ParserSourceArtifact.objects.create( source=source, load_batch=9001, status=ParserSourceArtifact.Status.PUBLISHED, source_published_at="2026-08-15", version="4.04" if source == "fns_sme_support_recipients" else "2026-08-15", sha256="a" * 64, original_name="synthetic-contract-fixture.zip", published_count=1, ) return OrganizationSourceRecord.objects.create( extension=extension, source=source, record_type=descriptor.record_type, external_id=f"synthetic-{source}", load_batch=9001, **normalized, ) def _canonical_records(): organization = Organization.objects.create( name="АО Синтетическая организация", full_name="Акционерное общество Синтетическая организация", inn="1234567890", ogrn="1027700132195", okpo="00123456", directory_imported_at=timezone.now(), opk_registry_membership=False, ) raw_budget = full_budget_fixture() # Unknown nested keys, leading zeroes, booleans and nulls must survive Zod. raw_budget["future_block"] = [ {"future_key": {"code": "0007", "nested": [None, False, 0, {"keep": True}]}} ] budget = _create_record( organization, "budget_ubpandnubp", normalize_budget_record(raw_budget) ) xml = etree.fromstring( # noqa: S320 - local synthetic fixture without entities _xml(numbers=("synthetic-support",), units=("1", "2", "3", "4", "5")) ) payload, amount = support_payload( "synthetic-document", xml.find("Документ/СвПредПод"), "2026-08-15" ) sme = _create_record( organization, "fns_sme_support_recipients", { "title": "Синтетическая мера поддержки", "record_date": payload["decision_date"], "amount": amount, "status": "", "url": "https://www.nalog.gov.ru/opendata/7707329152-rsmppp/", "payload": payload, }, ) sro = _create_record( organization, "sro_membership_check", normalize_sro_membership( { "name": organization.name, "full_name": organization.full_name, "inn": organization.inn, "ogrn": organization.ogrn, "region": "Синтетический регион", "sro_name": "Синтетическая СРО", "membership_status_raw": "Является членом", }, organization, sro_id="90001", sro_url="https://www.reestr-sro.ru/test-fixtures/sro-id-90001/", source_url="https://www.reestr-sro.ru/test-fixtures/lookup/", source_registry_date="2026-08-15", admitted=None, missing_reason="not_found", resolution="row_link", lookup_key="ogrn", lookup_value=organization.ogrn, ), ) fssp_org = Organization.objects.create( name="АО Проверка", inn="7707083893", ogrn="1027700132195", okpo="12345678", directory_imported_at=timezone.now(), opk_registry_membership=False, ) csv_fixture = Path(__file__).parents[2] / "fixtures/fssp/general.csv" row = next( iter_fssp_records(StringIO(csv_fixture.read_text()), dataset=GENERAL_DATASET) ) fssp = _create_record( fssp_org, FSSP_SOURCE, { "title": row.title, "record_date": row.record_date, "amount": row.amount, "status": row.status, "url": row.url, "payload": row.payload, }, ) sections = [] for source in ("checko_leasing", "checko_messages", "checko_licenses"): message_page = fixture("messages-page-1.json") message_page["data"]["Записи"][0]["Публикатор"] = None client = ( client_with(fixture("license.json")) if source == "checko_licenses" else client_with(message_page, fixture("messages-page-2.json")) ) row = next(collect_checko_section(client, organization=fssp_org, source=source)) if source == "checko_leasing": from apps.parsers.leasing_details import parse_leasing_html from tests.apps.parsers.test_leasing_details import URL, page row.payload["lease_details"] = parse_leasing_html(page(), url=URL) row.payload["lease_details_status"] = { "status": "loaded", "checked_at": timezone.now().isoformat(), } row.payload["upstream"]["future_nested"] = { "keep": [None, False, 0, {"unknown": "0007"}] } sections.append( _create_record( fssp_org, source, { "title": row.title, "record_date": row.record_date, "amount": None, "status": row.status, "url": row.url, "payload": row.payload, }, ) ) exchange = _create_record( fssp_org, "stock_exchange_activity", { "title": "Synthetic instrument", "record_date": "2026-10-05", "amount": None, "status": "", "url": "https://iss.moex.com/iss/securities/FIXTURE1.json", "payload": exchange_payload(), }, ) spb = OrganizationSourceRecord.objects.create( extension=exchange.extension, source="stock_exchange_activity", record_type="exchange_instrument", external_id="spb:FIXTURE1", load_batch=9001, title="Synthetic SPB instrument", url="https://spbexchange.ru/listing/securities/list/", payload=exchange_payload("spb"), ) return [budget, sme, sro, fssp, *sections, exchange, spb], raw_budget @pytest.mark.django_db def test_source_record_runtime_fixtures(): client = APIClient() client.force_authenticate(UserFactory.create_user()) records, raw_budget = _canonical_records() list_url = reverse("api_v2:organizations:organization-source-records-list") cases = [] def capture(name, operation, response, status=200): assert response.status_code == status, (name, response.status_code) # Rendered JSON is the actual wire representation, not DRF's .data. body = response.json() cases.append( {"name": name, "operation": operation, "status": status, "body": body} ) return body mixed = capture("mixed-list", "list", client.get(list_url)) assert len(mixed["data"]) == 9 assert mixed["meta"]["pagination"]["total_count"] == 9 details = {} listed_sources = set() for record in records: if record.source not in listed_sources: listing = capture( f"{record.source}-list", "list", client.get(list_url, {"source": record.source}), ) assert len(listing["data"]) == ( 2 if record.source == "stock_exchange_activity" else 1 ) listed_sources.add(record.source) detail_url = reverse( "api_v2:organizations:organization-source-records-detail", kwargs={"uid": record.uid}, ) details[record.source] = capture( f"{record.source}-spb-detail" if record is records[-1] else f"{record.source}-detail", "detail", client.get(detail_url), ) budget_payload = details["budget_ubpandnubp"]["payload"] assert budget_payload["upstream"] == raw_budget assert ( budget_payload["unclassified_blocks"]["future_block"] == raw_budget["future_block"] ) assert len(details["fns_sme_support_recipients"]["payload"]["support_sizes"]) == 5 assert details["sro_membership_check"]["payload"]["admission_date"] is None empty = capture( "empty-list", "list", client.get(list_url, {"search": "absent-fixture-value"}) ) assert empty["data"] == [] assert empty["meta"]["pagination"]["total_count"] == 0 assert empty["meta"]["pagination"]["total_pages"] == 0 capture( "invalid-list", "list", client.get(list_url, {"page_size": "broken"}), 400, ) for name, uid, status in ( ("invalid-detail", "broken", 400), ("missing-detail", str(uuid4()), 404), ): capture( name, "detail", client.get( reverse( "api_v2:organizations:organization-source-records-detail", kwargs={"uid": uid}, ) ), status, ) from core.celery import app as celery_app import_module("apps.parsers.tasks") sources = [ ("fssp_enforcement_proceedings", "fssp-enforcement-proceedings"), ("checko_leasing", "checko-leasing"), ("checko_messages", "checko-messages"), ("checko_licenses", "checko-licenses"), ("stock_exchange_activity", "stock-exchange-activity"), ] for source, slug in sources: capture( f"{source}-refresh-forbidden", "refresh", client.post(f"/api/v1/sources/{slug}/refresh/", {}, format="json"), 403, ) client.force_authenticate(UserFactory.create_user(is_staff=True)) for source, slug in sources: refresh_url = f"/api/v1/sources/{slug}/refresh/" capture( f"{source}-refresh-invalid", "refresh", client.post( refresh_url, {"params": {"file_url": "https://foreign.invalid"}}, format="json", ), 400, ) task = celery_app.tasks[f"parsers.{source}.refresh"] with patch.object(task, "apply_async"): capture( f"{source}-refresh-accepted", "refresh", client.post(refresh_url, {}, format="json"), 202, ) capture( f"{source}-refresh-conflict", "refresh", client.post(refresh_url, {}, format="json"), 409, ) capture( "missing-refresh", "refresh", client.post("/api/v1/sources/unknown-source/refresh/", {}, format="json"), 404, ) options_url = list_url + "filter-options/" capture( "message-type-options", "options", client.get(options_url, {"source_group": "leasing", "field": "message_type"}), 200, ) capture( "message-type-options-invalid", "options", client.get(options_url, {"source_group": "unknown", "field": "message_type"}), 400, ) user = UserFactory.create_superuser() client.force_authenticate(user) for state in ("failed", "success", "skipped"): job = BackgroundJob.objects.create( task_id=f"contract-{state}", task_name="test", user_id=user.id ) if state == "failed": job.fail("HTTPError: https://user:secret@example.test/?token=secret") else: job.complete(result={"status": state}) capture(f"job-{state}", "job", client.get(f"/api/v1/jobs/{job.task_id}/"), 200) # Ordinary pytest runs create no artifact. CI opts in with an explicit path. destination = os.environ.get("OPENAPI_RUNTIME_FIXTURES") if destination: Path(destination).write_text( json.dumps({"version": 1, "cases": cases}, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", )