|
| 1 | +"""AdCP 3.2 can traffic existing creatives without uploading assets (#1545).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pydantic import ValidationError |
| 10 | + |
| 11 | +from adcp.server.base import ADCPHandler, ToolContext |
| 12 | +from adcp.server.mcp_tools import create_tool_caller |
| 13 | +from adcp.types import CreativeAsset, SyncCreativesRequest |
| 14 | +from adcp.validation import ValidationHookConfig, validate_request |
| 15 | + |
| 16 | +_BASE = { |
| 17 | + "adcp_version": "3.2", |
| 18 | + "idempotency_key": "assignment-only-sync-0001", |
| 19 | + "account": {"account_id": "acct_1"}, |
| 20 | +} |
| 21 | +_ASSIGNMENTS = {"assignments": [{"creative_id": "cr_1", "package_id": "pkg_1"}]} |
| 22 | +_MUTATIONS = [ |
| 23 | + pytest.param(_ASSIGNMENTS, id="assignments"), |
| 24 | + *[ |
| 25 | + pytest.param( |
| 26 | + { |
| 27 | + "assignment_operations": [ |
| 28 | + { |
| 29 | + "operation": operation, |
| 30 | + "creative_id": "cr_1", |
| 31 | + "package_id": "pkg_1", |
| 32 | + **({"replaces_creative_id": "cr_old"} if operation == "replace" else {}), |
| 33 | + } |
| 34 | + ] |
| 35 | + }, |
| 36 | + id=operation, |
| 37 | + ) |
| 38 | + for operation in ("assign", "unassign", "replace") |
| 39 | + ], |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("mutation", _MUTATIONS) |
| 44 | +@pytest.mark.parametrize("json_input", [False, True], ids=["python", "json"]) |
| 45 | +def test_public_model_accepts_assignment_only_sync( |
| 46 | + mutation: dict[str, Any], json_input: bool |
| 47 | +) -> None: |
| 48 | + payload = {**_BASE, **mutation} |
| 49 | + request = ( |
| 50 | + SyncCreativesRequest.model_validate_json(json.dumps(payload)) |
| 51 | + if json_input |
| 52 | + else SyncCreativesRequest.model_validate(payload) |
| 53 | + ) |
| 54 | + |
| 55 | + assert request.creatives is None |
| 56 | + wire = request.model_dump(mode="json", exclude_unset=True) |
| 57 | + assert wire == payload |
| 58 | + assert validate_request("sync_creatives", wire, version="3.2").valid |
| 59 | + |
| 60 | + |
| 61 | +def test_empty_creatives_are_rejected_even_with_assignments() -> None: |
| 62 | + with pytest.raises(ValidationError) as caught: |
| 63 | + SyncCreativesRequest.model_validate({**_BASE, **_ASSIGNMENTS, "creatives": []}) |
| 64 | + assert caught.value.errors()[0]["loc"] == ("creatives",) |
| 65 | + assert caught.value.errors()[0]["type"] == "too_short" |
| 66 | + |
| 67 | + |
| 68 | +def test_sync_still_requires_assets_or_assignments() -> None: |
| 69 | + with pytest.raises(ValidationError) as caught: |
| 70 | + SyncCreativesRequest.model_validate(_BASE) |
| 71 | + assert caught.value.errors()[0]["type"] == "anyOf" |
| 72 | + |
| 73 | + |
| 74 | +def test_asset_sync_still_parses_canonical_creatives() -> None: |
| 75 | + request = SyncCreativesRequest.model_validate( |
| 76 | + { |
| 77 | + **_BASE, |
| 78 | + "creatives": [ |
| 79 | + {"creative_id": "cr_1", "name": "Banner", "format_kind": "image", "assets": {}} |
| 80 | + ], |
| 81 | + } |
| 82 | + ) |
| 83 | + assert request.creatives is not None |
| 84 | + assert isinstance(request.creatives[0], CreativeAsset) |
| 85 | + assert request.creatives[0].format_kind == "image" |
| 86 | + |
| 87 | + |
| 88 | +def test_public_schema_allows_omitting_creatives() -> None: |
| 89 | + assert "creatives" not in SyncCreativesRequest.model_json_schema()["required"] |
| 90 | + |
| 91 | + |
| 92 | +class _AssignmentHandler(ADCPHandler[Any]): |
| 93 | + adcp_capabilities = {"media_buy": {"features": {"canonical_creatives": True}}} |
| 94 | + |
| 95 | + def __init__(self) -> None: |
| 96 | + self.received: SyncCreativesRequest | None = None |
| 97 | + |
| 98 | + async def sync_creatives( |
| 99 | + self, params: SyncCreativesRequest, context: ToolContext | None = None |
| 100 | + ) -> dict[str, Any]: |
| 101 | + self.received = params |
| 102 | + return {"creatives": []} |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize("mutation", _MUTATIONS) |
| 106 | +async def test_assignment_only_sync_reaches_typed_handler(mutation: dict[str, Any]) -> None: |
| 107 | + handler = _AssignmentHandler() |
| 108 | + caller = create_tool_caller( |
| 109 | + handler, "sync_creatives", validation=ValidationHookConfig(requests="strict") |
| 110 | + ) |
| 111 | + |
| 112 | + result = await caller({**_BASE, **mutation}) |
| 113 | + |
| 114 | + assert result["creatives"] == [] |
| 115 | + assert isinstance(handler.received, SyncCreativesRequest) |
| 116 | + assert handler.received.creatives is None |
| 117 | + assert handler.received.model_dump(mode="json", exclude_unset=True) == {**_BASE, **mutation} |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize("version", ["3.0", "3.1"]) |
| 121 | +def test_older_version_schemas_still_require_creatives(version: str) -> None: |
| 122 | + payload = {**_BASE, **_ASSIGNMENTS} |
| 123 | + payload.pop("adcp_version") |
| 124 | + outcome = validate_request("sync_creatives", payload, version=version) |
| 125 | + |
| 126 | + assert not outcome.valid |
| 127 | + assert any( |
| 128 | + issue.pointer == "/creatives" and issue.keyword == "required" for issue in outcome.issues |
| 129 | + ) |
0 commit comments