Skip to content

Commit

Permalink
Restore default writing of browser open redirect file, add opt-in to …
Browse files Browse the repository at this point in the history
…skip (#1144)

fixes undefined
  • Loading branch information
bollwyvl committed Dec 21, 2022
1 parent 1c9c943 commit e35fbbc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
35 changes: 25 additions & 10 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,10 @@ def _default_info_file(self):
info_file = "jpserver-%s.json" % os.getpid()
return os.path.join(self.runtime_dir, info_file)

no_browser_open_file = Bool(
False, help="If True, do not write redirect HTML file disk, or show in messages."
)

browser_open_file = Unicode()

@default("browser_open_file")
Expand Down Expand Up @@ -2815,9 +2819,11 @@ def start_app(self):

self.write_server_info_file()

if not self.no_browser_open_file:
self.write_browser_open_files()

# Handle the browser opening.
if self.open_browser and not self.sock:
self.write_browser_open_files()
self.launch_browser()

if self.identity_provider.token and self.identity_provider.token_generated:
Expand All @@ -2840,17 +2846,26 @@ def start_app(self):
)
)
else:
self.log.critical(
"\n".join(
[
"\n",
if self.no_browser_open_file:
message = [
"\n",
_i18n("To access the server, copy and paste one of these URLs:"),
" %s" % self.display_url,
]
else:
message = [
"\n",
_i18n(
"To access the server, open this file in a browser:",
" %s" % urljoin("file:", pathname2url(self.browser_open_file)),
),
" %s" % urljoin("file:", pathname2url(self.browser_open_file)),
_i18n(
"Or copy and paste one of these URLs:",
" %s" % self.display_url,
]
)
)
),
" %s" % self.display_url,
]

self.log.critical("\n".join(message))

async def _cleanup(self):
"""General cleanup of files, extensions and kernels created
Expand Down
10 changes: 10 additions & 0 deletions tests/test_serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
list_running_servers,
random_ports,
)
from jupyter_server.utils import pathname2url, urljoin


def test_help_output():
Expand Down Expand Up @@ -484,3 +485,12 @@ async def test_shutdown_no_activity(jp_serverapp):
def test_running_server_info(jp_serverapp):
app: ServerApp = jp_serverapp
app.running_server_info(True)


@pytest.mark.parametrize("should_exist", [True, False])
def test_browser_open_files(jp_configurable_serverapp, should_exist, caplog):
app = jp_configurable_serverapp(no_browser_open_file=not should_exist)
assert os.path.exists(app.browser_open_file) == should_exist
url = urljoin("file:", pathname2url(app.browser_open_file))
url_messages = [rec.message for rec in caplog.records if url in rec.message]
assert url_messages if should_exist else not url_messages

0 comments on commit e35fbbc

Please sign in to comment.