1- # Copyright 2020-2021 The MathWorks, Inc.
1+ # Copyright 2020-2023 The MathWorks, Inc.
22
3- import os , inspect
4- import matlab_proxy , jupyter_matlab_proxy
3+ import inspect
4+ import os
55from pathlib import Path
6+
7+ import matlab_proxy
68from matlab_proxy .util .mwi import environment_variables as mwi_env
9+
10+ import jupyter_matlab_proxy
711from jupyter_matlab_proxy .jupyter_config import config
812
913
14+ def test_get_auth_token ():
15+ """Tests if _get_auth_token() function returns a token and token_hash as a dict by default."""
16+ r = jupyter_matlab_proxy ._get_auth_token ()
17+ assert r != None
18+ assert r .get ("token" ) != None
19+ assert r .get ("token_hash" ) != None
20+
21+
22+ def test_get_auth_token_with_token_auth_disabled (monkeypatch ):
23+ """Tests if _get_auth_token() function returns None if token authentication is explicitly disabled."""
24+ monkeypatch .setenv (mwi_env .get_env_name_enable_mwi_auth_token (), "False" )
25+ r = jupyter_matlab_proxy ._get_auth_token ()
26+ assert r == None
27+
28+
1029def test_get_env ():
1130 """Tests if _get_env() method returns the expected enviroment settings as a dict."""
1231
1332 port = 10000
1433 base_url = "/foo/"
1534 r = jupyter_matlab_proxy ._get_env (port , base_url )
16- assert r [mwi_env .get_env_name_app_port ()] == str (port )
17- assert r [mwi_env .get_env_name_base_url ()] == f"{ base_url } matlab"
35+ assert r .get (mwi_env .get_env_name_app_port ()) == str (port )
36+ assert r .get (mwi_env .get_env_name_base_url ()) == f"{ base_url } matlab"
37+ assert r .get (mwi_env .get_env_name_enable_mwi_auth_token ()) == "True"
38+ assert r .get (
39+ mwi_env .get_env_name_mwi_auth_token ()
40+ ) == jupyter_matlab_proxy ._mwi_auth_token .get ("token" )
41+
42+
43+ def test_get_env_with_token_auth_disabled (monkeypatch ):
44+ """Tests if _get_env() method returns the expected enviroment settings as a dict
45+ when token authentication is explicitly disabled.
46+ """
47+
48+ port = 10000
49+ base_url = "/foo/"
50+ monkeypatch .setattr (jupyter_matlab_proxy , "_mwi_auth_token" , None )
51+ r = jupyter_matlab_proxy ._get_env (port , base_url )
52+ assert r .get (mwi_env .get_env_name_app_port ()) == str (port )
53+ assert r .get (mwi_env .get_env_name_base_url ()) == f"{ base_url } matlab"
54+ assert r .get (mwi_env .get_env_name_enable_mwi_auth_token ()) == None
55+ assert r .get (mwi_env .get_env_name_mwi_auth_token ()) == None
1856
1957
20- def test_setup_matlab ():
58+ def test_setup_matlab (monkeypatch ):
2159 """Tests for a valid Server Process Configuration Dictionary
2260
2361 This test checks if the jupyter proxy returns the expected Server Process Configuration
@@ -27,6 +65,41 @@ def test_setup_matlab():
2765 package_path = Path (inspect .getfile (jupyter_matlab_proxy )).parent
2866 icon_path = package_path / "icon_open_matlab.svg"
2967
68+ expected_matlab_setup = {
69+ "command" : [
70+ matlab_proxy .get_executable_name (),
71+ "--config" ,
72+ config ["extension_name" ],
73+ ],
74+ "timeout" : 100 ,
75+ "environment" : jupyter_matlab_proxy ._get_env ,
76+ "absolute_url" : True ,
77+ "launcher_entry" : {
78+ "title" : "Open MATLAB" ,
79+ "icon_path" : icon_path ,
80+ },
81+ "request_headers_override" : {
82+ "mwi_auth_token" : jupyter_matlab_proxy ._mwi_auth_token .get ("token_hash" ),
83+ },
84+ }
85+
86+ actual_matlab_setup = jupyter_matlab_proxy .setup_matlab ()
87+
88+ assert expected_matlab_setup == actual_matlab_setup
89+ assert os .path .isfile (actual_matlab_setup ["launcher_entry" ]["icon_path" ])
90+
91+
92+ def test_setup_matlab_with_token_auth_disabled (monkeypatch ):
93+ """Tests for a valid Server Process Configuration Dictionary
94+
95+ This test checks if the jupyter proxy returns the expected Server Process Configuration
96+ Dictionary for the Matlab process when token authentication is explicitly disabled.
97+ """
98+ # Setup
99+ package_path = Path (inspect .getfile (jupyter_matlab_proxy )).parent
100+ icon_path = package_path / "icon_open_matlab.svg"
101+ monkeypatch .setattr (jupyter_matlab_proxy , "_mwi_auth_token" , None )
102+
30103 expected_matlab_setup = {
31104 "command" : [
32105 matlab_proxy .get_executable_name (),
0 commit comments