@@ -592,8 +592,7 @@ def test_notify_generic(self):
592592 self .assertEqual (len (outbox ),2 )
593593 self .assertIn ('Secretariat on ' + ipr .get_latest_event_submitted ().time .strftime ("%Y-%m-%d" ), get_payload_text (outbox [1 ]).replace ('\n ' ,' ' ))
594594
595- def test_process_response_email (self ):
596- # first send a mail
595+ def send_ipr_email_helper (self ):
597596 ipr = HolderIprDisclosureFactory ()
598597 url = urlreverse ('ietf.ipr.views.email' ,kwargs = { "id" : ipr .id })
599598 self .client .login (username = "secretary" , password = "secretary+password" )
@@ -607,27 +606,32 @@ def test_process_response_email(self):
607606 response_due = yesterday .isoformat ())
608607 empty_outbox ()
609608 r = self .client .post (url ,data ,follow = True )
610- #print r.content
611609 self .assertEqual (r .status_code ,200 )
612610 q = Message .objects .filter (reply_to = data ['reply_to' ])
613611 self .assertEqual (q .count (),1 )
614612 event = q [0 ].msgevents .first ()
615613 self .assertTrue (event .response_past_due ())
616614 self .assertEqual (len (outbox ), 1 )
617615 self .assertTrue ('joe@test.com' in outbox [0 ]['To' ])
618-
616+ return data ['reply_to' ], event
617+
618+ uninteresting_ipr_message_strings = [
619+ ("To: {to}\n Cc: {cc}\n From: joe@test.com\n Date: {date}\n Subject: test\n " ),
620+ ("Cc: {cc}\n From: joe@test.com\n Date: {date}\n Subject: test\n " ), # no To
621+ ("To: {to}\n From: joe@test.com\n Date: {date}\n Subject: test\n " ), # no Cc
622+ ("From: joe@test.com\n Date: {date}\n Subject: test\n " ), # no To or Cc
623+ ("Cc: {cc}\n Date: {date}\n Subject: test\n " ), # no To
624+ ("To: {to}\n Date: {date}\n Subject: test\n " ), # no Cc
625+ ("Date: {date}\n Subject: test\n " ), # no To or Cc
626+ ]
627+
628+ def test_process_response_email (self ):
629+ # first send a mail
630+ reply_to , event = self .send_ipr_email_helper ()
631+
619632 # test process response uninteresting messages
620633 addrs = gather_address_lists ('ipr_disclosure_submitted' ).as_strings ()
621- uninteresting_message_strings = [
622- ("To: {to}\n Cc: {cc}\n From: joe@test.com\n Date: {date}\n Subject: test\n " ),
623- ("Cc: {cc}\n From: joe@test.com\n Date: {date}\n Subject: test\n " ), # no To
624- ("To: {to}\n From: joe@test.com\n Date: {date}\n Subject: test\n " ), # no Cc
625- ("From: joe@test.com\n Date: {date}\n Subject: test\n " ), # no To or Cc
626- ("Cc: {cc}\n Date: {date}\n Subject: test\n " ), # no To
627- ("To: {to}\n Date: {date}\n Subject: test\n " ), # no Cc
628- ("Date: {date}\n Subject: test\n " ), # no To or Cc
629- ]
630- for message_string in uninteresting_message_strings :
634+ for message_string in self .uninteresting_ipr_message_strings :
631635 result = process_response_email (
632636 message_string .format (
633637 to = addrs .to ,
@@ -642,12 +646,41 @@ def test_process_response_email(self):
642646From: joe@test.com
643647Date: {}
644648Subject: test
645- """ .format (data [ ' reply_to' ], datetime .datetime .now ().ctime ())
649+ """ .format (reply_to , datetime .datetime .now ().ctime ())
646650 result = process_response_email (message_string )
647651
648- self .assertIsInstance (result ,Message )
652+ self .assertIsInstance (result , Message )
649653 self .assertFalse (event .response_past_due ())
650654
655+ def test_process_response_email_with_invalid_encoding (self ):
656+ """Interesting emails with invalid encoding should be handled"""
657+ reply_to , _ = self .send_ipr_email_helper ()
658+ # test process response
659+ message_string = """To: {}
660+ From: joe@test.com
661+ Date: {}
662+ Subject: test
663+ """ .format (reply_to , datetime .datetime .now ().ctime ())
664+ message_bytes = message_string .encode ('utf8' ) + b'\n Invalid stuff: \xfe \xff \n '
665+ result = process_response_email (message_bytes )
666+ self .assertIsInstance (result , Message )
667+ # \ufffd is a rhombus character with an inverse ?, used to replace invalid characters
668+ self .assertEqual (result .body , 'Invalid stuff: \ufffd \ufffd \n \n ' , # not sure where the extra \n is from
669+ 'Invalid characters should be replaced with \ufffd characters' )
670+
671+ def test_process_response_email_uninteresting_with_invalid_encoding (self ):
672+ """Uninteresting emails with invalid encoding should be quietly dropped"""
673+ self .send_ipr_email_helper ()
674+ addrs = gather_address_lists ('ipr_disclosure_submitted' ).as_strings ()
675+ for message_string in self .uninteresting_ipr_message_strings :
676+ message_bytes = message_string .format (
677+ to = addrs .to ,
678+ cc = addrs .cc ,
679+ date = datetime .datetime .now ().ctime (),
680+ ).encode ('utf8' ) + b'\n Invalid stuff: \xfe \xff \n '
681+ result = process_response_email (message_bytes )
682+ self .assertIsNone (result )
683+
651684 def test_ajax_search (self ):
652685 url = urlreverse ('ietf.ipr.views.ajax_search' )
653686 response = self .client .get (url + '?q=disclosure' )
0 commit comments