Show More
@@ -1,375 +1,378 b'' | |||||
1 | # coding: utf-8 |
|
1 | # coding: utf-8 | |
2 | """Tests for the notebook manager.""" |
|
2 | """Tests for the notebook manager.""" | |
3 | from __future__ import print_function |
|
3 | from __future__ import print_function | |
4 |
|
4 | |||
5 | import os |
|
5 | import os | |
6 |
|
6 | |||
7 | from tornado.web import HTTPError |
|
7 | from tornado.web import HTTPError | |
8 | from unittest import TestCase |
|
8 | from unittest import TestCase | |
9 | from tempfile import NamedTemporaryFile |
|
9 | from tempfile import NamedTemporaryFile | |
10 |
|
10 | |||
11 | from IPython.nbformat import v4 as nbformat |
|
11 | from IPython.nbformat import v4 as nbformat | |
12 |
|
12 | |||
13 | from IPython.utils.tempdir import TemporaryDirectory |
|
13 | from IPython.utils.tempdir import TemporaryDirectory | |
14 | from IPython.utils.traitlets import TraitError |
|
14 | from IPython.utils.traitlets import TraitError | |
15 | from IPython.html.utils import url_path_join |
|
15 | from IPython.html.utils import url_path_join | |
16 | from IPython.testing import decorators as dec |
|
16 | from IPython.testing import decorators as dec | |
17 |
|
17 | |||
18 | from ..filemanager import FileContentsManager |
|
18 | from ..filemanager import FileContentsManager | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | class TestFileContentsManager(TestCase): |
|
21 | class TestFileContentsManager(TestCase): | |
22 |
|
22 | |||
23 | def test_root_dir(self): |
|
23 | def test_root_dir(self): | |
24 | with TemporaryDirectory() as td: |
|
24 | with TemporaryDirectory() as td: | |
25 | fm = FileContentsManager(root_dir=td) |
|
25 | fm = FileContentsManager(root_dir=td) | |
26 | self.assertEqual(fm.root_dir, td) |
|
26 | self.assertEqual(fm.root_dir, td) | |
27 |
|
27 | |||
28 | def test_missing_root_dir(self): |
|
28 | def test_missing_root_dir(self): | |
29 | with TemporaryDirectory() as td: |
|
29 | with TemporaryDirectory() as td: | |
30 | root = os.path.join(td, 'notebook', 'dir', 'is', 'missing') |
|
30 | root = os.path.join(td, 'notebook', 'dir', 'is', 'missing') | |
31 | self.assertRaises(TraitError, FileContentsManager, root_dir=root) |
|
31 | self.assertRaises(TraitError, FileContentsManager, root_dir=root) | |
32 |
|
32 | |||
33 | def test_invalid_root_dir(self): |
|
33 | def test_invalid_root_dir(self): | |
34 | with NamedTemporaryFile() as tf: |
|
34 | with NamedTemporaryFile() as tf: | |
35 | self.assertRaises(TraitError, FileContentsManager, root_dir=tf.name) |
|
35 | self.assertRaises(TraitError, FileContentsManager, root_dir=tf.name) | |
36 |
|
36 | |||
37 | def test_get_os_path(self): |
|
37 | def test_get_os_path(self): | |
38 | # full filesystem path should be returned with correct operating system |
|
38 | # full filesystem path should be returned with correct operating system | |
39 | # separators. |
|
39 | # separators. | |
40 | with TemporaryDirectory() as td: |
|
40 | with TemporaryDirectory() as td: | |
41 | root = td |
|
41 | root = td | |
42 | fm = FileContentsManager(root_dir=root) |
|
42 | fm = FileContentsManager(root_dir=root) | |
43 | path = fm._get_os_path('/path/to/notebook/test.ipynb') |
|
43 | path = fm._get_os_path('/path/to/notebook/test.ipynb') | |
44 | rel_path_list = '/path/to/notebook/test.ipynb'.split('/') |
|
44 | rel_path_list = '/path/to/notebook/test.ipynb'.split('/') | |
45 | fs_path = os.path.join(fm.root_dir, *rel_path_list) |
|
45 | fs_path = os.path.join(fm.root_dir, *rel_path_list) | |
46 | self.assertEqual(path, fs_path) |
|
46 | self.assertEqual(path, fs_path) | |
47 |
|
47 | |||
48 | fm = FileContentsManager(root_dir=root) |
|
48 | fm = FileContentsManager(root_dir=root) | |
49 | path = fm._get_os_path('test.ipynb') |
|
49 | path = fm._get_os_path('test.ipynb') | |
50 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') |
|
50 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') | |
51 | self.assertEqual(path, fs_path) |
|
51 | self.assertEqual(path, fs_path) | |
52 |
|
52 | |||
53 | fm = FileContentsManager(root_dir=root) |
|
53 | fm = FileContentsManager(root_dir=root) | |
54 | path = fm._get_os_path('////test.ipynb') |
|
54 | path = fm._get_os_path('////test.ipynb') | |
55 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') |
|
55 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') | |
56 | self.assertEqual(path, fs_path) |
|
56 | self.assertEqual(path, fs_path) | |
57 |
|
57 | |||
58 | def test_checkpoint_subdir(self): |
|
58 | def test_checkpoint_subdir(self): | |
59 | subd = u'sub βir' |
|
59 | subd = u'sub βir' | |
60 | cp_name = 'test-cp.ipynb' |
|
60 | cp_name = 'test-cp.ipynb' | |
61 | with TemporaryDirectory() as td: |
|
61 | with TemporaryDirectory() as td: | |
62 | root = td |
|
62 | root = td | |
63 | os.mkdir(os.path.join(td, subd)) |
|
63 | os.mkdir(os.path.join(td, subd)) | |
64 | fm = FileContentsManager(root_dir=root) |
|
64 | fm = FileContentsManager(root_dir=root) | |
65 | cp_dir = fm.get_checkpoint_path('cp', 'test.ipynb') |
|
65 | cp_dir = fm.get_checkpoint_path('cp', 'test.ipynb') | |
66 | cp_subdir = fm.get_checkpoint_path('cp', '/%s/test.ipynb' % subd) |
|
66 | cp_subdir = fm.get_checkpoint_path('cp', '/%s/test.ipynb' % subd) | |
67 | self.assertNotEqual(cp_dir, cp_subdir) |
|
67 | self.assertNotEqual(cp_dir, cp_subdir) | |
68 | self.assertEqual(cp_dir, os.path.join(root, fm.checkpoint_dir, cp_name)) |
|
68 | self.assertEqual(cp_dir, os.path.join(root, fm.checkpoint_dir, cp_name)) | |
69 | self.assertEqual(cp_subdir, os.path.join(root, subd, fm.checkpoint_dir, cp_name)) |
|
69 | self.assertEqual(cp_subdir, os.path.join(root, subd, fm.checkpoint_dir, cp_name)) | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | class TestContentsManager(TestCase): |
|
72 | class TestContentsManager(TestCase): | |
73 |
|
73 | |||
74 | def setUp(self): |
|
74 | def setUp(self): | |
75 | self._temp_dir = TemporaryDirectory() |
|
75 | self._temp_dir = TemporaryDirectory() | |
76 | self.td = self._temp_dir.name |
|
76 | self.td = self._temp_dir.name | |
77 | self.contents_manager = FileContentsManager( |
|
77 | self.contents_manager = FileContentsManager( | |
78 | root_dir=self.td, |
|
78 | root_dir=self.td, | |
79 | ) |
|
79 | ) | |
80 |
|
80 | |||
81 | def tearDown(self): |
|
81 | def tearDown(self): | |
82 | self._temp_dir.cleanup() |
|
82 | self._temp_dir.cleanup() | |
83 |
|
83 | |||
84 | def make_dir(self, api_path): |
|
84 | def make_dir(self, api_path): | |
85 | """make subdirectory, rel_path is the relative path |
|
85 | """make subdirectory, rel_path is the relative path | |
86 | to that directory from the location where the server started""" |
|
86 | to that directory from the location where the server started""" | |
87 | os_path = self.contents_manager._get_os_path(api_path) |
|
87 | os_path = self.contents_manager._get_os_path(api_path) | |
88 | try: |
|
88 | try: | |
89 | os.makedirs(os_path) |
|
89 | os.makedirs(os_path) | |
90 | except OSError: |
|
90 | except OSError: | |
91 | print("Directory already exists: %r" % os_path) |
|
91 | print("Directory already exists: %r" % os_path) | |
92 |
|
92 | |||
93 | def symlink(self, src, dst): |
|
93 | def symlink(self, src, dst): | |
94 | """Make a symlink to src from dst |
|
94 | """Make a symlink to src from dst | |
95 |
|
95 | |||
96 | src and dst are api_paths |
|
96 | src and dst are api_paths | |
97 | """ |
|
97 | """ | |
98 | src_os_path = self.contents_manager._get_os_path(src) |
|
98 | src_os_path = self.contents_manager._get_os_path(src) | |
99 | dst_os_path = self.contents_manager._get_os_path(dst) |
|
99 | dst_os_path = self.contents_manager._get_os_path(dst) | |
100 | print(src_os_path, dst_os_path, os.path.isfile(src_os_path)) |
|
100 | print(src_os_path, dst_os_path, os.path.isfile(src_os_path)) | |
101 | os.symlink(src_os_path, dst_os_path) |
|
101 | os.symlink(src_os_path, dst_os_path) | |
102 |
|
102 | |||
103 |
|
103 | |||
104 | def add_code_cell(self, nb): |
|
104 | def add_code_cell(self, nb): | |
105 | output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"}) |
|
105 | output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"}) | |
106 | cell = nbformat.new_code_cell("print('hi')", outputs=[output]) |
|
106 | cell = nbformat.new_code_cell("print('hi')", outputs=[output]) | |
107 | nb.cells.append(cell) |
|
107 | nb.cells.append(cell) | |
108 |
|
108 | |||
109 | def new_notebook(self): |
|
109 | def new_notebook(self): | |
110 | cm = self.contents_manager |
|
110 | cm = self.contents_manager | |
111 | model = cm.new_untitled(type='notebook') |
|
111 | model = cm.new_untitled(type='notebook') | |
112 | name = model['name'] |
|
112 | name = model['name'] | |
113 | path = model['path'] |
|
113 | path = model['path'] | |
114 |
|
114 | |||
115 | full_model = cm.get(path) |
|
115 | full_model = cm.get(path) | |
116 | nb = full_model['content'] |
|
116 | nb = full_model['content'] | |
117 | self.add_code_cell(nb) |
|
117 | self.add_code_cell(nb) | |
118 |
|
118 | |||
119 | cm.save(full_model, path) |
|
119 | cm.save(full_model, path) | |
120 | return nb, name, path |
|
120 | return nb, name, path | |
121 |
|
121 | |||
122 | def test_new_untitled(self): |
|
122 | def test_new_untitled(self): | |
123 | cm = self.contents_manager |
|
123 | cm = self.contents_manager | |
124 | # Test in root directory |
|
124 | # Test in root directory | |
125 | model = cm.new_untitled(type='notebook') |
|
125 | model = cm.new_untitled(type='notebook') | |
126 | assert isinstance(model, dict) |
|
126 | assert isinstance(model, dict) | |
127 | self.assertIn('name', model) |
|
127 | self.assertIn('name', model) | |
128 | self.assertIn('path', model) |
|
128 | self.assertIn('path', model) | |
129 | self.assertIn('type', model) |
|
129 | self.assertIn('type', model) | |
130 | self.assertEqual(model['type'], 'notebook') |
|
130 | self.assertEqual(model['type'], 'notebook') | |
131 | self.assertEqual(model['name'], 'Untitled.ipynb') |
|
131 | self.assertEqual(model['name'], 'Untitled.ipynb') | |
132 | self.assertEqual(model['path'], 'Untitled.ipynb') |
|
132 | self.assertEqual(model['path'], 'Untitled.ipynb') | |
133 |
|
133 | |||
134 | # Test in sub-directory |
|
134 | # Test in sub-directory | |
135 | model = cm.new_untitled(type='directory') |
|
135 | model = cm.new_untitled(type='directory') | |
136 | assert isinstance(model, dict) |
|
136 | assert isinstance(model, dict) | |
137 | self.assertIn('name', model) |
|
137 | self.assertIn('name', model) | |
138 | self.assertIn('path', model) |
|
138 | self.assertIn('path', model) | |
139 | self.assertIn('type', model) |
|
139 | self.assertIn('type', model) | |
140 | self.assertEqual(model['type'], 'directory') |
|
140 | self.assertEqual(model['type'], 'directory') | |
141 | self.assertEqual(model['name'], 'Untitled Folder') |
|
141 | self.assertEqual(model['name'], 'Untitled Folder') | |
142 | self.assertEqual(model['path'], 'Untitled Folder') |
|
142 | self.assertEqual(model['path'], 'Untitled Folder') | |
143 | sub_dir = model['path'] |
|
143 | sub_dir = model['path'] | |
144 |
|
144 | |||
145 | model = cm.new_untitled(path=sub_dir) |
|
145 | model = cm.new_untitled(path=sub_dir) | |
146 | assert isinstance(model, dict) |
|
146 | assert isinstance(model, dict) | |
147 | self.assertIn('name', model) |
|
147 | self.assertIn('name', model) | |
148 | self.assertIn('path', model) |
|
148 | self.assertIn('path', model) | |
149 | self.assertIn('type', model) |
|
149 | self.assertIn('type', model) | |
150 | self.assertEqual(model['type'], 'file') |
|
150 | self.assertEqual(model['type'], 'file') | |
151 | self.assertEqual(model['name'], 'untitled') |
|
151 | self.assertEqual(model['name'], 'untitled') | |
152 | self.assertEqual(model['path'], '%s/untitled' % sub_dir) |
|
152 | self.assertEqual(model['path'], '%s/untitled' % sub_dir) | |
153 |
|
153 | |||
154 | def test_get(self): |
|
154 | def test_get(self): | |
155 | cm = self.contents_manager |
|
155 | cm = self.contents_manager | |
156 | # Create a notebook |
|
156 | # Create a notebook | |
157 | model = cm.new_untitled(type='notebook') |
|
157 | model = cm.new_untitled(type='notebook') | |
158 | name = model['name'] |
|
158 | name = model['name'] | |
159 | path = model['path'] |
|
159 | path = model['path'] | |
160 |
|
160 | |||
161 | # Check that we 'get' on the notebook we just created |
|
161 | # Check that we 'get' on the notebook we just created | |
162 | model2 = cm.get(path) |
|
162 | model2 = cm.get(path) | |
163 | assert isinstance(model2, dict) |
|
163 | assert isinstance(model2, dict) | |
164 | self.assertIn('name', model2) |
|
164 | self.assertIn('name', model2) | |
165 | self.assertIn('path', model2) |
|
165 | self.assertIn('path', model2) | |
166 | self.assertEqual(model['name'], name) |
|
166 | self.assertEqual(model['name'], name) | |
167 | self.assertEqual(model['path'], path) |
|
167 | self.assertEqual(model['path'], path) | |
168 |
|
168 | |||
169 | nb_as_file = cm.get(path, content=True, type='file') |
|
169 | nb_as_file = cm.get(path, content=True, type='file') | |
170 | self.assertEqual(nb_as_file['path'], path) |
|
170 | self.assertEqual(nb_as_file['path'], path) | |
171 | self.assertEqual(nb_as_file['type'], 'file') |
|
171 | self.assertEqual(nb_as_file['type'], 'file') | |
172 | self.assertEqual(nb_as_file['format'], 'text') |
|
172 | self.assertEqual(nb_as_file['format'], 'text') | |
173 | self.assertNotIsInstance(nb_as_file['content'], dict) |
|
173 | self.assertNotIsInstance(nb_as_file['content'], dict) | |
174 |
|
174 | |||
175 | nb_as_bin_file = cm.get(path, content=True, type='file', format='base64') |
|
175 | nb_as_bin_file = cm.get(path, content=True, type='file', format='base64') | |
176 | self.assertEqual(nb_as_bin_file['format'], 'base64') |
|
176 | self.assertEqual(nb_as_bin_file['format'], 'base64') | |
177 |
|
177 | |||
178 | # Test in sub-directory |
|
178 | # Test in sub-directory | |
179 | sub_dir = '/foo/' |
|
179 | sub_dir = '/foo/' | |
180 | self.make_dir('foo') |
|
180 | self.make_dir('foo') | |
181 | model = cm.new_untitled(path=sub_dir, ext='.ipynb') |
|
181 | model = cm.new_untitled(path=sub_dir, ext='.ipynb') | |
182 | model2 = cm.get(sub_dir + name) |
|
182 | model2 = cm.get(sub_dir + name) | |
183 | assert isinstance(model2, dict) |
|
183 | assert isinstance(model2, dict) | |
184 | self.assertIn('name', model2) |
|
184 | self.assertIn('name', model2) | |
185 | self.assertIn('path', model2) |
|
185 | self.assertIn('path', model2) | |
186 | self.assertIn('content', model2) |
|
186 | self.assertIn('content', model2) | |
187 | self.assertEqual(model2['name'], 'Untitled.ipynb') |
|
187 | self.assertEqual(model2['name'], 'Untitled.ipynb') | |
188 | self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name)) |
|
188 | self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name)) | |
189 |
|
189 | |||
190 | # Test getting directory model |
|
190 | # Test getting directory model | |
191 | dirmodel = cm.get('foo') |
|
191 | dirmodel = cm.get('foo') | |
192 | self.assertEqual(dirmodel['type'], 'directory') |
|
192 | self.assertEqual(dirmodel['type'], 'directory') | |
193 |
|
193 | |||
194 | with self.assertRaises(HTTPError): |
|
194 | with self.assertRaises(HTTPError): | |
195 | cm.get('foo', type='file') |
|
195 | cm.get('foo', type='file') | |
196 |
|
196 | |||
197 |
|
197 | |||
198 | @dec.skip_win32 |
|
198 | @dec.skip_win32 | |
199 | def test_bad_symlink(self): |
|
199 | def test_bad_symlink(self): | |
200 | cm = self.contents_manager |
|
200 | cm = self.contents_manager | |
201 | path = 'test bad symlink' |
|
201 | path = 'test bad symlink' | |
202 | self.make_dir(path) |
|
202 | self.make_dir(path) | |
203 |
|
203 | |||
204 | file_model = cm.new_untitled(path=path, ext='.txt') |
|
204 | file_model = cm.new_untitled(path=path, ext='.txt') | |
205 |
|
205 | |||
206 | # create a broken symlink |
|
206 | # create a broken symlink | |
207 | self.symlink("target", '%s/%s' % (path, 'bad symlink')) |
|
207 | self.symlink("target", '%s/%s' % (path, 'bad symlink')) | |
208 | model = cm.get(path) |
|
208 | model = cm.get(path) | |
209 | self.assertEqual(model['content'], [file_model]) |
|
209 | self.assertEqual(model['content'], [file_model]) | |
210 |
|
210 | |||
211 | @dec.skip_win32 |
|
211 | @dec.skip_win32 | |
212 | def test_good_symlink(self): |
|
212 | def test_good_symlink(self): | |
213 | cm = self.contents_manager |
|
213 | cm = self.contents_manager | |
214 | parent = 'test good symlink' |
|
214 | parent = 'test good symlink' | |
215 | name = 'good symlink' |
|
215 | name = 'good symlink' | |
216 | path = '{0}/{1}'.format(parent, name) |
|
216 | path = '{0}/{1}'.format(parent, name) | |
217 | self.make_dir(parent) |
|
217 | self.make_dir(parent) | |
218 |
|
218 | |||
219 | file_model = cm.new(path=parent + '/zfoo.txt') |
|
219 | file_model = cm.new(path=parent + '/zfoo.txt') | |
220 |
|
220 | |||
221 | # create a good symlink |
|
221 | # create a good symlink | |
222 | self.symlink(file_model['path'], path) |
|
222 | self.symlink(file_model['path'], path) | |
223 | symlink_model = cm.get(path, content=False) |
|
223 | symlink_model = cm.get(path, content=False) | |
224 | dir_model = cm.get(parent) |
|
224 | dir_model = cm.get(parent) | |
225 | self.assertEqual( |
|
225 | self.assertEqual( | |
226 | sorted(dir_model['content'], key=lambda x: x['name']), |
|
226 | sorted(dir_model['content'], key=lambda x: x['name']), | |
227 | [symlink_model, file_model], |
|
227 | [symlink_model, file_model], | |
228 | ) |
|
228 | ) | |
229 |
|
229 | |||
230 | def test_update(self): |
|
230 | def test_update(self): | |
231 | cm = self.contents_manager |
|
231 | cm = self.contents_manager | |
232 | # Create a notebook |
|
232 | # Create a notebook | |
233 | model = cm.new_untitled(type='notebook') |
|
233 | model = cm.new_untitled(type='notebook') | |
234 | name = model['name'] |
|
234 | name = model['name'] | |
235 | path = model['path'] |
|
235 | path = model['path'] | |
236 |
|
236 | |||
237 | # Change the name in the model for rename |
|
237 | # Change the name in the model for rename | |
238 | model['path'] = 'test.ipynb' |
|
238 | model['path'] = 'test.ipynb' | |
239 | model = cm.update(model, path) |
|
239 | model = cm.update(model, path) | |
240 | assert isinstance(model, dict) |
|
240 | assert isinstance(model, dict) | |
241 | self.assertIn('name', model) |
|
241 | self.assertIn('name', model) | |
242 | self.assertIn('path', model) |
|
242 | self.assertIn('path', model) | |
243 | self.assertEqual(model['name'], 'test.ipynb') |
|
243 | self.assertEqual(model['name'], 'test.ipynb') | |
244 |
|
244 | |||
245 | # Make sure the old name is gone |
|
245 | # Make sure the old name is gone | |
246 | self.assertRaises(HTTPError, cm.get, path) |
|
246 | self.assertRaises(HTTPError, cm.get, path) | |
247 |
|
247 | |||
248 | # Test in sub-directory |
|
248 | # Test in sub-directory | |
249 | # Create a directory and notebook in that directory |
|
249 | # Create a directory and notebook in that directory | |
250 | sub_dir = '/foo/' |
|
250 | sub_dir = '/foo/' | |
251 | self.make_dir('foo') |
|
251 | self.make_dir('foo') | |
252 | model = cm.new_untitled(path=sub_dir, type='notebook') |
|
252 | model = cm.new_untitled(path=sub_dir, type='notebook') | |
253 | path = model['path'] |
|
253 | path = model['path'] | |
254 |
|
254 | |||
255 | # Change the name in the model for rename |
|
255 | # Change the name in the model for rename | |
256 | d = path.rsplit('/', 1)[0] |
|
256 | d = path.rsplit('/', 1)[0] | |
257 | new_path = model['path'] = d + '/test_in_sub.ipynb' |
|
257 | new_path = model['path'] = d + '/test_in_sub.ipynb' | |
258 | model = cm.update(model, path) |
|
258 | model = cm.update(model, path) | |
259 | assert isinstance(model, dict) |
|
259 | assert isinstance(model, dict) | |
260 | self.assertIn('name', model) |
|
260 | self.assertIn('name', model) | |
261 | self.assertIn('path', model) |
|
261 | self.assertIn('path', model) | |
262 | self.assertEqual(model['name'], 'test_in_sub.ipynb') |
|
262 | self.assertEqual(model['name'], 'test_in_sub.ipynb') | |
263 | self.assertEqual(model['path'], new_path) |
|
263 | self.assertEqual(model['path'], new_path) | |
264 |
|
264 | |||
265 | # Make sure the old name is gone |
|
265 | # Make sure the old name is gone | |
266 | self.assertRaises(HTTPError, cm.get, path) |
|
266 | self.assertRaises(HTTPError, cm.get, path) | |
267 |
|
267 | |||
268 | def test_save(self): |
|
268 | def test_save(self): | |
269 | cm = self.contents_manager |
|
269 | cm = self.contents_manager | |
270 | # Create a notebook |
|
270 | # Create a notebook | |
271 | model = cm.new_untitled(type='notebook') |
|
271 | model = cm.new_untitled(type='notebook') | |
272 | name = model['name'] |
|
272 | name = model['name'] | |
273 | path = model['path'] |
|
273 | path = model['path'] | |
274 |
|
274 | |||
275 | # Get the model with 'content' |
|
275 | # Get the model with 'content' | |
276 | full_model = cm.get(path) |
|
276 | full_model = cm.get(path) | |
277 |
|
277 | |||
278 | # Save the notebook |
|
278 | # Save the notebook | |
279 | model = cm.save(full_model, path) |
|
279 | model = cm.save(full_model, path) | |
280 | assert isinstance(model, dict) |
|
280 | assert isinstance(model, dict) | |
281 | self.assertIn('name', model) |
|
281 | self.assertIn('name', model) | |
282 | self.assertIn('path', model) |
|
282 | self.assertIn('path', model) | |
283 | self.assertEqual(model['name'], name) |
|
283 | self.assertEqual(model['name'], name) | |
284 | self.assertEqual(model['path'], path) |
|
284 | self.assertEqual(model['path'], path) | |
285 |
|
285 | |||
286 | # Test in sub-directory |
|
286 | # Test in sub-directory | |
287 | # Create a directory and notebook in that directory |
|
287 | # Create a directory and notebook in that directory | |
288 | sub_dir = '/foo/' |
|
288 | sub_dir = '/foo/' | |
289 | self.make_dir('foo') |
|
289 | self.make_dir('foo') | |
290 | model = cm.new_untitled(path=sub_dir, type='notebook') |
|
290 | model = cm.new_untitled(path=sub_dir, type='notebook') | |
291 | name = model['name'] |
|
291 | name = model['name'] | |
292 | path = model['path'] |
|
292 | path = model['path'] | |
293 | model = cm.get(path) |
|
293 | model = cm.get(path) | |
294 |
|
294 | |||
295 | # Change the name in the model for rename |
|
295 | # Change the name in the model for rename | |
296 | model = cm.save(model, path) |
|
296 | model = cm.save(model, path) | |
297 | assert isinstance(model, dict) |
|
297 | assert isinstance(model, dict) | |
298 | self.assertIn('name', model) |
|
298 | self.assertIn('name', model) | |
299 | self.assertIn('path', model) |
|
299 | self.assertIn('path', model) | |
300 | self.assertEqual(model['name'], 'Untitled.ipynb') |
|
300 | self.assertEqual(model['name'], 'Untitled.ipynb') | |
301 | self.assertEqual(model['path'], 'foo/Untitled.ipynb') |
|
301 | self.assertEqual(model['path'], 'foo/Untitled.ipynb') | |
302 |
|
302 | |||
303 | def test_delete(self): |
|
303 | def test_delete(self): | |
304 | cm = self.contents_manager |
|
304 | cm = self.contents_manager | |
305 | # Create a notebook |
|
305 | # Create a notebook | |
306 | nb, name, path = self.new_notebook() |
|
306 | nb, name, path = self.new_notebook() | |
307 |
|
307 | |||
308 | # Delete the notebook |
|
308 | # Delete the notebook | |
309 | cm.delete(path) |
|
309 | cm.delete(path) | |
310 |
|
310 | |||
|
311 | # Check that deleting a non-existent path raises an error. | |||
|
312 | self.assertRaises(HTTPError, cm.delete, path) | |||
|
313 | ||||
311 | # Check that a 'get' on the deleted notebook raises and error |
|
314 | # Check that a 'get' on the deleted notebook raises and error | |
312 | self.assertRaises(HTTPError, cm.get, path) |
|
315 | self.assertRaises(HTTPError, cm.get, path) | |
313 |
|
316 | |||
314 | def test_copy(self): |
|
317 | def test_copy(self): | |
315 | cm = self.contents_manager |
|
318 | cm = self.contents_manager | |
316 | parent = u'Γ₯ b' |
|
319 | parent = u'Γ₯ b' | |
317 | name = u'nb β.ipynb' |
|
320 | name = u'nb β.ipynb' | |
318 | path = u'{0}/{1}'.format(parent, name) |
|
321 | path = u'{0}/{1}'.format(parent, name) | |
319 | self.make_dir(parent) |
|
322 | self.make_dir(parent) | |
320 | orig = cm.new(path=path) |
|
|||
321 |
|
323 | |||
|
324 | orig = cm.new(path=path) | |||
322 | # copy with unspecified name |
|
325 | # copy with unspecified name | |
323 | copy = cm.copy(path) |
|
326 | copy = cm.copy(path) | |
324 | self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy1.ipynb')) |
|
327 | self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy1.ipynb')) | |
325 |
|
328 | |||
326 | # copy with specified name |
|
329 | # copy with specified name | |
327 | copy2 = cm.copy(path, u'Γ₯ b/copy 2.ipynb') |
|
330 | copy2 = cm.copy(path, u'Γ₯ b/copy 2.ipynb') | |
328 | self.assertEqual(copy2['name'], u'copy 2.ipynb') |
|
331 | self.assertEqual(copy2['name'], u'copy 2.ipynb') | |
329 | self.assertEqual(copy2['path'], u'Γ₯ b/copy 2.ipynb') |
|
332 | self.assertEqual(copy2['path'], u'Γ₯ b/copy 2.ipynb') | |
330 | # copy with specified path |
|
333 | # copy with specified path | |
331 | copy2 = cm.copy(path, u'/') |
|
334 | copy2 = cm.copy(path, u'/') | |
332 | self.assertEqual(copy2['name'], name) |
|
335 | self.assertEqual(copy2['name'], name) | |
333 | self.assertEqual(copy2['path'], name) |
|
336 | self.assertEqual(copy2['path'], name) | |
334 |
|
337 | |||
335 | def test_trust_notebook(self): |
|
338 | def test_trust_notebook(self): | |
336 | cm = self.contents_manager |
|
339 | cm = self.contents_manager | |
337 | nb, name, path = self.new_notebook() |
|
340 | nb, name, path = self.new_notebook() | |
338 |
|
341 | |||
339 | untrusted = cm.get(path)['content'] |
|
342 | untrusted = cm.get(path)['content'] | |
340 | assert not cm.notary.check_cells(untrusted) |
|
343 | assert not cm.notary.check_cells(untrusted) | |
341 |
|
344 | |||
342 | # print(untrusted) |
|
345 | # print(untrusted) | |
343 | cm.trust_notebook(path) |
|
346 | cm.trust_notebook(path) | |
344 | trusted = cm.get(path)['content'] |
|
347 | trusted = cm.get(path)['content'] | |
345 | # print(trusted) |
|
348 | # print(trusted) | |
346 | assert cm.notary.check_cells(trusted) |
|
349 | assert cm.notary.check_cells(trusted) | |
347 |
|
350 | |||
348 | def test_mark_trusted_cells(self): |
|
351 | def test_mark_trusted_cells(self): | |
349 | cm = self.contents_manager |
|
352 | cm = self.contents_manager | |
350 | nb, name, path = self.new_notebook() |
|
353 | nb, name, path = self.new_notebook() | |
351 |
|
354 | |||
352 | cm.mark_trusted_cells(nb, path) |
|
355 | cm.mark_trusted_cells(nb, path) | |
353 | for cell in nb.cells: |
|
356 | for cell in nb.cells: | |
354 | if cell.cell_type == 'code': |
|
357 | if cell.cell_type == 'code': | |
355 | assert not cell.metadata.trusted |
|
358 | assert not cell.metadata.trusted | |
356 |
|
359 | |||
357 | cm.trust_notebook(path) |
|
360 | cm.trust_notebook(path) | |
358 | nb = cm.get(path)['content'] |
|
361 | nb = cm.get(path)['content'] | |
359 | for cell in nb.cells: |
|
362 | for cell in nb.cells: | |
360 | if cell.cell_type == 'code': |
|
363 | if cell.cell_type == 'code': | |
361 | assert cell.metadata.trusted |
|
364 | assert cell.metadata.trusted | |
362 |
|
365 | |||
363 | def test_check_and_sign(self): |
|
366 | def test_check_and_sign(self): | |
364 | cm = self.contents_manager |
|
367 | cm = self.contents_manager | |
365 | nb, name, path = self.new_notebook() |
|
368 | nb, name, path = self.new_notebook() | |
366 |
|
369 | |||
367 | cm.mark_trusted_cells(nb, path) |
|
370 | cm.mark_trusted_cells(nb, path) | |
368 | cm.check_and_sign(nb, path) |
|
371 | cm.check_and_sign(nb, path) | |
369 | assert not cm.notary.check_signature(nb) |
|
372 | assert not cm.notary.check_signature(nb) | |
370 |
|
373 | |||
371 | cm.trust_notebook(path) |
|
374 | cm.trust_notebook(path) | |
372 | nb = cm.get(path)['content'] |
|
375 | nb = cm.get(path)['content'] | |
373 | cm.mark_trusted_cells(nb, path) |
|
376 | cm.mark_trusted_cells(nb, path) | |
374 | cm.check_and_sign(nb, path) |
|
377 | cm.check_and_sign(nb, path) | |
375 | assert cm.notary.check_signature(nb) |
|
378 | assert cm.notary.check_signature(nb) |
General Comments 0
You need to be logged in to leave comments.
Login now