22# -*- coding: utf-8 -*-
33"""Tests of ipr management commands"""
44import mock
5+ import sys
56
67from django .core .management import call_command
78from django .test .utils import override_settings
@@ -26,7 +27,7 @@ def test_process_email(self, process_mock):
2627 @mock .patch ('ietf.utils.management.base.send_smtp' )
2728 @mock .patch ('ietf.ipr.management.commands.process_email.process_response_email' )
2829 def test_send_error_to_admin (self , process_mock , send_smtp_mock ):
29- """The process_email command should email the admins on error"""
30+ """The process_email command should email the admins on error in process_response_email """
3031 # arrange an mock error during processing
3132 process_mock .side_effect = RuntimeError ('mock error' )
3233
@@ -47,3 +48,32 @@ def test_send_error_to_admin(self, process_mock, send_smtp_mock):
4748 self .assertIn ('mock.py' , content , 'File where error occurred should be included in error email' )
4849 self .assertIn ('traceback' , traceback .lower (), 'Traceback should be attached to error email' )
4950 self .assertEqual (original , 'contents' , 'Original message should be attached to error email' )
51+
52+ @mock .patch ('ietf.utils.management.base.send_smtp' )
53+ @mock .patch ('ietf.ipr.management.commands.process_email.process_response_email' )
54+ def test_invalid_character_encodings (self , process_mock , send_smtp_mock ):
55+ """The process_email command should attach messages with invalid encoding when using a file input"""
56+ invalid_characters = b'\xfe \xff '
57+ with name_of_file_containing (invalid_characters , mode = 'wb' ) as filename :
58+ call_command ('process_email' , email_file = filename )
59+
60+ self .assertFalse (process_mock .called ) # should not even try to process illegally encoded messages
61+ self .assertTrue (send_smtp_mock .called )
62+ (msg ,) = send_smtp_mock .call_args .args
63+ parts = msg .get_payload ()
64+ self .assertEqual (len (parts ), 3 , 'Error email should contain message, traceback, and original message' )
65+
66+ @mock .patch .object (sys .stdin .buffer , 'read' )
67+ @mock .patch ('ietf.utils.management.base.send_smtp' )
68+ @mock .patch ('ietf.ipr.management.commands.process_email.process_response_email' )
69+ def test_invalid_character_encodings_via_stdin (self , process_mock , send_smtp_mock , stdin_read_mock ):
70+ """The process_email command should attach messages with invalid encoding when using stdin"""
71+ invalid_characters = b'\xfe \xff '
72+ stdin_read_mock .return_value = invalid_characters
73+ call_command ('process_email' )
74+
75+ self .assertFalse (process_mock .called ) # should not even try to process illegally encoded messages
76+ self .assertTrue (send_smtp_mock .called )
77+ (msg ,) = send_smtp_mock .call_args .args
78+ parts = msg .get_payload ()
79+ self .assertEqual (len (parts ), 3 , 'Error email should contain message, traceback, and original message' )
0 commit comments