-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_test.py
More file actions
82 lines (69 loc) · 3.08 KB
/
Copy pathquick_test.py
File metadata and controls
82 lines (69 loc) · 3.08 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
#!/usr/bin/env python3
"""
Quick test script for Rust JWT service
"""
import requests
import json
import sys
def quick_test(base_url="http://localhost:8080"):
"""Run a quick test of all endpoints"""
print(f"Testing JWT Authentication Service at {base_url}")
print("="*60)
# Test 1: Try to access protected route without token
print("\n1. Testing protected route without token...")
response = requests.get(f"{base_url}/api/protected")
print(f" Status: {response.status_code} (expected: 401)")
print(f" Response: {response.text[:100]}")
# Test 2: Register a new user
print("\n2. Registering new user...")
user_data = {
"username": "testuser_py",
"password": "testpassword123"
}
response = requests.post(f"{base_url}/api/auth/register", json=user_data)
print(f" Status: {response.status_code} (expected: 201 or 409 if exists)")
if response.status_code == 201:
print(" ✓ User registered successfully")
elif response.status_code == 409:
print(" ℹ User already exists (trying to login instead)")
else:
print(f" ✗ Unexpected response: {response.text}")
# Test 3: Login
print("\n3. Logging in...")
response = requests.post(f"{base_url}/api/auth/login", json=user_data)
print(f" Status: {response.status_code} (expected: 200)")
if response.status_code == 200:
data = response.json()
if data.get('success') and data.get('data', {}).get('token'):
token = data['data']['token']
print(f" ✓ Login successful")
print(f" Token: {token[:50]}...")
# Test 4: Access protected route with token
print("\n4. Accessing protected route with token...")
headers = {'Authorization': f'Bearer {token}'}
response = requests.get(f"{base_url}/api/protected", headers=headers)
print(f" Status: {response.status_code} (expected: 200)")
if response.status_code == 200:
protected_data = response.json()
print(f" ✓ Protected route accessed successfully")
print(f" Response: {json.dumps(protected_data, indent=2)}")
else:
print(f" ✗ Failed to access protected route: {response.text}")
else:
print(f" ✗ Login response missing token: {data}")
else:
print(f" ✗ Login failed: {response.text}")
# Test 5: Test invalid login
print("\n5. Testing invalid login...")
invalid_data = {
"username": "nonexistent",
"password": "wrongpassword"
}
response = requests.post(f"{base_url}/api/auth/login", json=invalid_data)
print(f" Status: {response.status_code} (expected: 401)")
print(f" ✓ Invalid credentials correctly rejected" if response.status_code == 401 else f" ✗ Unexpected response")
print("\n" + "="*60)
print("Test completed!")
if __name__ == "__main__":
base_url = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
quick_test(base_url)