|
4 | 4 | from unittest.mock import MagicMock
|
5 | 5 |
|
6 | 6 | import pytest
|
| 7 | +from tornado.httpclient import HTTPClientError |
7 | 8 | from tornado.httpserver import HTTPRequest
|
8 | 9 | from tornado.httputil import HTTPHeaders
|
9 | 10 | from tornado.websocket import WebSocketClosedError, WebSocketHandler
|
10 | 11 |
|
| 12 | +from jupyter_server.auth.decorator import allow_unauthenticated |
11 | 13 | from jupyter_server.base.websocket import WebSocketMixin
|
12 | 14 | from jupyter_server.serverapp import ServerApp
|
| 15 | +from jupyter_server.utils import url_path_join |
13 | 16 |
|
14 | 17 |
|
15 | 18 | class MockHandler(WebSocketMixin, WebSocketHandler):
|
@@ -60,3 +63,58 @@ async def test_ping_client_timeout(mixin):
|
60 | 63 | mixin.send_ping()
|
61 | 64 | with pytest.raises(WebSocketClosedError):
|
62 | 65 | mixin.write_message("hello")
|
| 66 | + |
| 67 | + |
| 68 | +class NoAuthRulesWebsocketHandler(MockHandler): |
| 69 | + pass |
| 70 | + |
| 71 | + |
| 72 | +class PermissiveWebsocketHandler(MockHandler): |
| 73 | + @allow_unauthenticated |
| 74 | + def get(self, *args, **kwargs) -> None: |
| 75 | + return super().get(*args, **kwargs) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize( |
| 79 | + "jp_server_config", [{"ServerApp": {"allow_unauthenticated_access": True}}] |
| 80 | +) |
| 81 | +async def test_websocket_auth_permissive(jp_serverapp, jp_ws_fetch): |
| 82 | + app: ServerApp = jp_serverapp |
| 83 | + app.web_app.add_handlers( |
| 84 | + ".*$", |
| 85 | + [ |
| 86 | + (url_path_join(app.base_url, "no-rules"), NoAuthRulesWebsocketHandler), |
| 87 | + (url_path_join(app.base_url, "permissive"), PermissiveWebsocketHandler), |
| 88 | + ], |
| 89 | + ) |
| 90 | + |
| 91 | + # should always permit access when `@allow_unauthenticated` is used |
| 92 | + ws = await jp_ws_fetch("permissive", headers={"Authorization": ""}) |
| 93 | + ws.close() |
| 94 | + |
| 95 | + # should allow access when no authentication rules are set up |
| 96 | + ws = await jp_ws_fetch("no-rules", headers={"Authorization": ""}) |
| 97 | + ws.close() |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize( |
| 101 | + "jp_server_config", [{"ServerApp": {"allow_unauthenticated_access": False}}] |
| 102 | +) |
| 103 | +async def test_websocket_auth_required(jp_serverapp, jp_ws_fetch): |
| 104 | + app: ServerApp = jp_serverapp |
| 105 | + app.web_app.add_handlers( |
| 106 | + ".*$", |
| 107 | + [ |
| 108 | + (url_path_join(app.base_url, "no-rules"), NoAuthRulesWebsocketHandler), |
| 109 | + (url_path_join(app.base_url, "permissive"), PermissiveWebsocketHandler), |
| 110 | + ], |
| 111 | + ) |
| 112 | + |
| 113 | + # should always permit access when `@allow_unauthenticated` is used |
| 114 | + ws = await jp_ws_fetch("permissive", headers={"Authorization": ""}) |
| 115 | + ws.close() |
| 116 | + |
| 117 | + # should forbid access when no authentication rules are set up |
| 118 | + with pytest.raises(HTTPClientError) as exception: |
| 119 | + ws = await jp_ws_fetch("no-rules", headers={"Authorization": ""}) |
| 120 | + assert exception.value.code == 403 |
0 commit comments