askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_auth.py · 8.1 KB · 331 lines
| 1 | """ |
| 2 | Test the auth system endpoint. |
| 3 | |
| 4 | Tests: |
| 5 | - User registration |
| 6 | - Login |
| 7 | - Token validation |
| 8 | - Password change |
| 9 | - Logout |
| 10 | - Account deletion |
| 11 | """ |
| 12 | |
| 13 | import requests |
| 14 | import json |
| 15 | |
| 16 | BASE_URL = 'http://localhost:8001' |
| 17 | |
| 18 | |
| 19 | def test_register_user(): |
| 20 | """Test user registration.""" |
| 21 | print('\n=== Test 1: Register User ===') |
| 22 | |
| 23 | response = requests.post( |
| 24 | f'{BASE_URL}/objects/advanced_auth', |
| 25 | json={ |
| 26 | 'action': 'register', |
| 27 | 'username': 'alice', |
| 28 | 'password': 'secret123', |
| 29 | 'email': 'alice@example.com', |
| 30 | } |
| 31 | ) |
| 32 | |
| 33 | print(f'Status: {response.status_code}') |
| 34 | data = response.json() |
| 35 | print(f'Response: {json.dumps(data, indent=2)}') |
| 36 | |
| 37 | assert response.status_code == 200 |
| 38 | assert data['status'] == 'ok' |
| 39 | assert 'token' in data |
| 40 | assert data['username'] == 'alice' |
| 41 | |
| 42 | print('✅ User registration works') |
| 43 | return data['token'] |
| 44 | |
| 45 | |
| 46 | def test_login_user(): |
| 47 | """Test user login.""" |
| 48 | print('\n=== Test 2: Login User ===') |
| 49 | |
| 50 | response = requests.post( |
| 51 | f'{BASE_URL}/objects/advanced_auth', |
| 52 | json={ |
| 53 | 'action': 'login', |
| 54 | 'username': 'alice', |
| 55 | 'password': 'secret123', |
| 56 | } |
| 57 | ) |
| 58 | |
| 59 | print(f'Status: {response.status_code}') |
| 60 | data = response.json() |
| 61 | print(f'Response: {json.dumps(data, indent=2)}') |
| 62 | |
| 63 | assert response.status_code == 200 |
| 64 | assert data['status'] == 'ok' |
| 65 | assert 'token' in data |
| 66 | assert data['username'] == 'alice' |
| 67 | |
| 68 | print('✅ Login works') |
| 69 | return data['token'] |
| 70 | |
| 71 | |
| 72 | def test_validate_token(token): |
| 73 | """Test token validation.""" |
| 74 | print('\n=== Test 3: Validate Token ===') |
| 75 | |
| 76 | response = requests.get( |
| 77 | f'{BASE_URL}/objects/advanced_auth', |
| 78 | params={'token': token} |
| 79 | ) |
| 80 | |
| 81 | print(f'Status: {response.status_code}') |
| 82 | data = response.json() |
| 83 | print(f'Response: {json.dumps(data, indent=2)}') |
| 84 | |
| 85 | assert response.status_code == 200 |
| 86 | assert data['status'] == 'ok' |
| 87 | assert data['user']['username'] == 'alice' |
| 88 | assert data['user']['email'] == 'alice@example.com' |
| 89 | |
| 90 | print('✅ Token validation works') |
| 91 | |
| 92 | |
| 93 | def test_invalid_token(): |
| 94 | """Test invalid token.""" |
| 95 | print('\n=== Test 4: Invalid Token ===') |
| 96 | |
| 97 | response = requests.get( |
| 98 | f'{BASE_URL}/objects/advanced_auth', |
| 99 | params={'token': 'invalid_token_xyz'} |
| 100 | ) |
| 101 | |
| 102 | print(f'Status: {response.status_code}') |
| 103 | data = response.json() |
| 104 | print(f'Response: {json.dumps(data, indent=2)}') |
| 105 | |
| 106 | assert response.status_code == 200 |
| 107 | assert data['status'] == 'error' |
| 108 | assert 'invalid' in data['message'].lower() |
| 109 | |
| 110 | print('✅ Invalid token detection works') |
| 111 | |
| 112 | |
| 113 | def test_change_email(token): |
| 114 | """Test changing email.""" |
| 115 | print('\n=== Test 5: Change Email ===') |
| 116 | |
| 117 | response = requests.put( |
| 118 | f'{BASE_URL}/objects/advanced_auth', |
| 119 | json={ |
| 120 | 'token': token, |
| 121 | 'email': 'alice.new@example.com', |
| 122 | } |
| 123 | ) |
| 124 | |
| 125 | print(f'Status: {response.status_code}') |
| 126 | data = response.json() |
| 127 | print(f'Response: {json.dumps(data, indent=2)}') |
| 128 | |
| 129 | assert response.status_code == 200 |
| 130 | assert data['status'] == 'ok' |
| 131 | |
| 132 | # Verify email changed |
| 133 | response = requests.get( |
| 134 | f'{BASE_URL}/objects/advanced_auth', |
| 135 | params={'token': token} |
| 136 | ) |
| 137 | data = response.json() |
| 138 | assert data['user']['email'] == 'alice.new@example.com' |
| 139 | |
| 140 | print('✅ Email change works') |
| 141 | |
| 142 | |
| 143 | def test_change_password(token): |
| 144 | """Test changing password.""" |
| 145 | print('\n=== Test 6: Change Password ===') |
| 146 | |
| 147 | response = requests.put( |
| 148 | f'{BASE_URL}/objects/advanced_auth', |
| 149 | json={ |
| 150 | 'token': token, |
| 151 | 'old_password': 'secret123', |
| 152 | 'password': 'newsecret456', |
| 153 | } |
| 154 | ) |
| 155 | |
| 156 | print(f'Status: {response.status_code}') |
| 157 | data = response.json() |
| 158 | print(f'Response: {json.dumps(data, indent=2)}') |
| 159 | |
| 160 | assert response.status_code == 200 |
| 161 | assert data['status'] == 'ok' |
| 162 | |
| 163 | # Verify can login with new password |
| 164 | response = requests.post( |
| 165 | f'{BASE_URL}/objects/advanced_auth', |
| 166 | json={ |
| 167 | 'action': 'login', |
| 168 | 'username': 'alice', |
| 169 | 'password': 'newsecret456', |
| 170 | } |
| 171 | ) |
| 172 | data = response.json() |
| 173 | assert data['status'] == 'ok' |
| 174 | |
| 175 | print('✅ Password change works') |
| 176 | return data['token'] |
| 177 | |
| 178 | |
| 179 | def test_logout(token): |
| 180 | """Test logout.""" |
| 181 | print('\n=== Test 7: Logout ===') |
| 182 | |
| 183 | response = requests.delete( |
| 184 | f'{BASE_URL}/objects/advanced_auth', |
| 185 | params={'token': token} |
| 186 | ) |
| 187 | |
| 188 | print(f'Status: {response.status_code}') |
| 189 | data = response.json() |
| 190 | print(f'Response: {json.dumps(data, indent=2)}') |
| 191 | |
| 192 | assert response.status_code == 200 |
| 193 | assert data['status'] == 'ok' |
| 194 | |
| 195 | # Verify token is now invalid |
| 196 | response = requests.get( |
| 197 | f'{BASE_URL}/objects/advanced_auth', |
| 198 | params={'token': token} |
| 199 | ) |
| 200 | data = response.json() |
| 201 | assert data['status'] == 'error' |
| 202 | |
| 203 | print('✅ Logout works') |
| 204 | |
| 205 | |
| 206 | def test_duplicate_registration(): |
| 207 | """Test duplicate registration.""" |
| 208 | print('\n=== Test 8: Duplicate Registration ===') |
| 209 | |
| 210 | response = requests.post( |
| 211 | f'{BASE_URL}/objects/advanced_auth', |
| 212 | json={ |
| 213 | 'action': 'register', |
| 214 | 'username': 'alice', |
| 215 | 'password': 'another_password', |
| 216 | 'email': 'another@example.com', |
| 217 | } |
| 218 | ) |
| 219 | |
| 220 | print(f'Status: {response.status_code}') |
| 221 | data = response.json() |
| 222 | print(f'Response: {json.dumps(data, indent=2)}') |
| 223 | |
| 224 | assert response.status_code == 200 |
| 225 | assert data['status'] == 'error' |
| 226 | assert 'already exists' in data['message'].lower() |
| 227 | |
| 228 | print('✅ Duplicate registration prevention works') |
| 229 | |
| 230 | |
| 231 | def test_view_logs(): |
| 232 | """Test viewing auth logs.""" |
| 233 | print('\n=== Test 9: View Auth Logs ===') |
| 234 | |
| 235 | response = requests.get( |
| 236 | f'{BASE_URL}/objects/advanced_auth', |
| 237 | params={'logs': 'true', 'limit': '10'} |
| 238 | ) |
| 239 | |
| 240 | print(f'Status: {response.status_code}') |
| 241 | data = response.json() |
| 242 | print(f'Response: {json.dumps(data, indent=2)}') |
| 243 | |
| 244 | assert response.status_code == 200 |
| 245 | assert 'logs' in data |
| 246 | |
| 247 | # Should have registration, login, logout events |
| 248 | print(f'Found {len(data["logs"])} log entries') |
| 249 | for log in data['logs'][:5]: |
| 250 | print(f' - {log.get("level", "INFO")}: {log.get("message", "")}') |
| 251 | |
| 252 | print('✅ Auth logging works') |
| 253 | |
| 254 | |
| 255 | def test_view_state(): |
| 256 | """Test viewing auth metadata.""" |
| 257 | print('\n=== Test 10: View Auth Metadata ===') |
| 258 | |
| 259 | response = requests.get( |
| 260 | f'{BASE_URL}/objects/advanced_auth', |
| 261 | params={'metadata': 'true'} |
| 262 | ) |
| 263 | |
| 264 | print(f'Status: {response.status_code}') |
| 265 | data = response.json() |
| 266 | print(f'Response: {json.dumps(data, indent=2)}') |
| 267 | |
| 268 | assert response.status_code == 200 |
| 269 | assert 'metadata' in data |
| 270 | |
| 271 | # Should have state_keys |
| 272 | metadata = data['metadata'] |
| 273 | print(f'State keys defined: {metadata.get("state_keys", [])}') |
| 274 | print(f'Log count: {metadata.get("log_count", 0)}') |
| 275 | print(f'Version count: {metadata.get("version_count", 0)}') |
| 276 | |
| 277 | assert 'state_keys' in metadata |
| 278 | assert 'users' in metadata['state_keys'] |
| 279 | assert 'tokens' in metadata['state_keys'] |
| 280 | |
| 281 | print('✅ Metadata introspection works') |
| 282 | |
| 283 | |
| 284 | if __name__ == '__main__': |
| 285 | print('Testing Auth System (Object Primitive Style)') |
| 286 | print('=' * 50) |
| 287 | |
| 288 | try: |
| 289 | # Test registration and get token |
| 290 | token1 = test_register_user() |
| 291 | |
| 292 | # Test login |
| 293 | token2 = test_login_user() |
| 294 | |
| 295 | # Test token validation |
| 296 | test_validate_token(token2) |
| 297 | |
| 298 | # Test invalid token |
| 299 | test_invalid_token() |
| 300 | |
| 301 | # Test email change |
| 302 | test_change_email(token2) |
| 303 | |
| 304 | # Test password change |
| 305 | token3 = test_change_password(token2) |
| 306 | |
| 307 | # Test logout |
| 308 | test_logout(token3) |
| 309 | |
| 310 | # Test duplicate registration |
| 311 | test_duplicate_registration() |
| 312 | |
| 313 | # Test logs |
| 314 | test_view_logs() |
| 315 | |
| 316 | # Test state |
| 317 | test_view_state() |
| 318 | |
| 319 | print('\n' + '=' * 50) |
| 320 | print('✅ All 10 auth tests passed!') |
| 321 | print('=' * 50) |
| 322 | |
| 323 | except AssertionError as e: |
| 324 | print(f'\n❌ Test failed: {e}') |
| 325 | raise |
| 326 | except requests.exceptions.ConnectionError: |
| 327 | print('\n❌ Error: Server not running') |
| 328 | print('Start server with: python run_server.py') |
| 329 | except Exception as e: |
| 330 | print(f'\n❌ Unexpected error: {e}') |
| 331 | raise |