Files
dbx-main/tests/test_notice_db.py
T
king 2aa6b6efae feat(notice): urgent notices centered on every ERP screen until acknowledged
- Super admin only: /notice/ page (title, body with images, show time KST, all or picked people,
  preview, edit with optional re-show, cancel/undo, per-person acknowledgement status)
- Shown on every ERP page (erp_base, main, admin) dead center; [확인] only (no Esc/backdrop close),
  queue in order, ack failure keeps the dialog, closes when acked/cancelled elsewhere
- project_db migration 024 (urgent_notices, targets, acks, images as bytea); shares ProjectStore.pool
- Body sanitized like task descriptions, images restricted to /notice/images/{id}, type sniffed
- Entry points: ERP sidebar foot and Project options dialog (super admin)
- Docs (NOTICE_MODULE, DEPLOYMENT 024, overview, CLAUDE.md); tests (DB-less, real PostgreSQL, jsdom)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:07:10 +09:00

88 lines
4.5 KiB
Python

"""긴급 공지 저장소 — 실제 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="<p>x</p>", 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'<p>그림</p><img src="{mine["url"]}"><img src="{theirs["url"]}">'
n = self.st.create_notice(title="그림", body=body, show_at="", to_all=True, targets=[], by=boss)
self.assertEqual(self.st.get_image(image_id=mine["id"])["notice_id"], n["id"])
self.assertIsNone(self.st.get_image(image_id=theirs["id"])["notice_id"]) # 남이 올린 이미지는 묶이지 않는다
self.assertEqual(bytes(self.st.get_image(image_id=mine["id"])["data"]), PNG)
with self.assertRaises(ValueError):
self.st.add_image(data=b"<svg/>", filename="x.svg", by=boss)
if __name__ == "__main__":
unittest.main()