Exemple #1
0
def test_remove_if_not_exists():
    from pyramid_storage import local

    s = local.LocalFileStorage("")

    with mock.patch('os.path.exists', lambda p: False):
        assert not s.delete("test.jpg")
Exemple #2
0
def test_file_not_allowed():
    from pyramid_storage import local

    fs = mock.Mock()
    fs.filename = "test.jpg"
    s = local.LocalFileStorage("", extensions="documents")

    assert not s.file_allowed(fs)
Exemple #3
0
def test_resolve_name_if_not_exists():
    from pyramid_storage import local
    s = local.LocalFileStorage("uploads")

    with mock.patch("os.path.exists", lambda p: False):
        name, path = s.resolve_name("test.jpg", "uploads")
        assert name == "test.jpg"
        assert path == "uploads" + os.path.sep + "test.jpg"
Exemple #4
0
def test_save_if_file_not_allowed():
    from pyramid_storage import local
    from pyramid_storage.exceptions import FileNotAllowed

    fs = mock.Mock()
    fs.filename = "test.jpg"
    s = local.LocalFileStorage("", extensions="documents")

    with pytest.raises(FileNotAllowed):
        s.save(fs)
Exemple #5
0
def test_resolve_name_if_exists():
    from pyramid_storage import local
    s = local.LocalFileStorage("uploads")

    def conditional_exists(path):
        return "1" not in path

    with mock.patch("os.path.exists", conditional_exists):
        name, path = s.resolve_name("test.jpg", "uploads")
        assert name == "test-1.jpg"
        assert path == "uploads" + os.path.sep + "test-1.jpg"
Exemple #6
0
def test_remove_if_exists():
    from pyramid_storage import local

    patches = (
        mock.patch('os.remove', mock.Mock()),
        mock.patch('os.path.exists', lambda p: True),
    )

    for patch in patches:
        patch.start()

    s = local.LocalFileStorage("")
    assert s.delete("test.jpg")
Exemple #7
0
def test_save_file():
    from pyramid_storage import local

    s = local.LocalFileStorage("uploads", extensions="images")

    patches = (
        mock.patch(_mock_open_name(), _mock_open),
        mock.patch("os.path.exists", lambda p: False),
        mock.patch("os.makedirs", lambda p: True),
        mock.patch("shutil.copyfileobj", lambda x, y: True),
    )

    for patch in patches:
        patch.start()

    name = s.save_file(mock.Mock(), "test.jpg")
    assert name == "test.jpg"

    for patch in patches:
        patch.stop()
Exemple #8
0
def test_save_in_folder():
    from pyramid_storage import local

    fs = mock.Mock()
    fs.filename = "test.jpg"

    s = local.LocalFileStorage("uploads", extensions="images")

    patches = (
        mock.patch(_mock_open_name()), _mock_open(),
        mock.patch("os.path.exists", lambda p: False),
        mock.patch("os.makedirs", lambda p: True),
        mock.patch("shutil.copyfileobj", lambda x, y: True),
    )

    for patch in patches:
        patch.start()

    name = s.save(fs, folder="photos")
    assert name == "photos%stest.jpg" % os.path.sep

    for patch in patches:
        patch.stop()
Exemple #9
0
def test_save_if_randomize():
    from pyramid_storage import local

    fs = mock.Mock()
    fs.filename = "test.jpg"

    s = local.LocalFileStorage("uploads", extensions="images")

    patches = (
        mock.patch(_mock_open_name(), _mock_open),
        mock.patch("os.path.exists", lambda p: False),
        mock.patch("os.makedirs", lambda p: True),
        mock.patch("shutil.copyfileobj", lambda x, y: True),
    )

    for patch in patches:
        patch.start()

    name = s.save(fs, randomize=True)
    assert name != "test.jpg"
    assert name.endswith(".jpg")

    for patch in patches:
        patch.stop()
Exemple #10
0
def test_extension_not_allowed_if_allowed_if_not_dotted():
    from pyramid_storage import local
    assert not local.LocalFileStorage("").extension_allowed("jpg", ("gif",))
Exemple #11
0
def test_path():
    from pyramid_storage import local
    s = local.LocalFileStorage("uploads")
    assert s.path("test.jpg") == "uploads" + os.path.sep + "test.jpg"
Exemple #12
0
def test_url():
    from pyramid_storage import local
    s = local.LocalFileStorage("", "http://localhost/")
    assert s.url("test.jpg") == "http://localhost/test.jpg"