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