11from __future__ import annotations
22
3- import random
4- import string
5- from typing import TYPE_CHECKING , Any , Union
3+ from typing import Any
64
75import pytest
8- from gotrue import Session , User
9-
10- if TYPE_CHECKING :
11- from supabase import Client
12-
13-
14- def _random_string (length : int = 10 ) -> str :
15- """Generate random string."""
16- return "" .join (random .choices (string .ascii_uppercase + string .digits , k = length ))
17-
18-
19- def _assert_authenticated_user (data : Union [Session , User , str , None ]) -> None :
20- """Raise assertion error if user is not logged in correctly."""
21- assert data is not None
22- assert isinstance (data , Session )
23- assert data .user is not None
24- assert data .user .aud == "authenticated"
256
267
278@pytest .mark .xfail (
@@ -34,85 +15,3 @@ def test_incorrect_values_dont_instanciate_client(url: Any, key: Any) -> None:
3415 from supabase import Client , create_client
3516
3617 _ : Client = create_client (url , key )
37-
38-
39- @pytest .mark .skip (reason = "TO FIX: Session does not terminate with test included." )
40- def test_client_auth (supabase : Client ) -> None :
41- """Ensure we can create an auth user, and login with it."""
42- # Create a random user login email and password.
43- random_email = f"{ _random_string (10 )} @supamail.com"
44- random_password = _random_string (20 )
45- # Sign up (and sign in).
46- user = supabase .auth .sign_up (
47- email = random_email ,
48- password = random_password ,
49- phone = None ,
50- )
51- _assert_authenticated_user (user )
52- # Sign out.
53- supabase .auth .sign_out ()
54- assert supabase .auth .user () is None
55- assert supabase .auth .session () is None
56- # Sign in (explicitly this time).
57- user = supabase .auth .sign_in (email = random_email , password = random_password )
58- _assert_authenticated_user (user )
59-
60-
61- def test_client_select (supabase : Client ) -> None :
62- """Ensure we can select data from a table."""
63- # TODO(fedden): Add this set back in (and expand on it) when postgrest and
64- # realtime libs are working.
65- data , _ = supabase .table ("countries" ).select ("*" ).execute ()
66- # Assert we pulled real data.
67- assert data
68-
69-
70- def test_client_insert (supabase : Client ) -> None :
71- """Ensure we can select data from a table."""
72- data , _ = supabase .table ("countries" ).select ("*" ).execute ()
73- # Assert we pulled real data.
74- previous_length = len (data )
75- new_row = {
76- "name" : "test name" ,
77- "iso2" : "test iso2" ,
78- "iso3" : "test iso3" ,
79- "local_name" : "test local name" ,
80- "continent" : None ,
81- }
82- result , _ = supabase .table ("countries" ).insert (new_row ).execute ()
83- # Check returned result for insert was valid.
84- assert result
85- data , _ = supabase .table ("countries" ).select ("*" ).execute ()
86- current_length = len (data )
87- # Ensure we've added a row remotely.
88- assert current_length == previous_length + 1
89-
90-
91- @pytest .mark .skip (reason = "missing permissions on test instance" )
92- def test_client_upload_file (supabase : Client ) -> None :
93- """Ensure we can upload files to a bucket"""
94-
95- TEST_BUCKET_NAME = "atestbucket"
96-
97- storage = supabase .storage ()
98- storage_file = storage .StorageFileAPI (TEST_BUCKET_NAME )
99-
100- filename = "test.jpeg"
101- filepath = f"tests/{ filename } "
102- mimetype = "image/jpeg"
103- options = {"contentType" : mimetype }
104-
105- storage_file .upload (filename , filepath , options )
106- files = storage_file .list ()
107- assert files
108-
109- image_info = None
110- for item in files :
111- if item .get ("name" ) == filename :
112- image_info = item
113- break
114-
115- assert image_info is not None
116- assert image_info .get ("metadata" , {}).get ("mimetype" ) == mimetype
117-
118- storage_file .remove ([filename ])
0 commit comments