|
1 | 1 | """Test Base Handlers"""
|
2 | 2 | import os
|
3 | 3 | import warnings
|
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
|
@@ -136,6 +136,32 @@ async def test_jupyter_handler_auth_required(jp_serverapp, jp_fetch):
|
136 | 136 | assert exception.value.code == 403
|
137 | 137 |
|
138 | 138 |
|
| 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 | + |
139 | 165 | def test_api_handler(jp_serverapp):
|
140 | 166 | app: ServerApp = jp_serverapp
|
141 | 167 | headers = HTTPHeaders({"Origin": "foo"})
|
|
0 commit comments