Rag_SaaS_Sys_V4 / scripts /test_auth_api.py
GodRad's picture
Upload 116 files
53ed279 verified
Raw History Blame Contribute Delete
3.27 kB
import requests
import time
import sys
import os
BASE_URL = "http://localhost:7860/v1"
def test_admin_auth():
print("Testing Admin Auth...")
# 1. Register Admin (if not exists)
reg_data = {
"username": "admin_test",
"email": "admin@test.com",
"password": "password123",
"registration_secret": "change-this-secret-in-production"
}
requests.post(f"{BASE_URL}/admin/auth/register", json=reg_data)
# 2. Login
login_data = {"username": "admin_test", "password": "password123"}
resp = requests.post(f"{BASE_URL}/admin/auth/login", json=login_data)
if resp.status_code != 200:
print(f"βœ— Admin login failed: {resp.text}")
return
token = resp.json()["access_token"]
print("βœ“ Admin login success")
# 3. Test /me
headers = {"Authorization": f"Bearer {token}"}
resp = requests.get(f"{BASE_URL}/admin/auth/me", headers=headers)
if resp.status_code == 200:
print(f"βœ“ Admin /me success: {resp.json()['username']}")
else:
print(f"βœ— Admin /me failed: {resp.text}")
def test_tenant_auth():
print("\nTesting Tenant Auth...")
# 1. Register Tenant
reg_data = {
"name": "Test Tenant",
"admin_email": "tenant@test.com",
"admin_password": "password123"
}
resp = requests.post(f"{BASE_URL}/auth/tenants", json=reg_data)
if resp.status_code != 200:
print(f"βœ— Tenant registration failed: {resp.text}")
return
tenant_id = resp.json()["tenant_id"]
print(f"βœ“ Tenant registered: {tenant_id}")
# 2. Approve Tenant (needs Admin)
# We'll use the admin token from previous test if available, or fetch again
login_data = {"username": "admin_test", "password": "password123"}
admin_token = requests.post(f"{BASE_URL}/admin/auth/login", json=login_data).json()["access_token"]
admin_headers = {"Authorization": f"Bearer {admin_token}"}
approval_data = {"status": "approved"}
resp = requests.post(f"{BASE_URL}/admin/tenants/{tenant_id}/approve", json=approval_data, headers=admin_headers)
if resp.status_code != 200:
print(f"βœ— Tenant approval failed: {resp.text}")
return
print("βœ“ Tenant approved")
# 3. Login Tenant
login_data = {
"tenant_id": tenant_id,
"username": "tenant@test.com",
"password": "password123"
}
resp = requests.post(f"{BASE_URL}/auth/login", json=login_data)
if resp.status_code != 200:
print(f"βœ— Tenant login failed: {resp.text}")
return
token = resp.json()["access_token"]
print("βœ“ Tenant login success")
# 4. Test /me
headers = {"Authorization": f"Bearer {token}"}
resp = requests.get(f"{BASE_URL}/auth/me", headers=headers)
if resp.status_code == 200:
print(f"βœ“ Tenant /me success: {resp.json()['tenant_id']}")
else:
print(f"βœ— Tenant /me failed: {resp.text}")
if __name__ == "__main__":
try:
test_admin_auth()
test_tenant_auth()
except Exception as e:
print(f"Error connecting to server: {e}. Is the server running on 7860?")