Skip to content

Commit 319a4d1

Browse files
committed
Call super.prepare() in all code paths, test it
1 parent 54c2ea2 commit 319a4d1

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

jupyter_server/base/handlers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ async def prepare(self) -> Awaitable[None] | None: # type:ignore[override]
638638
if not getattr(method, "__allow_unauthenticated", False):
639639
# reuse `web.authenticated` logic, which redirects to the login
640640
# page on GET and HEAD and otherwise raises 403
641-
return web.authenticated(lambda _method: None)(self)
641+
return web.authenticated(lambda _: super().prepare)(self)
642642

643643
return super().prepare()
644644

tests/base/test_handlers.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Test Base Handlers"""
22
import os
33
import warnings
4-
from unittest.mock import MagicMock
4+
from unittest.mock import MagicMock, patch
55

66
import pytest
77
from tornado.httpclient import HTTPClientError
@@ -136,6 +136,32 @@ async def test_jupyter_handler_auth_required(jp_serverapp, jp_fetch):
136136
assert exception.value.code == 403
137137

138138

139+
@pytest.mark.parametrize(
140+
"jp_server_config", [{"ServerApp": {"allow_unauthenticated_access": False}}]
141+
)
142+
async def test_jupyter_handler_auth_calls_prepare(jp_serverapp, jp_fetch):
143+
app: ServerApp = jp_serverapp
144+
app.web_app.add_handlers(
145+
".*$",
146+
[
147+
(url_path_join(app.base_url, "no-rules"), NoAuthRulesHandler),
148+
(url_path_join(app.base_url, "permissive"), PermissiveHandler),
149+
],
150+
)
151+
152+
# should call `super.prepare()` in `@allow_unauthenticated` code path
153+
with patch.object(JupyterHandler, "prepare", return_value=None) as mock:
154+
res = await jp_fetch("permissive", method="OPTIONS")
155+
assert res.code == 200
156+
assert mock.call_count == 1
157+
158+
# should call `super.prepare()` in code path that checks authentication
159+
with patch.object(JupyterHandler, "prepare", return_value=None) as mock:
160+
res = await jp_fetch("no-rules", method="OPTIONS")
161+
assert res.code == 200
162+
assert mock.call_count == 1
163+
164+
139165
def test_api_handler(jp_serverapp):
140166
app: ServerApp = jp_serverapp
141167
headers = HTTPHeaders({"Origin": "foo"})

tests/extension/test_handler.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from tornado.httpclient import HTTPClientError
32

43

54
@pytest.fixture()

0 commit comments

Comments
 (0)