|
| 1 | +# Copyright 2023 The MathWorks, Inc. |
| 2 | + |
| 3 | +# This file contains tests for jupyter_matlab_kernel.kernel |
| 4 | +from jupyter_matlab_kernel.kernel import ( |
| 5 | + start_matlab_proxy, |
| 6 | + MATLABConnectionError, |
| 7 | +) |
| 8 | + |
| 9 | +import pytest |
| 10 | +from jupyter_server import serverapp |
| 11 | +from mocks.mock_jupyter_server import MockJupyterServerFixture |
| 12 | +import mocks.mock_jupyter_server as MockJupyterServer |
| 13 | + |
| 14 | + |
| 15 | +def test_start_matlab_proxy_without_jupyter_server(): |
| 16 | + """ |
| 17 | + This test checks that trying to start matlab-proxy outside of a Jupyter environment |
| 18 | + raises an execption. |
| 19 | + """ |
| 20 | + with pytest.raises(MATLABConnectionError) as exceptionInfo: |
| 21 | + start_matlab_proxy() |
| 22 | + |
| 23 | + assert ( |
| 24 | + "MATLAB Kernel for Jupyter was unable to find the notebook server from which it was spawned!" |
| 25 | + in str(exceptionInfo.value) |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def test_start_matlab_proxy(MockJupyterServerFixture): |
| 30 | + """ |
| 31 | + This test checks start_matlab_proxy returns a correctly configured URL |
| 32 | + """ |
| 33 | + |
| 34 | + url, server, headers = start_matlab_proxy() |
| 35 | + assert server == MockJupyterServer.BASE_URL |
| 36 | + assert headers == MockJupyterServer.AUTHORISED_HEADERS |
| 37 | + expected_url = ( |
| 38 | + "http://localhost:" |
| 39 | + + MockJupyterServer.PORT |
| 40 | + + MockJupyterServer.BASE_URL |
| 41 | + + "matlab" |
| 42 | + ) |
| 43 | + assert url == expected_url |
| 44 | + |
| 45 | + |
| 46 | +def test_start_matlab_proxy_secure(monkeypatch, MockJupyterServerFixture): |
| 47 | + """ |
| 48 | + This test checks that start_matlab_proxy returns a HTTPS url if configured |
| 49 | + to do so. |
| 50 | + """ |
| 51 | + |
| 52 | + def fake_list_running_servers(*args, **kwargs): |
| 53 | + return [ |
| 54 | + { |
| 55 | + "pid": MockJupyterServer.PID, |
| 56 | + "port": MockJupyterServer.PORT, |
| 57 | + "base_url": MockJupyterServer.BASE_URL, |
| 58 | + "secure": True, |
| 59 | + "token": MockJupyterServer.TEST_TOKEN, |
| 60 | + "password": MockJupyterServer.PASSWORD, |
| 61 | + } |
| 62 | + ] |
| 63 | + |
| 64 | + monkeypatch.setattr(serverapp, "list_running_servers", fake_list_running_servers) |
| 65 | + |
| 66 | + url, _, _ = start_matlab_proxy() |
| 67 | + expected_url = ( |
| 68 | + "https://localhost:" |
| 69 | + + MockJupyterServer.PORT |
| 70 | + + MockJupyterServer.BASE_URL |
| 71 | + + "matlab" |
| 72 | + ) |
| 73 | + assert url == expected_url |
| 74 | + |
| 75 | + |
| 76 | +def test_start_matlab_proxy_jh_api_token(monkeypatch, MockJupyterServerFixture): |
| 77 | + """ |
| 78 | + The test checks that start_matlab_proxy makes use of the environment variable |
| 79 | + JUPYTERHUB_API_TOKEN if it is set. |
| 80 | + """ |
| 81 | + |
| 82 | + monkeypatch.setattr(MockJupyterServer, "TEST_TOKEN", None) |
| 83 | + |
| 84 | + monkeypatch.setenv("JUPYTERHUB_API_TOKEN", "test_jh_token") |
| 85 | + _, _, headers = start_matlab_proxy() |
| 86 | + assert headers == {"Authorization": "token test_jh_token"} |
0 commit comments