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