Repository navigation
Expand file tree
/
Copy pathtest_payment_servers.py
More file actions
749 lines (587 loc) · 30.9 KB
/
Copy pathtest_payment_servers.py
File metadata and controls
749 lines (587 loc) · 30.9 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
"""Live-API tests for create_x402_server + create_mppx_server factories.
The prior mock-based suite tracked x402 2.8 / pympp pre-release internal layout
(``x402.servers``, ``HTTPFacilitatorClient``, ``Mppx``, ``charge(currency=...)``).
x402 2.9 + pympp 0.6 shipped breaking refactors so the mocks no longer reflect
reality. We now run against the actually-installed peer deps — the tests skip
when the deps aren't present so the suite still runs in minimal envs.
"""
from __future__ import annotations
import importlib.util
from types import SimpleNamespace
from typing import Any
import pytest
from agentscore_commerce.payment import (
StripeRailSpec,
TempoRailSpec,
TempoSessionRailSpec,
create_mppx_server,
create_x402_server,
)
def _module_installed(name: str) -> bool:
# find_spec raises ModuleNotFoundError for a dotted name whose parent
# package is absent (e.g. "mpp.server" when pympp isn't installed) rather
# than returning None; treat that as "not installed" so minimal envs skip.
try:
return importlib.util.find_spec(name) is not None
except ModuleNotFoundError:
return False
_X402_INSTALLED = _module_installed("x402")
_MPPX_INSTALLED = _module_installed("mpp.server")
_TEMPO_INSTALLED = _module_installed("mpp.methods.tempo")
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_returns_resource_server() -> None:
"""create_x402_server with no rails returns a bare resource server."""
server = await create_x402_server(facilitator="http", initialize=False)
assert type(server).__name__ == "x402ResourceServer"
assert hasattr(server, "verify_payment")
assert hasattr(server, "settle_payment")
assert hasattr(server, "register")
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_registers_base_sepolia_scheme() -> None:
"""Base Sepolia rail registers the ExactEvmScheme on the right network."""
server = await create_x402_server(
facilitator="http",
rails=["x402-base-sepolia"],
initialize=False,
)
# x402 2.9 keeps registrations in `_schemes: {network: {scheme_name: instance}}`.
assert "eip155:84532" in server._schemes
assert "exact" in server._schemes["eip155:84532"]
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_coinbase_facilitator_wires_cdp_jwt(monkeypatch: pytest.MonkeyPatch) -> None:
"""``facilitator="coinbase"`` builds an HTTPFacilitatorClient pointed at the
CDP URL with a per-endpoint JWT auth provider — not a bare in-process facilitator.
This is the regression that 1.3.2 fixes: 1.3.0 + 1.3.1 both passed an empty
``x402Facilitator()`` instance and silently failed downstream when
``build_payment_requirements`` looked up the supported map.
"""
cdp_sdk_installed = importlib.util.find_spec("cdp.auth.utils.jwt") is not None
if not cdp_sdk_installed:
pytest.skip("cdp-sdk not installed (install via the `coinbase` extra)")
monkeypatch.setenv("CDP_API_KEY_ID", "test-key-id")
monkeypatch.setenv("CDP_API_KEY_SECRET", "test-key-secret")
server = await create_x402_server(
facilitator="coinbase",
rails=["x402-base-mainnet"],
initialize=False,
)
facilitator_clients = server._facilitator_clients
assert len(facilitator_clients) == 1
facilitator = facilitator_clients[0]
assert type(facilitator).__name__ == "HTTPFacilitatorClient"
assert facilitator.url == "https://api.cdp.coinbase.com/platform/v2/x402"
assert facilitator._auth_provider is not None
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_coinbase_without_creds_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""``facilitator="coinbase"`` with no CDP creds raises a clear ValueError."""
monkeypatch.delenv("CDP_API_KEY_ID", raising=False)
monkeypatch.delenv("CDP_API_KEY_SECRET", raising=False)
with pytest.raises(ValueError, match="CDP_API_KEY_ID and CDP_API_KEY_SECRET"):
await create_x402_server(facilitator="coinbase", rails=["x402-base-mainnet"], initialize=False)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
def test_build_x402_accepts_for_402_returns_dicts_from_typed_requirements() -> None:
"""``build_x402_accepts_for_402`` wraps ``server.build_payment_requirements`` and
returns the requirements as wire-shape dicts (via ``model_dump(by_alias=True, mode="json")``)
so merchants can drop them straight into the 402 response body without importing
Pydantic types or remembering to serialize.
"""
from x402.schemas import PaymentRequirements
from agentscore_commerce.payment import build_x402_accepts_for_402
captured: dict = {}
class _CapturingServer:
def build_payment_requirements(self, config, _ext=None):
captured["config"] = config
return [
PaymentRequirements(
scheme="exact",
network="eip155:8453",
amount="100000",
asset="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
pay_to="0x000000000000000000000000000000000000dEaD",
max_timeout_seconds=300,
extra={"name": "USD Coin", "version": "2"},
)
]
accepts = build_x402_accepts_for_402(
_CapturingServer(),
network="eip155:8453",
price="$0.10",
pay_to="0x000000000000000000000000000000000000dEaD",
)
# Caller-side: a typed ResourceConfig is passed to build_payment_requirements.
cfg = captured["config"]
assert cfg.network == "eip155:8453"
assert cfg.pay_to == "0x000000000000000000000000000000000000dEaD"
assert cfg.max_timeout_seconds == 300
# Returned shape: wire-form dicts, not Pydantic models.
assert isinstance(accepts, list)
assert len(accepts) == 1
assert isinstance(accepts[0], dict)
assert accepts[0]["network"] == "eip155:8453"
# Camel-case keys (by_alias=True) — facilitator + clients expect this shape.
assert accepts[0]["payTo"] == "0x000000000000000000000000000000000000dEaD"
assert accepts[0]["maxTimeoutSeconds"] == 300
assert accepts[0]["extra"] == {"name": "USD Coin", "version": "2"}
@pytest.mark.skipif(not _MPPX_INSTALLED or not _TEMPO_INSTALLED, reason="pympp[tempo] not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_tempo_returns_mpp_instance() -> None:
"""create_mppx_server with a Tempo charge rail returns a configured Mpp."""
server = await create_mppx_server(
secret_key="X" * 32,
rails={"tempo": TempoRailSpec(recipient="0x" + "00" * 20, testnet=True)},
)
assert type(server).__name__ == "Mpp"
# pympp 0.6 exposes intent-named methods directly (charge, pay, …) on the Mpp instance.
assert callable(getattr(server, "charge", None))
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_no_method_or_rails_raises() -> None:
with pytest.raises(ValueError, match="no method or rails"):
await create_mppx_server(secret_key="X" * 32)
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_tempo_session_raises_until_pympp_supports_it() -> None:
with pytest.raises(ImportError, match="SessionIntent"):
await create_mppx_server(
secret_key="X" * 32,
rails={
"tempo_session": TempoSessionRailSpec(
recipient="0x" + "00" * 20,
escrow_contract="0x" + "11" * 20,
store=object(),
),
},
)
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_stripe_requires_secret_key() -> None:
with pytest.raises(ValueError, match="profile_id and secret_key"):
await create_mppx_server(
secret_key="X" * 32,
rails={"stripe": StripeRailSpec(profile_id="profile_x")},
)
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_unknown_rail_spec_raises() -> None:
with pytest.raises(TypeError, match="unsupported rail spec"):
await create_mppx_server(
secret_key="X" * 32,
rails={"weird": "not-a-spec"}, # type: ignore[dict-item]
)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_auto_promotes_to_coinbase_when_env_set(monkeypatch: pytest.MonkeyPatch) -> None:
"""When facilitator='http' but CDP env vars are present, auto-promote to Coinbase."""
cdp_installed = importlib.util.find_spec("cdp.auth.utils.jwt") is not None
if not cdp_installed:
pytest.skip("cdp-sdk not installed")
monkeypatch.setenv("CDP_API_KEY_ID", "test-key-id")
monkeypatch.setenv("CDP_API_KEY_SECRET", "test-key-secret")
server = await create_x402_server(facilitator="http", initialize=False)
facilitator = server._facilitator_clients[0]
assert facilitator.url == "https://api.cdp.coinbase.com/platform/v2/x402"
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_passthrough_prebuilt_facilitator() -> None:
"""A non-string facilitator argument is passed through verbatim."""
sentinel = object()
server = await create_x402_server(facilitator=sentinel, initialize=False)
assert server._facilitator_clients[0] is sentinel
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_upto_rail_registers_upto_scheme() -> None:
server = await create_x402_server(
facilitator="http",
rails=["x402-base-sepolia-upto"],
initialize=False,
)
assert "eip155:84532" in server._schemes
# upto schemes register under their canonical scheme name in pympp 0.6+.
sepolia_schemes = server._schemes["eip155:84532"]
assert len(sepolia_schemes) >= 1
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_custom_scheme_registered() -> None:
"""A CustomScheme entry is registered alongside symbolic rails."""
from x402.mechanisms.evm.exact.server import ExactEvmScheme
from agentscore_commerce.payment import CustomScheme
custom = CustomScheme(network="eip155:8453", scheme=ExactEvmScheme())
server = await create_x402_server(
facilitator="http",
schemes=[custom],
initialize=False,
)
assert "eip155:8453" in server._schemes
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_bazaar_registers_extension(monkeypatch: pytest.MonkeyPatch) -> None:
"""`bazaar=True` looks up the extension and calls server.register_extension."""
# `find_spec` triggers a real import via x402's __init__, so guard against
# transitive ImportError (jsonschema / idna are part of `x402[extensions]`).
try:
import x402.extensions.bazaar
except ImportError:
pytest.skip("x402[extensions] not installed (jsonschema/idna unavailable)")
registered: list[Any] = []
# Patch register_extension on the class BEFORE creating the server so the
# bazaar branch records its extension call.
import x402
orig_init = x402.x402ResourceServer.__init__
def patched_init(self: Any, **kw: Any) -> None:
orig_init(self, **kw)
self.register_extension = registered.append # type: ignore[attr-defined]
monkeypatch.setattr(x402.x402ResourceServer, "__init__", patched_init)
await create_x402_server(
facilitator=object(), # opaque facilitator; never called with initialize=False
bazaar=True,
initialize=False,
)
assert len(registered) == 1
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
def test_build_x402_accepts_for_402_accepts_dict_requirements() -> None:
"""`build_payment_requirements` may return plain dicts (older versions / stubs)."""
from agentscore_commerce.payment import build_x402_accepts_for_402
class _DictServer:
def build_payment_requirements(self, _config: Any, _ext: Any = None) -> list[dict[str, Any]]:
return [{"scheme": "exact", "network": "eip155:8453", "payTo": "0xDEAD"}]
accepts = build_x402_accepts_for_402(
_DictServer(),
network="eip155:8453",
price="$0.10",
pay_to="0x000000000000000000000000000000000000dEaD",
)
assert accepts == [{"scheme": "exact", "network": "eip155:8453", "payTo": "0xDEAD"}]
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
def test_build_x402_accepts_for_402_rejects_unknown_requirement_type() -> None:
"""Anything other than a Pydantic model or dict raises TypeError."""
from agentscore_commerce.payment import build_x402_accepts_for_402
class _BadServer:
def build_payment_requirements(self, _config: Any, _ext: Any = None) -> list[object]:
return [object()]
with pytest.raises(TypeError, match="expected a Pydantic PaymentRequirements or a dict"):
build_x402_accepts_for_402(
_BadServer(),
network="eip155:8453",
price="$0.10",
pay_to="0x000000000000000000000000000000000000dEaD",
)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_coinbase_facilitator_emits_per_endpoint_bearer(monkeypatch: pytest.MonkeyPatch) -> None:
"""The Coinbase auth provider mints a per-endpoint JWT via cdp-sdk's generate_jwt.
Exercises `_mint_bearer` + `_create_headers` so they show up in coverage.
Mocks the real cdp JWT signer so the test doesn't need a valid EC private key.
"""
cdp_installed = importlib.util.find_spec("cdp.auth.utils.jwt") is not None
if not cdp_installed:
pytest.skip("cdp-sdk not installed")
monkeypatch.setenv("CDP_API_KEY_ID", "test-key-id")
monkeypatch.setenv("CDP_API_KEY_SECRET", "test-secret")
import cdp.auth.utils.jwt as cdp_jwt
captured: list[tuple[str, str]] = []
def _fake_generate_jwt(options: Any) -> str:
captured.append((options.request_method, options.request_path))
return "fake-jwt-token"
monkeypatch.setattr(cdp_jwt, "generate_jwt", _fake_generate_jwt)
server = await create_x402_server(
facilitator="coinbase",
rails=["x402-base-mainnet"],
initialize=False,
)
facilitator = server._facilitator_clients[0]
auth_provider = facilitator._auth_provider
headers = auth_provider.get_auth_headers()
assert headers.verify["Authorization"] == "Bearer fake-jwt-token"
assert headers.settle["Authorization"] == "Bearer fake-jwt-token"
assert headers.supported["Authorization"] == "Bearer fake-jwt-token"
# All three endpoints were minted distinct JWTs.
assert len(captured) == 3
methods = [c[0] for c in captured]
assert "POST" in methods # verify + settle
assert "GET" in methods # supported
# ---------------------------------------------------------------------------
# Peer-dep ImportError paths + stripe rail + realm (mppx_server branch gaps)
# ---------------------------------------------------------------------------
def _block_import(monkeypatch: pytest.MonkeyPatch, *blocked_prefixes: str) -> None:
"""Make importlib.import_module raise ImportError for the given module prefixes."""
import importlib
real = importlib.import_module
def _fake(name: str, *args: Any, **kwargs: Any) -> Any:
if any(name == p or name.startswith(p) for p in blocked_prefixes):
msg = f"blocked {name}"
raise ImportError(msg)
return real(name, *args, **kwargs)
monkeypatch.setattr(importlib, "import_module", _fake)
@pytest.mark.asyncio
async def test_create_mppx_server_missing_pympp_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""When `mpp.server` is unavailable, a guiding ImportError names the install command."""
_block_import(monkeypatch, "mpp.server")
with pytest.raises(ImportError, match=r"pympp not installed"):
await create_mppx_server(secret_key="X" * 32, rails={"tempo": TempoRailSpec(recipient="0x" + "00" * 20)})
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_missing_tempo_factory_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""When `mpp.methods.tempo` is unavailable, the tempo rail raises a guiding ImportError."""
_block_import(monkeypatch, "mpp.methods.tempo")
with pytest.raises(ImportError, match=r"pympp\[tempo\] not installed"):
await create_mppx_server(
secret_key="X" * 32,
rails={"tempo": TempoRailSpec(recipient="0x" + "00" * 20, testnet=True)},
)
@pytest.mark.skipif(not _MPPX_INSTALLED or not _TEMPO_INSTALLED, reason="pympp[tempo] not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_missing_charge_intent_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""A tempo module present but missing ChargeIntent raises the upgrade hint."""
import mpp.methods.tempo as tempo_mod
# Make getattr(module, "ChargeIntent", None) return None without removing `tempo`.
monkeypatch.setattr(tempo_mod, "ChargeIntent", None, raising=False)
with pytest.raises(ImportError, match=r"missing ChargeIntent"):
await create_mppx_server(
secret_key="X" * 32,
rails={"tempo": TempoRailSpec(recipient="0x" + "00" * 20, testnet=True)},
)
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_passes_realm_through() -> None:
"""A non-None `realm` is threaded into Mpp.create."""
server = await create_mppx_server(
secret_key="X" * 32,
rails={"tempo": TempoRailSpec(recipient="0x" + "00" * 20, testnet=True)},
realm="my-realm",
)
assert type(server).__name__ == "Mpp"
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_stripe_rail_resolves(monkeypatch: pytest.MonkeyPatch) -> None:
"""A fully-specified StripeRailSpec resolves a stripe method and constructs an Mpp.
``create_mppx_stripe`` is stubbed because the installed pympp build doesn't ship a
working ``stripe/charge`` factory; this exercises the stripe branch (resolve + break)
in ``create_mppx_server`` without depending on that peer-dep detail. pympp >=0.11's
``Mpp.create`` reads ``.name`` off each method for its uniqueness check, so the stub
returns a method-shaped object carrying one rather than a bare ``object()``.
"""
async def _fake_create_stripe(**_kwargs: Any) -> Any:
return SimpleNamespace(name="stripe")
monkeypatch.setattr("agentscore_commerce.stripe_multichain.mppx_stripe.create_mppx_stripe", _fake_create_stripe)
server = await create_mppx_server(
secret_key="X" * 32,
rails={"stripe": StripeRailSpec(profile_id="profile_x", secret_key="sk_test_x")},
)
assert type(server).__name__ == "Mpp"
@pytest.mark.skipif(not _MPPX_INSTALLED, reason="pympp not installed")
@pytest.mark.asyncio
async def test_create_mppx_server_with_prebuilt_method_skips_rail_resolution() -> None:
"""Passing `method=` directly skips the rail-resolution loop entirely (127->145)."""
import mpp.methods.tempo as tempo_mod
method = tempo_mod.tempo(
intents={"charge": tempo_mod.ChargeIntent()},
currency="0x" + "11" * 20,
recipient="0x" + "00" * 20,
chain_id=42431,
)
server = await create_mppx_server(secret_key="X" * 32, method=method)
assert type(server).__name__ == "Mpp"
# ---------------------------------------------------------------------------
# x402_server peer-dep + error-path branch gaps
# ---------------------------------------------------------------------------
def test__import_optional_returns_none_for_missing_module() -> None:
from agentscore_commerce.payment.x402_server import _import_optional
assert _import_optional("totally_nonexistent_module_xyz") is None
assert _import_optional("os") is not None
@pytest.mark.asyncio
async def test_create_x402_server_missing_x402_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""When the top-level `x402` package is unavailable, a guiding ImportError fires."""
import agentscore_commerce.payment.x402_server as mod
monkeypatch.setattr(mod, "_import_optional", lambda _name: None)
with pytest.raises(ImportError, match=r"x402 not installed"):
await create_x402_server(facilitator="http", initialize=False)
@pytest.mark.asyncio
async def test_create_x402_server_http_facilitator_missing_client_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""`facilitator='http'` but x402.http lacks HTTPFacilitatorClient → ImportError."""
import agentscore_commerce.payment.x402_server as mod
x402_top = type("X", (), {"x402ResourceServer": object})()
def _fake_import(name: str) -> Any:
if name == "x402":
return x402_top
if name == "x402.http":
return type("H", (), {})() # no HTTPFacilitatorClient
return None
monkeypatch.setattr(mod, "_import_optional", _fake_import)
monkeypatch.delenv("CDP_API_KEY_ID", raising=False)
monkeypatch.delenv("CDP_API_KEY_SECRET", raising=False)
with pytest.raises(ImportError, match=r"x402.http missing HTTPFacilitatorClient"):
await create_x402_server(facilitator="http", initialize=False)
@pytest.mark.asyncio
async def test_create_x402_server_coinbase_missing_cdp_sdk_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""`facilitator='coinbase'` with creds but no cdp-sdk → guiding ImportError."""
import agentscore_commerce.payment.x402_server as mod
x402_top = type("X", (), {"x402ResourceServer": object})()
def _fake_import(name: str) -> Any:
if name == "x402":
return x402_top
return None # cdp.auth.utils.jwt missing
monkeypatch.setattr(mod, "_import_optional", _fake_import)
monkeypatch.setenv("CDP_API_KEY_ID", "id")
monkeypatch.setenv("CDP_API_KEY_SECRET", "secret")
with pytest.raises(ImportError, match=r"cdp-sdk not installed"):
await create_x402_server(facilitator="coinbase", initialize=False)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_upto_scheme_missing_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""An upto rail whose scheme module/class is missing raises an x402[evm] hint."""
import agentscore_commerce.payment.x402_server as mod
real = mod._import_optional
def _fake_import(name: str) -> Any:
if name == "x402.mechanisms.evm.upto.server":
return None
return real(name)
monkeypatch.setattr(mod, "_import_optional", _fake_import)
with pytest.raises(ImportError, match=r"x402\[evm\] not installed"):
await create_x402_server(facilitator="http", rails=["x402-base-sepolia-upto"], initialize=False)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_exact_scheme_missing_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""An exact rail whose scheme module/class is missing raises an x402[evm] hint."""
import agentscore_commerce.payment.x402_server as mod
real = mod._import_optional
def _fake_import(name: str) -> Any:
if name == "x402.mechanisms.evm.exact.server":
return None
return real(name)
monkeypatch.setattr(mod, "_import_optional", _fake_import)
with pytest.raises(ImportError, match=r"x402\[evm\] not installed"):
await create_x402_server(facilitator="http", rails=["x402-base-mainnet"], initialize=False)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_bazaar_missing_extension_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""`bazaar=True` but the bazaar extension is unavailable → guiding ImportError."""
import agentscore_commerce.payment.x402_server as mod
real = mod._import_optional
def _fake_import(name: str) -> Any:
if name == "x402.extensions.bazaar":
return None
return real(name)
monkeypatch.setattr(mod, "_import_optional", _fake_import)
with pytest.raises(ImportError, match=r"x402\[extensions\] not installed"):
await create_x402_server(facilitator=object(), bazaar=True, initialize=False)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_bazaar_server_missing_register_extension_raises(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A server without register_extension raises a clear RuntimeError when bazaar=True."""
import x402
import agentscore_commerce.payment.x402_server as mod
real = mod._import_optional
def _fake_import(name: str) -> Any:
if name == "x402.extensions.bazaar":
return type("B", (), {"bazaar_resource_server_extension": object()})()
return real(name)
orig_init = x402.x402ResourceServer.__init__
def patched_init(self: Any, **kw: Any) -> None:
orig_init(self, **kw)
# Drop register_extension so the callable check fails.
self.register_extension = "not-callable"
monkeypatch.setattr(mod, "_import_optional", _fake_import)
monkeypatch.setattr(x402.x402ResourceServer, "__init__", patched_init)
with pytest.raises(RuntimeError, match=r"does not expose register_extension"):
await create_x402_server(facilitator=object(), bazaar=True, initialize=False)
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_awaits_async_initialize(monkeypatch: pytest.MonkeyPatch) -> None:
"""When the server's initialize() returns an awaitable, create_x402_server awaits it."""
import x402
awaited: list[bool] = []
async def _async_init(self: Any) -> None:
awaited.append(True)
monkeypatch.setattr(x402.x402ResourceServer, "initialize", _async_init, raising=False)
server = await create_x402_server(facilitator="http", initialize=True)
assert awaited == [True]
assert type(server).__name__ == "x402ResourceServer"
def test_build_x402_accepts_for_402_missing_x402_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""build_x402_accepts_for_402 raises a guiding ImportError when x402 is absent."""
import agentscore_commerce.payment.x402_server as mod
from agentscore_commerce.payment import build_x402_accepts_for_402
monkeypatch.setattr(mod, "_import_optional", lambda _name: None)
with pytest.raises(ImportError, match=r"x402 not installed"):
build_x402_accepts_for_402(object(), network="eip155:8453", price="$0.10", pay_to="0xDEAD")
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_sync_initialize_not_awaited(monkeypatch: pytest.MonkeyPatch) -> None:
"""A sync initialize() (x402 2.9 shape) is called but not awaited."""
import x402
calls: list[bool] = []
def _sync_init(self: Any) -> None:
calls.append(True)
monkeypatch.setattr(x402.x402ResourceServer, "initialize", _sync_init, raising=False)
server = await create_x402_server(facilitator="http", initialize=True)
assert calls == [True]
assert type(server).__name__ == "x402ResourceServer"
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_initialize_noncallable_skipped(monkeypatch: pytest.MonkeyPatch) -> None:
"""A server whose `initialize` attr isn't callable is skipped without error."""
import x402
monkeypatch.setattr(x402.x402ResourceServer, "initialize", "not-callable", raising=False)
server = await create_x402_server(facilitator="http", initialize=True)
assert type(server).__name__ == "x402ResourceServer"
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_non_base_rail_is_ignored() -> None:
"""A rail string that doesn't start with `x402-base` is silently skipped (no scheme)."""
server = await create_x402_server(
facilitator="http",
rails=["x402-unknown-network"], # type: ignore[list-item]
initialize=False,
)
# No EVM scheme registered for the unrecognized rail.
assert "eip155:8453" not in getattr(server, "_schemes", {})
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_two_exact_rails_reuse_module() -> None:
"""Two exact rails exercise the `evm_exact_module is None` cache reuse branch."""
server = await create_x402_server(
facilitator="http",
rails=["x402-base-mainnet", "x402-base-sepolia"],
initialize=False,
)
assert "eip155:8453" in server._schemes
assert "eip155:84532" in server._schemes
@pytest.mark.skipif(not _X402_INSTALLED, reason="x402 peer dep not installed")
@pytest.mark.asyncio
async def test_create_x402_server_two_upto_rails_reuse_module() -> None:
"""Two upto rails exercise the `evm_upto_module is None` cache reuse branch."""
server = await create_x402_server(
facilitator="http",
rails=["x402-base-mainnet-upto", "x402-base-sepolia-upto"],
initialize=False,
)
assert "eip155:8453" in server._schemes
assert "eip155:84532" in server._schemes
@pytest.mark.asyncio
async def test_coinbase_facilitator_missing_facilitator_config_raises(monkeypatch: pytest.MonkeyPatch) -> None:
"""Coinbase path with cdp present but x402.http lacking FacilitatorConfig → ImportError."""
import agentscore_commerce.payment.x402_server as mod
x402_top = type("X", (), {"x402ResourceServer": object})()
fake_cdp = type(
"J",
(),
{"JwtOptions": object, "generate_jwt": staticmethod(lambda _o: "jwt")},
)()
def _fake_import(name: str) -> Any:
if name == "x402":
return x402_top
if name == "cdp.auth.utils.jwt":
return fake_cdp
if name == "x402.http":
return type("H", (), {})() # no FacilitatorConfig / HTTPFacilitatorClient
return None
monkeypatch.setattr(mod, "_import_optional", _fake_import)
monkeypatch.setenv("CDP_API_KEY_ID", "id")
monkeypatch.setenv("CDP_API_KEY_SECRET", "secret")
with pytest.raises(ImportError, match=r"FacilitatorConfig / HTTPFacilitatorClient"):
await create_x402_server(facilitator="coinbase", initialize=False)