예제 #1
0
파일: message.py 프로젝트: dax/jmc
 def setUp(self):
     JCLTestCase.setUp(self, tables=[Account, GlobalSMTPAccount,
                                     AbstractSMTPAccount,
                                     SMTPAccount, User])
     self.handler = RootSendMailMessageHandler(None)
예제 #2
0
파일: message.py 프로젝트: dax/jmc
class RootSendMailMessageHandler_TestCase(JCLTestCase):
    def setUp(self):
        JCLTestCase.setUp(self, tables=[Account, GlobalSMTPAccount,
                                        AbstractSMTPAccount,
                                        SMTPAccount, User])
        self.handler = RootSendMailMessageHandler(None)

    def test_filter(self):
        user1 = User(jid="*****@*****.**")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="*****@*****.**")
        account11.default_account = True
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="*****@*****.**")
        message = Message(from_jid="*****@*****.**",
                          to_jid="*****@*****.**",
                          body="message")
        accounts = self.handler.filter(message, None)
        self.assertEquals(accounts.count(), 1)

    def test_filter_no_default_account(self):
        user1 = User(jid="*****@*****.**")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="*****@*****.**")
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="*****@*****.**")
        message = Message(from_jid="*****@*****.**",
                          to_jid="*****@*****.**",
                          body="message")
        accounts = self.handler.filter(message, None)
        self.assertEquals(accounts.count(), 2)
        self.assertEquals(accounts[0].name, "account11")

    def test_filter_wrong_dest(self):
        user1 = User(jid="*****@*****.**")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="*****@*****.**")
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="*****@*****.**")
        message = Message(from_jid="*****@*****.**",
                          to_jid="*****@*****.**",
                          body="message")
        accounts = self.handler.filter(message, None)
        self.assertEquals(accounts.count(), 2)

    def test_filter_wrong_user(self):
        user1 = User(jid="*****@*****.**")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="*****@*****.**")
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="*****@*****.**")
        message = Message(from_jid="*****@*****.**",
                          to_jid="*****@*****.**",
                          body="message")
        accounts = self.handler.filter(message, None)
        self.assertEquals(accounts.count(), 0)

    def test_handle(self):
        mock_account = MockSMTPAccount()
        message = Message(from_jid="*****@*****.**",
                          to_jid="jmc.test.com",
                          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].get_type(), None)
        self.assertEquals(result[0].get_from(), "jmc.test.com")
        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 % ("*****@*****.**"))
        self.assertEquals(mock_account.email_sent, 1)
        self.assertEquals(mock_account.email[0], "*****@*****.**")
        self.assertEquals(mock_account.email[1], "*****@*****.**")
        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="jmc.test.com",
                          subject="real subject",
                          body=None)
        result = self.handler.handle(\
            message, Lang.en, [mock_account])
        self.assertEquals(len(result), 0)

    def test_handle_email_not_found_in_header(self):
        message = Message(from_jid="*****@*****.**",
                          to_jid="jmc.test.com",
                          subject="message subject",
                          body="message body")
        accounts = [MockSMTPAccount()]
        result = self.handler.handle(message, Lang.en, accounts)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].get_type(), "error")
        self.assertEquals(result[0].get_from(), "jmc.test.com")
        self.assertEquals(result[0].get_to(), "*****@*****.**")
        self.assertEquals(result[0].get_subject(),
                          Lang.en.send_mail_error_no_to_header_subject)
        self.assertEquals(result[0].get_body(),
                          Lang.en.send_mail_error_no_to_header_body)

    def test_handle_no_jabber_subject(self):
        mock_account = MockSMTPAccount()
        message = Message(from_jid="*****@*****.**",
                          to_jid="jmc.test.com",
                          subject="",
                          body="To: [email protected]\nCc: [email protected]\n" \
                              + "Bcc: [email protected]\n" \
                              + "Subject: test subject\ntest body\n")
        message_to_send = self.handler.handle(\
            message, Lang.en, [mock_account])
        self.assertNotEquals(message_to_send, None)
        self.assertEquals(mock_account.email_sent, 1)
        self.assertEquals(mock_account.email[0], "*****@*****.**")
        self.assertEquals(mock_account.email[1], "*****@*****.**")
        self.assertEquals(mock_account.email[2], "test subject")
        self.assertEquals(mock_account.email[3], "test body\n")
        self.assertEquals(mock_account.email[4], {u"Bcc": "*****@*****.**",
                                                  u"Cc": "*****@*****.**"})