-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_hosting.py
More file actions
217 lines (176 loc) ยท 6.44 KB
/
Copy pathtest_hosting.py
File metadata and controls
217 lines (176 loc) ยท 6.44 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from __future__ import annotations
import json
from unittest.mock import mock_open
import click
import pytest
from pytest_mock import MockerFixture, MockFixture
from reflex_cli.utils.hosting import (
ScaleParams,
ScaleType,
authenticated_token,
delete_token_from_config,
get_authenticated_client,
get_existing_access_token,
get_selected_project,
normalize_project_id,
save_token_to_config,
)
@pytest.mark.parametrize(
"config_content, expected_token",
[
('{"access_token": "valid_token"}', "valid_token"),
("{}", ""),
(None, ""),
],
)
def test_get_existing_access_token(
mocker: MockerFixture, config_content: str | None, expected_token: str
):
mocker.patch("os.environ.get", return_value="")
mocker.patch("pathlib.Path.open", mock_open(read_data=config_content))
assert get_existing_access_token() == expected_token
mocker.patch("pathlib.Path.open", side_effect=FileNotFoundError("Test exception"))
assert get_existing_access_token() == ""
@pytest.mark.parametrize(
"file_exists, config_content",
[
(True, '{"access_token": "valid_token"}'),
(True, '{"another_key": "value"}'),
(False, ""),
],
)
def test_delete_token_from_config(
mocker: MockerFixture,
file_exists: bool,
config_content: str,
):
mocker.patch("pathlib.Path.exists", return_value=file_exists)
mock_os_remove = mocker.patch("pathlib.Path.unlink")
mocked_open = mock_open(read_data=config_content)
mocker.patch("pathlib.Path.open", mocked_open)
mock_json_load = mocker.patch(
"json.load", return_value=json.loads(config_content or "{}")
)
mock_json_dump = mocker.patch("json.dump")
delete_token_from_config()
if file_exists:
assert mocked_open.call_count == 2
mock_json_load.assert_called_once()
mock_json_dump.assert_called_once()
assert "access_token" not in mock_json_dump.call_args.args[0]
mock_os_remove.assert_called_once()
else:
mocked_open.assert_not_called()
mock_os_remove.assert_not_called()
def test_save_token_to_config(mocker: MockFixture):
mocker.patch("pathlib.Path.exists", return_value=False)
mock_makedirs = mocker.patch("pathlib.Path.mkdir")
save_token_to_config("test_token")
mock_makedirs.assert_called_once()
mocker.patch("pathlib.Path.exists", return_value=True)
mock_json_dump = mocker.patch("json.dump")
mocker.patch("pathlib.Path.open", mock_open())
save_token_to_config("test_token")
mock_json_dump.assert_called_once()
def test_authenticated_token_found_and_valid(mocker: MockFixture):
mocker.patch(
"reflex_cli.utils.hosting.get_existing_access_token",
return_value="valid_token",
)
mocker.patch(
"reflex_cli.utils.hosting.validate_token", return_value={"user_info": True}
)
token = authenticated_token()
assert token == ("valid_token", {"user_info": True})
def test_authenticated_token_not_found(mocker: MockFixture):
mocker.patch("reflex_cli.utils.hosting.get_existing_access_token", return_value="")
token = authenticated_token()
assert token == ("", {})
def test_authenticated_token_found_but_invalid(mocker: MockFixture):
mocker.patch(
"reflex_cli.utils.hosting.get_existing_access_token",
return_value="invalid_token",
)
mocker.patch(
"reflex_cli.utils.hosting.validate_token",
side_effect=ValueError("access denied"),
)
mocker.patch(
"reflex_cli.constants.hosting.Hosting.AUTH_RETRY_LIMIT", return_value=1
)
token = authenticated_token()
assert token == ("", {})
def test_authenticated_token_found_but_validation_fails(mocker: MockFixture):
mocker.patch(
"reflex_cli.utils.hosting.get_existing_access_token",
return_value="invalid_token",
)
mocker.patch(
"reflex_cli.utils.hosting.validate_token",
side_effect=ValueError("server error"),
)
mocker.patch(
"reflex_cli.utils.hosting.authenticate_on_browser",
return_value="new_valid_token",
)
mock_delete_token = mocker.patch(
"reflex_cli.utils.hosting.delete_token_from_config"
)
token = authenticated_token()
assert token == ("", {})
mock_delete_token.assert_called_once()
def test_authenticate_without_token_in_non_interactive_mode(mocker: MockerFixture):
mocker.patch("reflex_cli.utils.hosting.get_existing_access_token", return_value="")
with pytest.raises(click.exceptions.Exit):
get_authenticated_client(token=None, interactive=False)
def test_authenticate_with_env_token_in_non_interactive_mode(mocker: MockerFixture):
mocker.patch(
"reflex_cli.utils.hosting.get_existing_access_token", return_value="env_token"
)
mock_get_auth_client = mocker.patch(
"reflex_cli.utils.hosting.get_authentication_client"
)
mock_authenticated_client = mocker.MagicMock()
mock_get_auth_client.return_value = mock_authenticated_client
result = get_authenticated_client(token=None, interactive=False)
assert result == mock_authenticated_client
mock_get_auth_client.assert_called_once_with(None)
def test_scale_params_as_json_is_pure_when_type_is_unspecified():
"""ScaleParams.as_json should not mutate type when defaulting scale type."""
scale_params = ScaleParams(vm_type="shared-1x")
first = scale_params.as_json()
second = scale_params.as_json()
assert scale_params.type is None
assert first == second == {"type": ScaleType.REGION.value, "regions": {}}
@pytest.mark.parametrize(
"config_content, expected",
[
('{"project": "abc-uuid"}', "abc-uuid"),
('{"project": ""}', None),
('{"project": " "}', None),
('{"project": null}', None),
('{"project": 123}', None),
('{"project": []}', None),
("{}", None),
],
)
def test_get_selected_project_normalizes_empty_to_none(
mocker: MockerFixture, config_content: str, expected: str | None
):
mocker.patch("pathlib.Path.open", mock_open(read_data=config_content))
assert get_selected_project() == expected
@pytest.mark.parametrize(
"value, expected",
[
("abc-uuid", "abc-uuid"),
(" abc-uuid ", "abc-uuid"),
("", None),
(" ", None),
(None, None),
(123, None),
([], None),
({}, None),
],
)
def test_normalize_project_id(value: object, expected: str | None):
assert normalize_project_id(value) == expected