Commit a03a0840 authored by Raphaël Hertzog's avatar Raphaël Hertzog

mail: ensure the control bot does not choke on messages without any Subject header

Improve the unfold_header() function to return None when fed with a None.
parent 7ba53382
......@@ -260,6 +260,9 @@ class EmailUtilsTest(SimpleTestCase):
for folded, unfolded in test_values.items():
self.assertEqual(unfold_header(folded), unfolded)
def test_unfold_header_with_none_value(self):
self.assertIsNone(unfold_header(None))
class CustomEmailMessageTest(TestCase):
"""
......
......@@ -249,9 +249,13 @@ def unfold_header(header):
values to compose a reply message as Python's email API chokes on those
newline characters.
If header is None, the return value is None as well.
:param:header: the header value to unfold
:type param: str
:returns: the unfolded version of the header.
:rtype: str
"""
if header is None:
return None
return re.sub(r'\r?\n(\s)', r'\1', header, 0, re.MULTILINE)
......@@ -49,7 +49,7 @@ def send_response(original_message, message_text, recipient_email, cc=None):
response.
:param cc: A list of emails which should receive a CC of the response.
"""
subject = unfold_header(decode_header(original_message.get('Subject')))
subject = unfold_header(decode_header(original_message.get('Subject', '')))
if not subject:
subject = 'Your mail'
message_id = unfold_header(original_message.get('Message-ID', ''))
......
......@@ -364,12 +364,11 @@ class ControlBotBasic(EmailControlTest):
self.control_process()
def test_response_when_no_subject(self):
def test_response_when_empty_subject(self):
"""
Tests that the subject of the response when there is no subject set in
the request is correct.
"""
self.set_input_lines(["thanks"])
self.set_header('Subject', '')
self.set_input_lines(['help'])
......@@ -377,6 +376,18 @@ class ControlBotBasic(EmailControlTest):
self.assert_header_equal('Subject', 'Re: Your mail')
def test_response_when_no_subject(self):
"""
Tests that the subject of the response when there is no subject set in
the request is correct.
"""
del self.message['Subject']
self.set_input_lines(['help'])
self.control_process()
self.assert_header_equal('Subject', 'Re: Your mail')
def test_basic_echo_commands(self):
"""
Tests that commands are echoed in the response.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment