test_regular_user_applications.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. import requests
  3. import json
  4. # Test regular user (zhangsan) applications access to ensure they only see their own
  5. login_data = {
  6. 'username': 'zhangsan',
  7. 'password': '123456'
  8. }
  9. try:
  10. # Login as zhangsan 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('✅ Zhangsan user login successful')
  17. # Get user profile to check roles
  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(f'👤 Zhangsan roles: {profile.get("roles", [])}')
  25. # Get applications list
  26. apps_response = requests.get('http://localhost:8000/api/v1/apps', headers=headers)
  27. if apps_response.status_code == 200:
  28. apps_result = apps_response.json()
  29. if apps_result.get('code') == 0:
  30. apps_data = apps_result['data']
  31. apps = apps_data.get('items', [])
  32. total = apps_data.get('total', 0)
  33. print(f'✅ Zhangsan can access applications API')
  34. print(f'📱 Zhangsan sees {len(apps)} applications (total: {total})')
  35. if apps:
  36. for app in apps:
  37. print(f' App: {app["name"]} (Key: {app["app_key"]})')
  38. print(f' Description: {app.get("description", "N/A")}')
  39. print()
  40. print('⚠️ Regular user should only see their own applications!')
  41. else:
  42. print('✅ Regular user sees no applications (correct - they have not created any)')
  43. else:
  44. print(f'❌ Applications API error: {apps_result.get("message")}')
  45. else:
  46. print(f'❌ Applications API HTTP error: {apps_response.status_code}')
  47. else:
  48. print(f'❌ Login error: {login_result.get("message")}')
  49. else:
  50. print(f'❌ Login HTTP error: {login_response.status_code}')
  51. except Exception as e:
  52. print(f'❌ Request error: {e}')