-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
97 lines (83 loc) · 3.23 KB
/
Copy pathtest_api.py
File metadata and controls
97 lines (83 loc) · 3.23 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
import requests
import json
BASE_URL = "http://localhost:8000"
def test_root_endpoint():
"""Test the root endpoint"""
response = requests.get(f"{BASE_URL}/")
print("Root Endpoint Response:", response.json())
assert response.status_code == 200
def test_list_policies():
"""Test listing all policies"""
response = requests.get(f"{BASE_URL}/policies")
print("List Policies Response:", json.dumps(response.json(), indent=2))
assert response.status_code == 200
def test_evaluate_policy():
"""Test evaluating a single policy"""
# Sample input data for a data scientist trying to perform inference
input_data = {
"user": {"role": "data_scientist"},
"action": "infer",
"resource": {
"type": "llm_model",
"model_id": "gpt-4",
"status": "approved",
"monitoring": {"active": True}
},
"usecase": "customer_service"
}
# Use client ID and client secret in headers with the DSPAI prefix
headers = {
"X-DSPAI-Client-ID": "customer_service",
"X-DSPAI-Client-Secret": "password" # This matches the hashed secret in the policy file
}
payload = {
"input_data": input_data
}
response = requests.post(f"{BASE_URL}/evaluate", json=payload, headers=headers)
print("Evaluate Policy Response:", json.dumps(response.json(), indent=2))
assert response.status_code == 200
def test_batch_evaluate():
"""Test batch evaluation of multiple policies"""
# Sample input data for a business user trying to perform inference
input_data = {
"user": {"role": "business_user"},
"action": "infer",
"resource": {
"type": "llm_model",
"model_id": "gpt-4",
"status": "approved",
"monitoring": {"active": True}
},
"usecase": "customer_service"
}
# Use client ID and client secret in headers with the DSPAI prefix
headers = {
"X-DSPAI-Client-ID": "customer_service",
"X-DSPAI-Client-Secret": "password" # This matches the hashed secret in the policy file
}
response = requests.post(f"{BASE_URL}/batch-evaluate", json=input_data, headers=headers)
print("Batch Evaluate Response:", json.dumps(response.json(), indent=2))
assert response.status_code == 200
def test_generate_client_secret():
"""Test generating a hashed client secret"""
payload = {
"client_id": "test_client",
"plain_secret": "test_secret"
}
response = requests.post(f"{BASE_URL}/generate-client-secret", json=payload)
print("Generate Client Secret Response:", json.dumps(response.json(), indent=2))
assert response.status_code == 200
assert "hashed_secret" in response.json()
assert "salt" in response.json()
if __name__ == "__main__":
print("Testing DSP AI Control Tower OPA Policy Evaluator API")
try:
test_root_endpoint()
test_list_policies()
test_generate_client_secret()
test_evaluate_policy()
test_batch_evaluate()
print("\nAll tests passed successfully!")
except Exception as e:
print(f"\nTest failed: {str(e)}")
print("Make sure the API server is running with 'python app.py'")