コード例 #1
0
ファイル: message.py プロジェクト: dax/jmc
 def setUp(self):
     self.handler = SendMailMessageHandler(None)
コード例 #2
0
ファイル: message.py プロジェクト: dax/jmc
class SendMailMessageHandler_TestCase(unittest.TestCase):
    def setUp(self):
        self.handler = SendMailMessageHandler(None)

    def test_get_email_to_headers_from_message(self):
        to_regexp = re.compile("^\s*(?i)to\s*:\s*(?P<to_email>.*)")
        (message, to_header) = self.handler.get_email_headers_from_message(\
            "To: [email protected]\ntest body\n", [to_regexp], ["to_email"])
        self.assertEquals(message, "test body\n")
        self.assertEquals(to_header, ["*****@*****.**"])

    def test_get_email_headers_from_message(self):
        to_regexp = re.compile("^\s*(?i)to\s*:\s*(?P<to_email>.*)")
        cc_regexp = re.compile("^\s*(?i)cc\s*:\s*(?P<cc_email>.*)")
        bcc_regexp = re.compile("^\s*(?i)bcc\s*:\s*(?P<bcc_email>.*)")
        subject_regexp = re.compile("^\s*(?i)subject\s*:\s*(?P<subject_email>.*)")
        (message, headers) = self.handler.get_email_headers_from_message(\
            "To: [email protected]\nCc: [email protected]\n"
            + "Bcc: [email protected]\n"
            + "Subject: test subject\ntest body\n",
            [to_regexp, cc_regexp, bcc_regexp, subject_regexp],
            ["to_email", "cc_email", "bcc_email", "subject_email"])
        self.assertEquals(message, "test body\n")
        self.assertEquals(headers, ["*****@*****.**",
                                    "*****@*****.**",
                                    "*****@*****.**",
                                    "test subject"])

    def test_get_email_headers_from_message_unordered(self):
        to_regexp = re.compile("^\s*(?i)to\s*:\s*(?P<to_email>.*)")
        cc_regexp = re.compile("^\s*(?i)cc\s*:\s*(?P<cc_email>.*)")
        bcc_regexp = re.compile("^\s*(?i)bcc\s*:\s*(?P<bcc_email>.*)")
        subject_regexp = re.compile("^\s*(?i)subject\s*:\s*(?P<subject_email>.*)")
        (message, headers) = self.handler.get_email_headers_from_message(\
            "To: [email protected]\nCc: [email protected]\n"
            + "Bcc: [email protected]\n"
            + "Subject: test subject\ntest body\n",
            [cc_regexp, to_regexp, subject_regexp, bcc_regexp],
            ["cc_email", "to_email", "subject_email", "bcc_email"])
        self.assertEquals(message, "test body\n")
        self.assertEquals(headers, ["*****@*****.**",
                                    "*****@*****.**",
                                    "test subject",
                                    "*****@*****.**"])

    def test_handle(self):
        mock_account = MockSMTPAccount()
        message = Message(from_jid="*****@*****.**",
                          to_jid="*****@*****.**",
                          subject="real subject",
                          body="To: [email protected]\nCc: [email protected]\n" \
                              + "Bcc: [email protected]\n" \
                              + "Subject: test subject\ntest body\n")
        result = self.handler.handle(\
            message, Lang.en, [mock_account])
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].stanza_type, "message")
        self.assertEquals(result[0].get_from(), "*****@*****.**")
        self.assertEquals(result[0].get_to(), "*****@*****.**")
        self.assertEquals(result[0].get_subject(),
                          Lang.en.send_mail_ok_subject)
        self.assertEquals(result[0].get_body(),
                          Lang.en.send_mail_ok_body % \
                              ("[email protected], [email protected]"))
        self.assertEquals(mock_account.email_sent, 1)
        self.assertEquals(mock_account.email[0], "*****@*****.**")
        self.assertEquals(mock_account.email[1], "[email protected], [email protected]")
        self.assertEquals(mock_account.email[2], "real subject")
        self.assertEquals(mock_account.email[3], "test body\n")
        self.assertEquals(mock_account.email[4], {u"Bcc": "*****@*****.**",
                                                  u"Cc": "*****@*****.**"})

    def test_handle_ignore_empty_message_body(self):
        mock_account = MockSMTPAccount()
        message = Message(from_jid="*****@*****.**",
                          to_jid="*****@*****.**",
                          subject="real subject",
                          body=None)
        result = self.handler.handle(\
            message, Lang.en, [mock_account])
        self.assertEquals(len(result), 0)