"""긴급 공지 저장소 — 실제 PostgreSQL(로컬 테스트 DB 전용, PROJECT_TEST_DB_URL 이 localhost 일 때만). PROJECT_TEST_DB_URL=postgresql://postgres@127.0.0.1:55432/project_db python -m pytest tests/test_notice_db.py project_db init + 마이그레이션(024 포함)이 적용돼 있어야 한다. 가상의 사용자·공지만 만든다. """ import os import unittest import uuid from datetime import datetime, timedelta from urllib.parse import urlparse from app.timezone import KST DSN = os.getenv("PROJECT_TEST_DB_URL", "") ENABLED = bool(DSN) and urlparse(DSN).hostname in ("localhost", "127.0.0.1") PNG = b"\x89PNG\r\n\x1a\n" + b"1" * 32 @unittest.skipUnless(ENABLED, "PROJECT_TEST_DB_URL(localhost) not set") class NoticeDbTests(unittest.TestCase): @classmethod def setUpClass(cls): from app.modules.notice.db import NoticeStore from app.modules.project.db import ProjectStore cls.ps = ProjectStore(DSN) cls.st = NoticeStore(cls.ps.pool) cls.tag = uuid.uuid4().hex[:6] @classmethod def tearDownClass(cls): cls.ps.close() def em(self, name): return f"{name}-{self.tag}@test.local" def ids(self, email, now=None): return [n["id"] for n in self.st.pending_for(email=email, now=now)] def test_schedule_targets_ack_cancel_reshow(self): boss, a, b = self.em("boss"), self.em("a"), self.em("b") now = datetime.now(KST) allp = self.st.create_notice(title="전체", body="
x
", show_at="", to_all=True, targets=[], by=boss) some = self.st.create_notice(title="a 만", body="", show_at="", to_all=False, targets=[a], by=boss) later = self.st.create_notice(title="예약", body="", show_at=(now + timedelta(hours=2)).isoformat(), to_all=True, targets=[], by=boss) self.assertIn(allp["id"], self.ids(a)) self.assertIn(some["id"], self.ids(a)) self.assertNotIn(some["id"], self.ids(b)) # 고른 사람만 self.assertNotIn(later["id"], self.ids(a)) # 띄울 시각 전 self.assertIn(later["id"], self.ids(a, now=now + timedelta(hours=3))) self.assertTrue(self.st.ack(notice_id=allp["id"], email=a)) self.assertFalse(self.st.ack(notice_id=allp["id"], email=a)) # 두 번 눌러도 한 번 self.assertNotIn(allp["id"], self.ids(a)) self.assertIn(allp["id"], self.ids(b)) self.st.cancel_notice(notice_id=allp["id"], by=boss) self.assertNotIn(allp["id"], self.ids(b)) # 취소 = 아무에게도 안 뜸 self.st.cancel_notice(notice_id=allp["id"], by=boss, cancel=False) self.assertIn(allp["id"], self.ids(b)) row = next(n for n in self.st.list_notices() if n["id"] == allp["id"]) self.assertEqual(list(row["acks"]), [a]) # 고치기 — 받는 사람 바꾸기, reshow 면 확인한 사람에게도 다시 self.st.update_notice(notice_id=allp["id"], title="전체(수정)", body="", show_at="", to_all=True, targets=[], by=boss) self.assertNotIn(allp["id"], self.ids(a)) self.st.update_notice(notice_id=allp["id"], title="전체(다시)", body="", show_at="", to_all=True, targets=[], by=boss, reshow=True) self.assertIn(allp["id"], self.ids(a)) self.st.update_notice(notice_id=some["id"], title="b 로", body="", show_at="", to_all=False, targets=[b], by=boss) self.assertNotIn(some["id"], self.ids(a)) self.assertIn(some["id"], self.ids(b)) with self.assertRaises(KeyError): self.st.update_notice(notice_id=10 ** 12, title="x", body="", show_at="", to_all=True, targets=[], by=boss) def test_images_link_only_own_uploads(self): boss, other = self.em("boss"), self.em("other") mine = self.st.add_image(data=PNG, filename="a.png", by=boss) theirs = self.st.add_image(data=PNG, filename="b.png", by=other) body = f'그림