55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from bolt_pet import file_delivery
|
|
|
|
|
|
def test_sanitize_filename_strips_directory_components():
|
|
assert file_delivery.sanitize_filename("../../etc/passwd") == "passwd"
|
|
assert file_delivery.sanitize_filename("/absolute/path/report.pdf") == "report.pdf"
|
|
assert file_delivery.sanitize_filename("plain.txt") == "plain.txt"
|
|
|
|
|
|
def test_sanitize_filename_falls_back_on_empty_or_dots():
|
|
assert file_delivery.sanitize_filename("") == "delivered_file"
|
|
assert file_delivery.sanitize_filename("..") == "delivered_file"
|
|
assert file_delivery.sanitize_filename(".") == "delivered_file"
|
|
assert file_delivery.sanitize_filename(None) == "delivered_file"
|
|
|
|
|
|
def test_unique_path_returns_the_plain_name_when_free(tmp_path):
|
|
path = file_delivery.unique_path(tmp_path, "report.pdf")
|
|
assert path == tmp_path / "report.pdf"
|
|
|
|
|
|
def test_unique_path_suffixes_on_collision(tmp_path):
|
|
(tmp_path / "report.pdf").write_bytes(b"existing")
|
|
path = file_delivery.unique_path(tmp_path, "report.pdf")
|
|
assert path == tmp_path / "report (1).pdf"
|
|
|
|
(tmp_path / "report (1).pdf").write_bytes(b"also existing")
|
|
path = file_delivery.unique_path(tmp_path, "report.pdf")
|
|
assert path == tmp_path / "report (2).pdf"
|
|
|
|
|
|
def test_unique_path_creates_the_directory(tmp_path):
|
|
target = tmp_path / "nested" / "dir"
|
|
file_delivery.unique_path(target, "a.txt")
|
|
assert target.is_dir()
|
|
|
|
|
|
def test_save_writes_bytes_and_sanitizes_the_name(tmp_path):
|
|
path = file_delivery.save(tmp_path, "../sneaky/report.pdf", b"hello")
|
|
assert path == tmp_path / "report.pdf"
|
|
assert path.read_bytes() == b"hello"
|
|
|
|
|
|
def test_save_never_overwrites_an_existing_download(tmp_path):
|
|
first = file_delivery.save(tmp_path, "notes.txt", b"first")
|
|
second = file_delivery.save(tmp_path, "notes.txt", b"second")
|
|
assert first != second
|
|
assert first.read_bytes() == b"first"
|
|
assert second.read_bytes() == b"second"
|