-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest_oauth_endpoints.py
More file actions
169 lines (129 loc) · 4.44 KB
/
test_oauth_endpoints.py
File metadata and controls
169 lines (129 loc) · 4.44 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
#!/usr/bin/env python3
"""
Test OAuth endpoints locally
Usage:
python test_oauth_endpoints.py
"""
import requests
import json
# Adjust this to your local Flask server
BASE_URL = "http://localhost:5000"
def test_github_oauth():
"""Test GitHub OAuth endpoint"""
print("\n🧪 Testing GitHub OAuth endpoint...")
payload = {
"provider": "github",
"providerId": "test_12345678",
"email": "test_github@example.com",
"name": "Test GitHub User",
"image": "https://avatars.githubusercontent.com/u/12345678",
}
try:
response = requests.post(
f"{BASE_URL}/api/v1/user/auth/oauth/github",
json=payload,
headers={"Content-Type": "application/json"},
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
if response.status_code in [200, 201]:
print("✅ GitHub OAuth endpoint works!")
return response.json().get("access_token")
else:
print("❌ GitHub OAuth endpoint failed")
return None
except Exception as e:
print(f"❌ Error: {e}")
return None
def test_google_oauth():
"""Test Google OAuth endpoint"""
print("\n🧪 Testing Google OAuth endpoint...")
payload = {
"provider": "google",
"providerId": "test_1234567890",
"email": "test_google@gmail.com",
"name": "Test Google User",
"image": "https://lh3.googleusercontent.com/test",
}
try:
response = requests.post(
f"{BASE_URL}/api/v1/user/auth/oauth/google",
json=payload,
headers={"Content-Type": "application/json"},
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
if response.status_code in [200, 201]:
print("✅ Google OAuth endpoint works!")
return response.json().get("access_token")
else:
print("❌ Google OAuth endpoint failed")
return None
except Exception as e:
print(f"❌ Error: {e}")
return None
def test_existing_user():
"""Test OAuth with existing user (same email)"""
print("\n🧪 Testing OAuth with existing user...")
# Use same email as first test
payload = {
"provider": "github",
"providerId": "different_id_789",
"email": "test_github@example.com", # Same email as before
"name": "Same User Different Provider",
"image": "https://example.com/image.jpg",
}
try:
response = requests.post(
f"{BASE_URL}/api/v1/user/auth/oauth/github",
json=payload,
headers={"Content-Type": "application/json"},
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
if response.status_code == 200:
print("✅ Existing user login works!")
else:
print("⚠️ Expected 200 for existing user")
except Exception as e:
print(f"❌ Error: {e}")
def verify_token(token):
"""Verify JWT token works with protected endpoint"""
if not token:
print("\n⚠️ No token to verify")
return
print("\n🧪 Testing token with /verifytoken endpoint...")
try:
response = requests.get(
f"{BASE_URL}/api/v1/user/verifytoken",
headers={"Authorization": f"Bearer {token}"},
)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
if response.status_code == 200:
print("✅ JWT token is valid!")
else:
print("❌ Token verification failed")
except Exception as e:
print(f"❌ Error: {e}")
def main():
print("=" * 60)
print("🚀 OAuth Endpoints Test Suite")
print("=" * 60)
print(f"\nTesting against: {BASE_URL}")
print("\n⚠️ Make sure your Flask server is running!")
print(" Run: cd server && flask run")
# Test GitHub OAuth (new user)
github_token = test_github_oauth()
# Test Google OAuth (new user)
google_token = test_google_oauth()
# Test existing user
test_existing_user()
# Verify one of the tokens
if github_token:
verify_token(github_token)
print("\n" + "=" * 60)
print("✅ Testing complete!")
print("=" * 60)
if __name__ == "__main__":
main()