Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
test_auth_comprehensive.py · 8.4 KB · 288 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Comprehensive auth service testing |
| 4 | Tests all auth operations including user management and access control |
| 5 | """ |
| 6 | |
| 7 | import requests |
| 8 | import json |
| 9 | import time |
| 10 | |
| 11 | BASE_URL = "http://localhost:8010" |
| 12 | |
| 13 | def test_health(): |
| 14 | """Test service health""" |
| 15 | print("Testing service health...") |
| 16 | r = requests.get(f"{BASE_URL}/health") |
| 17 | assert r.status_code == 200 |
| 18 | data = r.json() |
| 19 | assert data["status"] == "healthy" |
| 20 | assert data["port"] == 8010 |
| 21 | print("✅ Health check passed") |
| 22 | |
| 23 | def test_register(): |
| 24 | """Test user registration""" |
| 25 | print("\nTesting registration...") |
| 26 | |
| 27 | # Register user1 |
| 28 | user1 = { |
| 29 | "username": "alice", |
| 30 | "email": "alice@example.com", |
| 31 | "password": "alice123" |
| 32 | } |
| 33 | r = requests.post(f"{BASE_URL}/api/register", json=user1) |
| 34 | assert r.status_code == 200 |
| 35 | data = r.json() |
| 36 | assert data["success"] == True |
| 37 | print("✅ User 'alice' registered") |
| 38 | |
| 39 | # Register user2 |
| 40 | user2 = { |
| 41 | "username": "bob", |
| 42 | "email": "bob@example.com", |
| 43 | "password": "bob456" |
| 44 | } |
| 45 | r = requests.post(f"{BASE_URL}/api/register", json=user2) |
| 46 | assert r.status_code == 200 |
| 47 | data = r.json() |
| 48 | assert data["success"] == True |
| 49 | print("✅ User 'bob' registered") |
| 50 | |
| 51 | # Try duplicate username |
| 52 | dup = { |
| 53 | "username": "alice", |
| 54 | "email": "alice2@example.com", |
| 55 | "password": "test" |
| 56 | } |
| 57 | r = requests.post(f"{BASE_URL}/api/register", json=dup) |
| 58 | data = r.json() |
| 59 | assert data["success"] == False |
| 60 | print("✅ Duplicate username rejected") |
| 61 | |
| 62 | def test_login(): |
| 63 | """Test user login""" |
| 64 | print("\nTesting login...") |
| 65 | |
| 66 | # Login as alice |
| 67 | creds = { |
| 68 | "username": "alice", |
| 69 | "password": "alice123" |
| 70 | } |
| 71 | r = requests.post(f"{BASE_URL}/api/login", json=creds) |
| 72 | assert r.status_code == 200 |
| 73 | data = r.json() |
| 74 | assert data["success"] == True |
| 75 | assert "token" in data |
| 76 | alice_token = data["token"] |
| 77 | print(f"✅ Alice logged in, token: {alice_token[:20]}...") |
| 78 | |
| 79 | # Login as bob |
| 80 | creds = { |
| 81 | "username": "bob", |
| 82 | "password": "bob456" |
| 83 | } |
| 84 | r = requests.post(f"{BASE_URL}/api/login", json=creds) |
| 85 | assert r.status_code == 200 |
| 86 | data = r.json() |
| 87 | assert data["success"] == True |
| 88 | assert "token" in data |
| 89 | bob_token = data["token"] |
| 90 | print(f"✅ Bob logged in, token: {bob_token[:20]}...") |
| 91 | |
| 92 | # Wrong password |
| 93 | bad_creds = { |
| 94 | "username": "alice", |
| 95 | "password": "wrong" |
| 96 | } |
| 97 | r = requests.post(f"{BASE_URL}/api/login", json=bad_creds) |
| 98 | data = r.json() |
| 99 | assert data["success"] == False |
| 100 | print("✅ Wrong password rejected") |
| 101 | |
| 102 | # Login with email |
| 103 | email_creds = { |
| 104 | "username": "alice@example.com", |
| 105 | "password": "alice123" |
| 106 | } |
| 107 | r = requests.post(f"{BASE_URL}/api/login", json=email_creds) |
| 108 | data = r.json() |
| 109 | assert data["success"] == True |
| 110 | print("✅ Login with email works") |
| 111 | |
| 112 | return alice_token, bob_token |
| 113 | |
| 114 | def test_verify_token(alice_token, bob_token): |
| 115 | """Test token verification""" |
| 116 | print("\nTesting token verification...") |
| 117 | |
| 118 | # Verify alice token |
| 119 | headers = {"Authorization": f"Bearer {alice_token}"} |
| 120 | r = requests.get(f"{BASE_URL}/api/verify", headers=headers) |
| 121 | assert r.status_code == 200 |
| 122 | data = r.json() |
| 123 | assert data["success"] == True |
| 124 | assert data["user"]["username"] == "alice" |
| 125 | print("✅ Alice token verified") |
| 126 | |
| 127 | # Verify bob token |
| 128 | headers = {"Authorization": f"Bearer {bob_token}"} |
| 129 | r = requests.get(f"{BASE_URL}/api/verify", headers=headers) |
| 130 | assert r.status_code == 200 |
| 131 | data = r.json() |
| 132 | assert data["success"] == True |
| 133 | assert data["user"]["username"] == "bob" |
| 134 | print("✅ Bob token verified") |
| 135 | |
| 136 | # Invalid token |
| 137 | headers = {"Authorization": "Bearer invalid_token"} |
| 138 | r = requests.get(f"{BASE_URL}/api/verify", headers=headers) |
| 139 | data = r.json() |
| 140 | assert data["success"] == False |
| 141 | print("✅ Invalid token rejected") |
| 142 | |
| 143 | def test_change_password(alice_token): |
| 144 | """Test password change""" |
| 145 | print("\nTesting password change...") |
| 146 | |
| 147 | headers = {"Authorization": f"Bearer {alice_token}"} |
| 148 | payload = { |
| 149 | "old_password": "alice123", |
| 150 | "new_password": "newalice456" |
| 151 | } |
| 152 | r = requests.post(f"{BASE_URL}/api/change-password", json=payload, headers=headers) |
| 153 | assert r.status_code == 200 |
| 154 | data = r.json() |
| 155 | assert data["success"] == True |
| 156 | print("✅ Password changed") |
| 157 | |
| 158 | # Try login with new password |
| 159 | creds = { |
| 160 | "username": "alice", |
| 161 | "password": "newalice456" |
| 162 | } |
| 163 | r = requests.post(f"{BASE_URL}/api/login", json=creds) |
| 164 | data = r.json() |
| 165 | assert data["success"] == True |
| 166 | print("✅ Login with new password works") |
| 167 | |
| 168 | # Old password shouldn't work |
| 169 | creds = { |
| 170 | "username": "alice", |
| 171 | "password": "alice123" |
| 172 | } |
| 173 | r = requests.post(f"{BASE_URL}/api/login", json=creds) |
| 174 | data = r.json() |
| 175 | assert data["success"] == False |
| 176 | print("✅ Old password rejected") |
| 177 | |
| 178 | def test_logout(bob_token): |
| 179 | """Test logout""" |
| 180 | print("\nTesting logout...") |
| 181 | |
| 182 | # Logout |
| 183 | headers = {"Authorization": f"Bearer {bob_token}"} |
| 184 | r = requests.post(f"{BASE_URL}/api/logout", headers=headers) |
| 185 | assert r.status_code == 200 |
| 186 | data = r.json() |
| 187 | assert data["success"] == True |
| 188 | print("✅ Bob logged out") |
| 189 | |
| 190 | # Token should be invalid now |
| 191 | r = requests.get(f"{BASE_URL}/api/verify", headers=headers) |
| 192 | data = r.json() |
| 193 | assert data["success"] == False |
| 194 | print("✅ Token invalidated after logout") |
| 195 | |
| 196 | def test_admin_login(): |
| 197 | """Test admin account""" |
| 198 | print("\nTesting admin account...") |
| 199 | |
| 200 | creds = { |
| 201 | "username": "admin", |
| 202 | "password": "admin123" |
| 203 | } |
| 204 | r = requests.post(f"{BASE_URL}/api/login", json=creds) |
| 205 | assert r.status_code == 200 |
| 206 | data = r.json() |
| 207 | assert data["success"] == True |
| 208 | assert data["user"]["is_admin"] == True |
| 209 | print("✅ Admin login works") |
| 210 | return data["token"] |
| 211 | |
| 212 | def test_access_control_simulation(): |
| 213 | """Simulate access control between users""" |
| 214 | print("\nSimulating access control...") |
| 215 | |
| 216 | # This demonstrates how the auth service would be used |
| 217 | # In a real system, other services would verify tokens |
| 218 | |
| 219 | # Create alice session |
| 220 | alice_login = requests.post(f"{BASE_URL}/api/login", json={ |
| 221 | "username": "alice@example.com", |
| 222 | "password": "newalice456" |
| 223 | }) |
| 224 | alice_token = alice_login.json()["token"] |
| 225 | |
| 226 | # Create bob session |
| 227 | bob_reg = requests.post(f"{BASE_URL}/api/register", json={ |
| 228 | "username": "charlie", |
| 229 | "email": "charlie@example.com", |
| 230 | "password": "charlie789" |
| 231 | }) |
| 232 | |
| 233 | bob_login = requests.post(f"{BASE_URL}/api/login", json={ |
| 234 | "username": "charlie", |
| 235 | "password": "charlie789" |
| 236 | }) |
| 237 | charlie_token = bob_login.json()["token"] |
| 238 | |
| 239 | # Verify different users get different tokens |
| 240 | assert alice_token != charlie_token |
| 241 | print("✅ Different users get different tokens") |
| 242 | |
| 243 | # Verify token contains user info |
| 244 | alice_verify = requests.get(f"{BASE_URL}/api/verify", |
| 245 | headers={"Authorization": f"Bearer {alice_token}"}) |
| 246 | charlie_verify = requests.get(f"{BASE_URL}/api/verify", |
| 247 | headers={"Authorization": f"Bearer {charlie_token}"}) |
| 248 | |
| 249 | alice_user = alice_verify.json()["user"] |
| 250 | charlie_user = charlie_verify.json()["user"] |
| 251 | |
| 252 | assert alice_user["username"] == "alice" |
| 253 | assert charlie_user["username"] == "charlie" |
| 254 | assert alice_user["user_id"] != charlie_user["user_id"] |
| 255 | print("✅ Tokens correctly identify different users") |
| 256 | |
| 257 | def run_all_tests(): |
| 258 | """Run all tests""" |
| 259 | print("=" * 50) |
| 260 | print("COMPREHENSIVE AUTH SERVICE TESTING") |
| 261 | print("=" * 50) |
| 262 | |
| 263 | test_health() |
| 264 | test_register() |
| 265 | alice_token, bob_token = test_login() |
| 266 | test_verify_token(alice_token, bob_token) |
| 267 | test_change_password(alice_token) |
| 268 | test_logout(bob_token) |
| 269 | admin_token = test_admin_login() |
| 270 | test_access_control_simulation() |
| 271 | |
| 272 | print("\n" + "=" * 50) |
| 273 | print("✅ ALL AUTH TESTS PASSED!") |
| 274 | print("=" * 50) |
| 275 | print("\nAuth service features verified:") |
| 276 | print("- User registration with duplicate prevention") |
| 277 | print("- Login with username or email") |
| 278 | print("- JWT token generation and verification") |
| 279 | print("- Password change functionality") |
| 280 | print("- Session logout and invalidation") |
| 281 | print("- Admin account access") |
| 282 | print("- User isolation (different tokens for different users)") |
| 283 | print("\nThe auth service is ready for integration with other services!") |
| 284 | |
| 285 | if __name__ == "__main__": |
| 286 | # Wait a bit for service to be ready |
| 287 | time.sleep(2) |
| 288 | run_all_tests() |