1
- # Copyright 2020-2021 The MathWorks, Inc.
1
+ # Copyright 2020-2023 The MathWorks, Inc.
2
2
3
- import os , inspect
4
- import matlab_proxy , jupyter_matlab_proxy
3
+ import inspect
4
+ import os
5
5
from pathlib import Path
6
+
7
+ import matlab_proxy
6
8
from matlab_proxy .util .mwi import environment_variables as mwi_env
9
+
10
+ import jupyter_matlab_proxy
7
11
from jupyter_matlab_proxy .jupyter_config import config
8
12
9
13
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
+
10
29
def test_get_env ():
11
30
"""Tests if _get_env() method returns the expected enviroment settings as a dict."""
12
31
13
32
port = 10000
14
33
base_url = "/foo/"
15
34
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
18
56
19
57
20
- def test_setup_matlab ():
58
+ def test_setup_matlab (monkeypatch ):
21
59
"""Tests for a valid Server Process Configuration Dictionary
22
60
23
61
This test checks if the jupyter proxy returns the expected Server Process Configuration
@@ -27,6 +65,41 @@ def test_setup_matlab():
27
65
package_path = Path (inspect .getfile (jupyter_matlab_proxy )).parent
28
66
icon_path = package_path / "icon_open_matlab.svg"
29
67
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
+
30
103
expected_matlab_setup = {
31
104
"command" : [
32
105
matlab_proxy .get_executable_name (),
0 commit comments