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