forked from jupyter/notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
103 lines (76 loc) · 2.81 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import pytest
from tornado.httpclient import HTTPClientError
from notebook.app import JupyterNotebookApp, NotebookHandler, TreeHandler
@pytest.fixture
def notebooks(jp_create_notebook, notebookapp):
nbpaths = (
"notebook1.ipynb",
"jlab_test_notebooks/notebook2.ipynb",
"jlab_test_notebooks/level2/notebook3.ipynb",
)
for nb in nbpaths:
jp_create_notebook(nb)
return nbpaths
async def test_notebook_handler(notebooks, jp_fetch):
for nbpath in notebooks:
r = await jp_fetch("/", nbpath)
assert r.code == 200
# Check that the lab template is loaded
html = r.body.decode()
assert "Jupyter Notebook" in html
r = await jp_fetch("/notebooks", nbpath)
assert r.code == 200
# Check that the lab template is loaded
html = r.body.decode()
assert "Jupyter Notebook" in html
redirected_url = None
def redirect(self, url):
nonlocal redirected_url
redirected_url = url
NotebookHandler.redirect = redirect
await jp_fetch("notebooks", "jlab_test_notebooks")
assert redirected_url == "/a%40b/tree/jlab_test_notebooks"
async def test_tree_handler(notebooks, notebookapp, jp_fetch):
app: JupyterNotebookApp = notebookapp
r = await jp_fetch("tree", "jlab_test_notebooks")
assert r.code == 200
# Check that the tree template is loaded
html = r.body.decode()
assert "<title>Home</title>" in html
redirected_url = None
def redirect(self, url):
nonlocal redirected_url
redirected_url = url
TreeHandler.redirect = redirect
await jp_fetch("tree", "notebook1.ipynb")
assert redirected_url == "/a%40b/notebooks/notebook1.ipynb"
with open(os.path.join(app.serverapp.root_dir, "foo.txt"), "w") as fid:
fid.write("hello")
await jp_fetch("tree", "foo.txt")
assert redirected_url == "/a%40b/files/foo.txt"
with pytest.raises(HTTPClientError):
await jp_fetch("tree", "does_not_exist.ipynb")
async def test_console_handler(notebookapp, jp_fetch):
r = await jp_fetch("consoles", "foo")
assert r.code == 200
html = r.body.decode()
assert "- Console</title>" in html
async def test_terminals_handler(notebookapp, jp_fetch):
r = await jp_fetch("terminals", "foo")
assert r.code == 200
html = r.body.decode()
assert "- Terminal</title>" in html
async def test_edit_handler(notebooks, jp_fetch):
r = await jp_fetch("edit", "notebook1.ipynb")
assert r.code == 200
html = r.body.decode()
assert "- Edit</title>" in html
async def test_app(notebookapp):
app: JupyterNotebookApp = notebookapp
assert app.static_dir
assert app.templates_dir
assert app.app_settings_dir
assert app.schemas_dir
assert app.user_settings_dir
assert app.workspaces_dir