File size: 2,049 Bytes
e22cec2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from supabase import create_client, Client
from dotenv import load_dotenv

# Load credentials from your .env file
load_dotenv()

url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_ANON_KEY")

if not url or not key:
    print("Error: Missing Supabase credentials in .env")
    exit(1)

supabase: Client = create_client(url, key)

# --- CONFIGURATION ---
# Replace these with the email/password of the test user you created in Supabase Auth
TEST_EMAIL = "test@tuklascope.com"
TEST_PASSWORD = "123"


def test_storage():
    try:
        # 1. Authenticate as the user (Simulating Flutter login)
        print("Logging in...")
        auth_response = supabase.auth.sign_in_with_password(
            {"email": TEST_EMAIL, "password": TEST_PASSWORD}
        )
        user_id = auth_response.user.id
        print(f"Success! Authenticated as User ID: {user_id}")

        # 2. Define the exact path dictated by our RLS policy: scans/{user_id}/dummy.jpg
        file_path = f"{user_id}/dummy.jpg"

        # 3. Attempt the Upload
        print(f"Attempting to upload to: scans/{file_path}")
        with open("dummy.jpg", "rb") as f:
            # We use file_options to set the content-type so it renders as an image in browsers
            supabase.storage.from_("scans").upload(
                file=f,
                path=file_path,
                file_options={"content-type": "image/jpeg"}
            )

        print("Upload successful! RLS allowed the insert.")

        # 4. Generate the Public URL (Simulating what Flutter sends to the /save endpoint)
        public_url = supabase.storage.from_("scans").get_public_url(file_path)
        print(f"\nPublic URL generated:\n{public_url}")
        print("\nTest passed. You can now use this URL in your curl /save payload.")

    except Exception as e:
        print(f"\nTest Failed: {str(e)}")
        print("If it says 'new row violates row-level security policy', your folder path or auth state is wrong.")


if __name__ == "__main__":
    test_storage()