Parent: [98dec2] (diff)

Download this file

test_sendmail.py    172 lines (154 with data), 6.2 kB

  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
# -*- coding: utf-8 -*-
import os
import unittest
from datetime import timedelta
from mock import Mock
from pylons import g, c
import ming
from ming.orm import session, ThreadLocalORMSession
from alluratest.controller import setup_basic_test, setup_global_objects
from allura import model as M
from allura.lib import helpers as h
from forgemail.lib.util import SMTPClient, encode_email_part
from forgemail.reactors import common_react
class TestSendmail(unittest.TestCase):
def setUp(self):
setup_basic_test()
setup_global_objects()
ThreadLocalORMSession.flush_all()
ThreadLocalORMSession.close_all()
self.mail = SMTPClient()
self.mail._client = Mock(spec=[
'sendmail'])
self.mail._client.sendmail = Mock()
def test_addr(self):
self.mail.sendmail(
['test@example.com'],
'test@example.com',
'test@example.com',
'Subject',
'message_id@example.com',
None,
encode_email_part('Test message', 'text/plain'))
assert self.mail._client.sendmail.called
def test_bad_addr(self):
self.mail.sendmail(
['@example.com'],
'test@example.com',
'test@example.com',
'Subject',
'message_id@example.com',
None,
encode_email_part('Test message', 'text/plain'))
assert not self.mail._client.sendmail.called
def test_user(self):
u = M.User.by_username('test-admin')
u.set_pref('display_name', u'Rick Copeland')
u.set_pref('email_address', 'test@example.com')
self.mail.sendmail(
['test@example.com'],
u.email_address_header(),
u.email_address_header(),
'Subject',
'message_id@example.com',
None,
encode_email_part('Test message', 'text/plain'))
assert self.mail._client.sendmail.called
args, kwargs = self.mail._client.sendmail.call_args
assert args[0] == 'noreply@sourceforge.net'
assert '"Rick Copeland" <test@example.com>' in args[2]
def test_user_unicode(self):
u = M.User.by_username('test-admin')
u.set_pref('display_name', u'Rick Cop��land')
u.set_pref('email_address', 'test@example.com')
self.mail.sendmail(
['test@example.com'],
u.email_address_header(),
u.email_address_header(),
'Subject',
'message_id@example.com',
None,
encode_email_part('Test message', 'text/plain'))
assert self.mail._client.sendmail.called
args, kwargs = self.mail._client.sendmail.call_args
assert args[0] == 'noreply@sourceforge.net'
assert '=?utf-8?q?=22Rick_Cop=C3=A9land=22?= <test@example.com>' in args[2]
class TestReactor(unittest.TestCase):
def setUp(self):
setup_basic_test()
setup_global_objects()
ThreadLocalORMSession.flush_all()
ThreadLocalORMSession.close_all()
common_react.smtp_client._client = Mock(spec=[
'sendmail'])
common_react.smtp_client._client.sendmail = self.sendmail = Mock()
def test_send_mail(self):
addr = 'test@example.com'
common_react.send_email(None, {
'from':addr,
'reply_to':addr,
'destinations':[addr],
'message_id':'test@example.com',
'subject':'Test message',
'text':'Test message'})
assert self.sendmail.called
args, kwargs = self.sendmail.call_args
(from_addr, ids, msg) = args
assert 'Content-Type: text/plain; charset="iso-8859-1"\n' in msg, msg
assert 'Subject: =?iso-8859-1?q?Test?= =?iso-8859-1?q?message?=\n' in msg, msg
def test_send_mail_unicode(self):
addr = 'test@example.com'
common_react.send_email(None, {
'from':addr,
'reply_to':addr,
'destinations':[addr],
'message_id':'test@example.com',
'subject': u'Test ��� message',
'text': u'Test ��� message'})
assert self.sendmail.called
args, kwargs = self.sendmail.call_args
(from_addr, ids, msg) = args
assert 'Content-Type: text/plain; charset="utf-8"\n' in msg, msg
assert 'Subject: Test =?utf-8?b?4peO?= message\n' in msg, msg
def test_user(self):
u = M.User.by_username('test-admin')
u.set_pref('display_name', u'Rick Copeland')
u.set_pref('email_address', 'test@example.com')
addr = str(u._id)
common_react.send_email(None, {
'from':addr,
'reply_to':addr,
'destinations':[addr],
'message_id':'test@example.com',
'subject':'Test message',
'text':'Test message'})
assert self.sendmail.called
args, kwargs = self.sendmail.call_args
assert args[0] == 'noreply@sourceforge.net'
assert '"Rick Copeland" <test@example.com>' in args[2]
def test_bad_user(self):
addr = 'test@example.com'
common_react.send_email(None, {
'from':addr,
'reply_to':addr,
'destinations':[None],
'message_id':'test@example.com',
'subject':'Test message',
'text':'Test message'})
assert not self.sendmail.called
def test_user_unicode(self):
u = M.User.by_username('test-admin')
u.set_pref('display_name', u'Rick Cop��land')
u.set_pref('email_address', 'test@example.com')
addr = str(u._id)
common_react.send_email(None, {
'from':addr,
'reply_to':addr,
'destinations':[addr],
'message_id':'test@example.com',
'subject':'Test message',
'text':'Test message'})
assert self.sendmail.called
args, kwargs = self.sendmail.call_args
assert args[0] == 'noreply@sourceforge.net'
assert '=?utf-8?q?=22Rick_Cop=C3=A9land=22?= <test@example.com>' in args[2]