Skip to content

Commit 5e7615d

Browse files
committed
Add test for identity provider being used
The websocket test is failing as of this commit because as pointed out in review the `_maybe_auth` is using the default user rather the one from `IdentityProvider`
1 parent 319a4d1 commit 5e7615d

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

tests/base/test_handlers.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from tornado.httpserver import HTTPRequest
99
from tornado.httputil import HTTPHeaders
1010

11-
from jupyter_server.auth import AllowAllAuthorizer, IdentityProvider
11+
from jupyter_server.auth import AllowAllAuthorizer, IdentityProvider, User
1212
from jupyter_server.auth.decorator import allow_unauthenticated
1313
from jupyter_server.base.handlers import (
1414
APIHandler,
@@ -162,6 +162,37 @@ async def test_jupyter_handler_auth_calls_prepare(jp_serverapp, jp_fetch):
162162
assert mock.call_count == 1
163163

164164

165+
class IndiscriminateIdentityProvider(IdentityProvider):
166+
async def get_user(self, handler):
167+
return User(username="test")
168+
169+
170+
@pytest.mark.parametrize(
171+
"jp_server_config", [{"ServerApp": {"allow_unauthenticated_access": False}}]
172+
)
173+
async def test_jupyter_handler_auth_respsects_identity_provider(jp_serverapp, jp_fetch):
174+
app: ServerApp = jp_serverapp
175+
app.web_app.add_handlers(
176+
".*$",
177+
[(url_path_join(app.base_url, "no-rules"), NoAuthRulesHandler)],
178+
)
179+
180+
def fetch():
181+
return jp_fetch("no-rules", method="OPTIONS", headers={"Authorization": ""})
182+
183+
# If no identity provider is set the following request should fail
184+
# because the default tornado user would not be found:
185+
with pytest.raises(HTTPClientError) as exception:
186+
await fetch()
187+
assert exception.value.code == 403
188+
189+
iidp = IndiscriminateIdentityProvider()
190+
# should allow access with the user set be the identity provider
191+
with patch.dict(jp_serverapp.web_app.settings, {"identity_provider": iidp}):
192+
res = await fetch()
193+
assert res.code == 200
194+
195+
165196
def test_api_handler(jp_serverapp):
166197
app: ServerApp = jp_serverapp
167198
headers = HTTPHeaders({"Origin": "foo"})

tests/base/test_websocket.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
"""Test Base Websocket classes"""
22
import logging
33
import time
4-
from unittest.mock import MagicMock
4+
from unittest.mock import MagicMock, patch
55

66
import pytest
77
from tornado.httpclient import HTTPClientError
88
from tornado.httpserver import HTTPRequest
99
from tornado.httputil import HTTPHeaders
1010
from tornado.websocket import WebSocketClosedError, WebSocketHandler
1111

12+
from jupyter_server.auth import IdentityProvider, User
1213
from jupyter_server.auth.decorator import allow_unauthenticated
1314
from jupyter_server.base.websocket import WebSocketMixin
1415
from jupyter_server.serverapp import ServerApp
@@ -118,3 +119,34 @@ async def test_websocket_auth_required(jp_serverapp, jp_ws_fetch):
118119
with pytest.raises(HTTPClientError) as exception:
119120
ws = await jp_ws_fetch("no-rules", headers={"Authorization": ""})
120121
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

Comments
 (0)