06af036f86
Windows tray app (desktop/notifier, C# .NET Framework 4.8 WinForms, single exe, no installer/admin) that shows the same big alert popup as the web even when the browser is in the background: layered topmost window that does not steal focus, blinking border, open/close actions, sound, autostart toggle, DPAPI-encrypted token. Server side (project_db migration 023): - notifier_links / notifier_tokens (SHA-256 hashes only) - device link flow: link/start -> browser approve at /project/notifier -> link/poll issues token once - Bearer API for alerts/read/logout reusing _alerts_payload; token re-checks current user record and project permission each call - /project/notifier page (approve, connected PCs, revoke, download) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
5.6 KiB
Python
129 lines
5.6 KiB
Python
"""PC 알림 프로그램 API(023) — 토큰 인증·연결 요청/승인/발급·마이그레이션 미적용 503. DB 없이 가짜 저장소로."""
|
|
import importlib
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
from psycopg.errors import UndefinedTable
|
|
|
|
api = importlib.import_module("app.modules.project.router")
|
|
|
|
ROWS = [
|
|
{"id": 8, "type": "mention", "title": "나를 태그함: A", "is_read": False, "task_id": 1, "project_id": 1},
|
|
{"id": 6, "type": "assigned", "title": "작업 배정: C", "is_read": False, "task_id": 3, "project_id": 1},
|
|
]
|
|
|
|
|
|
class FakeStore:
|
|
def __init__(self):
|
|
self.links = {} # poll → {"code", "email"}
|
|
self.tokens = {"good-token": "me@test.local"}
|
|
self.read = []
|
|
|
|
def list_notifications(self, recipient_email, unread_only=False, limit=100):
|
|
return ROWS[:limit]
|
|
|
|
def mark_read(self, notification_id, recipient_email):
|
|
self.read.append((notification_id, recipient_email))
|
|
|
|
def notifier_token_email(self, token):
|
|
return self.tokens.get(token)
|
|
|
|
def notifier_start_link(self, device_name):
|
|
self.links["poll-1"] = {"code": "ABCD-EFGH", "email": None, "device": device_name, "used": False}
|
|
return {"code": "ABCD-EFGH", "poll": "poll-1", "expires_in": 600}
|
|
|
|
def notifier_approve_link(self, code, email):
|
|
for link in self.links.values():
|
|
if link["code"] == code.upper() and link["email"] is None:
|
|
link["email"] = email
|
|
return True
|
|
return False
|
|
|
|
def notifier_poll_link(self, poll):
|
|
link = self.links.get(poll)
|
|
if not link or link["used"]:
|
|
return {"status": "expired"}
|
|
if not link["email"]:
|
|
return {"status": "pending"}
|
|
link["used"] = True
|
|
self.tokens["new-token"] = link["email"]
|
|
return {"status": "ok", "token": "new-token", "email": link["email"]}
|
|
|
|
def notifier_revoke_token(self, token):
|
|
self.tokens.pop(token, None)
|
|
|
|
|
|
USER = {"email": "me@test.local", "name": "나"}
|
|
|
|
|
|
class NotifierApiTests(unittest.TestCase):
|
|
def setUp(self):
|
|
app = FastAPI()
|
|
app.include_router(api.router)
|
|
self.client = TestClient(app)
|
|
self.st = FakeStore()
|
|
self.patches = [
|
|
patch.object(api, "_db_or_503", return_value=self.st),
|
|
patch.object(api, "_visible_notifications", side_effect=lambda st, user, items: items),
|
|
patch.object(api, "_user_by_email", side_effect=lambda e: dict(USER) if e == USER["email"] else None),
|
|
patch("app.store.has_module", return_value=True),
|
|
]
|
|
for p in self.patches:
|
|
p.start()
|
|
|
|
def tearDown(self):
|
|
for p in self.patches:
|
|
p.stop()
|
|
|
|
def alerts(self, token, qs="?after=0"):
|
|
return self.client.get("/project/api/notifier/alerts" + qs, headers={"Authorization": "Bearer " + token})
|
|
|
|
def test_alerts_with_token_returns_same_payload_as_web_plus_name(self):
|
|
res = self.alerts("good-token")
|
|
self.assertEqual(res.status_code, 200)
|
|
out = res.json()
|
|
self.assertEqual([a["id"] for a in out["alerts"]], [6, 8])
|
|
self.assertEqual((out["me"], out["latest_id"], out["name"]), ("me@test.local", 8, "나"))
|
|
|
|
def test_alerts_rejects_missing_or_unknown_token(self):
|
|
self.assertEqual(self.client.get("/project/api/notifier/alerts").status_code, 401)
|
|
self.assertEqual(self.alerts("bad").status_code, 401)
|
|
|
|
def test_alerts_rejects_user_without_project_module(self):
|
|
with patch("app.store.has_module", return_value=False):
|
|
self.assertEqual(self.alerts("good-token").status_code, 401)
|
|
|
|
def test_read_marks_only_for_token_owner(self):
|
|
res = self.client.post("/project/api/notifier/alerts/8/read", headers={"Authorization": "Bearer good-token"})
|
|
self.assertEqual(res.status_code, 200)
|
|
self.assertEqual(self.st.read, [(8, "me@test.local")])
|
|
|
|
def test_link_flow_pending_then_token_once(self):
|
|
start = self.client.post("/project/api/notifier/link/start", json={"device": "KING-PC"}).json()
|
|
self.assertEqual(start["approve_path"], "/project/notifier?code=ABCD-EFGH")
|
|
self.assertEqual(self.client.post("/project/api/notifier/link/poll", json={"poll": start["poll"]}).json()["status"], "pending")
|
|
with patch.object(api, "_require_user", return_value=dict(USER)):
|
|
self.assertEqual(self.client.post("/project/api/notifier/link/approve", json={"code": "abcd-efgh"}).status_code, 200)
|
|
self.assertEqual(self.client.post("/project/api/notifier/link/approve", json={"code": "ABCD-EFGH"}).status_code, 404)
|
|
got = self.client.post("/project/api/notifier/link/poll", json={"poll": start["poll"]}).json()
|
|
self.assertEqual((got["status"], got["token"]), ("ok", "new-token"))
|
|
self.assertEqual(self.client.post("/project/api/notifier/link/poll", json={"poll": start["poll"]}).json()["status"], "expired")
|
|
self.assertEqual(self.alerts("new-token").status_code, 200)
|
|
|
|
def test_logout_revokes_token(self):
|
|
self.client.post("/project/api/notifier/logout", headers={"Authorization": "Bearer good-token"})
|
|
self.assertEqual(self.alerts("good-token").status_code, 401)
|
|
|
|
def test_missing_migration_is_503(self):
|
|
def boom(**_):
|
|
raise UndefinedTable("notifier_links")
|
|
self.st.notifier_start_link = boom
|
|
res = self.client.post("/project/api/notifier/link/start", json={"device": "X"})
|
|
self.assertEqual(res.status_code, 503)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|