-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathtest_client.py
166 lines (110 loc) · 5.58 KB
/
test_client.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from __future__ import annotations
import os
from typing import Any
from unittest.mock import MagicMock
import pytest
from gotrue import SyncMemoryStorage
from supabase import Client, ClientOptions, SupabaseException, create_client
@pytest.mark.xfail(
reason="None of these values should be able to instantiate a client object"
)
@pytest.mark.parametrize("url", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []])
@pytest.mark.parametrize("key", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []])
def test_incorrect_values_dont_instantiate_client(url: Any, key: Any) -> None:
"""Ensure we can't instantiate client with invalid values."""
try:
_: Client = create_client(url, key)
except SupabaseException as e:
pass
def test_function_initialization() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(url, key)
assert client.functions
def test_postgrest_schema() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(url, key)
assert client.postgrest
assert client.postgrest.schema("new_schema")
def test_rpc_client() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(url, key)
assert client.rpc("test_fn")
def test_uses_key_as_authorization_header_by_default() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(url, key)
assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == f"Bearer {key}"
assert client.postgrest.session.headers.get("apiKey") == key
assert client.postgrest.session.headers.get("Authorization") == f"Bearer {key}"
assert client.auth._headers.get("apiKey") == key
assert client.auth._headers.get("Authorization") == f"Bearer {key}"
assert client.storage.session.headers.get("apiKey") == key
assert client.storage.session.headers.get("Authorization") == f"Bearer {key}"
def test_supports_setting_a_global_authorization_header() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
authorization = f"Bearer secretuserjwt"
options = ClientOptions(headers={"Authorization": authorization})
client = create_client(url, key, options)
assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == authorization
assert client.postgrest.session.headers.get("apiKey") == key
assert client.postgrest.session.headers.get("Authorization") == authorization
assert client.auth._headers.get("apiKey") == key
assert client.auth._headers.get("Authorization") == authorization
assert client.storage.session.headers.get("apiKey") == key
assert client.storage.session.headers.get("Authorization") == authorization
def test_updates_the_authorization_header_on_auth_events() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(url, key)
assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == f"Bearer {key}"
mock_session = MagicMock(access_token="secretuserjwt")
realtime_mock = MagicMock()
client.realtime = realtime_mock
client._listen_to_auth_events("SIGNED_IN", mock_session)
updated_authorization = f"Bearer {mock_session.access_token}"
assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == updated_authorization
assert client.postgrest.session.headers.get("apiKey") == key
assert (
client.postgrest.session.headers.get("Authorization") == updated_authorization
)
assert client.auth._headers.get("apiKey") == key
assert client.auth._headers.get("Authorization") == updated_authorization
assert client.storage.session.headers.get("apiKey") == key
assert client.storage.session.headers.get("Authorization") == updated_authorization
def test_mutable_headers_issue():
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
shared_options = ClientOptions(
storage=SyncMemoryStorage(), headers={"Authorization": "Bearer initial-token"}
)
client1 = create_client(url, key, shared_options)
client2 = create_client(url, key, shared_options)
client1.options.headers["Authorization"] = "Bearer modified-token"
assert client2.options.headers["Authorization"] == "Bearer initial-token"
def test_global_authorization_header_issue():
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
authorization = "Bearer secretuserjwt"
options = ClientOptions(headers={"Authorization": authorization})
client = create_client(url, key, options)
assert client.options.headers.get("apiKey") == key
def test_mutable_headers_issue():
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
shared_options = ClientOptions(
headers={"Authorization": "Bearer initial-token", "x-site": "supanew.site"}
)
client1 = create_client(url, key, shared_options)
client2 = create_client(url, key, shared_options)
client1.options.replace({"headers": {"Authorization": "Bearer modified-token"}})
assert client2.options.headers["Authorization"] == "Bearer initial-token"
assert client2.options.headers["x-site"] == "supanew.site"
assert client1.options.headers["x-site"] == "supanew.site"