Skip to content

Commit 387008b

Browse files
krisctlPrabhakar Kumar
authored andcommitted
Enables ability to select from multiple licenses when using Online Licensing.
1 parent fb7ffb6 commit 387008b

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed

src/jupyter_matlab_kernel/mwi_comm_helpers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,24 @@ def fetch_matlab_proxy_status(url, headers):
3434
resp = requests.get(url + "/get_status", headers=headers, verify=False)
3535
if resp.status_code == requests.codes.OK:
3636
data = resp.json()
37-
is_matlab_licensed = data["licensing"] != None
37+
is_matlab_licensed = check_licensing_status(data)
38+
3839
matlab_status = data["matlab"]["status"]
3940
matlab_proxy_has_error = data["error"] != None
4041
return is_matlab_licensed, matlab_status, matlab_proxy_has_error
4142
else:
4243
resp.raise_for_status()
4344

4445

46+
def check_licensing_status(data):
47+
licensing_status = data["licensing"] != None
48+
49+
# Check for entitlementId only in the event of online licensing
50+
if licensing_status and data["licensing"]["type"] == "mhlm":
51+
licensing_status = data["licensing"]["entitlementId"] != None
52+
return licensing_status
53+
54+
4555
def send_execution_request_to_matlab(url, headers, code):
4656
"""
4757
Evaluate MATLAB code and capture results.

tests/unit/jupyter_matlab_kernel/mocks/mock_http_responses.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,72 @@ def raise_for_status(self):
2424
class MockMatlabProxyStatusResponse:
2525
"""A mock of a matlab-proxy status response."""
2626

27-
def __init__(self, is_licensed, matlab_status, has_error) -> None:
27+
def __init__(self, lic_type, matlab_status, has_error) -> None:
2828
"""Construct a mock matlab-proxy status response.
2929
3030
Args:
3131
is_licensed (bool): indicates if MATLAB is licensed.
3232
matlab_status (string): indicates the MATLAB status, i.e. is it "starting", "running" etc.
3333
has_error (bool): indicates if there is an error with MATLAB
3434
"""
35-
self.licensed = is_licensed
35+
36+
self.licensed = self.process_license_type(lic_type)
3637
self.matlab_status = matlab_status
37-
self.error = has_error
38+
self.error = MockError("An example error") if has_error else None
3839

3940
status_code = requests.codes.ok
4041

42+
@staticmethod
43+
def handle_entitled_mhlm():
44+
return {
45+
"type": "mhlm",
46+
"emailAddress": "test@mathworks.com",
47+
"entitlements": [
48+
{
49+
"id": "123456",
50+
"label": "MATLAB - Staff Use",
51+
"license_number": "123456",
52+
}
53+
],
54+
"entitlementId": "123456",
55+
}
56+
57+
@staticmethod
58+
def handle_unentitled_mhlm():
59+
return {
60+
"type": "mhlm",
61+
"emailAddress": "test@mathworks.com",
62+
"entitlements": [
63+
{
64+
"id": "123456",
65+
"label": "MATLAB - Staff Use",
66+
"license_number": "123456",
67+
}
68+
],
69+
"entitlementId": None,
70+
}
71+
72+
@staticmethod
73+
def handle_nlm():
74+
return {"type": "nlm", "connectionString": "123@internal"}
75+
76+
@staticmethod
77+
def handle_existing_license():
78+
return {"type": "existing_license"}
79+
80+
@staticmethod
81+
def default():
82+
return None
83+
84+
def process_license_type(self, lic_type):
85+
types = {
86+
"mhlm_entitled": self.handle_entitled_mhlm,
87+
"mhlm_unentitled": self.handle_unentitled_mhlm,
88+
"nlm": self.handle_nlm,
89+
"existing_license": self.handle_existing_license,
90+
}
91+
return types.get(lic_type, self.default)()
92+
4193
def json(self):
4294
"""Return a matlab-proxy status JSON object."""
4395
return {
@@ -74,3 +126,13 @@ def __init__(self, error_message: str) -> None:
74126
def raise_for_status(self):
75127
"""Raise a HTTPError with custom error message."""
76128
raise HTTPError(self.error_message)
129+
130+
131+
class MockError(Exception):
132+
"""An example error used for testing purposes
133+
134+
Args:
135+
Exception (Any): Dummy exception
136+
"""
137+
138+
pass

tests/unit/jupyter_matlab_kernel/test_mwi_comm_helpers.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,39 @@ def mock_get(*args, **kwargs):
3434
assert MockUnauthorisedRequestResponse().exception_msg in str(exceptionInfo.value)
3535

3636

37-
def test_fetch_matlab_proxy_status(monkeypatch):
37+
@pytest.mark.parametrize(
38+
"input_lic_type, expected_license_status",
39+
[
40+
("mhlm_entitled", True),
41+
("mhlm_unentitled", False),
42+
("nlm", True),
43+
("existing_license", True),
44+
("default", False),
45+
],
46+
)
47+
def test_fetch_matlab_proxy_status(
48+
input_lic_type, expected_license_status, monkeypatch
49+
):
3850
"""
3951
This test checks that fetch_matlab_proxy_status returns the correct
4052
values for a valid request to matlab-proxy.
4153
"""
4254

4355
def mock_get(*args, **kwargs):
44-
return MockMatlabProxyStatusResponse(True, "up", False)
56+
return MockMatlabProxyStatusResponse(
57+
lic_type=input_lic_type, matlab_status="up", has_error=False
58+
)
4559

4660
monkeypatch.setattr(requests, "get", mock_get)
4761

4862
(
49-
is_matlab_licened,
63+
is_matlab_licensed,
5064
matlab_status,
5165
matlab_proxy_has_error,
5266
) = fetch_matlab_proxy_status("", "{}")
53-
assert is_matlab_licened == True
67+
assert is_matlab_licensed == expected_license_status
5468
assert matlab_status == "up"
55-
assert matlab_proxy_has_error == True
69+
assert matlab_proxy_has_error == False
5670

5771

5872
def test_interrupt_request_bad_request(monkeypatch):

0 commit comments

Comments
 (0)