test_app_user_profile.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python3
  2. import requests
  3. import json
  4. # Test admin user profile to see roles
  5. login_data = {
  6. 'username': 'admin',
  7. 'password': 'Admin123456'
  8. }
  9. try:
  10. # Login as admin user
  11. login_response = requests.post('http://localhost:8000/api/v1/auth/login', json=login_data)
  12. if login_response.status_code == 200:
  13. login_result = login_response.json()
  14. if login_result.get('code') == 0:
  15. token = login_result['data']['access_token']
  16. print('✅ Admin user login successful')
  17. # Get user profile
  18. headers = {'Authorization': f'Bearer {token}'}
  19. profile_response = requests.get('http://localhost:8000/api/v1/users/profile', headers=headers)
  20. if profile_response.status_code == 200:
  21. profile_result = profile_response.json()
  22. if profile_result.get('code') == 0:
  23. profile = profile_result['data']
  24. print('✅ Admin user profile:')
  25. print(f' Username: {profile.get("username")}')
  26. print(f' Email: {profile.get("email")}')
  27. print(f' Is Superuser: {profile.get("is_superuser")}')
  28. print(f' Is Active: {profile.get("is_active")}')
  29. print(f' Roles: {profile.get("roles", [])}')
  30. else:
  31. print(f'❌ Profile API error: {profile_result.get("message")}')
  32. else:
  33. print(f'❌ Profile API HTTP error: {profile_response.status_code}')
  34. else:
  35. print(f'❌ Login error: {login_result.get("message")}')
  36. else:
  37. print(f'❌ Login HTTP error: {login_response.status_code}')
  38. except Exception as e:
  39. print(f'❌ Request error: {e}')
  40. # Now test with app user (need to find correct password first)
  41. print('\n' + '='*50)
  42. print('Testing app user...')
  43. # Try common passwords for app user
  44. app_passwords = ['App123456', 'app123456', 'password', '123456', 'admin']
  45. for password in app_passwords:
  46. try:
  47. login_data = {'username': 'app', 'password': password}
  48. login_response = requests.post('http://localhost:8000/api/v1/auth/login', json=login_data)
  49. if login_response.status_code == 200:
  50. login_result = login_response.json()
  51. if login_result.get('code') == 0:
  52. token = login_result['data']['access_token']
  53. print(f'✅ App user login successful with password: {password}')
  54. # Get user profile
  55. headers = {'Authorization': f'Bearer {token}'}
  56. profile_response = requests.get('http://localhost:8000/api/v1/users/profile', headers=headers)
  57. if profile_response.status_code == 200:
  58. profile_result = profile_response.json()
  59. if profile_result.get('code') == 0:
  60. profile = profile_result['data']
  61. print('✅ App user profile:')
  62. print(f' Username: {profile.get("username")}')
  63. print(f' Email: {profile.get("email")}')
  64. print(f' Is Superuser: {profile.get("is_superuser")}')
  65. print(f' Is Active: {profile.get("is_active")}')
  66. print(f' Roles: {profile.get("roles", [])}')
  67. break
  68. else:
  69. print(f'❌ Profile API error: {profile_result.get("message")}')
  70. else:
  71. print(f'❌ Profile API HTTP error: {profile_response.status_code}')
  72. break
  73. except Exception as e:
  74. continue
  75. else:
  76. print('❌ Could not find correct password for app user')