|
| 1 | +"""Selftests for the JupyterLab notebook-cleanup helpers on WorkbenchClient. |
| 2 | +
|
| 3 | +These cover the pure URL/header builders and the ``delete_jupyter_notebook`` |
| 4 | +method (via an ``httpx.MockTransport``), so the contents-API teardown that keeps |
| 5 | +JupyterLab sessions fresh is exercised without a live Workbench. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import httpx |
| 11 | +import pytest |
| 12 | + |
| 13 | +from vip.clients.workbench import ( |
| 14 | + WorkbenchClient, |
| 15 | + jupyterlab_app_base, |
| 16 | + jupyterlab_contents_delete_url, |
| 17 | + jupyterlab_xsrf_headers, |
| 18 | +) |
| 19 | + |
| 20 | +# -- jupyterlab_app_base ----------------------------------------------------- |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "page_url, expected", |
| 25 | + [ |
| 26 | + # Standard per-session proxy prefix, notebook open under /lab/tree. |
| 27 | + ( |
| 28 | + "https://wb.example.com/s/abc123/lab/tree/Untitled.ipynb", |
| 29 | + "https://wb.example.com/s/abc123", |
| 30 | + ), |
| 31 | + # Bare /lab root. |
| 32 | + ("https://wb.example.com/s/abc123/lab", "https://wb.example.com/s/abc123"), |
| 33 | + # Query string and fragment are stripped. |
| 34 | + ( |
| 35 | + "https://wb.example.com/s/xyz/lab/tree/Untitled1.ipynb?reset#cell", |
| 36 | + "https://wb.example.com/s/xyz", |
| 37 | + ), |
| 38 | + # No /lab segment — return the URL trimmed rather than raising. |
| 39 | + ("https://wb.example.com/s/abc123/", "https://wb.example.com/s/abc123"), |
| 40 | + # Real Workbench shape (validated live via CDP on pwb.demo): JupyterLab |
| 41 | + # inserts a /lab/workspaces/<id>/tree/<nb> segment. The base must still |
| 42 | + # cut at /lab and match PageConfig.baseUrl (".../s/<sid>"). |
| 43 | + ( |
| 44 | + "https://pwb.demo.soleng.posit.it/s/0da2921f2ed72297f2efa/lab/workspaces/auto-m/tree/Untitled7.ipynb", |
| 45 | + "https://pwb.demo.soleng.posit.it/s/0da2921f2ed72297f2efa", |
| 46 | + ), |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_jupyterlab_app_base(page_url, expected): |
| 50 | + assert jupyterlab_app_base(page_url) == expected |
| 51 | + |
| 52 | + |
| 53 | +# -- jupyterlab_contents_delete_url ------------------------------------------ |
| 54 | + |
| 55 | + |
| 56 | +def test_contents_delete_url_basic(): |
| 57 | + url = jupyterlab_contents_delete_url( |
| 58 | + "https://wb.example.com/s/abc123/lab/tree/Untitled.ipynb", "Untitled.ipynb" |
| 59 | + ) |
| 60 | + assert url == "https://wb.example.com/s/abc123/api/contents/Untitled.ipynb" |
| 61 | + |
| 62 | + |
| 63 | +def test_contents_delete_url_quotes_spaces_and_strips_slashes(): |
| 64 | + url = jupyterlab_contents_delete_url("https://wb.example.com/s/abc123/lab", "/_vip run 1.ipynb") |
| 65 | + # Leading slash stripped, space percent-encoded. |
| 66 | + assert url == "https://wb.example.com/s/abc123/api/contents/_vip%20run%201.ipynb" |
| 67 | + |
| 68 | + |
| 69 | +# -- jupyterlab_xsrf_headers ------------------------------------------------- |
| 70 | + |
| 71 | + |
| 72 | +def test_xsrf_headers_present(): |
| 73 | + assert jupyterlab_xsrf_headers({"_xsrf": "tok123"}) == {"X-XSRFToken": "tok123"} |
| 74 | + |
| 75 | + |
| 76 | +def test_xsrf_headers_absent(): |
| 77 | + assert jupyterlab_xsrf_headers({"other": "v"}) == {} |
| 78 | + |
| 79 | + |
| 80 | +# -- delete_jupyter_notebook (MockTransport) --------------------------------- |
| 81 | + |
| 82 | + |
| 83 | +def _client_with_handler(handler) -> WorkbenchClient: |
| 84 | + wc = WorkbenchClient("https://wb.example.com") |
| 85 | + wc._client.close() |
| 86 | + wc._client = httpx.Client( |
| 87 | + base_url="https://wb.example.com", |
| 88 | + transport=httpx.MockTransport(handler), |
| 89 | + ) |
| 90 | + return wc |
| 91 | + |
| 92 | + |
| 93 | +def test_delete_notebook_success_sends_xsrf_and_targets_contents_api(): |
| 94 | + seen: dict[str, object] = {} |
| 95 | + |
| 96 | + def handler(request: httpx.Request) -> httpx.Response: |
| 97 | + seen["method"] = request.method |
| 98 | + seen["path"] = request.url.path |
| 99 | + seen["xsrf"] = request.headers.get("X-XSRFToken") |
| 100 | + return httpx.Response(204) |
| 101 | + |
| 102 | + wc = _client_with_handler(handler) |
| 103 | + ok = wc.delete_jupyter_notebook( |
| 104 | + "https://wb.example.com/s/abc123/lab/tree/Untitled.ipynb", |
| 105 | + "Untitled.ipynb", |
| 106 | + {"_xsrf": "tok123"}, |
| 107 | + ) |
| 108 | + assert ok is True |
| 109 | + assert seen["method"] == "DELETE" |
| 110 | + assert seen["path"] == "/s/abc123/api/contents/Untitled.ipynb" |
| 111 | + assert seen["xsrf"] == "tok123" |
| 112 | + |
| 113 | + |
| 114 | +def test_delete_notebook_404_treated_as_success(): |
| 115 | + def handler(request: httpx.Request) -> httpx.Response: |
| 116 | + return httpx.Response(404) |
| 117 | + |
| 118 | + wc = _client_with_handler(handler) |
| 119 | + assert ( |
| 120 | + wc.delete_jupyter_notebook("https://wb.example.com/s/abc/lab", "Untitled.ipynb", {}) is True |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def test_delete_notebook_server_error_is_false(): |
| 125 | + def handler(request: httpx.Request) -> httpx.Response: |
| 126 | + return httpx.Response(500) |
| 127 | + |
| 128 | + wc = _client_with_handler(handler) |
| 129 | + assert ( |
| 130 | + wc.delete_jupyter_notebook( |
| 131 | + "https://wb.example.com/s/abc/lab", "Untitled.ipynb", {"_xsrf": "t"} |
| 132 | + ) |
| 133 | + is False |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def test_delete_notebook_transport_error_is_false(): |
| 138 | + def handler(request: httpx.Request) -> httpx.Response: |
| 139 | + raise httpx.ConnectError("boom") |
| 140 | + |
| 141 | + wc = _client_with_handler(handler) |
| 142 | + assert ( |
| 143 | + wc.delete_jupyter_notebook("https://wb.example.com/s/abc/lab", "Untitled.ipynb", {}) |
| 144 | + is False |
| 145 | + ) |
0 commit comments