From 71dcf9fc8b368b5cef4dd00235431a503b4a2f84 Mon Sep 17 00:00:00 2001 From: Kesara Rathnayake Date: Sun, 6 Nov 2022 12:13:02 +0000 Subject: [PATCH] fix: Use correct codec for decoding meeting materials (#4697) --- ietf/meeting/tests_views.py | 30 ++++++++++++++++++++++++++++-- ietf/meeting/views.py | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ietf/meeting/tests_views.py b/ietf/meeting/tests_views.py index 554f0d722..ab2272117 100644 --- a/ietf/meeting/tests_views.py +++ b/ietf/meeting/tests_views.py @@ -121,7 +121,7 @@ class BaseMeetingTestCase(TestCase): settings.MEETINGHOST_LOGO_PATH = self.saved_meetinghost_logo_path super().tearDown() - def write_materials_file(self, meeting, doc, content): + def write_materials_file(self, meeting, doc, content, charset="utf-8"): path = os.path.join(self.materials_dir, "%s/%s/%s" % (meeting.number, doc.type_id, doc.uploaded_filename)) dirname = os.path.dirname(path) @@ -129,7 +129,7 @@ class BaseMeetingTestCase(TestCase): os.makedirs(dirname) if isinstance(content, str): - content = content.encode() + content = content.encode(charset) with io.open(path, "wb") as f: f.write(content) @@ -387,6 +387,32 @@ class MeetingTests(BaseMeetingTestCase): self.do_test_materials(meeting, session) + @override_settings(MEETING_MATERIALS_SERVE_LOCALLY=True) + def test_meeting_materials_non_utf8(self): + meeting = make_meeting_test_data() + session = Session.objects.filter(meeting=meeting, group__acronym="mars").first() + doc = session.materials.get(type="minutes") + self.write_materials_file(meeting, + doc, + "1. More work items underway\n\n2. The draft will be finished before next meeting\n\n - É", + charset="iso-8859-1") + url = urlreverse("ietf.meeting.views.materials_document", + kwargs=dict(num=meeting.number, document=session.minutes())) + + for accept, cont_type, content in [ + ('text/html,text/plain,text/markdown', 'text/html', '
  • \n

    More work items underway

    \n
  • '), + ('text/markdown,text/html,text/plain', 'text/markdown', '1. More work items underway'), + ('text/plain,text/markdown, text/html', 'text/plain', '1. More work items underway'), + ('text/html', 'text/html', '
  • \n

    More work items underway

    \n
  • '), + ('text/markdown', 'text/markdown', '1. More work items underway'), + ('text/plain', 'text/plain', '1. More work items underway'), + ]: + client = Client(HTTP_ACCEPT=accept) + r = client.get(url) + rtype = r['Content-Type'].split(';')[0] + self.assertEqual(cont_type, rtype) + self.assertContains(r, content) + @override_settings(MEETING_MATERIALS_SERVE_LOCALLY=True) def do_test_materials(self, meeting, session): diff --git a/ietf/meeting/views.py b/ietf/meeting/views.py index 3e4a118b9..16443dab4 100644 --- a/ietf/meeting/views.py +++ b/ietf/meeting/views.py @@ -261,7 +261,7 @@ def materials_document(request, document, num=None, ext=None): content_type = content_type.replace('plain', 'markdown', 1) break; elif atype[0] == 'text/html': - bytes = "\n\n\n%s\n\n\n" % markdown.markdown(bytes.decode()) + bytes = "\n\n\n%s\n\n\n" % markdown.markdown(bytes.decode(encoding=chset)) content_type = content_type.replace('plain', 'html', 1) break; elif atype[0] == 'text/plain':