Exemple #1
0
def test_get_os_path(tmp_path):
    fm = FileContentsManager(root_dir=str(tmp_path))
    path = fm._get_os_path("/path/to/notebook/test.ipynb")
    rel_path_list = "/path/to/notebook/test.ipynb".split("/")
    fs_path = os.path.join(fm.root_dir, *rel_path_list)
    assert path == fs_path

    fm = FileContentsManager(root_dir=str(tmp_path))
    path = fm._get_os_path("test.ipynb")
    fs_path = os.path.join(fm.root_dir, "test.ipynb")
    assert path == fs_path

    fm = FileContentsManager(root_dir=str(tmp_path))
    path = fm._get_os_path("////test.ipynb")
    fs_path = os.path.join(fm.root_dir, "test.ipynb")
    assert path == fs_path
Exemple #2
0
def test_escape_root(tmp_path):
    td = str(tmp_path)
    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 pytest.raises(HTTPError) as e:
        cm.get('..')
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.get('foo/../../../bar')
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.delete('../foo')
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.rename('../foo', '../bar')
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.save(model={
            'type': 'file',
            'content': u'',
            'format': 'text',
        },
                path='../foo')
    expected_http_error(e, 404)
Exemple #3
0
def test_escape_root(tmp_path):
    td = str(tmp_path)
    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 pytest.raises(HTTPError) as e:
        cm.get("..")
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.get("foo/../../../bar")
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.delete("../foo")
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.rename("../foo", "../bar")
    expected_http_error(e, 404)

    with pytest.raises(HTTPError) as e:
        cm.save(model={
            "type": "file",
            "content": u"",
            "format": "text",
        },
                path="../foo")
    expected_http_error(e, 404)
Exemple #4
0
def test_get_os_path(tmp_path):
    fm = FileContentsManager(root_dir=str(tmp_path))
    path = fm._get_os_path('/path/to/notebook/test.ipynb')
    rel_path_list = '/path/to/notebook/test.ipynb'.split('/')
    fs_path = os.path.join(fm.root_dir, *rel_path_list)
    assert path == fs_path

    fm = FileContentsManager(root_dir=str(tmp_path))
    path = fm._get_os_path('test.ipynb')
    fs_path = os.path.join(fm.root_dir, 'test.ipynb')
    assert path == fs_path

    fm = FileContentsManager(root_dir=str(tmp_path))
    path = fm._get_os_path('////test.ipynb')
    fs_path = os.path.join(fm.root_dir, 'test.ipynb')
    assert path == fs_path
Exemple #5
0
def test_checkpoint_subdir(tmp_path):
    subd = 'sub ∂ir'
    cp_name = 'test-cp.ipynb'
    fm = FileContentsManager(root_dir=str(tmp_path))
    tmp_path.joinpath(subd).mkdir()
    cpm = fm.checkpoints
    cp_dir = cpm.checkpoint_path('cp', 'test.ipynb')
    cp_subdir = cpm.checkpoint_path('cp', '/%s/test.ipynb' % subd)
    assert cp_dir != cp_subdir
    assert cp_dir == os.path.join(str(tmp_path), cpm.checkpoint_dir, cp_name)
Exemple #6
0
def test_checkpoint_subdir(tmp_path):
    subd = "sub ∂ir"
    cp_name = "test-cp.ipynb"
    fm = FileContentsManager(root_dir=str(tmp_path))
    tmp_path.joinpath(subd).mkdir()
    cpm = fm.checkpoints
    cp_dir = cpm.checkpoint_path("cp", "test.ipynb")
    cp_subdir = cpm.checkpoint_path("cp", "/%s/test.ipynb" % subd)
    assert cp_dir != cp_subdir
    assert cp_dir == os.path.join(str(tmp_path), cpm.checkpoint_dir, cp_name)
Exemple #7
0
def test_good_symlink(tmp_path):
    td = str(tmp_path)
    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
    symlink(cm, file_model['path'], path)
    symlink_model = cm.get(path, content=False)
    dir_model = cm.get(parent)
    assert sorted(dir_model['content'],
                  key=lambda x: x['name']) == [symlink_model, file_model]
Exemple #8
0
def test_bad_symlink(tmp_path):
    td = str(tmp_path)

    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
    symlink(cm, "target", '%s/%s' % (path, 'bad symlink'))
    model = cm.get(path)

    contents = {content['name']: content for content in model['content']}
    assert 'untitled.txt' in contents
    assert contents['untitled.txt'] == file_model
    assert 'bad symlink' in contents
Exemple #9
0
def test_bad_symlink(tmp_path):
    td = str(tmp_path)

    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
    symlink(cm, "target", "%s/%s" % (path, "bad symlink"))
    model = cm.get(path)

    contents = {content["name"]: content for content in model["content"]}
    assert "untitled.txt" in contents
    assert contents["untitled.txt"] == file_model
    assert "bad symlink" in contents
Exemple #10
0
def test_403(tmp_path):
    if hasattr(os, 'getuid'):
        if os.getuid() == 0:
            raise pytest.skip("Can't test permissions as root")
    if sys.platform.startswith('win'):
        raise pytest.skip("Can't test permissions on Windows")

    td = str(tmp_path)
    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:
        assert e.status_code == 403
Exemple #11
0
def test_good_symlink(tmp_path):
    td = str(tmp_path)
    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
    symlink(cm, file_model["path"], path)
    symlink_model = cm.get(path, content=False)
    dir_model = cm.get(parent)
    assert sorted(dir_model["content"], key=lambda x: x["name"]) == [
        symlink_model,
        file_model,
    ]
Exemple #12
0
def contents_manager(request, tmp_path):
    return FileContentsManager(root_dir=str(tmp_path),
                               use_atomic_writing=request.param)
Exemple #13
0
def test_missing_root_dir(tmp_path):
    root = tmp_path / "notebook" / "dir" / "is" / "missing"
    with pytest.raises(TraitError):
        FileContentsManager(root_dir=str(root))
Exemple #14
0
def test_invalid_root_dir(tmp_path):
    temp_file = tmp_path / 'file.txt'
    temp_file.write_text('')
    with pytest.raises(TraitError):
        FileContentsManager(root_dir=str(temp_file))
Exemple #15
0
def test_missing_root_dir(tmp_path):
    root = tmp_path / 'notebook' / 'dir' / 'is' / 'missing'
    with pytest.raises(TraitError):
        FileContentsManager(root_dir=str(root))
Exemple #16
0
def test_root_dir(tmp_path):
    fm = FileContentsManager(root_dir=str(tmp_path))
    assert fm.root_dir == str(tmp_path)
def jp_contents_manager(request, tmp_path):
    """Returns a FileContentsManager instance based on the use_atomic_writing parameter value."""
    return FileContentsManager(root_dir=str(tmp_path),
                               use_atomic_writing=request.param)
 def _contents_manager(self):
     """simplest reasonable kernel manager"""
     return FileContentsManager(root_dir=self.notebook_dir, parent=self)