返回 last30days-skill
test_trend_file_permissions.py
根目录 / tests / test_trend_file_permissions.py
1 import json
2 import os
3 import sqlite3
4 import stat
5 from contextlib import closing
6 from pathlib import Path, PureWindowsPath
7 from types import SimpleNamespace
8
9 import briefing
10 import pytest
11 import store
12 from lib import library_index
13
14
15 pytestmark = pytest.mark.skipif(os.name != "posix", reason="POSIX file permissions")
16
17
18 @pytest.fixture
19 def trend_store(tmp_path, monkeypatch):
20 database = tmp_path / "archive #1" / "research.db"
21 archive = tmp_path / "briefs"
22 monkeypatch.setattr(store, "_db_override", database)
23 monkeypatch.setattr(briefing, "BRIEFS_DIR", archive)
24 old_umask = os.umask(0o022)
25 try:
26 topic = store.add_topic("Weather stations")
27 run = store.record_run(topic["id"], status="completed")
28 store.store_findings(run, topic["id"], [{
29 "source": "reddit",
30 "source_url": "https://example.invalid/stations",
31 "source_title": "Weather stations near the sea",
32 "content": "The weather stations measure wind speed.",
33 }])
34 yield database, archive
35 finally:
36 os.umask(old_umask)
37
38
39 def _sqlite_files(database):
40 return database, Path(f"{database}-wal"), Path(f"{database}-shm")
41
42
43 def test_store_and_live_wal_sidecars_are_owner_only(trend_store):
44 database, _ = trend_store
45 with closing(store._connect()) as conn:
46 assert conn.execute("SELECT name FROM topics").fetchone()[0] == "Weather stations"
47 assert all(stat.S_IMODE(path.stat().st_mode) == 0o600 for path in _sqlite_files(database))
48
49
50 @pytest.mark.parametrize("reader", ["store", "library"])
51 def test_reopening_existing_store_hardens_live_files_without_losing_data(trend_store, reader):
52 database, archive = trend_store
53 with closing(store._connect()) as conn:
54 assert conn.execute("SELECT count(*) FROM findings").fetchone()[0] == 1
55 paths = _sqlite_files(database)
56 for path in paths:
57 path.chmod(0o644)
58 if reader == "store":
59 assert store.list_topics()[0]["name"] == "Weather stations"
60 else:
61 matches = library_index.search(
62 "weather stations", db_path=archive / "not-created.db", store_db_path=database
63 )
64 assert matches[0].headline == "Weather stations near the sea"
65 assert all(stat.S_IMODE(path.stat().st_mode) == 0o600 for path in paths)
66
67
68 def test_library_search_uses_native_path_for_windows_unc_store(trend_store, monkeypatch):
69 database, archive = trend_store
70 native_path = PureWindowsPath("//server/share/research.db")
71
72 def connect(path, *, uri=False):
73 assert path == str(native_path)
74 assert uri is False
75 return sqlite3.connect(database)
76
77 monkeypatch.setattr(library_index, "os", SimpleNamespace(name="nt"))
78 monkeypatch.setattr(library_index, "prepare_private_sqlite_files", lambda _path: native_path)
79 monkeypatch.setattr(library_index, "sqlite3", SimpleNamespace(
80 connect=connect, Row=sqlite3.Row, DatabaseError=sqlite3.DatabaseError
81 ))
82
83 matches = library_index.search(
84 "weather stations", db_path=archive / "not-created.db", store_db_path=database
85 )
86
87 assert [match.headline for match in matches] == ["Weather stations near the sea"]
88
89
90 @pytest.mark.parametrize("weekly", [False, True], ids=["daily", "weekly"])
91 def test_briefing_archive_is_owner_only_on_create_and_rewrite(trend_store, weekly):
92 _, archive = trend_store
93 generate = briefing.generate_weekly if weekly else briefing.generate_daily
94 result = generate()
95 path = next(archive.glob("*.json"))
96 assert stat.S_IMODE(path.stat().st_mode) == 0o600
97 assert json.loads(path.read_text(encoding="utf-8")) == result
98
99 path.chmod(0o644)
100 result = generate()
101 assert stat.S_IMODE(path.stat().st_mode) == 0o600
102 assert json.loads(path.read_text(encoding="utf-8")) == result
103
103 lines PYTHON