datatracker/ietf/utils/test_textupload.py
Sasha Romijn fd53f98854 Fix #2584 - Add additional content validation for uploaded texts.
Permitted MIME types are now text/plain, text/markdown and text/x-rst.
This applies to all usages of get_cleaned_text_file_content(),
including reviews, but also other similar places where text can either
be written either into a textarea or uploaded.

Commit ready for merge.
 - Legacy-Id: 16922
2019-10-23 20:15:30 +00:00

38 lines
1.5 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Copyright The IETF Trust 2015-2019, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from .textupload import get_cleaned_text_file_content
from ietf.utils.test_utils import TestCase
class GetCleanedTextFileContentTest(TestCase):
def test_no_file(self):
self.assertEqual(get_cleaned_text_file_content(None), "")
def test_valid_file(self):
data = 'testing 👾'
uploaded_file = SimpleUploadedFile('data.txt', data.encode('utf-8'))
self.assertEqual(get_cleaned_text_file_content(uploaded_file), data)
def test_invalid_mime_type_gif(self):
data = 'GIF89a;'
uploaded_file = SimpleUploadedFile('data.txt', data.encode('utf-8'))
with self.assertRaises(ValidationError) as context:
get_cleaned_text_file_content(uploaded_file)
self.assertIn('does not appear to be a text file', context.exception.message)
self.assertIn('image/gif', context.exception.message)
def test_invalid_mime_type_rst(self):
data = r'{\rtf1}'
uploaded_file = SimpleUploadedFile('data.txt', data.encode('utf-8'))
with self.assertRaises(ValidationError) as context:
get_cleaned_text_file_content(uploaded_file)
self.assertIn('does not appear to be a text file', context.exception.message)
self.assertIn('text/rtf', context.exception.message)