-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_upstream_helpers.py
More file actions
439 lines (351 loc) · 16.1 KB
/
Copy pathtest_upstream_helpers.py
File metadata and controls
439 lines (351 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"""Tests for the upstream HTTP client and translation map helpers.
Mirrors the JS test/upstream-helpers.test.js coverage with two
adaptations for the Python shape:
* Methods return parsed JSON directly (``dict`` or ``None``), not a
``{status, body}`` envelope.
* Non-2xx responses raise :class:`AdcpError` with spec-conformant
codes (AUTH_REQUIRED / PERMISSION_DENIED / MEDIA_BUY_NOT_FOUND /
RATE_LIMITED / SERVICE_UNAVAILABLE / INVALID_REQUEST).
"""
from __future__ import annotations
import json
import httpx
import pytest
import respx
from adcp.decisioning import (
ApiKey,
DynamicBearer,
NoAuth,
StaticBearer,
TranslationMap,
UpstreamHttpClient,
create_translation_map,
create_upstream_http_client,
)
from adcp.decisioning.types import AdcpError
BASE = "https://upstream.example.com"
# ---------------------------------------------------------------------------
# create_translation_map
# ---------------------------------------------------------------------------
class TestTranslationMap:
def setup_method(self) -> None:
self.channel_map: TranslationMap[str, str] = create_translation_map(
{
"olv": "video",
"ctv": "ctv",
"display": "display",
"streaming_audio": "audio",
}
)
def test_to_upstream_returns_b_side(self) -> None:
assert self.channel_map.to_upstream("olv") == "video"
assert self.channel_map.to_upstream("ctv") == "ctv"
def test_to_adcp_returns_a_side(self) -> None:
assert self.channel_map.to_adcp("video") == "olv"
assert self.channel_map.to_adcp("audio") == "streaming_audio"
def test_to_upstream_raises_on_unknown(self) -> None:
with pytest.raises(KeyError) as exc_info:
self.channel_map.to_upstream("unknown")
message = str(exc_info.value)
assert "'unknown'" in message
assert "'olv'" in message
assert "'ctv'" in message
assert "'display'" in message
assert "'streaming_audio'" in message
def test_to_adcp_raises_on_unknown(self) -> None:
with pytest.raises(KeyError) as exc_info:
self.channel_map.to_adcp("unknown_upstream")
message = str(exc_info.value)
assert "'unknown_upstream'" in message
assert "'video'" in message
assert "'audio'" in message
def test_has_adcp(self) -> None:
assert self.channel_map.has_adcp("olv") is True
assert self.channel_map.has_adcp("video") is False
assert self.channel_map.has_adcp("missing") is False
def test_has_upstream(self) -> None:
assert self.channel_map.has_upstream("video") is True
assert self.channel_map.has_upstream("olv") is False
assert self.channel_map.has_upstream("missing") is False
def test_collision_detected_at_construction(self) -> None:
with pytest.raises(ValueError, match="translation collision"):
create_translation_map({"a": "X", "b": "X"})
def test_default_upstream_fallback(self) -> None:
m: TranslationMap[str, str] = create_translation_map(
{"olv": "video"}, default_upstream="STANDARD"
)
assert m.to_upstream("olv") == "video"
assert m.to_upstream("unknown") == "STANDARD"
def test_default_adcp_fallback(self) -> None:
m: TranslationMap[str, str] = create_translation_map(
{"olv": "video"}, default_adcp="display"
)
assert m.to_adcp("video") == "olv"
assert m.to_adcp("unknown_upstream") == "display"
# ---------------------------------------------------------------------------
# create_upstream_http_client — happy paths and auth
# ---------------------------------------------------------------------------
@respx.mock
async def test_get_returns_parsed_json_with_static_bearer() -> None:
route = respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={"ok": True}))
client = create_upstream_http_client(BASE, auth=StaticBearer(token="tok_123"))
result = await client.get("/items")
assert result == {"ok": True}
assert route.called
assert route.calls.last.request.headers["Authorization"] == "Bearer tok_123"
await client.aclose()
@respx.mock
async def test_get_dynamic_bearer_called_per_request() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json=[]))
calls: list[object] = []
async def get_token(ctx: object) -> str:
calls.append(ctx)
return "fresh_token"
client = create_upstream_http_client(BASE, auth=DynamicBearer(get_token=get_token))
await client.get("/items")
assert calls == [None]
request = respx.calls.last.request
assert request.headers["Authorization"] == "Bearer fresh_token"
await client.aclose()
@respx.mock
async def test_get_api_key_header_injected() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
client = create_upstream_http_client(
BASE, auth=ApiKey(header_name="X-Api-Key", key="secret_key")
)
await client.get("/items")
assert respx.calls.last.request.headers["X-Api-Key"] == "secret_key"
await client.aclose()
@respx.mock
async def test_get_no_auth_sends_no_authorization_header() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
client = create_upstream_http_client(BASE, auth=NoAuth())
await client.get("/items")
assert "Authorization" not in respx.calls.last.request.headers
await client.aclose()
@respx.mock
async def test_default_auth_is_no_auth() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
client = create_upstream_http_client(BASE) # no auth=
await client.get("/items")
assert "Authorization" not in respx.calls.last.request.headers
await client.aclose()
@respx.mock
async def test_query_params_serialized_and_none_dropped() -> None:
route = respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json=[]))
client = create_upstream_http_client(BASE)
await client.get("/items", params={"limit": 10, "q": "hello world", "skip_me": None})
assert route.called
url = str(respx.calls.last.request.url)
assert "limit=10" in url
# httpx URL-encodes the space as +
assert "q=hello+world" in url or "q=hello%20world" in url
assert "skip_me" not in url
await client.aclose()
@respx.mock
async def test_post_sends_json_body_and_default_headers() -> None:
route = respx.post(f"{BASE}/items").mock(return_value=httpx.Response(201, json={"id": "x"}))
client = create_upstream_http_client(
BASE,
auth=NoAuth(),
default_headers={"X-Tenant": "tenant_1"},
)
result = await client.post("/items", json={"name": "test"})
assert result == {"id": "x"}
assert route.called
request = respx.calls.last.request
assert request.method == "POST"
assert request.headers["X-Tenant"] == "tenant_1"
assert request.headers["Content-Type"] == "application/json"
assert json.loads(request.content) == {"name": "test"}
await client.aclose()
@respx.mock
async def test_put_sends_json_body() -> None:
respx.put(f"{BASE}/items/x").mock(
return_value=httpx.Response(200, json={"id": "x", "name": "updated"})
)
client = create_upstream_http_client(BASE)
result = await client.put("/items/x", json={"name": "updated"})
assert result == {"id": "x", "name": "updated"}
request = respx.calls.last.request
assert request.method == "PUT"
assert json.loads(request.content) == {"name": "updated"}
await client.aclose()
@respx.mock
async def test_delete_sends_correct_method() -> None:
respx.delete(f"{BASE}/items/1").mock(return_value=httpx.Response(204))
client = create_upstream_http_client(BASE)
result = await client.delete("/items/1")
assert result == {} # 204 → empty dict
assert respx.calls.last.request.method == "DELETE"
await client.aclose()
@respx.mock
async def test_per_call_headers_override_defaults() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
client = create_upstream_http_client(
BASE, default_headers={"X-Tenant": "default", "X-Other": "stays"}
)
await client.get("/items", headers={"X-Tenant": "override"})
headers = respx.calls.last.request.headers
assert headers["X-Tenant"] == "override"
assert headers["X-Other"] == "stays"
await client.aclose()
@respx.mock
async def test_default_headers_merge_with_auth() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
client = create_upstream_http_client(
BASE,
auth=StaticBearer(token="abc"),
default_headers={"X-Tenant": "t1"},
)
await client.get("/items")
headers = respx.calls.last.request.headers
assert headers["Authorization"] == "Bearer abc"
assert headers["X-Tenant"] == "t1"
await client.aclose()
# ---------------------------------------------------------------------------
# 404 → None vs MEDIA_BUY_NOT_FOUND
# ---------------------------------------------------------------------------
@respx.mock
async def test_get_404_returns_none_when_treat_404_as_none_default() -> None:
respx.get(f"{BASE}/items/missing").mock(return_value=httpx.Response(404, text="not found"))
client = create_upstream_http_client(BASE)
result = await client.get("/items/missing")
assert result is None
await client.aclose()
@respx.mock
async def test_get_404_raises_media_buy_not_found_when_disabled() -> None:
respx.get(f"{BASE}/items/missing").mock(return_value=httpx.Response(404, text="not found"))
client = create_upstream_http_client(BASE, treat_404_as_none=False)
with pytest.raises(AdcpError) as exc_info:
await client.get("/items/missing")
assert exc_info.value.code == "MEDIA_BUY_NOT_FOUND"
await client.aclose()
@respx.mock
async def test_get_404_with_custom_not_found_code() -> None:
respx.get(f"{BASE}/creatives/x").mock(return_value=httpx.Response(404))
client = create_upstream_http_client(BASE, treat_404_as_none=False)
with pytest.raises(AdcpError) as exc_info:
await client.get("/creatives/x", not_found_code="CREATIVE_NOT_FOUND")
assert exc_info.value.code == "CREATIVE_NOT_FOUND"
await client.aclose()
@respx.mock
async def test_post_404_always_raises() -> None:
# POST is not a lookup; treat_404_as_none doesn't apply.
respx.post(f"{BASE}/items").mock(return_value=httpx.Response(404))
client = create_upstream_http_client(BASE) # treat_404_as_none=True by default
with pytest.raises(AdcpError) as exc_info:
await client.post("/items", json={})
assert exc_info.value.code == "MEDIA_BUY_NOT_FOUND"
await client.aclose()
# ---------------------------------------------------------------------------
# Error projection — status → AdcpError code
# ---------------------------------------------------------------------------
@respx.mock
async def test_401_raises_auth_required() -> None:
respx.get(f"{BASE}/x").mock(return_value=httpx.Response(401, text="unauthorized"))
client = create_upstream_http_client(BASE)
with pytest.raises(AdcpError) as exc_info:
await client.get("/x")
assert exc_info.value.code == "AUTH_REQUIRED"
assert exc_info.value.recovery == "terminal"
await client.aclose()
@respx.mock
async def test_403_raises_permission_denied() -> None:
respx.get(f"{BASE}/x").mock(return_value=httpx.Response(403, text="forbidden"))
client = create_upstream_http_client(BASE)
with pytest.raises(AdcpError) as exc_info:
await client.get("/x")
assert exc_info.value.code == "PERMISSION_DENIED"
await client.aclose()
@respx.mock
async def test_429_raises_rate_limited_transient() -> None:
respx.get(f"{BASE}/x").mock(return_value=httpx.Response(429, text="too many"))
client = create_upstream_http_client(BASE)
with pytest.raises(AdcpError) as exc_info:
await client.get("/x")
assert exc_info.value.code == "RATE_LIMITED"
assert exc_info.value.recovery == "transient"
await client.aclose()
@respx.mock
async def test_500_raises_service_unavailable() -> None:
respx.get(f"{BASE}/x").mock(return_value=httpx.Response(500, text="oops"))
client = create_upstream_http_client(BASE)
with pytest.raises(AdcpError) as exc_info:
await client.get("/x")
assert exc_info.value.code == "SERVICE_UNAVAILABLE"
assert exc_info.value.recovery == "transient"
await client.aclose()
@respx.mock
async def test_400_raises_invalid_request_correctable() -> None:
respx.get(f"{BASE}/x").mock(return_value=httpx.Response(400, text="bad"))
client = create_upstream_http_client(BASE)
with pytest.raises(AdcpError) as exc_info:
await client.get("/x")
assert exc_info.value.code == "INVALID_REQUEST"
assert exc_info.value.recovery == "retry_with_changes"
await client.aclose()
# ---------------------------------------------------------------------------
# auth_context passthrough
# ---------------------------------------------------------------------------
@respx.mock
async def test_dynamic_bearer_receives_auth_context() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
captured: list[object] = []
async def get_token(ctx: object) -> str:
captured.append(ctx)
return "tok_for_acme"
client = create_upstream_http_client(BASE, auth=DynamicBearer(get_token=get_token))
await client.get("/items", auth_context={"operator_id": "acme"})
assert captured == [{"operator_id": "acme"}]
assert respx.calls.last.request.headers["Authorization"] == "Bearer tok_for_acme"
await client.aclose()
@respx.mock
async def test_dynamic_bearer_per_call_routing() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
respx.post(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
keys = {"acme": "tok_acme", "globex": "tok_globex"}
async def get_token(ctx: object) -> str:
if isinstance(ctx, dict):
return keys.get(ctx.get("operator_id", ""), "master")
return "master"
client = create_upstream_http_client(BASE, auth=DynamicBearer(get_token=get_token))
await client.get("/items", auth_context={"operator_id": "acme"})
await client.post("/items", json={"x": 1}, auth_context={"operator_id": "globex"})
auths = [c.request.headers["Authorization"] for c in respx.calls]
assert auths == ["Bearer tok_acme", "Bearer tok_globex"]
await client.aclose()
@respx.mock
async def test_dynamic_bearer_principal_passthrough() -> None:
respx.get(f"{BASE}/items").mock(return_value=httpx.Response(200, json={}))
async def get_token(ctx: object) -> str:
if isinstance(ctx, dict):
principal = ctx.get("principal")
if isinstance(principal, str):
return principal
return "fallback"
client = create_upstream_http_client(BASE, auth=DynamicBearer(get_token=get_token))
await client.get("/items", auth_context={"principal": "caller_token_xyz"})
assert respx.calls.last.request.headers["Authorization"] == "Bearer caller_token_xyz"
await client.aclose()
# ---------------------------------------------------------------------------
# Pool reuse + context manager
# ---------------------------------------------------------------------------
@respx.mock
async def test_async_context_manager_closes_client() -> None:
respx.get(f"{BASE}/x").mock(return_value=httpx.Response(200, json={}))
async with create_upstream_http_client(BASE) as client:
assert isinstance(client, UpstreamHttpClient)
await client.get("/x")
assert client._client is not None # type: ignore[union-attr]
assert client._client is None # type: ignore[union-attr]
@respx.mock
async def test_pool_reused_across_calls() -> None:
respx.get(f"{BASE}/a").mock(return_value=httpx.Response(200, json={}))
respx.get(f"{BASE}/b").mock(return_value=httpx.Response(200, json={}))
client = create_upstream_http_client(BASE)
await client.get("/a")
underlying = client._client # type: ignore[union-attr]
await client.get("/b")
assert client._client is underlying # type: ignore[union-attr]
await client.aclose()