Skip to content

Commit 88d022d

Browse files
committed
add test for template handler
1 parent e282d59 commit 88d022d

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

jupyter_server/extension/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def _prepare_templates(self):
351351
self.settings.update({
352352
"{}_template_paths".format(self.extension_name): self.template_paths
353353
})
354+
self.initialize_templates()
354355

355356
@staticmethod
356357
def initialize_server(argv=[], load_other_extensions=True, **kwargs):

jupyter_server/pytest_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def mkdir(tmp_path, *parts):
5252
config_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "config"))
5353
runtime_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "runtime"))
5454
root_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "root_dir"))
55-
template_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "template_dir"))
55+
template_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "templates"))
5656
system_jupyter_path = pytest.fixture(
5757
lambda tmp_path: mkdir(tmp_path, "share", "jupyter")
5858
)

tests/extension/conftest.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from jupyter_core import paths
77
from jupyter_server.extension import serverextension
88
from jupyter_server.extension.serverextension import _get_config_dir
9-
from jupyter_server.extension.application import ExtensionApp
10-
from jupyter_server.extension.handler import ExtensionHandler
9+
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
10+
from jupyter_server.extension.handler import ExtensionHandler, ExtensionHandlerJinjaMixin
1111

1212
# ----------------- Mock Extension App ----------------------
1313

@@ -17,14 +17,21 @@ def get(self):
1717
self.finish(self.config.mock_trait)
1818

1919

20-
class MockExtensionApp(ExtensionApp):
20+
class MockExtensionTemplateHandler(ExtensionHandlerJinjaMixin, ExtensionHandler):
21+
22+
def get(self):
23+
self.write(self.render_template("index.html"))
24+
25+
26+
class MockExtensionApp(ExtensionAppJinjaMixin, ExtensionApp):
2127
extension_name = 'mockextension'
2228
mock_trait = Unicode('mock trait', config=True)
2329

2430
loaded = False
2531

2632
def initialize_handlers(self):
2733
self.handlers.append(('/mock', MockExtensionHandler))
34+
self.handlers.append(('/mock_template', MockExtensionTemplateHandler))
2835
self.loaded = True
2936

3037
@staticmethod
@@ -39,6 +46,28 @@ def _make_mock_extension_app(**kwargs):
3946
kwargs.setdefault('template_paths', [str(template_dir)])
4047
return MockExtensionApp(**kwargs)
4148

49+
# TODO Should the index template creation be only be done only once?
50+
index = template_dir.joinpath("index.html")
51+
index.write_text("""
52+
<!DOCTYPE HTML>
53+
<html>
54+
<head>
55+
<meta charset="utf-8">
56+
<title>{% block title %}Jupyter Server 1{% endblock %}</title>
57+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
58+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
59+
{% block meta %}
60+
{% endblock %}
61+
</head>
62+
<body>
63+
<div id="site">
64+
{% block site %}
65+
{% endblock site %}
66+
</div>
67+
{% block after_site %}
68+
{% endblock after_site %}
69+
</body>
70+
</html>""")
4271
return _make_mock_extension_app
4372

4473

tests/extension/test_handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ async def test_handler(fetch, extended_serverapp):
1313
assert r.body.decode() == 'mock trait'
1414

1515

16+
async def test_handler_template(fetch, extended_serverapp):
17+
r = await fetch(
18+
'mock_template',
19+
method='GET'
20+
)
21+
assert r.code == 200
22+
23+
1624
async def test_handler_setting(fetch, serverapp, make_mock_extension_app):
1725
# Configure trait in Mock Extension.
1826
m = make_mock_extension_app(mock_trait='test mock trait')

0 commit comments

Comments
 (0)