-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_function_client.py
199 lines (143 loc) · 6.81 KB
/
test_function_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from unittest.mock import AsyncMock, Mock, patch
import pytest
from httpx import HTTPError, Response, Timeout
# Import the class to test
from supabase_functions import AsyncFunctionsClient
from supabase_functions.errors import FunctionsHttpError, FunctionsRelayError
from supabase_functions.utils import FunctionRegion
from supabase_functions.version import __version__
@pytest.fixture
def valid_url():
return "https://example.com"
@pytest.fixture
def default_headers():
return {"Authorization": "Bearer valid.jwt.token"}
@pytest.fixture
def client(valid_url, default_headers):
return AsyncFunctionsClient(
url=valid_url, headers=default_headers, timeout=10, verify=True
)
async def test_init_with_valid_params(valid_url, default_headers):
client = AsyncFunctionsClient(
url=valid_url, headers=default_headers, timeout=10, verify=True
)
assert client.url == valid_url
assert "User-Agent" in client.headers
assert client.headers["User-Agent"] == f"supabase-py/functions-py v{__version__}"
assert client._client.timeout == Timeout(10)
@pytest.mark.parametrize("invalid_url", ["not-a-url", "ftp://invalid.com", "", None])
def test_init_with_invalid_url(https://github.com/invalid_url,%20default_headers):
with pytest.raises(ValueError, match="url must be a valid HTTP URL string"):
AsyncFunctionsClient(url=invalid_url, headers=default_headers, timeout=10)
async def test_set_auth_valid_token(client: AsyncFunctionsClient):
valid_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
client.set_auth(valid_token)
assert client.headers["Authorization"] == f"Bearer {valid_token}"
async def test_set_auth_invalid_token(client: AsyncFunctionsClient):
invalid_token = "invalid-token"
with pytest.raises(
ValueError, match="token must be a valid JWT authorization token string."
):
client.set_auth(invalid_token)
async def test_invoke_success_json(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.json.return_value = {"message": "success"}
mock_response.raise_for_status = Mock()
mock_response.headers = {}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
result = await client.invoke(
"test-function", {"responseType": "json", "body": {"test": "data"}}
)
assert result == {"message": "success"}
mock_request.assert_called_once()
_, kwargs = mock_request.call_args
assert kwargs["json"] == {"test": "data"}
async def test_invoke_success_binary(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.content = b"binary content"
mock_response.raise_for_status = Mock()
mock_response.headers = {}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
result = await client.invoke("test-function")
assert result == b"binary content"
mock_request.assert_called_once()
async def test_invoke_with_region(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.json.return_value = {"message": "success"}
mock_response.raise_for_status = Mock()
mock_response.headers = {}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
await client.invoke("test-function", {"region": FunctionRegion("us-east-1")})
_, kwargs = mock_request.call_args
assert kwargs["headers"]["x-region"] == "us-east-1"
async def test_invoke_with_region_string(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.json.return_value = {"message": "success"}
mock_response.raise_for_status = Mock()
mock_response.headers = {}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
with pytest.warns(UserWarning, match=r"Use FunctionRegion\(us-east-1\)"):
await client.invoke("test-function", {"region": "us-east-1"})
_, kwargs = mock_request.call_args
assert kwargs["headers"]["x-region"] == "us-east-1"
async def test_invoke_with_http_error(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.json.return_value = {"error": "Custom error message"}
mock_response.raise_for_status.side_effect = HTTPError("HTTP Error")
mock_response.headers = {}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
with pytest.raises(FunctionsHttpError, match="Custom error message"):
await client.invoke("test-function")
async def test_invoke_with_relay_error(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.json.return_value = {"error": "Relay error message"}
mock_response.raise_for_status = Mock()
mock_response.headers = {"x-relay-header": "true"}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
with pytest.raises(FunctionsRelayError, match="Relay error message"):
await client.invoke("test-function")
async def test_invoke_invalid_function_name(client: AsyncFunctionsClient):
with pytest.raises(ValueError, match="function_name must a valid string value."):
await client.invoke("")
async def test_invoke_with_string_body(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.json.return_value = {"message": "success"}
mock_response.raise_for_status = Mock()
mock_response.headers = {}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
await client.invoke("test-function", {"body": "string data"})
_, kwargs = mock_request.call_args
assert kwargs["headers"]["Content-Type"] == "text/plain"
async def test_invoke_with_json_body(client: AsyncFunctionsClient):
mock_response = Mock(spec=Response)
mock_response.json.return_value = {"message": "success"}
mock_response.raise_for_status = Mock()
mock_response.headers = {}
with patch.object(
client._client, "request", new_callable=AsyncMock
) as mock_request:
mock_request.return_value = mock_response
await client.invoke("test-function", {"body": {"key": "value"}})
_, kwargs = mock_request.call_args
assert kwargs["headers"]["Content-Type"] == "application/json"