Spaces:
Sleeping
Sleeping
Download scripts/test_auth_api.py from GodRad/Rag_SaaS_Sys_V4: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/spaces/GodRad/Rag_SaaS_Sys_V4/resolve/main/scripts/test_auth_api.py
- Command line
-
hf download hf://spaces/GodRad/Rag_SaaS_Sys_V4/scripts/test_auth_api.py
-
curl -L -o test_auth_api.py https://huggingface.co/spaces/GodRad/Rag_SaaS_Sys_V4/resolve/main/scripts/test_auth_api.py
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?") | |