-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_api.py
More file actions
138 lines (124 loc) · 4.45 KB
/
Copy pathtest_api.py
File metadata and controls
138 lines (124 loc) · 4.45 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
import requests
import json
import time
# Base URL for the API
BASE_URL = "http://localhost:5000/api"
def test_health_check():
"""Test the health check endpoint"""
print("=== Testing Health Check ===")
try:
response = requests.get(f"{BASE_URL}/health")
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
return response.status_code == 200
except Exception as e:
print(f"Error: {e}")
return False
def test_user_registration():
"""Test user registration"""
print("\n=== Testing User Registration ===")
try:
data = {
"username": "testuser",
"email": "test@example.com",
"password": "testpassword123"
}
response = requests.post(f"{BASE_URL}/auth/register", json=data)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
return response.status_code == 201
except Exception as e:
print(f"Error: {e}")
return False
def test_user_login():
"""Test user login"""
print("\n=== Testing User Login ===")
try:
data = {
"email": "test@example.com",
"password": "testpassword123"
}
response = requests.post(f"{BASE_URL}/auth/login", json=data)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
if response.status_code == 200:
token = response.json().get('token')
return token
return None
except Exception as e:
print(f"Error: {e}")
return None
def test_chat_endpoint(token):
"""Test chat endpoint"""
print("\n=== Testing Chat Endpoint ===")
try:
headers = {"Authorization": f"Bearer {token}"}
data = {
"message": "Hello, I need career advice"
}
response = requests.post(f"{BASE_URL}/chat", json=data, headers=headers)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
return response.status_code == 200
except Exception as e:
print(f"Error: {e}")
return False
def test_career_recommendations(token):
"""Test career recommendations endpoint"""
print("\n=== Testing Career Recommendations ===")
try:
headers = {"Authorization": f"Bearer {token}"}
data = {
"interests": ["technology", "programming"],
"skills": ["Python", "JavaScript"],
"experience": "beginner"
}
response = requests.post(f"{BASE_URL}/career/recommendations", json=data, headers=headers)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
return response.status_code == 200
except Exception as e:
print(f"Error: {e}")
return False
def test_protected_endpoint(token):
"""Test protected endpoint"""
print("\n=== Testing Protected Endpoint ===")
try:
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(f"{BASE_URL}/user/profile", headers=headers)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
return response.status_code == 200
except Exception as e:
print(f"Error: {e}")
return False
def main():
"""Run all tests"""
print("Starting API Tests...")
print("Make sure the Flask server is running on http://localhost:5000")
# Test health check
health_ok = test_health_check()
if not health_ok:
print("❌ Health check failed. Server might not be running.")
return
# Test registration
registration_ok = test_user_registration()
# Test login
token = test_user_login()
if not token:
print("❌ Login failed. Cannot proceed with authenticated tests.")
return
# Test authenticated endpoints
chat_ok = test_chat_endpoint(token)
career_ok = test_career_recommendations(token)
protected_ok = test_protected_endpoint(token)
# Summary
print("\n=== Test Summary ===")
print(f"Health Check: {'✅' if health_ok else '❌'}")
print(f"Registration: {'✅' if registration_ok else '❌'}")
print(f"Login: {'✅' if token else '❌'}")
print(f"Chat: {'✅' if chat_ok else '❌'}")
print(f"Career Recommendations: {'✅' if career_ok else '❌'}")
print(f"Protected Endpoint: {'✅' if protected_ok else '❌'}")
if __name__ == "__main__":
main()