forked from supabase/supabase-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_client.py
91 lines (60 loc) · 3.31 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
from __future__ import annotations
import os
from typing import Any
from unittest.mock import MagicMock
import pytest
from supabase import Client, create_client
from supabase.lib.client_options import ClientOptions
@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."""
_: Client = create_client(url, key)
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")
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