示例#1
0
 def test_strWithNegativeCode(self):
     """
     If the response code supplied to L{SMTPClientError} is negative, it
     is excluded from the string representation.
     """
     err = smtp.SMTPClientError(-1, "foo bar")
     self.assertEquals(str(err), "foo bar")
示例#2
0
 def test_str(self):
     """
     The string representation of a L{SMTPClientError} instance includes
     the response code and response string.
     """
     err = smtp.SMTPClientError(123, "some text")
     self.assertEquals(str(err), "123 some text")
示例#3
0
 def test_sendNonFatalError(self):
     """
     If L{smtp.SMTPClient.sendError} is called with an L{SMTPClientError}
     which is not fatal, it sends C{"QUIT"} and waits for the server to
     close the connection.
     """
     client = smtp.SMTPClient(None)
     transport = StringTransport()
     client.makeConnection(transport)
     client.sendError(smtp.SMTPClientError(123, "foo", isFatal=False))
     self.assertEqual(transport.value(), "QUIT\r\n")
     self.assertFalse(transport.disconnecting)
示例#4
0
 def test_sendFatalError(self):
     """
     If L{smtp.SMTPClient.sendError} is called with an L{SMTPClientError}
     which is fatal, it disconnects its transport without writing anything
     more to it.
     """
     client = smtp.SMTPClient(None)
     transport = StringTransport()
     client.makeConnection(transport)
     client.sendError(smtp.SMTPClientError(123, "foo", isFatal=True))
     self.assertEqual(transport.value(), "")
     self.assertTrue(transport.disconnecting)
示例#5
0
 def test_strWithLog(self):
     """
     If a line log is supplied to L{SMTPClientError}, its contents are
     included in the string representation of the exception instance.
     """
     log = LineLog(10)
     log.append("testlog")
     log.append("secondline")
     err = smtp.SMTPClientError(100, "test error", log=log.str())
     self.assertEquals(str(err), "100 test error\n"
                       "testlog\n"
                       "secondline\n")
示例#6
0
    def getMailData(self):
        """Return file-like object containing data of message to be sent.

        Lines in the file should be delimited by '\\n'.
        """
        # Rewind the file in case part of it was read while attempting to
        # send the message.
        if not os.path.exists(self.mailFile.name):
            # content is removed from disk as soon as the mailing is closed
            raise smtp.SMTPClientError(471,
                                       "Sending aborted. Mailing stopped.")
        self.mailFile.seek(0, 0)
        return self.mailFile
示例#7
0
    def getMailFrom(self):
        """Return the email address the mail is from."""
        logging.getLogger("sendmail").debug(
            "[%s] Calling getMailFrom for '%s'", self.factory.targetDomain,
            self.transport.getPeer())

        n = self.factory.getNextEmail()
        if n:
            fromEmail, toEmails, filename, deferred = n
            if not os.path.exists(filename):
                # content is removed from disk as soon as the mailing is closed
                raise smtp.SMTPClientError(
                    471, "Sending aborted. Mailing stopped.")
            self.fromEmail = fromEmail
            self.toEmails = toEmails
            self.mailFile = open(filename, 'rt')
            self.result = deferred
            #WHY? self.result.addBoth(self._removeDeferred)
            return str(self.fromEmail)
        return None
示例#8
0
文件: test_mail.py 项目: mavit/fmn
class EmailBackendTests(Base):
    @mock.patch('fmn.delivery.backends.mail.get_fas_email',
                mock.Mock(return_value='*****@*****.**'))
    @mock.patch('fmn.delivery.backends.mail.smtp.sendmail',
                mock.Mock(side_effect=smtp.SMTPClientError(550, 'boop')))
    def test_deliver_550_matching_email(self):
        """
        Assert when email fails with 550 and the email set matches FAS, the preference is
        disabled.
        """
        user = models.User(openid='jcline.id.fedoraproject.org',
                           openid_url='http://jcline.id.fedoraproject.org')
        self.sess.add(user)
        context = models.Context(name='email',
                                 description='description',
                                 detail_name='email',
                                 icon='wat')
        self.sess.add(context)
        self.sess.commit()
        defaults.create_defaults_for(
            self.sess,
            user,
            detail_values={'email': '*****@*****.**'})
        self.sess.commit()
        preference = models.Preference.query.filter_by(
            context_name='email',
            openid='jcline.id.fedoraproject.org').first()
        preference.enabled = True
        self.sess.add(preference)
        self.sess.commit()
        config = {
            'fmn.email.mailserver': 'localhost:25',
            'fmn.email.from_address': '*****@*****.**',
        }
        backend = mail.EmailBackend(config)
        recipient = {
            'email address': '*****@*****.**',
            'user': '******',
        }

        backend.deliver('my mail', recipient, {})

        preference = models.Preference.query.filter_by(
            context_name='email',
            openid='jcline.id.fedoraproject.org').first()
        self.assertFalse(preference.enabled)

    @mock.patch('fmn.delivery.backends.mail.get_fas_email',
                mock.Mock(return_value='*****@*****.**'))
    @mock.patch('fmn.delivery.backends.mail.smtp.sendmail',
                mock.Mock(side_effect=smtp.SMTPClientError(550, 'boop')))
    def test_deliver_550_new_email(self):
        """
        Assert when email fails with 550 and the email set doesn't match FAS,
        the preference is updated with the new email.
        """
        user = models.User(openid='jcline.id.fedoraproject.org',
                           openid_url='http://jcline.id.fedoraproject.org')
        self.sess.add(user)
        context = models.Context(name='email',
                                 description='description',
                                 detail_name='email',
                                 icon='wat')
        self.sess.add(context)
        self.sess.commit()
        defaults.create_defaults_for(
            self.sess,
            user,
            detail_values={'email': '*****@*****.**'})
        self.sess.commit()
        preference = models.Preference.query.filter_by(
            context_name='email',
            openid='jcline.id.fedoraproject.org').first()
        preference.enabled = True
        self.sess.add(preference)
        self.sess.commit()
        config = {
            'fmn.email.mailserver': 'localhost:25',
            'fmn.email.from_address': '*****@*****.**',
        }
        backend = mail.EmailBackend(config)
        recipient = {
            'email address': '*****@*****.**',
            'user': '******',
        }

        backend.deliver('my mail', recipient, {})

        preference = models.Preference.query.filter_by(
            context_name='email',
            openid='jcline.id.fedoraproject.org').first()
        self.assertTrue(preference.enabled)
        self.assertEqual(u'*****@*****.**',
                         preference.detail_values[0].value)