Skip to content

Commit 1e2e57e

Browse files
pranaverma92Prabhakar Kumar
authored andcommitted
Updates integration tests to account for matlab-proxy's token authentication.
1 parent abaa827 commit 1e2e57e

File tree

4 files changed

+48
-35
lines changed

4 files changed

+48
-35
lines changed

tests/integration/integration_tests_with_license/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def matlab_proxy_fixture(module_monkeypatch, request):
8080
mwi_base_url = "/matlab-test"
8181
module_monkeypatch.setenv(mwi_env.get_env_name_testing(), "false")
8282
module_monkeypatch.setenv(mwi_env.get_env_name_development(), "false")
83-
module_monkeypatch.setenv(mwi_env.get_env_name_enable_mwi_auth_token(), "false")
8483

8584
# Start matlab-proxy-app for testing
8685
input_env = {

tests/integration/integration_tests_with_license/test_http_end_points.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import requests
1111
import re
1212
from requests.adapters import HTTPAdapter, Retry
13-
from urllib.parse import urlparse
13+
from urllib.parse import urlparse, parse_qs
1414
from logging_util import create_test_logger
1515

1616
_logger = create_test_logger(
@@ -66,6 +66,14 @@ def __enter__(self):
6666

6767
utils.wait_server_info_ready(self.mwi_app_port)
6868
parsed_url = urlparse(utils.get_connection_string(self.mwi_app_port))
69+
70+
self.headers = {
71+
"mwi_auth_token": (
72+
parse_qs(parsed_url.query)["mwi_auth_token"][0]
73+
if "mwi_auth_token" in parse_qs(parsed_url.query)
74+
else ""
75+
)
76+
}
6977
self.connection_scheme = parsed_url.scheme
7078
self.url = parsed_url.scheme + "://" + parsed_url.netloc + parsed_url.path
7179
return self
@@ -89,7 +97,7 @@ def _move(source, destination):
8997
_logger.exception(err)
9098

9199

92-
def _send_http_get_request(uri, connection_scheme, http_endpoint=""):
100+
def _send_http_get_request(uri, connection_scheme, headers, http_endpoint=""):
93101
"""Send HTTP request to matlab-proxy server.
94102
Returns HTTP response JSON"""
95103

@@ -99,20 +107,24 @@ def _send_http_get_request(uri, connection_scheme, http_endpoint=""):
99107
with requests.Session() as s:
100108
retries = Retry(total=10, backoff_factor=0.1)
101109
s.mount(f"{connection_scheme}://", HTTPAdapter(max_retries=retries))
102-
response = s.get(request_uri, verify=False)
110+
response = s.get(request_uri, headers=headers, verify=False)
103111
json_response = json.loads(response.text)
104112

105113
return json_response
106114

107115

108-
def _check_matlab_status(connection_scheme, status, uri):
116+
def _check_matlab_status(matlab_proxy_app_fixture, status):
117+
uri = matlab_proxy_app_fixture.url
118+
connection_scheme = matlab_proxy_app_fixture.connection_scheme
119+
headers = matlab_proxy_app_fixture.headers
120+
109121
matlab_status = None
110122

111123
start_time = time.time()
112124
while matlab_status != status and (time.time() - start_time < MAX_TIMEOUT):
113125
time.sleep(1)
114126
res = _send_http_get_request(
115-
uri, connection_scheme, http_endpoint="/get_status"
127+
uri, connection_scheme, headers, http_endpoint="/get_status"
116128
)
117129
matlab_status = res["matlab"]["status"]
118130

@@ -146,11 +158,7 @@ def test_matlab_is_up(matlab_proxy_app_fixture):
146158
matlab_proxy_app_fixture: A pytest fixture which yields a real matlab server to be used by tests.
147159
"""
148160

149-
status = _check_matlab_status(
150-
matlab_proxy_app_fixture.connection_scheme,
151-
"up",
152-
matlab_proxy_app_fixture.url,
153-
)
161+
status = _check_matlab_status(matlab_proxy_app_fixture, "up")
154162
assert status == "up"
155163

156164

@@ -162,12 +170,7 @@ def test_stop_matlab(matlab_proxy_app_fixture):
162170
matlab_proxy_app_fixture: A pytest fixture which yields a real matlab server to be used by tests.
163171
"""
164172

165-
_logger.info("Testing stopping")
166-
status = _check_matlab_status(
167-
matlab_proxy_app_fixture.connection_scheme,
168-
"up",
169-
matlab_proxy_app_fixture.url,
170-
)
173+
status = _check_matlab_status(matlab_proxy_app_fixture, "up")
171174
assert status == "up"
172175

173176
http_endpoint_to_test = "/stop_matlab"
@@ -179,15 +182,10 @@ def test_stop_matlab(matlab_proxy_app_fixture):
179182
f"{matlab_proxy_app_fixture.connection_scheme}://",
180183
HTTPAdapter(max_retries=retries),
181184
)
182-
s.delete(stop_url, verify=False)
185+
s.delete(stop_url, headers=matlab_proxy_app_fixture.headers, verify=False)
183186

184-
status = _check_matlab_status(
185-
matlab_proxy_app_fixture.connection_scheme,
186-
"down",
187-
matlab_proxy_app_fixture.url,
188-
)
187+
status = _check_matlab_status(matlab_proxy_app_fixture, "down")
189188
assert status == "down"
190-
_logger.info("stop_matlab test passed")
191189

192190

193191
async def test_print_message(matlab_proxy_app_fixture):
@@ -198,9 +196,7 @@ async def test_print_message(matlab_proxy_app_fixture):
198196
199197
"""
200198
# Checks if matlab proxy is in "up" state or not
201-
status = _check_matlab_status(
202-
matlab_proxy_app_fixture.connection_scheme, "up", matlab_proxy_app_fixture.url
203-
)
199+
status = _check_matlab_status(matlab_proxy_app_fixture, "up")
204200
assert status == "up"
205201

206202
uri_regex = f"{matlab_proxy_app_fixture.connection_scheme}://[a-zA-Z0-9\-_.]+:{matlab_proxy_app_fixture.mwi_app_port}{matlab_proxy_app_fixture.mwi_base_url}"

tests/integration/integration_tests_without_license/conftest.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import requests
66
from logging_util import create_test_logger
77
import os
8+
from urllib.parse import urlparse, parse_qs
89

910
_logger = create_test_logger(
1011
__name__, log_file_path=os.getenv("MWI_INTEG_TESTS_LOG_FILE_PATH")
@@ -26,6 +27,28 @@ class object: Object of class MonkeyPatch
2627
yield mp
2728

2829

30+
@pytest.fixture
31+
def parse_matlab_proxy_url():
32+
# Get the base URL from a utility function or environment variable
33+
mwi_app_port = os.environ["MWI_APP_PORT"]
34+
35+
# Construct the parameters dictionary
36+
parsed_url = urlparse(utils.get_connection_string(mwi_app_port))
37+
38+
headers = {
39+
"mwi_auth_token": (
40+
["mwi_auth_token"][0]
41+
if "mwi_auth_token" in parse_qs(parsed_url.query)
42+
else ""
43+
)
44+
}
45+
46+
connection_scheme = parsed_url.scheme
47+
48+
# Return the base URL and parameters as a tuple
49+
return parsed_url, headers, connection_scheme
50+
51+
2952
@pytest.fixture(scope="module", autouse=True)
3053
def start_matlab_proxy_fixture(module_monkeypatch):
3154
"""Starts the matlab proxy process"""
@@ -39,7 +62,6 @@ def start_matlab_proxy_fixture(module_monkeypatch):
3962
input_env = {
4063
"MWI_APP_PORT": mwi_app_port,
4164
"MWI_BASE_URL": mwi_base_url,
42-
"MWI_ENABLE_TOKEN_AUTH": "false",
4365
}
4466

4567
import matlab_proxy

tests/integration/integration_tests_without_license/test_matlab_is_down_if_unlicensed.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@
88
from urllib.parse import urlparse
99

1010

11-
def test_matlab_down():
11+
def test_matlab_down(parse_matlab_proxy_url):
1212
"""Test that matlab is down and no license is picked up"""
1313

14-
mwi_app_port = os.environ["MWI_APP_PORT"]
14+
parsed_url, headers, connection_scheme = parse_matlab_proxy_url
1515
http_endpoint = "/get_status"
16-
17-
parsed_url = urlparse(utils.get_connection_string(mwi_app_port))
18-
19-
connection_scheme = parsed_url.scheme
2016
uri = (
2117
connection_scheme + "://" + parsed_url.netloc + parsed_url.path + http_endpoint
2218
)
@@ -26,7 +22,7 @@ def test_matlab_down():
2622
with requests.Session() as s:
2723
retries = Retry(total=10, backoff_factor=0.1)
2824
s.mount(f"{connection_scheme}://", HTTPAdapter(max_retries=retries))
29-
response = s.get(uri, verify=False)
25+
response = s.get(uri, headers=headers, verify=False)
3026
json_response = json.loads(response.text)
3127

3228
matlab_status = json_response["matlab"]["status"]

0 commit comments

Comments
 (0)