|
1 | 1 | """Test Base Websocket classes"""
|
2 | 2 | import logging
|
3 | 3 | import time
|
4 |
| -from unittest.mock import MagicMock |
| 4 | +from unittest.mock import MagicMock, patch |
5 | 5 |
|
6 | 6 | import pytest
|
7 | 7 | from tornado.httpclient import HTTPClientError
|
8 | 8 | from tornado.httpserver import HTTPRequest
|
9 | 9 | from tornado.httputil import HTTPHeaders
|
10 | 10 | from tornado.websocket import WebSocketClosedError, WebSocketHandler
|
11 | 11 |
|
| 12 | +from jupyter_server.auth import IdentityProvider, User |
12 | 13 | from jupyter_server.auth.decorator import allow_unauthenticated
|
13 | 14 | from jupyter_server.base.websocket import WebSocketMixin
|
14 | 15 | from jupyter_server.serverapp import ServerApp
|
@@ -118,3 +119,34 @@ async def test_websocket_auth_required(jp_serverapp, jp_ws_fetch):
|
118 | 119 | with pytest.raises(HTTPClientError) as exception:
|
119 | 120 | ws = await jp_ws_fetch("no-rules", headers={"Authorization": ""})
|
120 | 121 | assert exception.value.code == 403
|
| 122 | + |
| 123 | + |
| 124 | +class IndiscriminateIdentityProvider(IdentityProvider): |
| 125 | + async def get_user(self, handler): |
| 126 | + return User(username="test") |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize( |
| 130 | + "jp_server_config", [{"ServerApp": {"allow_unauthenticated_access": False}}] |
| 131 | +) |
| 132 | +async def test_websocket_auth_respsects_identity_provider(jp_serverapp, jp_ws_fetch): |
| 133 | + app: ServerApp = jp_serverapp |
| 134 | + app.web_app.add_handlers( |
| 135 | + ".*$", |
| 136 | + [(url_path_join(app.base_url, "no-rules"), NoAuthRulesWebsocketHandler)], |
| 137 | + ) |
| 138 | + |
| 139 | + def fetch(): |
| 140 | + return jp_ws_fetch("no-rules", headers={"Authorization": ""}) |
| 141 | + |
| 142 | + # If no identity provider is set the following request should fail |
| 143 | + # because the default tornado user would not be found: |
| 144 | + with pytest.raises(HTTPClientError) as exception: |
| 145 | + await fetch() |
| 146 | + assert exception.value.code == 403 |
| 147 | + |
| 148 | + iidp = IndiscriminateIdentityProvider() |
| 149 | + # should allow access with the user set be the identity provider |
| 150 | + with patch.dict(jp_serverapp.web_app.settings, {"identity_provider": iidp}): |
| 151 | + res = await fetch() |
| 152 | + assert res.code == 200 |
0 commit comments