matlab-proxy 0.26.0__py3-none-any.whl → 0.27.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of matlab-proxy might be problematic. Click here for more details.
- matlab_proxy/app.py +50 -39
- matlab_proxy/app_state.py +9 -3
- matlab_proxy/default_configuration.py +2 -2
- matlab_proxy/gui/index.html +2 -2
- matlab_proxy/gui/static/css/{index.BSVLACuY.css → index.BLxKpbak.css} +2 -2
- matlab_proxy/gui/static/js/{index.CZgGkMCD.js → index.CKi3IRxe.js} +16 -16
- matlab_proxy/settings.py +5 -5
- matlab_proxy/util/__init__.py +2 -2
- matlab_proxy/util/list_servers.py +2 -2
- matlab_proxy/util/mwi/environment_variables.py +5 -0
- matlab_proxy/util/mwi/logger.py +1 -2
- matlab_proxy/util/mwi/session_name.py +28 -0
- matlab_proxy/util/mwi/token_auth.py +2 -2
- matlab_proxy/util/mwi/validators.py +3 -5
- {matlab_proxy-0.26.0.dist-info → matlab_proxy-0.27.1.dist-info}/METADATA +37 -25
- {matlab_proxy-0.26.0.dist-info → matlab_proxy-0.27.1.dist-info}/RECORD +23 -53
- {matlab_proxy-0.26.0.dist-info → matlab_proxy-0.27.1.dist-info}/WHEEL +1 -2
- matlab_proxy_manager/README.md +85 -0
- matlab_proxy_manager/lib/README.md +53 -0
- matlab_proxy_manager/storage/README.md +54 -0
- matlab_proxy_manager/web/README.md +37 -0
- matlab_proxy-0.26.0.dist-info/top_level.txt +0 -3
- tests/integration/__init__.py +0 -1
- tests/integration/integration_tests_with_license/__init__.py +0 -1
- tests/integration/integration_tests_with_license/conftest.py +0 -47
- tests/integration/integration_tests_with_license/test_http_end_points.py +0 -397
- tests/integration/integration_tests_without_license/__init__.py +0 -1
- tests/integration/integration_tests_without_license/conftest.py +0 -116
- tests/integration/integration_tests_without_license/test_matlab_is_down_if_unlicensed.py +0 -49
- tests/integration/utils/__init__.py +0 -1
- tests/integration/utils/integration_tests_utils.py +0 -352
- tests/integration/utils/licensing.py +0 -152
- tests/unit/__init__.py +0 -1
- tests/unit/conftest.py +0 -66
- tests/unit/test_app.py +0 -1299
- tests/unit/test_app_state.py +0 -1094
- tests/unit/test_constants.py +0 -7
- tests/unit/test_ddux.py +0 -22
- tests/unit/test_devel.py +0 -246
- tests/unit/test_non_dev_mode.py +0 -169
- tests/unit/test_settings.py +0 -679
- tests/unit/util/__init__.py +0 -3
- tests/unit/util/mwi/__init__.py +0 -1
- tests/unit/util/mwi/embedded_connector/__init__.py +0 -1
- tests/unit/util/mwi/embedded_connector/test_helpers.py +0 -29
- tests/unit/util/mwi/embedded_connector/test_request.py +0 -64
- tests/unit/util/mwi/test_custom_http_headers.py +0 -281
- tests/unit/util/mwi/test_download.py +0 -152
- tests/unit/util/mwi/test_logger.py +0 -82
- tests/unit/util/mwi/test_token_auth.py +0 -303
- tests/unit/util/mwi/test_validators.py +0 -364
- tests/unit/util/test_cookie_jar.py +0 -252
- tests/unit/util/test_mw.py +0 -550
- tests/unit/util/test_util.py +0 -221
- tests/utils/__init__.py +0 -1
- tests/utils/logging_util.py +0 -81
- {matlab_proxy-0.26.0.dist-info → matlab_proxy-0.27.1.dist-info}/entry_points.txt +0 -0
- {matlab_proxy-0.26.0.dist-info → matlab_proxy-0.27.1.dist-info/licenses}/LICENSE.md +0 -0
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# Copyright 2022 The MathWorks, Inc.
|
|
2
|
-
|
|
3
|
-
from matlab_proxy.util import mwi
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def test_get_data_to_eval_mcode():
|
|
7
|
-
necessary_keys = ["uuid", "messages", "computeToken"]
|
|
8
|
-
mcode = "exit"
|
|
9
|
-
data = mwi.embedded_connector.helpers.get_data_to_eval_mcode(mcode)
|
|
10
|
-
|
|
11
|
-
assert set(necessary_keys).issubset(set(data.keys()))
|
|
12
|
-
assert data["messages"]["Eval"][0]["mcode"] == mcode
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def test_get_data_to_feval_mcode():
|
|
16
|
-
necessary_keys = ["uuid", "messages", "computeToken"]
|
|
17
|
-
m_function = "round"
|
|
18
|
-
args = [3.135, 2]
|
|
19
|
-
nargout = 2
|
|
20
|
-
data = mwi.embedded_connector.helpers.get_data_to_feval_mcode(
|
|
21
|
-
m_function, *args, nargout=nargout
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
assert set(necessary_keys).issubset(set(data.keys()))
|
|
25
|
-
feval_data = data["messages"]["FEval"][0]
|
|
26
|
-
|
|
27
|
-
assert feval_data["function"] == m_function
|
|
28
|
-
assert feval_data["arguments"] == args
|
|
29
|
-
assert feval_data["nargout"] == nargout
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# Copyright 2022-2023 The MathWorks, Inc.
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
from matlab_proxy.util import mwi
|
|
5
|
-
from matlab_proxy.util.mwi.exceptions import EmbeddedConnectorError
|
|
6
|
-
from tests.unit.util import MockResponse
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
async def test_send_request_success(mocker):
|
|
10
|
-
"""Test to check the happy path for send_request
|
|
11
|
-
Args:
|
|
12
|
-
mocker : Built in pytest fixture
|
|
13
|
-
"""
|
|
14
|
-
# Arrange
|
|
15
|
-
json_data = {"hello": "world"}
|
|
16
|
-
payload = json_data
|
|
17
|
-
mock_resp = MockResponse(payload=payload, ok=True)
|
|
18
|
-
_ = mocker.patch("aiohttp.ClientSession.request", return_value=mock_resp)
|
|
19
|
-
|
|
20
|
-
# Act
|
|
21
|
-
res = await mwi.embedded_connector.send_request(
|
|
22
|
-
url="https://localhost:3000", data=json_data, method="GET"
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
# Assert
|
|
26
|
-
assert json_data["hello"] == res["hello"]
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
async def test_send_request_failure(mocker):
|
|
30
|
-
"""Test to check if send_request fails when
|
|
31
|
-
1) EC does not respond
|
|
32
|
-
2) url or method is not supplied
|
|
33
|
-
Args:
|
|
34
|
-
mocker : Built in pytest fixture
|
|
35
|
-
"""
|
|
36
|
-
|
|
37
|
-
# Arrange
|
|
38
|
-
json_data = {"hello": "world"}
|
|
39
|
-
|
|
40
|
-
payload = json_data
|
|
41
|
-
mock_resp = MockResponse(payload=payload, ok=False)
|
|
42
|
-
_ = mocker.patch("aiohttp.ClientSession.request", return_value=mock_resp)
|
|
43
|
-
|
|
44
|
-
# Doesnt have url or data or method
|
|
45
|
-
mock_resp = MockResponse(payload=payload, ok=False)
|
|
46
|
-
|
|
47
|
-
# Act
|
|
48
|
-
|
|
49
|
-
# Failed to communicate with EmbeddedConnector
|
|
50
|
-
with pytest.raises(EmbeddedConnectorError):
|
|
51
|
-
_ = await mwi.embedded_connector.send_request(
|
|
52
|
-
url="https://localhost:3000", method="GET", data=json_data
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
for key in ["url", "method"]:
|
|
56
|
-
options = {
|
|
57
|
-
"url": "https://localhost:3000",
|
|
58
|
-
"data": json_data,
|
|
59
|
-
"method": "GET",
|
|
60
|
-
}
|
|
61
|
-
options[key] = ""
|
|
62
|
-
# Assert
|
|
63
|
-
with pytest.raises(EmbeddedConnectorError):
|
|
64
|
-
_ = await mwi.embedded_connector.send_request(**options)
|
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
# Copyright 2020-2022 The MathWorks, Inc.
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
import os
|
|
5
|
-
import stat
|
|
6
|
-
import time
|
|
7
|
-
from json.decoder import JSONDecodeError
|
|
8
|
-
|
|
9
|
-
import pytest
|
|
10
|
-
from matlab_proxy.util import system
|
|
11
|
-
from matlab_proxy.util.mwi import custom_http_headers
|
|
12
|
-
from matlab_proxy.util.mwi import environment_variables as mwi_env
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def test_get_custom_header_env_var():
|
|
16
|
-
"""Test to check if the __get_custom_header_env_var() method returns the expected environment variable name"""
|
|
17
|
-
assert (
|
|
18
|
-
mwi_env.get_env_name_custom_http_headers()
|
|
19
|
-
== custom_http_headers.__get_custom_header_env_var()
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def test_get_exception_statement():
|
|
24
|
-
"""Test to check if __get_exception_statement() contains 'JSON data' in the generic exception statement returned by __get_exception_statement()"""
|
|
25
|
-
assert "JSON data" in custom_http_headers.__get_exception_statement()
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
@pytest.fixture(name="non_existent_temp_json_file")
|
|
29
|
-
def non_existent_random_json_file_fixture(tmp_path):
|
|
30
|
-
"""Pytest fixture which returns a non-existent random json file within a temporary directory
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
tmp_path : Built in Pytest fixture.
|
|
34
|
-
|
|
35
|
-
Returns:
|
|
36
|
-
PosixPath: of the non-existent random json file.
|
|
37
|
-
"""
|
|
38
|
-
random_file = tmp_path / f'{str(time.time()).replace(".", "")}.json'
|
|
39
|
-
return random_file
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
@pytest.fixture(name="monkeypatch_env_var_with_json_file")
|
|
43
|
-
def monkeypatch_env_var_with_json_file_fixture(
|
|
44
|
-
monkeypatch, non_existent_temp_json_file
|
|
45
|
-
):
|
|
46
|
-
"""Pytest fixture which monkeypatches the env variable returned by the __get_custom_header_env_var()
|
|
47
|
-
method with the value returned by the pytest fixture non_existent_tmp_json_file
|
|
48
|
-
|
|
49
|
-
Args:
|
|
50
|
-
monkeypatch : Built in pytest fixture
|
|
51
|
-
non_existent_temp_json_file: Pytest fixture which returns a string containing path
|
|
52
|
-
to non-existent file in a temporary directory.
|
|
53
|
-
"""
|
|
54
|
-
monkeypatch.setenv(
|
|
55
|
-
mwi_env.get_env_name_custom_http_headers(),
|
|
56
|
-
str(non_existent_temp_json_file),
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
@pytest.fixture(name="valid_json_content")
|
|
61
|
-
def valid_json_content_fixture():
|
|
62
|
-
"""Pytest fixture which returns valid JSON data as a string
|
|
63
|
-
|
|
64
|
-
Returns:
|
|
65
|
-
[String]: containing valid JSON data.
|
|
66
|
-
"""
|
|
67
|
-
return '{"abcd": "hello"}'
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
@pytest.fixture(name="json_file_with_valid_json")
|
|
71
|
-
def json_file_with_valid_json_fixture(non_existent_temp_json_file, valid_json_content):
|
|
72
|
-
"""Pytest fixture which returns a random json file containing valid json data
|
|
73
|
-
|
|
74
|
-
Args:
|
|
75
|
-
non_existent_temp_json_file : Pytest fixture which returns a non-existent random json file.
|
|
76
|
-
valid_json_content : Pytest fixture which returns valid JSON data as a string.
|
|
77
|
-
|
|
78
|
-
Returns:
|
|
79
|
-
[PosixPath]: Of a random json file containing valid json data.
|
|
80
|
-
"""
|
|
81
|
-
json_file_with_valid_content = non_existent_temp_json_file
|
|
82
|
-
with open(json_file_with_valid_content, "w") as f:
|
|
83
|
-
f.write(valid_json_content)
|
|
84
|
-
|
|
85
|
-
return json_file_with_valid_content
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
def test_get_file_contents_with_valid_json(json_file_with_valid_json):
|
|
89
|
-
"""Test to check if __get_file_contents() returns valid json data as a dict from a file
|
|
90
|
-
|
|
91
|
-
Args:
|
|
92
|
-
json_file_with_valid_json : Pytest fixture which returns a non-existent random json file.
|
|
93
|
-
"""
|
|
94
|
-
file_content = None
|
|
95
|
-
with open(json_file_with_valid_json, "r") as f:
|
|
96
|
-
file_content = json.load(f)
|
|
97
|
-
|
|
98
|
-
assert file_content == custom_http_headers.__get_file_contents(
|
|
99
|
-
json_file_with_valid_json
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
@pytest.fixture(name="invalid_json_content")
|
|
104
|
-
def invalid_json_content_fixture():
|
|
105
|
-
"""Pytest fixture which returns invalid json data as a string
|
|
106
|
-
|
|
107
|
-
Returns:
|
|
108
|
-
[String]: containing invalid json data.
|
|
109
|
-
"""
|
|
110
|
-
return '{"abcd"= "hello"}'
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
@pytest.fixture(name="json_file_with_invalid_json")
|
|
114
|
-
def json_file_with_invalid_json_fixture(
|
|
115
|
-
non_existent_temp_json_file, invalid_json_content
|
|
116
|
-
):
|
|
117
|
-
"""Pytest fixture which returns a path to a random json file containing invalid json data.
|
|
118
|
-
|
|
119
|
-
Args:
|
|
120
|
-
non_existent_temp_json_file : Pytest fixture which returns a non-existent random json file.
|
|
121
|
-
invalid_json_content : Pytest fixture which returns invalid json data as a string.
|
|
122
|
-
|
|
123
|
-
Returns:
|
|
124
|
-
PosixPath: to a random file containing invalid json data.
|
|
125
|
-
"""
|
|
126
|
-
json_file_with_invalid_content = non_existent_temp_json_file
|
|
127
|
-
with open(json_file_with_invalid_content, "w") as f:
|
|
128
|
-
f.write(invalid_json_content)
|
|
129
|
-
|
|
130
|
-
return json_file_with_invalid_content
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
def test_get_file_contents_with_invalid_json(json_file_with_invalid_json, capsys):
|
|
134
|
-
"""Test to check __get_file_contents() raises JSONDecodeError when invalid json data is present in a file.
|
|
135
|
-
|
|
136
|
-
Args:
|
|
137
|
-
json_file_with_invalid_json : Pytest fixture which returns a non-existent random json file.
|
|
138
|
-
"""
|
|
139
|
-
with pytest.raises(SystemExit):
|
|
140
|
-
custom_http_headers.__get_file_contents(json_file_with_invalid_json)
|
|
141
|
-
out, err = capsys.readouterr()
|
|
142
|
-
assert JSONDecodeError.__name__ in out
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
def test_check_file_validity_no_read_access(non_existent_temp_json_file, capsys):
|
|
146
|
-
"""Test to check if OSError is raised when trying to read file with no read access.
|
|
147
|
-
|
|
148
|
-
Args:
|
|
149
|
-
non_existent_temp_json_file : Pytest fixture which returns a non-existent random json file.
|
|
150
|
-
"""
|
|
151
|
-
temp_json_file_no_read_access = non_existent_temp_json_file
|
|
152
|
-
|
|
153
|
-
# Not possible to create a file in windows without read permission
|
|
154
|
-
# So not creating the file itself to check for SystemExit exception.
|
|
155
|
-
if not system.is_windows():
|
|
156
|
-
temp_json_file_no_read_access.touch(mode=stat.S_IWUSR)
|
|
157
|
-
|
|
158
|
-
with pytest.raises(SystemExit):
|
|
159
|
-
custom_http_headers.__check_file_validity(temp_json_file_no_read_access)
|
|
160
|
-
out, err = capsys.readouterr()
|
|
161
|
-
assert OSError.__name__ in out
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
def test_check_file_validity_no_error(non_existent_temp_json_file, valid_json_content):
|
|
165
|
-
"""Test to check __check_file_validity() does not raise any exception when the custom headers file
|
|
166
|
-
exists, has valid JSON data and the current python process has read access to the file.
|
|
167
|
-
|
|
168
|
-
Args:
|
|
169
|
-
non_existent_temp_json_file : Pytest fixture which returns a non-existent random json file.
|
|
170
|
-
"""
|
|
171
|
-
|
|
172
|
-
random_json_file_with_valid_content = non_existent_temp_json_file
|
|
173
|
-
with open(random_json_file_with_valid_content, "w") as f:
|
|
174
|
-
f.write(valid_json_content)
|
|
175
|
-
|
|
176
|
-
assert (
|
|
177
|
-
custom_http_headers.__check_file_validity(random_json_file_with_valid_content)
|
|
178
|
-
is True
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
def test_get_no_env_var():
|
|
183
|
-
"""Test to check if get() returns an empty dict, when mwi_custom_http_headers env variable is not present."""
|
|
184
|
-
assert custom_http_headers.get() == dict()
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
def test_get_with_json_file_no_error(
|
|
188
|
-
monkeypatch_env_var_with_json_file, valid_json_content
|
|
189
|
-
):
|
|
190
|
-
"""Test to check if expected json content is returned by get() as a dict when the
|
|
191
|
-
environment variable returned by __get_custom_header_env_var() contains a path to a JSON file
|
|
192
|
-
|
|
193
|
-
Args:
|
|
194
|
-
monkeypatch_env_var_with_json_file : Pytest fixture which monkeypatches the env variable returned by __get_custom_header_env_var() to
|
|
195
|
-
have a non-existent temporary json file.
|
|
196
|
-
valid_json_content : Pytest fixture which returns valid json data as a string.
|
|
197
|
-
"""
|
|
198
|
-
tmp_file_path = os.getenv(mwi_env.get_env_name_custom_http_headers())
|
|
199
|
-
|
|
200
|
-
with open(tmp_file_path, "w") as f:
|
|
201
|
-
f.write(valid_json_content)
|
|
202
|
-
|
|
203
|
-
assert custom_http_headers.get() == json.loads(valid_json_content)
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
def test_get_with_json_file_raise_exception(
|
|
207
|
-
monkeypatch_env_var_with_json_file, invalid_json_content, capsys
|
|
208
|
-
):
|
|
209
|
-
"""Test to check if the get() method raises SystemExit exception when the environment variable returned by __get_custom_env_var()
|
|
210
|
-
contains path to a file with invalid JSON data. Also asserts for JSONDecodeError in stdout.
|
|
211
|
-
|
|
212
|
-
Args:
|
|
213
|
-
monkeypatch_env_var_with_json_file : Pytest fixture which monkeypatches the env variable returned by __get_custom_header_env_var() to
|
|
214
|
-
have a non-existent temporary json file.
|
|
215
|
-
invalid_json_content : Pytest fixture which returns invalid json data as a string.
|
|
216
|
-
"""
|
|
217
|
-
tmp_file_path = os.getenv(mwi_env.get_env_name_custom_http_headers())
|
|
218
|
-
|
|
219
|
-
with open(tmp_file_path, "w") as f:
|
|
220
|
-
f.write(invalid_json_content)
|
|
221
|
-
|
|
222
|
-
with pytest.raises(SystemExit):
|
|
223
|
-
custom_http_headers.get()
|
|
224
|
-
out, err = capsys.readouterr()
|
|
225
|
-
assert JSONDecodeError.__name__ in out
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
@pytest.fixture(name="monkeypatch_env_var_with_invalid_json_string")
|
|
229
|
-
def monkeypatch_env_var_with_invalid_json_string_fixture(
|
|
230
|
-
monkeypatch, invalid_json_content
|
|
231
|
-
):
|
|
232
|
-
"""Pytest fixture which monkeypatches the env variable returned by the __get_custom_header_env_var()
|
|
233
|
-
method with the value returned by the pytest fixture non_existent_temp_json_file
|
|
234
|
-
|
|
235
|
-
Args:
|
|
236
|
-
monkeypatch : Built in pytest fixture
|
|
237
|
-
non_existent_temp_json_file: Pytest fixture which returns a string containing path
|
|
238
|
-
to non-existent file in a temporary directory.
|
|
239
|
-
"""
|
|
240
|
-
monkeypatch.setenv(mwi_env.get_env_name_custom_http_headers(), invalid_json_content)
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
def test_get_with_invalid_json_string(
|
|
244
|
-
monkeypatch_env_var_with_invalid_json_string, capsys
|
|
245
|
-
):
|
|
246
|
-
"""Test to check if the get() method raises SystemExit exception when the environment variable returned by __get_custom_env_var()
|
|
247
|
-
contains invalid JSON data as a string. Also asserts for JSONDecodeError in stdout.
|
|
248
|
-
|
|
249
|
-
Args:
|
|
250
|
-
monkeypatch_env_var_with_invalid_json_string : Pytest fixture which monkeypatches the env variable returned by __get_custom_header_env_var() to
|
|
251
|
-
contain invalid JSON data as a string.
|
|
252
|
-
"""
|
|
253
|
-
with pytest.raises(SystemExit):
|
|
254
|
-
headers = custom_http_headers.get()
|
|
255
|
-
out, err = capsys.readouterr()
|
|
256
|
-
assert JSONDecodeError.__name__ in out
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
@pytest.fixture(name="monkeypatch_env_var_with_valid_json_string")
|
|
260
|
-
def monkeypatch_env_var_with_valid_json_string_fixture(monkeypatch, valid_json_content):
|
|
261
|
-
"""Pytest fixture which monkeypatches the env variable returned by the __get_custom_header_env_var()
|
|
262
|
-
method with the value returned by the pytest fixture non_existent_tmp_json_file
|
|
263
|
-
|
|
264
|
-
Args:
|
|
265
|
-
monkeypatch : Built in pytest fixture
|
|
266
|
-
non_existent_temp_json_file: Pytest fixture which returns a string containing path
|
|
267
|
-
to non-existent file in a temporary directory.
|
|
268
|
-
"""
|
|
269
|
-
monkeypatch.setenv(mwi_env.get_env_name_custom_http_headers(), valid_json_content)
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
def test_get_with_valid_json_string(monkeypatch_env_var_with_valid_json_string):
|
|
273
|
-
"""Test to check if expected json content is returned by get() as a dict when the
|
|
274
|
-
environment variable contains valid JSON data as a string
|
|
275
|
-
|
|
276
|
-
Args:
|
|
277
|
-
monkeypatch_env_var_with_valid_json_string : Pytest fixture which monkeypatches the env variable returned by __get_custom_header_env_var() to
|
|
278
|
-
contain valid JSON data as a string.
|
|
279
|
-
"""
|
|
280
|
-
headers = json.loads(os.getenv(mwi_env.get_env_name_custom_http_headers()))
|
|
281
|
-
assert headers == custom_http_headers.get()
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 The MathWorks, Inc.
|
|
2
|
-
import pytest
|
|
3
|
-
|
|
4
|
-
from matlab_proxy.util.mwi.download import (
|
|
5
|
-
_get_download_payload_path,
|
|
6
|
-
get_download_url,
|
|
7
|
-
is_download_request,
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
# Mock the request object
|
|
12
|
-
@pytest.fixture
|
|
13
|
-
def mock_request_fixture(mocker):
|
|
14
|
-
mock_req = mocker.MagicMock()
|
|
15
|
-
mock_req.app = {
|
|
16
|
-
"settings": {"base_url": ""},
|
|
17
|
-
"state": mocker.MagicMock(),
|
|
18
|
-
}
|
|
19
|
-
mock_req.rel_url = mocker.MagicMock()
|
|
20
|
-
return mock_req
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def _get_expected_output_based_on_os_type(paths: list) -> str:
|
|
24
|
-
import matlab_proxy.util.system as system
|
|
25
|
-
|
|
26
|
-
return "\\".join(paths) if system.is_windows() else "/".join(paths)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
# Test for is_download_request function
|
|
30
|
-
@pytest.mark.parametrize(
|
|
31
|
-
"test_base_url, path, expected",
|
|
32
|
-
[
|
|
33
|
-
("/", "/download/something", True),
|
|
34
|
-
("", "/download/something", True),
|
|
35
|
-
("/base", "/base/download/something", True),
|
|
36
|
-
("/base", "/download/something", False),
|
|
37
|
-
],
|
|
38
|
-
ids=[
|
|
39
|
-
"/ base url and path starting with /download",
|
|
40
|
-
"empty base url and path starting with /download",
|
|
41
|
-
"non-empty base url and path starting with that base url",
|
|
42
|
-
"non-empty base url and path not starting with that base url",
|
|
43
|
-
],
|
|
44
|
-
)
|
|
45
|
-
def test_is_download_request(mock_request_fixture, test_base_url, path, expected):
|
|
46
|
-
mock_request_fixture.app["settings"]["base_url"] = test_base_url
|
|
47
|
-
mock_request_fixture.rel_url.path = path
|
|
48
|
-
assert is_download_request(mock_request_fixture) == expected
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
# Test for _get_download_payload_path function
|
|
52
|
-
# This test is a bit tricky since it involves file system operations and OS checks.
|
|
53
|
-
# We will mock system.is_windows() and test for both Windows and Posix systems.
|
|
54
|
-
@pytest.mark.parametrize(
|
|
55
|
-
"is_windows, test_base_url, path, expected",
|
|
56
|
-
[
|
|
57
|
-
(
|
|
58
|
-
True,
|
|
59
|
-
"",
|
|
60
|
-
"/downloadC:\\some\\path\\to\\file.txt",
|
|
61
|
-
"C:\\some\\path\\to\\file.txt",
|
|
62
|
-
),
|
|
63
|
-
(
|
|
64
|
-
True,
|
|
65
|
-
"/base",
|
|
66
|
-
"/base/downloadC:\\some\\path\\to\\file.txt",
|
|
67
|
-
"C:\\some\\path\\to\\file.txt",
|
|
68
|
-
),
|
|
69
|
-
(
|
|
70
|
-
False,
|
|
71
|
-
"",
|
|
72
|
-
"/download/some/path/to/file.txt",
|
|
73
|
-
_get_expected_output_based_on_os_type(["/some", "path", "to", "file.txt"]),
|
|
74
|
-
),
|
|
75
|
-
(
|
|
76
|
-
False,
|
|
77
|
-
"/base",
|
|
78
|
-
"/base/download/some/path/to/file.txt",
|
|
79
|
-
_get_expected_output_based_on_os_type(["/some", "path", "to", "file.txt"]),
|
|
80
|
-
),
|
|
81
|
-
],
|
|
82
|
-
ids=[
|
|
83
|
-
"Windows with null base url",
|
|
84
|
-
"Windows with non-null base url",
|
|
85
|
-
"Linux with null base url",
|
|
86
|
-
"Linux with non-null base url",
|
|
87
|
-
],
|
|
88
|
-
)
|
|
89
|
-
def test_get_download_payload_path(
|
|
90
|
-
mock_request_fixture, mocker, is_windows, test_base_url, path, expected
|
|
91
|
-
):
|
|
92
|
-
mocker.patch("matlab_proxy.util.system.is_windows", return_value=is_windows)
|
|
93
|
-
mock_request_fixture.app["settings"]["base_url"] = test_base_url
|
|
94
|
-
mock_request_fixture.rel_url.path = path
|
|
95
|
-
assert _get_download_payload_path(mock_request_fixture) == expected
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def test_get_download_payload_path_invalid_request(mock_request_fixture):
|
|
99
|
-
test_base_url = "/base"
|
|
100
|
-
path = "/download/something"
|
|
101
|
-
|
|
102
|
-
mock_request_fixture.app["settings"]["base_url"] = test_base_url
|
|
103
|
-
mock_request_fixture.rel_url.path = path
|
|
104
|
-
|
|
105
|
-
assert _get_download_payload_path(mock_request_fixture) is None
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
@pytest.mark.parametrize(
|
|
109
|
-
"response_json, expected_url",
|
|
110
|
-
[
|
|
111
|
-
(
|
|
112
|
-
{
|
|
113
|
-
"messages": {
|
|
114
|
-
"FEvalResponse": [
|
|
115
|
-
{"isError": False, "results": ["http://download-url.com"]}
|
|
116
|
-
]
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
"http://download-url.com",
|
|
120
|
-
),
|
|
121
|
-
({"messages": {"FEvalResponse": [{"isError": True}]}}, None),
|
|
122
|
-
],
|
|
123
|
-
ids=["connector returning correct download url", "connector returning an error"],
|
|
124
|
-
)
|
|
125
|
-
async def test_get_download_url(
|
|
126
|
-
mock_request_fixture, mocker, response_json, expected_url
|
|
127
|
-
):
|
|
128
|
-
test_base_url = "/"
|
|
129
|
-
path = "/download/some/path/to/file.txt"
|
|
130
|
-
|
|
131
|
-
mock_request_fixture.app["state"].settings = {
|
|
132
|
-
"mwi_server_url": "http://mwi-server.com"
|
|
133
|
-
}
|
|
134
|
-
mock_request_fixture.app["settings"]["base_url"] = test_base_url
|
|
135
|
-
mock_request_fixture.rel_url.path = path
|
|
136
|
-
|
|
137
|
-
mocker.patch(
|
|
138
|
-
"matlab_proxy.util.mwi.embedded_connector.helpers.get_data_to_feval_mcode",
|
|
139
|
-
return_value={},
|
|
140
|
-
)
|
|
141
|
-
mocker.patch(
|
|
142
|
-
"matlab_proxy.util.mwi.embedded_connector.helpers.get_mvm_endpoint",
|
|
143
|
-
return_value="http://mwi-server.com",
|
|
144
|
-
)
|
|
145
|
-
mocker.patch(
|
|
146
|
-
"matlab_proxy.util.mwi.embedded_connector.send_request",
|
|
147
|
-
return_value=response_json,
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
download_url = await get_download_url(mock_request_fixture)
|
|
151
|
-
|
|
152
|
-
assert download_url == expected_url
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
# Copyright 2020-2024 The MathWorks, Inc.
|
|
2
|
-
"""This file tests methods present in matlab_proxy/util/mwi_logger.py"""
|
|
3
|
-
|
|
4
|
-
import logging
|
|
5
|
-
import os
|
|
6
|
-
import pytest
|
|
7
|
-
from matlab_proxy.util.mwi import logger as mwi_logger
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def test_get():
|
|
11
|
-
"""This test checks if the get method returns a logger with expected name"""
|
|
12
|
-
logger = mwi_logger.get()
|
|
13
|
-
# Okay to use hidden API for testing only.
|
|
14
|
-
assert logger.name == mwi_logger.__get_mw_logger_name()
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def test_get_mw_logger_name():
|
|
18
|
-
"""Test to lock down the name of the logger used."""
|
|
19
|
-
# Okay to use hidden API for testing only.
|
|
20
|
-
assert "MATLABProxyApp" == mwi_logger.__get_mw_logger_name()
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def test_get_with_no_environment_variables(monkeypatch):
|
|
24
|
-
"""This test checks if the get method returns a logger with default settings if no environment variable is set"""
|
|
25
|
-
# Delete the environment variables if they do exist
|
|
26
|
-
env_names_list = mwi_logger.get_environment_variable_names()
|
|
27
|
-
monkeypatch.delenv(env_names_list[0], raising=False)
|
|
28
|
-
monkeypatch.delenv(env_names_list[1], raising=False)
|
|
29
|
-
|
|
30
|
-
logger = mwi_logger.get(init=True)
|
|
31
|
-
assert logger.isEnabledFor(logging.INFO) == True
|
|
32
|
-
assert len(logger.handlers) == 0
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def test_get_with_environment_variables(monkeypatch, tmp_path):
|
|
36
|
-
"""This test checks if the get method returns a logger with the specified settings"""
|
|
37
|
-
env_names_list = mwi_logger.get_environment_variable_names()
|
|
38
|
-
monkeypatch.setenv(env_names_list[0], "CRITICAL")
|
|
39
|
-
monkeypatch.setenv(env_names_list[1], str(tmp_path / "testing123.log"))
|
|
40
|
-
|
|
41
|
-
logger = mwi_logger.get(init=True)
|
|
42
|
-
|
|
43
|
-
# Verify that environment variable controlling level is respected
|
|
44
|
-
assert logger.isEnabledFor(logging.CRITICAL) == True
|
|
45
|
-
|
|
46
|
-
# Verify that environment variable setting the file is respected
|
|
47
|
-
assert len(logger.handlers) == 1
|
|
48
|
-
assert os.path.basename(logger.handlers[0].baseFilename) == "testing123.log"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
@pytest.mark.parametrize(
|
|
52
|
-
"log_level, expected_level",
|
|
53
|
-
[
|
|
54
|
-
("DEBUG", logging.DEBUG),
|
|
55
|
-
("INFO", logging.INFO),
|
|
56
|
-
("WARNING", logging.WARNING),
|
|
57
|
-
("debug", logging.DEBUG),
|
|
58
|
-
("info", logging.INFO),
|
|
59
|
-
("warning", logging.WARNING),
|
|
60
|
-
],
|
|
61
|
-
)
|
|
62
|
-
def test_set_logging_configuration_known_logging_levels(
|
|
63
|
-
monkeypatch, log_level, expected_level
|
|
64
|
-
):
|
|
65
|
-
"""This test checks if the logger is set with correct level for known log levels"""
|
|
66
|
-
env_names_list = mwi_logger.get_environment_variable_names()
|
|
67
|
-
monkeypatch.setenv(env_names_list[0], log_level)
|
|
68
|
-
logger = mwi_logger.get(init=True)
|
|
69
|
-
assert (
|
|
70
|
-
logger.isEnabledFor(expected_level) == True
|
|
71
|
-
), f"Error in initialising the logger with {log_level}"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
@pytest.mark.parametrize("log_level", ["ABC", "abc"])
|
|
75
|
-
def test_set_logging_configuration_unknown_logging_levels(monkeypatch, log_level):
|
|
76
|
-
"""This test checks if the logger is set with INFO level for unknown log levels"""
|
|
77
|
-
env_names_list = mwi_logger.get_environment_variable_names()
|
|
78
|
-
monkeypatch.setenv(env_names_list[0], log_level)
|
|
79
|
-
logger = mwi_logger.get(init=True)
|
|
80
|
-
assert (
|
|
81
|
-
logger.isEnabledFor(logging.INFO) == True
|
|
82
|
-
), "Error in initialising the default logger"
|