-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
133 lines (112 loc) · 4.35 KB
/
Copy pathtest_api.py
File metadata and controls
133 lines (112 loc) · 4.35 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
import requests
import json
import logging
from datetime import datetime
import uuid
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
BASE_URL = "http://localhost:8000"
def generate_unique_email():
"""Generate a unique email for testing"""
unique_id = str(uuid.uuid4())[:8]
return f"test_{unique_id}@example.com"
def test_create_customer():
"""Test customer creation"""
url = f"{BASE_URL}/customers"
data = {
"name": "John Doe",
"email": generate_unique_email(),
"company": "Test Corp"
}
response = requests.post(url, json=data)
logger.info(f"Create customer response: {response.status_code}")
assert response.status_code == 200, f"Failed to create customer: {response.text}"
return response.json()
def test_create_api_key(customer_email):
"""Test API key creation"""
url = f"{BASE_URL}/api/keys" # Fixed endpoint
data = {
"customer_email": customer_email,
"name": "Test Key",
"allowed_models": [1, 2] # Allow both GPT-2 and LawGENT
}
response = requests.post(url, json=data)
logger.info(f"Create API key response: {response.status_code}")
assert response.status_code == 200, f"Failed to create API key: {response.text}"
return response.json()
def test_list_models(api_key):
"""Test model listing"""
url = f"{BASE_URL}/models"
headers = {"X-API-Key": api_key}
response = requests.get(url, headers=headers)
logger.info(f"List models response: {response.status_code}")
assert response.status_code == 200, f"Failed to list models: {response.text}"
return response.json()
def test_generate_text(api_key):
"""Test text generation"""
url = f"{BASE_URL}/generate"
headers = {"X-API-Key": api_key}
data = {
"text": "Write a test message",
"model_id": 1, # Using GPT-2
"max_length": 50
}
response = requests.post(url, json=data, headers=headers)
logger.info(f"Generate text response: {response.status_code}")
assert response.status_code == 200, f"Failed to generate text: {response.text}"
return response.json()
def test_query(api_key):
"""Test query endpoint"""
url = f"{BASE_URL}/query"
headers = {"X-API-Key": api_key}
data = {
"text": "Test query",
"max_length": 50
}
response = requests.post(url, json=data, headers=headers)
logger.info(f"Query response: {response.status_code}")
assert response.status_code == 200, f"Failed to query: {response.text}"
return response.json()
def run_all_tests():
"""Run all API tests"""
try:
logger.info("Starting API tests...")
# Test 1: Create customers
customer1 = test_create_customer()
logger.info(f"Created customer 1: {customer1}")
# Create another customer with different data
global BASE_URL
BASE_URL = "http://localhost:8000" # Reset URL
data = {
"name": "Jane Smith",
"email": generate_unique_email(),
"company": "Tech Corp"
}
response = requests.post(f"{BASE_URL}/customers", json=data)
customer2 = response.json()
logger.info(f"Created customer 2: {customer2}")
# Test 2: Create API keys for both customers
api_key1 = test_create_api_key(customer1["email"])
logger.info(f"Created API key 1: {api_key1}")
api_key2 = test_create_api_key(customer2["email"])
logger.info(f"Created API key 2: {api_key2}")
# Test 3: List models with first API key
models = test_list_models(api_key1["key"])
logger.info(f"Listed models: {models}")
# Test 4: Generate text with both API keys
gen1 = test_generate_text(api_key1["key"])
logger.info(f"Generated text 1: {gen1}")
gen2 = test_generate_text(api_key2["key"])
logger.info(f"Generated text 2: {gen2}")
# Test 5: Query with both API keys
query1 = test_query(api_key1["key"])
logger.info(f"Query 1: {query1}")
query2 = test_query(api_key2["key"])
logger.info(f"Query 2: {query2}")
logger.info("All tests completed successfully!")
except Exception as e:
logger.error(f"Test failed: {str(e)}")
raise
if __name__ == "__main__":
run_all_tests()