Files
dbx-main/tests/test_project_goal_rich_text.py
T

55 lines
2.8 KiB
Python

import json
import unittest
from app.modules.project import store
def rich(runs):
return store.GOAL_RICH_PREFIX + json.dumps(runs, ensure_ascii=False)
class GoalRichTextTests(unittest.TestCase):
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 '&lt;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>') == '&lt;b&gt;legacy&lt;/b&gt;'
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 '&lt;선택&gt;' 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')