# tests for module store (paste_bin/store.py) import os from paste_bin.store import PasteStore def _make(tmp_path): return PasteStore(str(tmp_path / "store"), str(tmp_path / "uploads")) def test_store_uses_validate_id_checker(): # the store must import validate's is_valid_paste_id — never a # locally redefined shadow copy import paste_bin.store as store_mod from paste_bin import validate assert store_mod.is_valid_paste_id is validate.is_valid_paste_id def _text_meta(title="t", expires_at=None): return { "type": "text", "title": title, "created_at": "2026-01-01T00:00:00+00:00", "expires_at": expires_at, "filename": None, "mimetype": None, "filepath": None, } def test_ctor_creates_dirs(tmp_path): _make(tmp_path) assert (tmp_path / "store").is_dir() assert (tmp_path / "uploads").is_dir() def test_meta_roundtrip(tmp_path): s = _make(tmp_path) s.save_meta("a" * 16, _text_meta("hello")) assert s.load_meta("a" * 16)["title"] == "hello" assert s.load_meta("b" * 16) is None # missing assert s.load_meta("../etc/passwd") is None # invalid id # corrupt JSON -> None, never raises bad = "c" * 16 s.save_meta(bad, _text_meta()) (tmp_path / "store" / bad).write_text("{not json", encoding="utf-8") assert s.load_meta(bad) is None def test_content_written_verbatim(tmp_path): s = _make(tmp_path) payload = " leading spaces\n\ntrailing newline\n" s.write_content("d" * 16, payload) assert s.read_content("d" * 16) == payload # no stripping assert s.read_content("e" * 16) is None def test_write_upload_returns_absolute_path(tmp_path): s = _make(tmp_path) path = s.write_upload("f" * 16, b"\x00\x01binary", "bin") assert os.path.isabs(path) with open(path, "rb") as f: assert f.read() == b"\x00\x01binary" assert (tmp_path / "uploads" / ("f" * 16 + ".bin")).is_file() def test_save_meta_leaves_no_tmp_files(tmp_path): s = _make(tmp_path) s.save_meta("1" * 16, _text_meta()) leftovers = [p.name for p in (tmp_path / "store").iterdir() if ".tmp." in p.name] assert leftovers == [] def test_delete_removes_meta_content_and_upload(tmp_path): s = _make(tmp_path) pid = "2" * 16 meta = { "type": "image", "title": "i", "created_at": "2026-01-01T00:00:00+00:00", "expires_at": None, "filename": "x.png", "mimetype": "image/png", "filepath": None, } s.save_meta(pid, meta) s.write_content(pid, "stray") path = s.write_upload(pid, b"\x89PNG", "png") meta["filepath"] = path s.delete(pid) assert not (tmp_path / "store" / pid).exists() assert not (tmp_path / "store" / (pid + ".txt")).exists() assert not os.path.exists(path) def test_is_expired(tmp_path): s = _make(tmp_path) assert s.is_expired({"expires_at": None}) is False assert s.is_expired({"expires_at": "2020-01-01T00:00:00+00:00"}) is True assert s.is_expired({"expires_at": "2999-01-01T00:00:00+00:00"}) is False def test_cleanup_expired_deletes_only_expired_and_stale_tmp(tmp_path): s = _make(tmp_path) pid_expired = "3" * 16 pid_fresh = "4" * 16 s.save_meta(pid_expired, _text_meta("old", "2020-01-01T00:00:00+00:00")) s.write_content(pid_expired, "gone soon") s.save_meta(pid_fresh, _text_meta("new", "2999-01-01T00:00:00+00:00")) s.write_content(pid_fresh, "keep") # stale tmp file left by a crashed write (old app never cleaned these) stale = tmp_path / "store" / ("5" * 16 + ".tmp.12345") stale.write_text("{}", encoding="utf-8") n = s.cleanup_expired() assert n == 1 assert s.load_meta(pid_expired) is None assert not (tmp_path / "store" / (pid_expired + ".txt")).exists() assert s.load_meta(pid_fresh) is not None assert (tmp_path / "store" / (pid_fresh + ".txt")).is_file() assert not stale.exists()