Ejemplo n.º 1
0
    def scenario_does_not_use_auth(self, smtp_user):
        email = Email(from_addr='*****@*****.**', to_addr='*****@*****.**')

        send_email(email,
                   'smtp.domain.com',
                   25,
                   smtp_user=smtp_user,
                   smtp_pass='******')

        self.assertFalse(self.server.starttls.called)
        self.assertFalse(self.server.login.called)
def send_email_for_commit_if_needed(config, db, commit):
    """Sends an email for the given commit, depending on several conditions.
    """
    if not config['email'].getboolean('enabled'):
        # Sending of emails is disabled.
        return

    if db.email_sent_for_commit(commit):
        # The email has already been sent.
        return

    commit_results = db.get_results_for_commit(commit)
    if (not commit_results.build_has_failed() and
            not commit_results.has_failed_tests()):
        # There is nothing to report.
        return

    if db.topmost_commit_has_succeeded():
        # There is no need to send the email if the topmost commit has
        # succeeded.
        return

    email = prepare_email(
        commit_results,
        config['email']['subject_prefix'],
        config['web']['main_page_url'],
        config['web']['wiki_page_url']
    )

    # Set proper addresses.
    def set_addr_if_defined(addr_type):
        addr_name = '{}_addr'.format(addr_type)
        addr = config['email'][addr_name]
        if addr:
            setattr(email, addr_name, addr)
    set_addr_if_defined('from')
    set_addr_if_defined('to')
    set_addr_if_defined('reply_to')
    set_addr_if_defined('cc')

    # Send the email.
    logging.info("sending email to '{}' concerning commit {}".format(
        email.to_addr, commit.short_hash()))
    send_email(
        email,
        config['email']['smtp_server'],
        config['email']['smtp_port'],
        config['email']['smtp_user'],
        config['email']['smtp_pass']
    )
    db.insert_email_for_commit(email, commit)
Ejemplo n.º 3
0
    def test_sends_email_correctly(self):
        FROM_ADDR = '*****@*****.**'
        TO_ADDR = '*****@*****.**'
        email = Email(from_addr=FROM_ADDR, to_addr=TO_ADDR)
        SMTP_SERVER = 'smtp.domain.com'
        SMTP_PORT = 25
        SMTP_USER = '******'
        SMTP_PASS = '******'

        send_email(email, SMTP_SERVER, SMTP_PORT, SMTP_USER, SMTP_PASS)

        self.smtplib_SMTP_mock.assert_called_once_with(SMTP_SERVER, SMTP_PORT)
        self.server.starttls.assert_called_once_with()
        self.server.login.assert_called_once_with(SMTP_USER, SMTP_PASS)
        self.server.sendmail.assert_called_once_with(FROM_ADDR, [TO_ADDR],
                                                     email.as_string)
        self.server.quit.assert_called_once_with()