Skip to content

Commit

Permalink
add jupyter extensions to userprofile handler
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-sanders committed Nov 4, 2024
1 parent def8d01 commit ebe5306
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion cylc/uiserver/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import json
import getpass
import os
import re
from typing import TYPE_CHECKING, Callable, Dict

from graphene_tornado.tornado_graphql_handler import TornadoGraphQLHandler
Expand All @@ -44,6 +45,7 @@


ME = getpass.getuser()
RE_SLASH = x = re.compile('\/+')


def authorised(fun: Callable) -> Callable:
Expand Down Expand Up @@ -258,7 +260,6 @@ def set_default_headers(self) -> None:
self.set_header("Content-Type", 'application/json')

@web.authenticated
# @authorised TODO: I can't think why we would want to authorise this
def get(self):
user_info = {
**self.current_user.__dict__,
Expand All @@ -281,6 +282,19 @@ def get(self):
user_info['mode'] = 'single user'
else:
user_info['mode'] = 'multi user'

user_info['extensions'] = {
app.name: RE_SLASH.sub(
'/', f'{self.serverapp.base_url}/{app.default_url}'
)
for extension_apps
in self.serverapp.extension_manager.extension_apps.values()
# filter out extensions that do not provide a default_url OR
# set it to the root endpoint.
for app in extension_apps
if getattr(app, 'default_url', '/') != '/'
}

self.write(json.dumps(user_info))


Expand Down
17 changes: 17 additions & 0 deletions cylc/uiserver/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from functools import partial
from getpass import getuser
import json
from unittest import mock
from unittest.mock import MagicMock
import pytest
Expand Down Expand Up @@ -120,3 +121,19 @@ def test_assert_callback_handler_gets_called(self):
self.io_loop.run_sync(handler.open,
get_async_test_timeout())
handler.subscription_server.handle.assert_called_once()


@pytest.mark.integration
async def test_userprofile(
jp_fetch, cylc_uis, jp_serverapp,
):
"""Test the userprofile endpoint."""
# patch the default_url back to how it is set in cylc.uiserver.app
cylc_uis.default_url = '/cylc'

response = await jp_fetch('cylc', 'userprofile')
user_profile = json.loads(response.body.decode())
assert user_profile['username'] == getuser()
assert user_profile['owner'] == getuser()
assert 'read' in user_profile['permissions']
assert 'cylc' in user_profile['extensions']

0 comments on commit ebe5306

Please sign in to comment.