91 lines
5.1 KiB
Python
91 lines
5.1 KiB
Python
import json
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from app.modules.project import store
|
|
from app.modules.project.router import _month_plan
|
|
|
|
|
|
def rich(runs):
|
|
return store.GOAL_RICH_PREFIX + json.dumps(runs, ensure_ascii=False)
|
|
|
|
|
|
class GoalRichTextTests(unittest.TestCase):
|
|
def test_summary_keeps_all_styles_except_font_size(self):
|
|
body = rich([{"text": "<계획>\n 다음 줄", "bold": True, "italic": True,
|
|
"underline": True, "color": "#DC2626", "size": 32},
|
|
{"text": " 해제", "bold": False, "italic": False,
|
|
"underline": False, "size": 10}])
|
|
html = store.goal_text_html(body, include_size=False)
|
|
for style in ("font-weight:700", "font-style:italic", "text-decoration:underline",
|
|
"color:#dc2626", "font-weight:400", "font-style:normal", "text-decoration:none"):
|
|
self.assertIn(style, html)
|
|
self.assertIn("<계획>\n 다음 줄", html)
|
|
self.assertNotIn("font-size", html)
|
|
self.assertIn("font-size:32px", store.goal_text_html(body))
|
|
self.assertIn("font-size:10px", store.goal_text_html(body))
|
|
|
|
def test_month_plan_preserves_formatted_merged_cells_and_literal_legacy_text(self):
|
|
body = rich([{"text": "<img src=x onerror=alert(1)>\n 계획", "bold": True,
|
|
"italic": True, "underline": True, "color": "#2563EB", "size": 24}])
|
|
rows = [{"label": "인플", "cells": {7: body}, "merges": {7: (3, 2)}},
|
|
{"label": "숨긴 줄", "cells": {}, "merges": {}},
|
|
{"label": "쿠팡", "cells": {9: "<b>평문</b>"}, "merges": {}},
|
|
{"label": "빈 칸", "cells": {9: rich([{"text": " ", "bold": True}])}, "merges": {}}]
|
|
fake = SimpleNamespace(goals_enabled=lambda: True,
|
|
get_goal_board=lambda **kw: {"areas": [{"name": "유통", "color": "#cdebf8", "rows": rows}]})
|
|
plans = _month_plan(fake, 2026, 9)
|
|
lines = plans[0]["lines"]
|
|
self.assertEqual(len(lines), 2)
|
|
self.assertEqual(lines[0]["range"], "7~9월")
|
|
self.assertEqual(lines[0]["text"], store.goal_text_plain(body))
|
|
self.assertEqual(lines[0]["text_html"], store.goal_text_html(body, include_size=False))
|
|
self.assertNotIn("<img", lines[0]["text_html"])
|
|
self.assertNotIn("font-size", lines[0]["text_html"])
|
|
self.assertEqual(lines[1]["text_html"], "<b>평문</b>")
|
|
self.assertEqual(rows[0]["cells"][7], body)
|
|
|
|
def test_selected_styles_are_safe_and_legacy_text_stays_literal(self):
|
|
body = rich([{"text": "<img src=x onerror=alert(1)>", "bold": True,
|
|
"color": "#FF0000", "size": 20, "onclick": "alert(1)"},
|
|
{"text": " untouched", "color": "url(javascript:x)", "size": 99}])
|
|
stored = store.validate_goal_text(body)
|
|
html = store.goal_text_html(stored)
|
|
assert '<img' in html and '<img' not in html
|
|
assert 'font-weight:700' in html and 'color:#ff0000' in html and 'font-size:20px' in html
|
|
assert 'javascript' not in html and 'onclick' not in stored and '99px' not in html
|
|
assert store.goal_text_html('<b>legacy</b>') == '<b>legacy</b>'
|
|
|
|
|
|
def test_rich_merge_preserves_styles_and_plain_meeting_plan(self):
|
|
value = store.merge_goal_texts(['first', rich([{"text": "second", "bold": True, "size": 18}])])
|
|
assert store.goal_text_plain(value) == 'first\nsecond'
|
|
assert 'font-size:18px' in store.goal_text_html(value)
|
|
assert store.merge_goal_texts(['a', 'b']) == 'a\nb'
|
|
assert store.validate_goal_text(rich([{"text": " "}])) == ''
|
|
|
|
def test_italic_and_underline_survive_save_render_and_merge(self):
|
|
body = rich([{"text": "<선택>", "italic": True, "underline": True, "size": 16},
|
|
{"text": "해제", "italic": False, "underline": False},
|
|
{"text": "invalid", "italic": "url(javascript:x)", "underline": 1}])
|
|
stored = store.validate_goal_text(body)
|
|
runs = store.goal_text_runs(stored)
|
|
assert runs[0]["italic"] is True and runs[0]["underline"] is True
|
|
assert runs[1]["italic"] is False and runs[1]["underline"] is False
|
|
assert runs[2] == {"text": "invalid"}
|
|
html = store.goal_text_html(stored)
|
|
assert '<선택>' in html and 'font-style:italic' in html and 'text-decoration:underline' in html
|
|
assert 'font-style:normal' in html and 'text-decoration:none' in html
|
|
assert 'javascript' not in stored
|
|
merged = store.merge_goal_texts([stored, 'plain'])
|
|
assert store.goal_text_runs(merged)[0]["underline"] is True
|
|
assert store.goal_text_plain(merged) == '<선택>해제invalid\nplain'
|
|
|
|
|
|
def test_rich_payload_limits_and_malformed_data(self):
|
|
with self.assertRaises(ValueError):
|
|
store.validate_goal_text(rich([{"text": "x" * (store.GOAL_TEXT_MAX + 1)}]))
|
|
with self.assertRaises(ValueError):
|
|
store.validate_goal_text(store.GOAL_RICH_PREFIX + '{"unexpected":1}')
|
|
with self.assertRaises(ValueError):
|
|
store.validate_goal_text(store.GOAL_RICH_PREFIX + '[broken')
|