1010
1111from matlab_proxy .app_state import AppState
1212from matlab_proxy .util .mwi .exceptions import LicensingError , MatlabError
13+ from tests .unit .util import MockResponse
1314
1415
1516@pytest .fixture
@@ -24,6 +25,36 @@ def app_state_fixture():
2425 return app_state
2526
2627
28+ @pytest .fixture
29+ def sample_token_headers_fixture ():
30+ return {"mwi_auth_token_name" : "asdf" }
31+
32+
33+ @pytest .fixture
34+ def app_state_with_token_auth_fixture (
35+ app_state_fixture , sample_token_headers_fixture , tmp_path
36+ ):
37+ """Pytest fixture which returns AppState instance with token authentication enabled.
38+
39+ Args:
40+ app_state_fixture (AppState): Pytest fixture
41+ tmp_path (str): Built-in pytest fixture
42+
43+ Returns:
44+ (AppState, dict): Instance of the AppState class with token authentication enabled and token headers
45+ """
46+ tmp_matlab_ready_file = Path (tmp_path ) / "tmp_file.txt"
47+ tmp_matlab_ready_file .touch ()
48+ ((mwi_auth_token_name , mwi_auth_token_hash ),) = sample_token_headers_fixture .items ()
49+ app_state_fixture .matlab_session_files ["matlab_ready_file" ] = tmp_matlab_ready_file
50+ app_state_fixture .settings ["mwi_is_token_auth_enabled" ] = True
51+ app_state_fixture .settings ["mwi_auth_token_name" ] = mwi_auth_token_name
52+ app_state_fixture .settings ["mwi_auth_token_hash" ] = mwi_auth_token_hash
53+ app_state_fixture .settings ["mwi_server_url" ] = "http://localhost:8888"
54+
55+ return app_state_fixture
56+
57+
2758@pytest .fixture
2859def mocker_os_patching_fixture (mocker , platform ):
2960 """A pytest fixture which patches the is_* functions in system.py module
@@ -38,12 +69,20 @@ def mocker_os_patching_fixture(mocker, platform):
3869 mocker .patch ("matlab_proxy.app_state.system.is_linux" , return_value = False )
3970 mocker .patch ("matlab_proxy.app_state.system.is_windows" , return_value = False )
4071 mocker .patch ("matlab_proxy.app_state.system.is_mac" , return_value = False )
72+ mocker .patch ("matlab_proxy.app_state.system.is_posix" , return_value = False )
73+
4174 if platform == "linux" :
4275 mocker .patch ("matlab_proxy.app_state.system.is_linux" , return_value = True )
76+ mocker .patch ("matlab_proxy.app_state.system.is_posix" , return_value = True )
77+
4378 elif platform == "windows" :
4479 mocker .patch ("matlab_proxy.app_state.system.is_windows" , return_value = True )
80+ mocker .patch ("matlab_proxy.app_state.system.is_posix" , return_value = False )
81+
4582 else :
4683 mocker .patch ("matlab_proxy.app_state.system.is_mac" , return_value = True )
84+ mocker .patch ("matlab_proxy.app_state.system.is_posix" , return_value = True )
85+
4786 return mocker
4887
4988
@@ -397,9 +436,12 @@ def test_env_variables_filtration_for_xvfb_process(
397436 assert filtered_env_vars .get (env_var ) == is_filtered
398437
399438
400- @pytest .mark .parametrize ("platform" , [("linux" ), ("windows" ), ("mac" )])
439+ @pytest .mark .parametrize (
440+ "platform, expected_output" ,
441+ [("linux" , "stdout" ), ("windows" , "file" ), ("mac" , "stdout" )],
442+ )
401443async def test_setup_env_for_matlab (
402- mocker_os_patching_fixture , platform , app_state_fixture , tmp_path
444+ mocker_os_patching_fixture , platform , expected_output , app_state_fixture , tmp_path
403445):
404446 """Test to check MW_DIAGNOSTIC_DEST is set appropriately for posix and non-posix systems
405447
@@ -411,7 +453,6 @@ async def test_setup_env_for_matlab(
411453 """
412454
413455 # Arrange
414- expected_log_file_path = tmp_path / "matlab_logs.txt"
415456 app_state_fixture .licensing = {"type" : "existing_license" }
416457 app_state_fixture .settings = {"mwapikey" : None , "matlab_display" : ":1" }
417458 app_state_fixture .mwi_logs_dir = tmp_path
@@ -423,7 +464,47 @@ async def test_setup_env_for_matlab(
423464 matlab_env = await app_state_fixture ._AppState__setup_env_for_matlab ()
424465
425466 # Assert
426- if "linux" or "mac" in platform :
427- assert matlab_env ["MW_DIAGNOSTIC_DEST" ] == "stdout"
428- else :
429- assert matlab_env ["MW_DIAGNOSTIC_DEST" ] == expected_log_file_path
467+ assert expected_output in matlab_env ["MW_DIAGNOSTIC_DEST" ]
468+
469+
470+ @pytest .mark .parametrize (
471+ "function_to_call ,mock_response" ,
472+ [
473+ ("_get_matlab_connector_status" , MockResponse (ok = True )),
474+ (
475+ "_AppState__send_stop_request_to_matlab" ,
476+ MockResponse (
477+ ok = True , payload = {"messages" : {"EvalResponse" : [{"isError" : None }]}}
478+ ),
479+ ),
480+ ],
481+ ids = ["request matlab connector status" , "send request to stop matlab" ],
482+ )
483+ async def test_requests_sent_by_matlab_proxy_have_headers (
484+ app_state_with_token_auth_fixture ,
485+ function_to_call ,
486+ mock_response ,
487+ mocker ,
488+ sample_token_headers_fixture ,
489+ ):
490+ """Test to check if token headers are included in requests sent by matlab-proxy when authentication is enabled
491+
492+ Args:
493+ app_state_fixture_with_token_auth (AppState): Instance of AppState class with token authentication enabled
494+ mocker : Built-in pytest fixture
495+ """
496+ # Arrange
497+ mocked_request = mocker .patch (
498+ "aiohttp.ClientSession.request" , return_value = mock_response
499+ )
500+
501+ # Act
502+ # Call the function passed as a string
503+ method = getattr (app_state_with_token_auth_fixture , function_to_call )
504+ _ = await method ()
505+
506+ # Assert
507+ connector_status_request_headers = list (mocked_request .call_args_list )[0 ].kwargs [
508+ "headers"
509+ ]
510+ assert sample_token_headers_fixture == connector_status_request_headers
0 commit comments