test_manager.py
530 lines
| 18.3 KiB
| text/x-python
|
PythonLexer
MinRK
|
r13136 | # coding: utf-8 | ||
MinRK
|
r7637 | """Tests for the notebook manager.""" | ||
Thomas Kluyver
|
r13348 | from __future__ import print_function | ||
MinRK
|
r7637 | |||
import os | ||||
Min RK
|
r20575 | import sys | ||
Min RK
|
r19628 | import time | ||
Min RK
|
r20658 | from contextlib import contextmanager | ||
Zachary Sailer
|
r13046 | |||
Min RK
|
r20490 | from nose import SkipTest | ||
Zachary Sailer
|
r13046 | from tornado.web import HTTPError | ||
MinRK
|
r7637 | from unittest import TestCase | ||
from tempfile import NamedTemporaryFile | ||||
MinRK
|
r18607 | from IPython.nbformat import v4 as nbformat | ||
MinRK
|
r15655 | |||
MinRK
|
r7637 | from IPython.utils.tempdir import TemporaryDirectory | ||
from IPython.utils.traitlets import TraitError | ||||
MinRK
|
r17710 | from IPython.testing import decorators as dec | ||
MinRK
|
r7637 | |||
MinRK
|
r17524 | from ..filemanager import FileContentsManager | ||
MinRK
|
r7637 | |||
Brian E. Granger
|
r15079 | |||
Scott Sanderson
|
r19612 | def _make_dir(contents_manager, api_path): | ||
""" | ||||
Make a directory. | ||||
""" | ||||
os_path = contents_manager._get_os_path(api_path) | ||||
try: | ||||
os.makedirs(os_path) | ||||
except OSError: | ||||
print("Directory already exists: %r" % os_path) | ||||
MinRK
|
r17524 | class TestFileContentsManager(TestCase): | ||
MinRK
|
r7637 | |||
Scott Sanderson
|
r20743 | @contextmanager | ||
def assertRaisesHTTPError(self, status, msg=None): | ||||
msg = msg or "Should have raised HTTPError(%i)" % status | ||||
try: | ||||
yield | ||||
except HTTPError as e: | ||||
self.assertEqual(e.status_code, status) | ||||
else: | ||||
self.fail(msg) | ||||
Scott Sanderson
|
r19612 | def symlink(self, contents_manager, src, dst): | ||
Min RK
|
r19611 | """Make a symlink to src from dst | ||
src and dst are api_paths | ||||
""" | ||||
Scott Sanderson
|
r19612 | src_os_path = contents_manager._get_os_path(src) | ||
dst_os_path = contents_manager._get_os_path(dst) | ||||
Min RK
|
r19611 | print(src_os_path, dst_os_path, os.path.isfile(src_os_path)) | ||
os.symlink(src_os_path, dst_os_path) | ||||
MinRK
|
r17524 | def test_root_dir(self): | ||
MinRK
|
r7637 | with TemporaryDirectory() as td: | ||
MinRK
|
r17524 | fm = FileContentsManager(root_dir=td) | ||
self.assertEqual(fm.root_dir, td) | ||||
MinRK
|
r7637 | |||
MinRK
|
r17524 | def test_missing_root_dir(self): | ||
MinRK
|
r7637 | with TemporaryDirectory() as td: | ||
MinRK
|
r17524 | root = os.path.join(td, 'notebook', 'dir', 'is', 'missing') | ||
self.assertRaises(TraitError, FileContentsManager, root_dir=root) | ||||
MinRK
|
r7637 | |||
MinRK
|
r17524 | def test_invalid_root_dir(self): | ||
MinRK
|
r7637 | with NamedTemporaryFile() as tf: | ||
MinRK
|
r17524 | self.assertRaises(TraitError, FileContentsManager, root_dir=tf.name) | ||
MinRK
|
r7637 | |||
Zachary Sailer
|
r13032 | def test_get_os_path(self): | ||
# full filesystem path should be returned with correct operating system | ||||
# separators. | ||||
with TemporaryDirectory() as td: | ||||
MinRK
|
r17524 | root = td | ||
fm = FileContentsManager(root_dir=root) | ||||
MinRK
|
r18749 | path = fm._get_os_path('/path/to/notebook/test.ipynb') | ||
Paul Ivanov
|
r13034 | rel_path_list = '/path/to/notebook/test.ipynb'.split('/') | ||
MinRK
|
r17524 | fs_path = os.path.join(fm.root_dir, *rel_path_list) | ||
Paul Ivanov
|
r13034 | self.assertEqual(path, fs_path) | ||
MinRK
|
r17524 | fm = FileContentsManager(root_dir=root) | ||
MinRK
|
r15420 | path = fm._get_os_path('test.ipynb') | ||
MinRK
|
r17524 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') | ||
Paul Ivanov
|
r13034 | self.assertEqual(path, fs_path) | ||
MinRK
|
r17524 | fm = FileContentsManager(root_dir=root) | ||
MinRK
|
r18749 | path = fm._get_os_path('////test.ipynb') | ||
MinRK
|
r17524 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') | ||
Paul Ivanov
|
r13034 | self.assertEqual(path, fs_path) | ||
MinRK
|
r17523 | |||
MinRK
|
r16440 | def test_checkpoint_subdir(self): | ||
subd = u'sub ∂ir' | ||||
cp_name = 'test-cp.ipynb' | ||||
with TemporaryDirectory() as td: | ||||
MinRK
|
r17524 | root = td | ||
MinRK
|
r16440 | os.mkdir(os.path.join(td, subd)) | ||
MinRK
|
r17524 | fm = FileContentsManager(root_dir=root) | ||
Scott Sanderson
|
r19839 | cpm = fm.checkpoints | ||
Scott Sanderson
|
r19786 | cp_dir = cpm.checkpoint_path( | ||
Scott Sanderson
|
r19727 | 'cp', 'test.ipynb' | ||
) | ||||
Scott Sanderson
|
r19786 | cp_subdir = cpm.checkpoint_path( | ||
Scott Sanderson
|
r19727 | 'cp', '/%s/test.ipynb' % subd | ||
) | ||||
MinRK
|
r16440 | self.assertNotEqual(cp_dir, cp_subdir) | ||
Scott Sanderson
|
r19727 | self.assertEqual(cp_dir, os.path.join(root, cpm.checkpoint_dir, cp_name)) | ||
self.assertEqual(cp_subdir, os.path.join(root, subd, cpm.checkpoint_dir, cp_name)) | ||||
Min RK
|
r19611 | |||
@dec.skip_win32 | ||||
def test_bad_symlink(self): | ||||
Scott Sanderson
|
r19612 | with TemporaryDirectory() as td: | ||
cm = FileContentsManager(root_dir=td) | ||||
path = 'test bad symlink' | ||||
_make_dir(cm, path) | ||||
file_model = cm.new_untitled(path=path, ext='.txt') | ||||
# create a broken symlink | ||||
self.symlink(cm, "target", '%s/%s' % (path, 'bad symlink')) | ||||
model = cm.get(path) | ||||
self.assertEqual(model['content'], [file_model]) | ||||
Min RK
|
r19611 | |||
@dec.skip_win32 | ||||
def test_good_symlink(self): | ||||
Scott Sanderson
|
r19612 | with TemporaryDirectory() as td: | ||
cm = FileContentsManager(root_dir=td) | ||||
parent = 'test good symlink' | ||||
name = 'good symlink' | ||||
path = '{0}/{1}'.format(parent, name) | ||||
_make_dir(cm, parent) | ||||
file_model = cm.new(path=parent + '/zfoo.txt') | ||||
# create a good symlink | ||||
self.symlink(cm, file_model['path'], path) | ||||
symlink_model = cm.get(path, content=False) | ||||
dir_model = cm.get(parent) | ||||
self.assertEqual( | ||||
sorted(dir_model['content'], key=lambda x: x['name']), | ||||
[symlink_model, file_model], | ||||
) | ||||
Min RK
|
r20490 | |||
def test_403(self): | ||||
if hasattr(os, 'getuid'): | ||||
if os.getuid() == 0: | ||||
raise SkipTest("Can't test permissions as root") | ||||
Min RK
|
r20575 | if sys.platform.startswith('win'): | ||
raise SkipTest("Can't test permissions on Windows") | ||||
Min RK
|
r20490 | |||
with TemporaryDirectory() as td: | ||||
cm = FileContentsManager(root_dir=td) | ||||
model = cm.new_untitled(type='file') | ||||
os_path = cm._get_os_path(model['path']) | ||||
os.chmod(os_path, 0o400) | ||||
try: | ||||
with cm.open(os_path, 'w') as f: | ||||
f.write(u"don't care") | ||||
except HTTPError as e: | ||||
self.assertEqual(e.status_code, 403) | ||||
else: | ||||
self.fail("Should have raised HTTPError(403)") | ||||
MinRK
|
r17523 | |||
Scott Sanderson
|
r20743 | def test_escape_root(self): | ||
with TemporaryDirectory() as td: | ||||
cm = FileContentsManager(root_dir=td) | ||||
# make foo, bar next to root | ||||
with open(os.path.join(cm.root_dir, '..', 'foo'), 'w') as f: | ||||
f.write('foo') | ||||
with open(os.path.join(cm.root_dir, '..', 'bar'), 'w') as f: | ||||
f.write('bar') | ||||
with self.assertRaisesHTTPError(404): | ||||
cm.get('..') | ||||
with self.assertRaisesHTTPError(404): | ||||
cm.get('foo/../../../bar') | ||||
with self.assertRaisesHTTPError(404): | ||||
cm.delete('../foo') | ||||
with self.assertRaisesHTTPError(404): | ||||
cm.rename('../foo', '../bar') | ||||
with self.assertRaisesHTTPError(404): | ||||
cm.save(model={ | ||||
'type': 'file', | ||||
'content': u'', | ||||
'format': 'text', | ||||
}, path='../foo') | ||||
Zachary Sailer
|
r13032 | |||
MinRK
|
r17525 | class TestContentsManager(TestCase): | ||
Min RK
|
r19604 | |||
MinRK
|
r15655 | def setUp(self): | ||
self._temp_dir = TemporaryDirectory() | ||||
self.td = self._temp_dir.name | ||||
MinRK
|
r17524 | self.contents_manager = FileContentsManager( | ||
root_dir=self.td, | ||||
MinRK
|
r15656 | ) | ||
MinRK
|
r17523 | |||
MinRK
|
r15655 | def tearDown(self): | ||
self._temp_dir.cleanup() | ||||
Min RK
|
r20658 | |||
Min RK
|
r19604 | def make_dir(self, api_path): | ||
Min RK
|
r19674 | """make a subdirectory at api_path | ||
override in subclasses if contents are not on the filesystem. | ||||
""" | ||||
Scott Sanderson
|
r19612 | _make_dir(self.contents_manager, api_path) | ||
MinRK
|
r17523 | |||
MinRK
|
r15655 | def add_code_cell(self, nb): | ||
MinRK
|
r18607 | output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"}) | ||
cell = nbformat.new_code_cell("print('hi')", outputs=[output]) | ||||
MinRK
|
r18584 | nb.cells.append(cell) | ||
MinRK
|
r17523 | |||
MinRK
|
r15655 | def new_notebook(self): | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
Min RK
|
r18759 | model = cm.new_untitled(type='notebook') | ||
MinRK
|
r15655 | name = model['name'] | ||
path = model['path'] | ||||
MinRK
|
r17523 | |||
Thomas Kluyver
|
r18791 | full_model = cm.get(path) | ||
MinRK
|
r15655 | nb = full_model['content'] | ||
Min RK
|
r19628 | nb['metadata']['counter'] = int(1e6 * time.time()) | ||
MinRK
|
r15655 | self.add_code_cell(nb) | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | cm.save(full_model, path) | ||
MinRK
|
r15655 | return nb, name, path | ||
MinRK
|
r17523 | |||
Min RK
|
r18759 | def test_new_untitled(self): | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
MinRK
|
r15656 | # Test in root directory | ||
Min RK
|
r18759 | model = cm.new_untitled(type='notebook') | ||
MinRK
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
Min RK
|
r18759 | self.assertIn('type', model) | ||
self.assertEqual(model['type'], 'notebook') | ||||
Min RK
|
r18813 | self.assertEqual(model['name'], 'Untitled.ipynb') | ||
self.assertEqual(model['path'], 'Untitled.ipynb') | ||||
MinRK
|
r15656 | |||
# Test in sub-directory | ||||
Min RK
|
r18759 | model = cm.new_untitled(type='directory') | ||
MinRK
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
Min RK
|
r18759 | self.assertIn('type', model) | ||
self.assertEqual(model['type'], 'directory') | ||||
Min RK
|
r18813 | self.assertEqual(model['name'], 'Untitled Folder') | ||
self.assertEqual(model['path'], 'Untitled Folder') | ||||
Min RK
|
r18759 | sub_dir = model['path'] | ||
model = cm.new_untitled(path=sub_dir) | ||||
assert isinstance(model, dict) | ||||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertIn('type', model) | ||||
self.assertEqual(model['type'], 'file') | ||||
Min RK
|
r18813 | self.assertEqual(model['name'], 'untitled') | ||
self.assertEqual(model['path'], '%s/untitled' % sub_dir) | ||||
Zachary Sailer
|
r13046 | |||
Scott Sanderson
|
r20834 | def test_modified_date(self): | ||
cm = self.contents_manager | ||||
# Create a new notebook. | ||||
nb, name, path = self.new_notebook() | ||||
model = cm.get(path) | ||||
# Add a cell and save. | ||||
self.add_code_cell(model['content']) | ||||
cm.save(model, path) | ||||
# Reload notebook and verify that last_modified incremented. | ||||
saved = cm.get(path) | ||||
Min RK
|
r20918 | self.assertGreaterEqual(saved['last_modified'], model['last_modified']) | ||
Scott Sanderson
|
r20834 | |||
# Move the notebook and verify that last_modified stayed the same. | ||||
# (The frontend fires a warning if last_modified increases on the | ||||
# renamed file.) | ||||
new_path = 'renamed.ipynb' | ||||
cm.rename(path, new_path) | ||||
renamed = cm.get(new_path) | ||||
Scott Sanderson
|
r20841 | self.assertGreaterEqual( | ||
renamed['last_modified'], | ||||
saved['last_modified'], | ||||
) | ||||
Scott Sanderson
|
r20834 | |||
MinRK
|
r17524 | def test_get(self): | ||
cm = self.contents_manager | ||||
MinRK
|
r15656 | # Create a notebook | ||
Min RK
|
r18759 | model = cm.new_untitled(type='notebook') | ||
MinRK
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
# Check that we 'get' on the notebook we just created | ||||
Thomas Kluyver
|
r18791 | model2 = cm.get(path) | ||
MinRK
|
r15656 | assert isinstance(model2, dict) | ||
self.assertIn('name', model2) | ||||
self.assertIn('path', model2) | ||||
self.assertEqual(model['name'], name) | ||||
self.assertEqual(model['path'], path) | ||||
Min RK
|
r19391 | nb_as_file = cm.get(path, content=True, type='file') | ||
Thomas Kluyver
|
r18781 | self.assertEqual(nb_as_file['path'], path) | ||
self.assertEqual(nb_as_file['type'], 'file') | ||||
self.assertEqual(nb_as_file['format'], 'text') | ||||
self.assertNotIsInstance(nb_as_file['content'], dict) | ||||
Min RK
|
r19391 | nb_as_bin_file = cm.get(path, content=True, type='file', format='base64') | ||
Thomas Kluyver
|
r18788 | self.assertEqual(nb_as_bin_file['format'], 'base64') | ||
MinRK
|
r15656 | # Test in sub-directory | ||
sub_dir = '/foo/' | ||||
Min RK
|
r19604 | self.make_dir('foo') | ||
Min RK
|
r18759 | model = cm.new_untitled(path=sub_dir, ext='.ipynb') | ||
Thomas Kluyver
|
r18791 | model2 = cm.get(sub_dir + name) | ||
MinRK
|
r15656 | assert isinstance(model2, dict) | ||
self.assertIn('name', model2) | ||||
self.assertIn('path', model2) | ||||
self.assertIn('content', model2) | ||||
Min RK
|
r18813 | self.assertEqual(model2['name'], 'Untitled.ipynb') | ||
MinRK
|
r18749 | self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name)) | ||
Thomas Kluyver
|
r18781 | |||
Scott Sanderson
|
r19618 | # Test with a regular file. | ||
file_model_path = cm.new_untitled(path=sub_dir, ext='.txt')['path'] | ||||
file_model = cm.get(file_model_path) | ||||
self.assertDictContainsSubset( | ||||
{ | ||||
'content': u'', | ||||
Scott Sanderson
|
r19619 | 'format': u'text', | ||
Scott Sanderson
|
r19618 | 'mimetype': u'text/plain', | ||
'name': u'untitled.txt', | ||||
'path': u'foo/untitled.txt', | ||||
'type': u'file', | ||||
'writable': True, | ||||
}, | ||||
file_model, | ||||
) | ||||
self.assertIn('created', file_model) | ||||
self.assertIn('last_modified', file_model) | ||||
Thomas Kluyver
|
r18781 | # Test getting directory model | ||
Scott Sanderson
|
r19609 | |||
# Create a sub-sub directory to test getting directory contents with a | ||||
# subdir. | ||||
Scott Sanderson
|
r19610 | self.make_dir('foo/bar') | ||
Thomas Kluyver
|
r18791 | dirmodel = cm.get('foo') | ||
Thomas Kluyver
|
r18781 | self.assertEqual(dirmodel['type'], 'directory') | ||
Scott Sanderson
|
r19609 | self.assertIsInstance(dirmodel['content'], list) | ||
Scott Sanderson
|
r19618 | self.assertEqual(len(dirmodel['content']), 3) | ||
Scott Sanderson
|
r19610 | self.assertEqual(dirmodel['path'], 'foo') | ||
self.assertEqual(dirmodel['name'], 'foo') | ||||
Scott Sanderson
|
r19609 | |||
# Directory contents should match the contents of each individual entry | ||||
# when requested with content=False. | ||||
model2_no_content = cm.get(sub_dir + name, content=False) | ||||
Scott Sanderson
|
r19618 | file_model_no_content = cm.get(u'foo/untitled.txt', content=False) | ||
Scott Sanderson
|
r19610 | sub_sub_dir_no_content = cm.get('foo/bar', content=False) | ||
self.assertEqual(sub_sub_dir_no_content['path'], 'foo/bar') | ||||
self.assertEqual(sub_sub_dir_no_content['name'], 'bar') | ||||
Scott Sanderson
|
r19609 | for entry in dirmodel['content']: | ||
# Order isn't guaranteed by the spec, so this is a hacky way of | ||||
# verifying that all entries are matched. | ||||
if entry['path'] == sub_sub_dir_no_content['path']: | ||||
self.assertEqual(entry, sub_sub_dir_no_content) | ||||
elif entry['path'] == model2_no_content['path']: | ||||
self.assertEqual(entry, model2_no_content) | ||||
Scott Sanderson
|
r19618 | elif entry['path'] == file_model_no_content['path']: | ||
self.assertEqual(entry, file_model_no_content) | ||||
Scott Sanderson
|
r19609 | else: | ||
self.fail("Unexpected directory entry: %s" % entry()) | ||||
Thomas Kluyver
|
r18781 | |||
with self.assertRaises(HTTPError): | ||||
Min RK
|
r19391 | cm.get('foo', type='file') | ||
Thomas Kluyver
|
r18781 | |||
MinRK
|
r17524 | def test_update(self): | ||
cm = self.contents_manager | ||||
MinRK
|
r15656 | # Create a notebook | ||
Min RK
|
r18759 | model = cm.new_untitled(type='notebook') | ||
MinRK
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
# Change the name in the model for rename | ||||
MinRK
|
r18749 | model['path'] = 'test.ipynb' | ||
model = cm.update(model, path) | ||||
MinRK
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], 'test.ipynb') | ||||
# Make sure the old name is gone | ||||
Thomas Kluyver
|
r18791 | self.assertRaises(HTTPError, cm.get, path) | ||
MinRK
|
r15656 | |||
# Test in sub-directory | ||||
# Create a directory and notebook in that directory | ||||
sub_dir = '/foo/' | ||||
Min RK
|
r19604 | self.make_dir('foo') | ||
Min RK
|
r18759 | model = cm.new_untitled(path=sub_dir, type='notebook') | ||
MinRK
|
r15656 | path = model['path'] | ||
MinRK
|
r17523 | |||
MinRK
|
r15656 | # Change the name in the model for rename | ||
MinRK
|
r18749 | d = path.rsplit('/', 1)[0] | ||
new_path = model['path'] = d + '/test_in_sub.ipynb' | ||||
model = cm.update(model, path) | ||||
MinRK
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], 'test_in_sub.ipynb') | ||||
MinRK
|
r18749 | self.assertEqual(model['path'], new_path) | ||
MinRK
|
r17523 | |||
MinRK
|
r15656 | # Make sure the old name is gone | ||
Thomas Kluyver
|
r18791 | self.assertRaises(HTTPError, cm.get, path) | ||
Zachary Sailer
|
r13046 | |||
MinRK
|
r17524 | def test_save(self): | ||
cm = self.contents_manager | ||||
MinRK
|
r15656 | # Create a notebook | ||
Min RK
|
r18759 | model = cm.new_untitled(type='notebook') | ||
MinRK
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
# Get the model with 'content' | ||||
Thomas Kluyver
|
r18791 | full_model = cm.get(path) | ||
MinRK
|
r15656 | |||
# Save the notebook | ||||
MinRK
|
r18749 | model = cm.save(full_model, path) | ||
MinRK
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], name) | ||||
self.assertEqual(model['path'], path) | ||||
# Test in sub-directory | ||||
# Create a directory and notebook in that directory | ||||
sub_dir = '/foo/' | ||||
Min RK
|
r19604 | self.make_dir('foo') | ||
Min RK
|
r18759 | model = cm.new_untitled(path=sub_dir, type='notebook') | ||
MinRK
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
Thomas Kluyver
|
r18791 | model = cm.get(path) | ||
MinRK
|
r15656 | |||
# Change the name in the model for rename | ||||
MinRK
|
r18749 | model = cm.save(model, path) | ||
MinRK
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
Min RK
|
r18813 | self.assertEqual(model['name'], 'Untitled.ipynb') | ||
self.assertEqual(model['path'], 'foo/Untitled.ipynb') | ||||
Zachary Sailer
|
r13046 | |||
MinRK
|
r17524 | def test_delete(self): | ||
cm = self.contents_manager | ||||
MinRK
|
r15656 | # Create a notebook | ||
nb, name, path = self.new_notebook() | ||||
MinRK
|
r17523 | |||
MinRK
|
r15656 | # Delete the notebook | ||
MinRK
|
r18749 | cm.delete(path) | ||
MinRK
|
r17523 | |||
Scott Sanderson
|
r19608 | # Check that deleting a non-existent path raises an error. | ||
self.assertRaises(HTTPError, cm.delete, path) | ||||
MinRK
|
r15656 | # Check that a 'get' on the deleted notebook raises and error | ||
Thomas Kluyver
|
r18791 | self.assertRaises(HTTPError, cm.get, path) | ||
MinRK
|
r17523 | |||
Scott Sanderson
|
r20921 | def test_delete_root(self): | ||
cm = self.contents_manager | ||||
with self.assertRaises(HTTPError) as err: | ||||
cm.delete('') | ||||
self.assertEqual(err.exception.status_code, 400) | ||||
MinRK
|
r17524 | def test_copy(self): | ||
cm = self.contents_manager | ||||
MinRK
|
r18749 | parent = u'Ã¥ b' | ||
MinRK
|
r15656 | name = u'nb √.ipynb' | ||
MinRK
|
r18749 | path = u'{0}/{1}'.format(parent, name) | ||
Min RK
|
r19604 | self.make_dir(parent) | ||
MinRK
|
r17523 | |||
Scott Sanderson
|
r19608 | orig = cm.new(path=path) | ||
MinRK
|
r15656 | # copy with unspecified name | ||
MinRK
|
r18749 | copy = cm.copy(path) | ||
Min RK
|
r18813 | self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy1.ipynb')) | ||
MinRK
|
r17523 | |||
MinRK
|
r15656 | # copy with specified name | ||
MinRK
|
r18749 | copy2 = cm.copy(path, u'Ã¥ b/copy 2.ipynb') | ||
MinRK
|
r15656 | self.assertEqual(copy2['name'], u'copy 2.ipynb') | ||
MinRK
|
r18749 | self.assertEqual(copy2['path'], u'Ã¥ b/copy 2.ipynb') | ||
Min RK
|
r18813 | # copy with specified path | ||
copy2 = cm.copy(path, u'/') | ||||
self.assertEqual(copy2['name'], name) | ||||
self.assertEqual(copy2['path'], name) | ||||
MinRK
|
r17523 | |||
MinRK
|
r15655 | def test_trust_notebook(self): | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
MinRK
|
r15655 | nb, name, path = self.new_notebook() | ||
MinRK
|
r17523 | |||
Thomas Kluyver
|
r18791 | untrusted = cm.get(path)['content'] | ||
MinRK
|
r17524 | assert not cm.notary.check_cells(untrusted) | ||
MinRK
|
r17523 | |||
MinRK
|
r15656 | # print(untrusted) | ||
MinRK
|
r18749 | cm.trust_notebook(path) | ||
Thomas Kluyver
|
r18791 | trusted = cm.get(path)['content'] | ||
MinRK
|
r15656 | # print(trusted) | ||
MinRK
|
r17524 | assert cm.notary.check_cells(trusted) | ||
MinRK
|
r17523 | |||
MinRK
|
r15655 | def test_mark_trusted_cells(self): | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
MinRK
|
r15655 | nb, name, path = self.new_notebook() | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | cm.mark_trusted_cells(nb, path) | ||
MinRK
|
r18584 | for cell in nb.cells: | ||
MinRK
|
r15655 | if cell.cell_type == 'code': | ||
MinRK
|
r18255 | assert not cell.metadata.trusted | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | cm.trust_notebook(path) | ||
Thomas Kluyver
|
r18791 | nb = cm.get(path)['content'] | ||
MinRK
|
r18584 | for cell in nb.cells: | ||
MinRK
|
r15655 | if cell.cell_type == 'code': | ||
MinRK
|
r18255 | assert cell.metadata.trusted | ||
MinRK
|
r15655 | |||
def test_check_and_sign(self): | ||||
MinRK
|
r17524 | cm = self.contents_manager | ||
MinRK
|
r15655 | nb, name, path = self.new_notebook() | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | cm.mark_trusted_cells(nb, path) | ||
cm.check_and_sign(nb, path) | ||||
MinRK
|
r17524 | assert not cm.notary.check_signature(nb) | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | cm.trust_notebook(path) | ||
Thomas Kluyver
|
r18791 | nb = cm.get(path)['content'] | ||
MinRK
|
r18749 | cm.mark_trusted_cells(nb, path) | ||
cm.check_and_sign(nb, path) | ||||
MinRK
|
r17524 | assert cm.notary.check_signature(nb) | ||