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": "", "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 'legacy') == '<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')