-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_supabase.py
More file actions
103 lines (91 loc) · 3.51 KB
/
test_supabase.py
File metadata and controls
103 lines (91 loc) · 3.51 KB
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
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""
Simple Supabase connection test script.
This script tests both the Supabase Python client and direct REST API access.
"""
import os
import requests
from dotenv import load_dotenv
from supabase import create_client
# Load environment variables
load_dotenv()
# Get Supabase credentials from environment and strip any whitespace
SUPABASE_URL = os.getenv("SUPABASE_URL", "").strip()
SUPABASE_ANON_KEY = os.getenv("SUPABASE_KEY", "").strip()
SUPABASE_SERVICE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY", "").strip()
print("=== Supabase Connection Test ===")
print(f"URL: {SUPABASE_URL}")
print(f"Anon Key: {SUPABASE_ANON_KEY[:10]}...{SUPABASE_ANON_KEY[-5:]}")
print(
f"Service Key: {SUPABASE_SERVICE_KEY[:10]}...{SUPABASE_SERVICE_KEY[-5:]}"
) # Line break
print("=" * 30)
# Test 1: Try with Python client and anon key
print("\n1. Testing with Python client and anon key:")
try:
client = create_client(SUPABASE_URL, SUPABASE_ANON_KEY)
print("✅ Client initialized successfully")
# Try a simple query
try:
response = client.table("_dummy_table_").select("*").limit(1).execute()
print("✅ Query executed successfully")
except Exception as e:
print(f"❌ Query failed (expected for non-existent table): {e}")
print("✅ But client connection was established")
except Exception as e:
print(f"❌ Failed to initialize client: {e}")
# Test 2: Try with Python client and service role key
print("\n2. Testing with Python client and service role key:")
try:
client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
print("✅ Client initialized successfully")
# Try a simple query
try:
response = client.table("_dummy_table_").select("*").limit(1).execute()
print("✅ Query executed successfully")
except Exception as e:
print(f"❌ Query failed (expected for non-existent table): {e}")
print("✅ But client connection was established")
except Exception as e:
print(f"❌ Failed to initialize client: {e}")
# Test 3: Direct REST API test with anon key
print("\n3. Testing direct REST API with anon key:")
headers = {
"apikey": SUPABASE_ANON_KEY,
"Authorization": f"Bearer {SUPABASE_ANON_KEY}",
"Content-Type": "application/json",
}
try:
response = requests.get(
f"{SUPABASE_URL}/rest/v1/_dummy_table_?limit=1", headers=headers
)
print(f"Status code: {response.status_code}")
if response.status_code == 200:
print("✅ API connection successful")
elif response.status_code == 404:
print("✅ API connection successful (404 expected for non-existent table)")
else:
print(f"❌ API connection failed: {response.text}")
except Exception as e:
print(f"❌ API request failed: {e}")
# Test 4: Direct REST API test with service role key
print("\n4. Testing direct REST API with service role key:")
headers = {
"apikey": SUPABASE_SERVICE_KEY,
"Authorization": f"Bearer {SUPABASE_SERVICE_KEY}",
"Content-Type": "application/json",
}
try:
response = requests.get(
f"{SUPABASE_URL}/rest/v1/_dummy_table_?limit=1", headers=headers
)
print(f"Status code: {response.status_code}")
if response.status_code == 200:
print("✅ API connection successful")
elif response.status_code == 404:
print("✅ API connection successful (404 expected for non-existent table)")
else:
print(f"❌ API connection failed: {response.text}")
except Exception as e:
print(f"❌ API request failed: {e}")
print("\n=== Test Complete ===")