-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_implementation.py
More file actions
executable file
·199 lines (164 loc) · 7.91 KB
/
Copy pathtest_implementation.py
File metadata and controls
executable file
·199 lines (164 loc) · 7.91 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
"""
Test script to validate the implemented functionality
"""
import asyncio
import httpx
import json
from sqlalchemy.ext.asyncio import AsyncSession
from database import get_async_session, init_db
from service.user_service import UserService
from model.user import UserCreate
BASE_URL = "http://localhost:8000"
async def test_database_operations():
"""Test basic database operations"""
print("Testing database operations...")
# Initialize database
await init_db()
async for db in get_async_session():
# Test user creation
user_data = UserCreate(
name="Test API User",
email="test-api@example.com",
is_active=True
)
user = await UserService.create_user(db, user_data)
print(f"✓ Created user: {user.client_id}")
# Test user retrieval
retrieved_user = await UserService.get_user(db, user.client_id)
assert retrieved_user.name == "Test API User"
print("✓ User retrieval works")
# Test authentication
auth_user = await UserService.authenticate_user(db, user.client_id, user.client_secret)
assert auth_user is not None
print("✓ User authentication works")
# Test secret regeneration
new_secret = await UserService.regenerate_client_secret(db, user.client_id)
assert new_secret != user.client_secret
print("✓ Secret regeneration works")
await db.commit()
break
async def test_api_endpoints():
"""Test API endpoints"""
print("\nTesting API endpoints...")
async with httpx.AsyncClient() as client:
# Test health endpoint without auth
response = await client.get(f"{BASE_URL}/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
print("✓ Health endpoint works without auth")
# Get a user for testing (we'll use the first one in the database)
async for db in get_async_session():
users = await UserService.get_users(db, limit=1)
if users:
test_user = await UserService.get_user_with_secret(db, users[0].client_id)
api_key = f"{test_user.client_id}:{test_user.client_secret}"
# Test health endpoint with auth
response = await client.get(f"{BASE_URL}/health?api_key={api_key}")
assert response.status_code == 200
data = response.json()
assert "authenticated_user" in data
print("✓ Health endpoint works with API key auth")
# Test secured endpoint with Bearer token
headers = {"Authorization": f"Bearer {api_key}"}
response = await client.get(f"{BASE_URL}/secure-endpoint", headers=headers)
assert response.status_code == 200
data = response.json()
assert "message" in data
print("✓ Secured endpoint works with Bearer token")
# Test invalid credentials
response = await client.get(f"{BASE_URL}/secure-endpoint",
headers={"Authorization": "Bearer invalid:credentials"})
assert response.status_code == 401
print("✓ Authentication properly rejects invalid credentials")
break
async def test_admin_api():
"""Test admin API endpoints"""
print("\nTesting admin API endpoints...")
# Admin credentials for HTTP Basic Auth
admin_auth = ("admin", "Opg#842+9914")
async with httpx.AsyncClient() as client:
# Test creating user via API with admin auth
user_data = {
"name": "API Created User",
"email": "api-created@example.com",
"is_active": True
}
response = await client.post(f"{BASE_URL}/admin/users/create", data=user_data, auth=admin_auth)
assert response.status_code == 303 # Redirect response
print("✓ Admin user creation API works")
# Test admin users page with auth
response = await client.get(f"{BASE_URL}/admin/users", auth=admin_auth)
assert response.status_code == 200
print("✓ Admin users page accessible")
# Test admin dashboard with auth
response = await client.get(f"{BASE_URL}/admin/", auth=admin_auth)
assert response.status_code == 200
print("✓ Admin dashboard accessible")
# Test that admin endpoints are protected (without auth should return 401)
response = await client.get(f"{BASE_URL}/admin/users")
assert response.status_code == 401
print("✓ Admin endpoints properly protected")
async def test_agent_execute_with_parameters():
"""Test agent execution with parameters"""
print("\nTesting agent execution with parameters...")
async with httpx.AsyncClient() as client:
# Get a user for testing
async for db in get_async_session():
users = await UserService.get_users(db, limit=1)
if users:
test_user = await UserService.get_user_with_secret(db, users[0].client_id)
api_key = f"{test_user.client_id}:{test_user.client_secret}"
headers = {"Authorization": f"Bearer {api_key}"}
# Test agent execution without parameters
request_data = {
"agent_name": "translation-agent",
"provider": "openai",
"model": "gpt-4",
"message": "Hello world",
"user_id": test_user.client_id
}
response = await client.post(f"{BASE_URL}/agent/execute",
json=request_data, headers=headers)
print(f"✓ Agent execution without parameters: status {response.status_code}")
# Test agent execution with parameters
request_data_with_params = {
"agent_name": "translation-agent",
"provider": "openai",
"model": "gpt-4",
"message": "Hello world",
"user_id": test_user.client_id,
"parameters": {
"target_language": "German",
"style": "formal",
"preserve_formatting": True
}
}
response = await client.post(f"{BASE_URL}/agent/execute",
json=request_data_with_params, headers=headers)
print(f"✓ Agent execution with parameters: status {response.status_code}")
if response.status_code == 200:
result = response.json()
print(f"✓ Agent execution successful: job_id {result.get('job_id')}")
elif response.status_code == 404:
print("✓ Agent not found response as expected (no API keys configured)")
elif response.status_code == 500:
print("✓ Server error as expected (likely missing API keys)")
else:
print(f" Response: {response.text}")
break
async def main():
"""Run all tests"""
print("Starting KIGate implementation tests...\n")
try:
await test_database_operations()
await test_api_endpoints()
await test_admin_api()
await test_agent_execute_with_parameters()
print("\n🎉 All tests passed! Implementation is working correctly.")
except Exception as e:
print(f"\n❌ Test failed: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(main())