-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth.py
More file actions
121 lines (101 loc) · 3.82 KB
/
test_auth.py
File metadata and controls
121 lines (101 loc) · 3.82 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
"""
Test Authentication Endpoints
Quick test script to verify auth system works
"""
import requests
import json
BASE_URL = "http://localhost:8000"
def test_auth():
print("=" * 60)
print("Testing QuantTerminal Authentication")
print("=" * 60)
print()
# Test 1: Register
print("1. Testing Registration...")
register_data = {
"username": "test_trader",
"email": "trader@example.com",
"password": "SecurePass123!"
}
response = requests.post(f"{BASE_URL}/auth/register", json=register_data)
print(f"Status: {response.status_code}")
if response.status_code == 201:
data = response.json()
token = data["access_token"]
print(f"✅ Registration successful!")
print(f" User: {data['user']['username']}")
print(f" Token: {token[:50]}...")
print()
# Test 2: Get Profile
print("2. Testing Protected Route (Get Profile)...")
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(f"{BASE_URL}/auth/me", headers=headers)
print(f"Status: {response.status_code}")
if response.status_code == 200:
user = response.json()
print(f"✅ Profile retrieved!")
print(f" Username: {user['username']}")
print(f" Email: {user['email']}")
print()
else:
print(f"❌ Failed: {response.text}")
return
# Test 3: Login
print("3. Testing Login...")
login_data = {
"email": "trader@example.com",
"password": "SecurePass123!"
}
response = requests.post(f"{BASE_URL}/auth/login", json=login_data)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
new_token = data["access_token"]
print(f"✅ Login successful!")
print(f" New Token: {new_token[:50]}...")
print()
else:
print(f"❌ Failed: {response.text}")
# Test 4: Wrong Password
print("4. Testing Login with Wrong Password...")
wrong_login = {
"email": "trader@example.com",
"password": "WrongPassword"
}
response = requests.post(f"{BASE_URL}/auth/login", json=wrong_login)
print(f"Status: {response.status_code}")
if response.status_code == 401:
print(f"✅ Correctly rejected invalid credentials")
print()
else:
print(f"❌ Unexpected response")
print("=" * 60)
print("✅ All Authentication Tests Passed!")
print("=" * 60)
elif response.status_code == 400:
print(f"⚠️ User already exists (run script again to test login)")
print()
# Just test login if user exists
print("Testing Login with existing user...")
login_data = {
"email": "trader@example.com",
"password": "SecurePass123!"
}
response = requests.post(f"{BASE_URL}/auth/login", json=login_data)
if response.status_code == 200:
data = response.json()
print(f"✅ Login successful!")
print(f" User: {data['user']['username']}")
print(f" Token: {data['access_token'][:50]}...")
else:
print(f"❌ Login failed: {response.text}")
else:
print(f"❌ Registration failed: {response.text}")
if __name__ == "__main__":
try:
test_auth()
except requests.exceptions.ConnectionError:
print("❌ Cannot connect to server. Make sure it's running:")
print(" python -m uvicorn backend.main:app --reload --port 8000")
except Exception as e:
print(f"❌ Error: {e}")