Files
@ e63bcce18fef
Branch filter:
Location: kallithea/kallithea/tests/other/test_mail.py
e63bcce18fef
7.6 KiB
text/x-python
py3: automatic migration with 2to3 -f unicode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | # -*- coding: utf-8 -*-
import mock
import kallithea
from kallithea.model.db import User
from kallithea.tests import base
class smtplib_mock(object):
@classmethod
def SMTP(cls, server, port, local_hostname):
return smtplib_mock()
def ehlo(self):
pass
def quit(self):
pass
def sendmail(self, sender, dest, msg):
smtplib_mock.lastsender = sender
smtplib_mock.lastdest = dest
smtplib_mock.lastmsg = msg
pass
@mock.patch('kallithea.lib.rcmail.smtp_mailer.smtplib', smtplib_mock)
class TestMail(base.TestController):
def test_send_mail_trivial(self):
mailserver = 'smtp.mailserver.org'
recipients = ['rcpt1', 'rcpt2']
envelope_from = 'noreply@mailserver.org'
subject = 'subject'
body = 'body'
html_body = 'html_body'
config_mock = {
'smtp_server': mailserver,
'app_email_from': envelope_from,
}
with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body)
assert smtplib_mock.lastdest == set(recipients)
assert smtplib_mock.lastsender == envelope_from
assert 'From: %s' % envelope_from in smtplib_mock.lastmsg
assert 'Subject: %s' % subject in smtplib_mock.lastmsg
assert body in smtplib_mock.lastmsg
assert html_body in smtplib_mock.lastmsg
def test_send_mail_no_recipients(self):
mailserver = 'smtp.mailserver.org'
recipients = []
envelope_from = 'noreply@mailserver.org'
email_to = 'admin@mailserver.org'
subject = 'subject'
body = 'body'
html_body = 'html_body'
config_mock = {
'smtp_server': mailserver,
'app_email_from': envelope_from,
'email_to': email_to,
}
with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body)
assert smtplib_mock.lastdest == set([base.TEST_USER_ADMIN_EMAIL, email_to])
assert smtplib_mock.lastsender == envelope_from
assert 'From: %s' % envelope_from in smtplib_mock.lastmsg
assert 'Subject: %s' % subject in smtplib_mock.lastmsg
assert body in smtplib_mock.lastmsg
assert html_body in smtplib_mock.lastmsg
def test_send_mail_no_recipients_multiple_email_to(self):
mailserver = 'smtp.mailserver.org'
recipients = []
envelope_from = 'noreply@mailserver.org'
email_to = 'admin@mailserver.org,admin2@example.com'
subject = 'subject'
body = 'body'
html_body = 'html_body'
config_mock = {
'smtp_server': mailserver,
'app_email_from': envelope_from,
'email_to': email_to,
}
with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body)
assert smtplib_mock.lastdest == set([base.TEST_USER_ADMIN_EMAIL] + email_to.split(','))
assert smtplib_mock.lastsender == envelope_from
assert 'From: %s' % envelope_from in smtplib_mock.lastmsg
assert 'Subject: %s' % subject in smtplib_mock.lastmsg
assert body in smtplib_mock.lastmsg
assert html_body in smtplib_mock.lastmsg
def test_send_mail_no_recipients_no_email_to(self):
mailserver = 'smtp.mailserver.org'
recipients = []
envelope_from = 'noreply@mailserver.org'
subject = 'subject'
body = 'body'
html_body = 'html_body'
config_mock = {
'smtp_server': mailserver,
'app_email_from': envelope_from,
}
with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body)
assert smtplib_mock.lastdest == set([base.TEST_USER_ADMIN_EMAIL])
assert smtplib_mock.lastsender == envelope_from
assert 'From: %s' % envelope_from in smtplib_mock.lastmsg
assert 'Subject: %s' % subject in smtplib_mock.lastmsg
assert body in smtplib_mock.lastmsg
assert html_body in smtplib_mock.lastmsg
def test_send_mail_with_author(self):
mailserver = 'smtp.mailserver.org'
recipients = ['rcpt1', 'rcpt2']
envelope_from = 'noreply@mailserver.org'
subject = 'subject'
body = 'body'
html_body = 'html_body'
author = User.get_by_username(base.TEST_USER_REGULAR_LOGIN)
config_mock = {
'smtp_server': mailserver,
'app_email_from': envelope_from,
}
with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, author=author)
assert smtplib_mock.lastdest == set(recipients)
assert smtplib_mock.lastsender == envelope_from
assert 'From: "Kallithea Admin (no-reply)" <%s>' % envelope_from in smtplib_mock.lastmsg
assert 'Subject: %s' % subject in smtplib_mock.lastmsg
assert body in smtplib_mock.lastmsg
assert html_body in smtplib_mock.lastmsg
def test_send_mail_with_author_full_mail_from(self):
mailserver = 'smtp.mailserver.org'
recipients = ['ræcpt1', 'receptor2 <rcpt2@example.com>', 'tæst@example.com', 'Tæst <test@example.com>']
envelope_addr = 'noreply@mailserver.org'
envelope_from = 'Söme Næme <%s>' % envelope_addr
subject = 'subject'
body = 'body'
html_body = 'html_body'
author = User.get_by_username(base.TEST_USER_REGULAR_LOGIN)
config_mock = {
'smtp_server': mailserver,
'app_email_from': envelope_from,
}
with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, author=author)
assert smtplib_mock.lastdest == set(recipients)
assert smtplib_mock.lastsender == envelope_from
assert 'From: "Kallithea Admin (no-reply)" <%s>' % envelope_addr in smtplib_mock.lastmsg
assert 'Subject: %s' % subject in smtplib_mock.lastmsg
assert body in smtplib_mock.lastmsg
assert html_body in smtplib_mock.lastmsg
def test_send_mail_extra_headers(self):
mailserver = 'smtp.mailserver.org'
recipients = ['rcpt1', 'rcpt2']
envelope_from = 'noreply@mailserver.org'
subject = 'subject'
body = 'body'
html_body = 'html_body'
author = User(name='foo', lastname='(fubar) "baz"')
headers = {'extra': 'yes'}
config_mock = {
'smtp_server': mailserver,
'app_email_from': envelope_from,
}
with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body,
author=author, headers=headers)
assert smtplib_mock.lastdest == set(recipients)
assert smtplib_mock.lastsender == envelope_from
assert r'From: "foo (fubar) \"baz\" (no-reply)" <%s>' % envelope_from in smtplib_mock.lastmsg
assert 'Subject: %s' % subject in smtplib_mock.lastmsg
assert body in smtplib_mock.lastmsg
assert html_body in smtplib_mock.lastmsg
assert 'Extra: yes' in smtplib_mock.lastmsg
# verify that headers dict hasn't mutated by send_email
assert headers == {'extra': 'yes'}
|