-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_main.py
More file actions
64 lines (44 loc) · 1.73 KB
/
test_main.py
File metadata and controls
64 lines (44 loc) · 1.73 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
import base64
import json
import pytest
from fastapi import FastAPI
def _make_payment_header(address: str) -> str:
"""Build a Payment authorization header with the given recipient address."""
challenge_request = base64.b64encode(
json.dumps({"recipient": address}).encode()
).decode()
credential = {
"challenge": {
"id": "test-id",
"realm": "test-realm",
"method": "tempo",
"intent": "charge",
"request": challenge_request,
},
"payload": {},
}
return "Payment " + base64.b64encode(json.dumps(credential).encode()).decode()
@pytest.fixture(autouse=True)
def _set_env(monkeypatch):
monkeypatch.setenv("STRIPE_SECRET_KEY", "sk_test_fake")
def test_app_is_fastapi():
from main import app
assert isinstance(app, FastAPI)
def test_extract_recipient_returns_none_for_no_header():
from main import _extract_recipient_from_authorization
assert _extract_recipient_from_authorization(None) is None
assert _extract_recipient_from_authorization("Bearer token") is None
def test_extract_recipient_with_valid_payment_header():
from main import _extract_recipient_from_authorization, payment_cache
address = "0xabc123"
payment_cache[address] = True
header = _make_payment_header(address)
result = _extract_recipient_from_authorization(header)
assert result == address
# cleanup
payment_cache.pop(address, None)
def test_extract_recipient_rejects_unknown_address():
from main import _extract_recipient_from_authorization
header = _make_payment_header("0xunknown")
with pytest.raises(ValueError, match="not found in server cache"):
_extract_recipient_from_authorization(header)