Poller: final review fixes (no secret in the image, Storage's real error read, non-http attachment skipped, failing conversation named, duplicate message saved once)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d80b6face8
commit
b9dddaf221
8 files changed
+121
-34
No files matched your search
@@ -12,7 +12,7 @@ class FakeSupabase:
|
||||
self.tables: dict[str, dict[str, dict]] = {"conversations": {}, "messages": {}, "checkpoints": {}}
|
||||
self.objects: dict[str, bytes] = {}
|
||||
self.requests: list[tuple[str, str, dict]] = [] # (method, path with query, headers with lower-case names)
|
||||
self.fail: dict[str, int] = {} # path prefix → error code to return
|
||||
self.fail: dict[str, int | tuple[int, dict]] = {} # path prefix → error code, or (code, body) as Storage does
|
||||
fake = self
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
@@ -29,9 +29,10 @@ class FakeSupabase:
|
||||
raw = self.rfile.read(n) if n else b""
|
||||
fake.requests.append((self.command, self.path, {k.lower(): v for k, v in self.headers.items()}))
|
||||
url = urllib.parse.urlsplit(self.path)
|
||||
for prefix, code in fake.fail.items():
|
||||
for prefix, failure in fake.fail.items():
|
||||
if url.path.startswith(prefix):
|
||||
return self._answer(code, {"message": "failure"})
|
||||
code, body = failure if isinstance(failure, tuple) else (failure, {"message": "failure"})
|
||||
return self._answer(code, body)
|
||||
if url.path.startswith("/rest/v1/"):
|
||||
table = url.path.removeprefix("/rest/v1/")
|
||||
key = KEYS.get(table, "uuid")
|
||||
@@ -42,7 +43,10 @@ class FakeSupabase:
|
||||
if vals[0].startswith("eq."):
|
||||
rows = [r for r in rows if str(r.get(col)) == vals[0][3:]]
|
||||
return self._answer(200, rows)
|
||||
for row in json.loads(raw):
|
||||
rows = json.loads(raw)
|
||||
if len({r[key] for r in rows}) < len(rows): # PostgREST: "cannot affect row a second time"
|
||||
return self._answer(500, {"code": "21000"})
|
||||
for row in rows:
|
||||
fake.tables[table][row[key]] = fake.tables[table].get(row[key], {}) | row
|
||||
return self._answer(201)
|
||||
if url.path == "/storage/v1/object/list/attachments":
|
||||
|
||||
@@ -21,6 +21,14 @@ class TestGuardPoller(unittest.TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
Supabase("http://x", "k")._request("POST", "/api/v1/conversations", b"")
|
||||
|
||||
def test_secrets_never_in_the_image(self): # the build context is the repository root
|
||||
ignored = (ROOT / ".dockerignore").read_text().split()
|
||||
self.assertIn("**/.env", ignored)
|
||||
self.assertIn(".git", ignored)
|
||||
|
||||
def test_db_password_not_in_the_container_env(self): # poller/.env is the container's env_file
|
||||
self.assertIsNone(re.search(r"DB_PASSWORD[^\n]*>> poller/\.env", (ROOT / "docs" / "production.md").read_text()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
+29
-3
@@ -81,12 +81,38 @@ class TestPoll(unittest.TestCase):
|
||||
self.assertTrue(any("skipped: 404" in l for l in self.lines))
|
||||
self.assertIn("conversations", self.sb.tables["checkpoints"])
|
||||
|
||||
def test_attachment_refused_by_storage_skipped(self):
|
||||
self.sb.fail["/storage/v1/object/attachments/"] = 413
|
||||
def test_attachment_refused_by_storage_skipped(self): # Storage answers 400, the real reason in the body
|
||||
self.sb.fail["/storage/v1/object/attachments/"] = (400, {"statusCode": "413", "code": "EntityTooLarge"})
|
||||
self.assertEqual(self.run_once(), 2)
|
||||
self.assertTrue(any("skipped: 413" in l for l in self.lines))
|
||||
self.assertTrue(any("skipped: 400 EntityTooLarge" in l for l in self.lines))
|
||||
self.assertIn("conversations", self.sb.tables["checkpoints"])
|
||||
|
||||
def test_missing_bucket_stops_the_run(self): # not an attachment's fault: never skip and lose them all
|
||||
self.sb.fail["/storage/v1/object/attachments/"] = (400, {"statusCode": "404", "code": "NoSuchBucket"})
|
||||
with self.assertRaises(urllib.error.HTTPError) as cm:
|
||||
self.run_once()
|
||||
cm.exception.close()
|
||||
self.assertNotIn("conversations", self.sb.tables["checkpoints"])
|
||||
|
||||
def test_non_http_attachment_skipped(self): # Lecteur refuses it (ValueError): skipped, never blocks the runs
|
||||
self.serve(piece("passwd", "file:///etc/passwd", uuid="a1"))
|
||||
self.assertEqual(self.run_once(), 2)
|
||||
self.assertTrue(self.sb.tables["messages"]["m08:00"]["raw"]["attachments"][0]["absent"])
|
||||
self.assertIn("conversations", self.sb.tables["checkpoints"])
|
||||
|
||||
def test_failure_names_the_conversation(self):
|
||||
self.sb.fail["/rest/v1/messages"] = 500
|
||||
with self.assertRaises(urllib.error.HTTPError) as cm:
|
||||
self.run_once()
|
||||
cm.exception.close()
|
||||
self.assertTrue(any("#2" in l and "u2" in l for l in self.lines))
|
||||
|
||||
def test_same_message_twice_saved_once(self): # a page shift while a long thread is read repeats a message
|
||||
m = message("09:00")
|
||||
self.ld.reponses[("GET", "/api/v1/conversations/u1/messages")] = pages([m, dict(m)])
|
||||
self.assertEqual(self.run_once(), 2)
|
||||
self.assertEqual(self.sb.tables["messages"]["m09:00"]["conversation_uuid"], "u1")
|
||||
|
||||
def test_other_storage_error_stops_the_run(self): # a passing failure: the next run starts again
|
||||
self.sb.fail["/storage/v1/object/attachments/"] = 500
|
||||
with self.assertRaises(urllib.error.HTTPError) as cm:
|
||||
|
||||
Reference in new issue
Block a user