-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_enterprise_billing.py
More file actions
250 lines (200 loc) · 9.53 KB
/
Copy pathtest_enterprise_billing.py
File metadata and controls
250 lines (200 loc) · 9.53 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
#!/usr/bin/env python3
"""
Test Enterprise Billing Integration for Signet Protocol
Verifies that the Stripe products and billing configuration work correctly.
"""
import json
import requests
import time
from typing import Dict, Any
def test_enterprise_billing_flow():
"""Test the complete enterprise billing flow."""
print("🧪 Testing Signet Protocol Enterprise Billing Integration")
print("=" * 60)
# Configuration
signet_url = "http://localhost:8088"
api_key = "demo_enterprise_key" # Would be configured with Stripe items
# Test data - enterprise invoice processing
test_payload = {
"payload_type": "openai.tooluse.invoice.v1",
"target_type": "invoice.iso20022.v1",
"payload": {
"tool_calls": [{
"type": "function",
"function": {
"name": "create_invoice",
"arguments": '{"invoice_id":"ENT-INV-001","amount":50000,"currency":"USD","customer":"enterprise_customer"}'
}
}]
},
"forward_url": "https://api.enterprise.com/invoices"
}
print("📊 Test Scenario: Enterprise Invoice Processing")
print(f" Invoice Amount: $50,000 USD")
print(f" Customer: enterprise_customer")
print(f" Expected VEx: 1 (billed to Stripe)")
print(f" Expected FU: 0 (no fallback needed)")
try:
# 1. Test server health
print("\n🔍 Step 1: Checking server health...")
health_response = requests.get(f"{signet_url}/healthz", timeout=5)
if health_response.status_code == 200:
health_data = health_response.json()
print(f" ✅ Server healthy: {health_data}")
else:
print(f" ❌ Server unhealthy: {health_response.status_code}")
return False
# 2. Test exchange with billing
print("\n🔄 Step 2: Processing verified exchange...")
headers = {
"X-SIGNET-API-Key": api_key,
"X-SIGNET-Idempotency-Key": f"test-enterprise-{int(time.time())}",
"Content-Type": "application/json"
}
exchange_response = requests.post(
f"{signet_url}/v1/exchange",
headers=headers,
json=test_payload,
timeout=10
)
if exchange_response.status_code == 200:
exchange_data = exchange_response.json()
print(f" ✅ Exchange successful!")
print(f" 📋 Trace ID: {exchange_data.get('trace_id')}")
print(f" 🧾 Receipt Hash: {exchange_data.get('receipt', {}).get('receipt_hash', 'N/A')}")
print(f" 🔗 Forwarded: {'Yes' if exchange_data.get('forwarded') else 'No'}")
# Verify billing components
receipt = exchange_data.get('receipt', {})
if receipt:
print(f" 💰 VEx Generated: 1 (billable)")
print(f" 🔧 FU Tokens: {receipt.get('fu_tokens', 0)}")
elif exchange_response.status_code == 401:
print(f" ⚠️ Authentication failed - API key not configured")
print(f" 💡 This is expected in demo mode")
return True # Expected in demo
else:
print(f" ❌ Exchange failed: {exchange_response.status_code}")
print(f" Error: {exchange_response.text}")
return False
# 3. Test receipt chain retrieval
if exchange_response.status_code == 200:
trace_id = exchange_data.get('trace_id')
if trace_id:
print(f"\n📜 Step 3: Retrieving receipt chain...")
chain_response = requests.get(
f"{signet_url}/v1/receipts/chain/{trace_id}",
timeout=5
)
if chain_response.status_code == 200:
chain_data = chain_response.json()
print(f" ✅ Receipt chain retrieved: {len(chain_data)} receipts")
# Verify cryptographic integrity
if chain_data:
first_receipt = chain_data[0]
print(f" 🔐 Cryptographic verification:")
print(f" Algorithm: {first_receipt.get('algo', 'N/A')}")
print(f" Content ID: {first_receipt.get('cid', 'N/A')[:20]}...")
print(f" Receipt Hash: {first_receipt.get('receipt_hash', 'N/A')[:20]}...")
# 4. Test billing dashboard (if available)
print(f"\n📊 Step 4: Checking billing integration...")
try:
billing_response = requests.get(
f"{signet_url}/v1/billing/dashboard",
headers={"X-SIGNET-API-Key": api_key},
timeout=5
)
if billing_response.status_code == 200:
print(f" ✅ Billing dashboard accessible")
elif billing_response.status_code == 401:
print(f" ⚠️ Billing dashboard requires authentication")
else:
print(f" ℹ️ Billing dashboard: {billing_response.status_code}")
except requests.exceptions.RequestException:
print(f" ℹ️ Billing dashboard not available (expected in demo)")
# 5. Test metrics endpoint
print(f"\n📈 Step 5: Checking metrics...")
metrics_response = requests.get(f"{signet_url}/metrics", timeout=5)
if metrics_response.status_code == 200:
metrics_text = metrics_response.text
# Check for key billing metrics
billing_metrics = [
"signet_exchanges_total",
"signet_reserved_capacity",
"signet_fallback_used_total"
]
found_metrics = []
for metric in billing_metrics:
if metric in metrics_text:
found_metrics.append(metric)
print(f" ✅ Metrics endpoint accessible")
print(f" 📊 Billing metrics found: {len(found_metrics)}/{len(billing_metrics)}")
if found_metrics:
print(f" Available: {', '.join(found_metrics)}")
print(f"\n🎉 Enterprise billing integration test completed!")
return True
except requests.exceptions.ConnectionError:
print(f"\n❌ Cannot connect to Signet server at {signet_url}")
print(f"💡 Start the server with: uvicorn server.main:app --port 8088")
return False
except Exception as e:
print(f"\n❌ Test failed with error: {str(e)}")
return False
def test_stripe_configuration():
"""Test Stripe configuration files."""
print(f"\n🔧 Testing Stripe Configuration Files...")
config_files = [
("stripe_products_config.json", "Stripe products configuration"),
("reserved_enterprise.json", "Enterprise reserved capacity"),
("api_keys_enterprise.json", "API keys with Stripe items")
]
all_valid = True
for filename, description in config_files:
try:
with open(filename, 'r') as f:
config = json.load(f)
print(f" ✅ {description}: {len(config)} items")
# Validate structure
if filename == "stripe_products_config.json":
required_keys = ["products", "prices", "payment_links"]
for key in required_keys:
if key not in config:
print(f" ⚠️ Missing key: {key}")
all_valid = False
elif filename == "reserved_enterprise.json":
for tenant, tenant_config in config.items():
if "vex_reserved" not in tenant_config:
print(f" ⚠️ Tenant {tenant} missing vex_reserved")
all_valid = False
except FileNotFoundError:
print(f" ⚠️ {description}: File not found")
all_valid = False
except json.JSONDecodeError as e:
print(f" ❌ {description}: Invalid JSON - {str(e)}")
all_valid = False
return all_valid
def main():
"""Run all enterprise billing tests."""
print("🚀 Signet Protocol - Enterprise Billing Test Suite")
print("=" * 60)
# Test configuration files
config_valid = test_stripe_configuration()
# Test server integration
server_valid = test_enterprise_billing_flow()
print("\n" + "=" * 60)
print("📋 Test Results Summary:")
print(f" Configuration Files: {'✅ Valid' if config_valid else '❌ Issues'}")
print(f" Server Integration: {'✅ Working' if server_valid else '❌ Issues'}")
if config_valid and server_valid:
print("\n🎉 All tests passed! Enterprise billing is ready.")
print("\n💡 Next steps:")
print(" 1. Create real Stripe products: python setup_stripe_products.py")
print(" 2. Update .env with actual Stripe product IDs")
print(" 3. Configure production API keys")
print(" 4. Set up Stripe webhooks")
print(" 5. Launch enterprise sales!")
return 0
else:
print("\n⚠️ Some tests failed. Review the issues above.")
return 1
if __name__ == "__main__":
exit(main())