-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathtest_client.py
139 lines (93 loc) · 4.59 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
from __future__ import annotations
import os
from typing import Any
from unittest.mock import MagicMock
import pytest
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_init_client_with_access_token() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")
client = create_client(
url, key, options=ClientOptions(access_token=lambda: "secretuserjwt")
)
assert client.access_token is not None
with pytest.raises(SupabaseException) as e:
client.auth.get_user()
assert (
str(e.value.message)
== "Supabase Client is configured with the access_token option, accessing supabase.auth.get_user is not possible."
)