Skip to content

Commit 2c52111

Browse files
krisctlrashedmyt
authored andcommitted
Enable the test which was disabled in v0.9.1
1 parent e0cba8d commit 2c52111

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

tests/unit/jupyter_matlab_kernel/mocks/mock_jupyter_server.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"""
77

88

9-
import pytest
9+
import os
1010

11+
import pytest
1112
import requests
12-
import os
1313
from jupyter_server import serverapp
1414

1515
PID = "server 1"
@@ -18,7 +18,7 @@
1818
SECURE = False
1919
TEST_TOKEN = "test_token"
2020
LICENSING = True
21-
AUTHORISED_HEADERS = {"Authorization": "token test_token"}
21+
AUTHORIZED_HEADERS = {"Authorization": "token test_token"}
2222
PASSWORD = ""
2323

2424

@@ -46,8 +46,11 @@ def fake_list_running_servers(*args, **kwargs):
4646
]
4747

4848
class MockResponse:
49-
status_code = requests.codes.ok
50-
text = "MWI_MATLAB_PROXY_IDENTIFIER"
49+
def __init__(
50+
self, status_code=requests.codes.ok, text="MWI_MATLAB_PROXY_IDENTIFIER"
51+
) -> None:
52+
self.status_code = status_code
53+
self.text = text
5154

5255
@staticmethod
5356
def json():
@@ -58,7 +61,12 @@ def json():
5861
}
5962

6063
def mock_get(*args, **kwargs):
61-
return MockResponse()
64+
# Return a successful matlab_proxy startup message if there is any header present,
65+
# else return an unsuccessful result (via status codes other than 200)
66+
if "headers" in kwargs and kwargs["headers"]:
67+
return MockResponse()
68+
else:
69+
return MockResponse(status_code=requests.codes.unavailable)
6270

6371
monkeypatch.setattr(serverapp, "list_running_servers", fake_list_running_servers)
6472
monkeypatch.setattr(os, "getppid", fake_getppid)

tests/unit/jupyter_matlab_kernel/test_kernel.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# Copyright 2023 The MathWorks, Inc.
22

33
# This file contains tests for jupyter_matlab_kernel.kernel
4-
from jupyter_matlab_kernel.kernel import (
5-
start_matlab_proxy,
6-
MATLABConnectionError,
7-
)
8-
4+
import mocks.mock_jupyter_server as MockJupyterServer
95
import pytest
106
from jupyter_server import serverapp
117
from mocks.mock_jupyter_server import MockJupyterServerFixture
12-
import mocks.mock_jupyter_server as MockJupyterServer
8+
9+
from jupyter_matlab_kernel.kernel import MATLABConnectionError, start_matlab_proxy
1310

1411

1512
def test_start_matlab_proxy_without_jupyter_server():
@@ -33,7 +30,7 @@ def test_start_matlab_proxy(MockJupyterServerFixture):
3330

3431
url, server, headers = start_matlab_proxy()
3532
assert server == MockJupyterServer.BASE_URL
36-
assert headers == MockJupyterServer.AUTHORISED_HEADERS
33+
assert headers == MockJupyterServer.AUTHORIZED_HEADERS
3734
expected_url = (
3835
"http://localhost:"
3936
+ MockJupyterServer.PORT
@@ -73,15 +70,14 @@ def fake_list_running_servers(*args, **kwargs):
7370
assert url == expected_url
7471

7572

76-
# TODO: Need to update usage of mock response in this test.
77-
# def test_start_matlab_proxy_jh_api_token(monkeypatch, MockJupyterServerFixture):
78-
# """
79-
# The test checks that start_matlab_proxy makes use of the environment variable
80-
# JUPYTERHUB_API_TOKEN if it is set.
81-
# """
82-
83-
# monkeypatch.setattr(MockJupyterServer, "TEST_TOKEN", None)
73+
def test_start_matlab_proxy_jh_api_token(monkeypatch, MockJupyterServerFixture):
74+
"""
75+
The test checks that start_matlab_proxy makes use of the environment variable
76+
JUPYTERHUB_API_TOKEN if it is set.
77+
"""
78+
token = "test_jh_token"
79+
monkeypatch.setattr(MockJupyterServer, "TEST_TOKEN", None)
8480

85-
# monkeypatch.setenv("JUPYTERHUB_API_TOKEN", "test_jh_token")
86-
# _, _, headers = start_matlab_proxy()
87-
# assert headers == {"Authorization": "token test_jh_token"}
81+
monkeypatch.setenv("JUPYTERHUB_API_TOKEN", token)
82+
_, _, headers = start_matlab_proxy()
83+
assert headers == {"Authorization": f"token {token}"}

0 commit comments

Comments
 (0)