File size: 3,267 Bytes
53ed279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

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?")