~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/mail/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
Import upstream version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# coding: utf-8
 
2
 
2
3
r"""
3
4
# Tests for the django.core.mail.
4
5
 
 
6
>>> import os
 
7
>>> import shutil
 
8
>>> import tempfile
 
9
>>> from StringIO import StringIO
5
10
>>> from django.conf import settings
6
11
>>> from django.core import mail
7
12
>>> from django.core.mail import EmailMessage, mail_admins, mail_managers, EmailMultiAlternatives
 
13
>>> from django.core.mail import send_mail, send_mass_mail
 
14
>>> from django.core.mail.backends.base import BaseEmailBackend
 
15
>>> from django.core.mail.backends import console, dummy, locmem, filebased, smtp
8
16
>>> from django.utils.translation import ugettext_lazy
9
17
 
10
18
# Test normal ascii character case:
85
93
>>> mail_managers('hi','there')
86
94
>>> len(mail.outbox)
87
95
1
88
 
>>> settings.ADMINS = old_admins
89
 
>>> settings.MANAGERS = old_managers
90
96
 
91
97
# Make sure we can manually set the From header (#9214)
92
98
 
95
101
>>> message['From']
96
102
'from@example.com'
97
103
 
 
104
# Regression for #13259 - Make sure that headers are not changed
 
105
# when calling EmailMessage.message()
 
106
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
107
>>> message = email.message()
 
108
>>> message['From']
 
109
'from@example.com'
 
110
>>> message = email.message()
 
111
>>> message['From']
 
112
'from@example.com'
 
113
 
 
114
# Regression for #11144 - When a to/from/cc header contains unicode,
 
115
# make sure the email addresses are parsed correctly (especially
 
116
# with regards to commas)
 
117
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['"Firstname Sürname" <to@example.com>','other@example.com'])
 
118
>>> email.message()['To']
 
119
'=?utf-8?q?Firstname_S=C3=BCrname?= <to@example.com>, other@example.com'
 
120
 
 
121
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['"Sürname, Firstname" <to@example.com>','other@example.com'])
 
122
>>> email.message()['To']
 
123
'=?utf-8?q?S=C3=BCrname=2C_Firstname?= <to@example.com>, other@example.com'
 
124
 
 
125
# Regression for #6918 - When a header contains unicode,
 
126
# make sure headers can be set with a different encoding than utf-8
 
127
>>> email = EmailMessage('Message from Firstname Sürname', 'Content', 'from@example.com', ['"Sürname, Firstname" <to@example.com>','other@example.com'])
 
128
>>> email.encoding = 'iso-8859-1'
 
129
>>> email.message()['To']
 
130
'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <to@example.com>, other@example.com'
 
131
>>> email.message()['Subject'].encode()
 
132
u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?='
 
133
 
 
134
# Make sure headers can be set with a different encoding than utf-8 in SafeMIMEMultipart as well
 
135
>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
 
136
>>> subject, from_email, to = 'hello', 'from@example.com', '"Sürname, Firstname" <to@example.com>'
 
137
>>> text_content = 'This is an important message.'
 
138
>>> html_content = '<p>This is an <strong>important</strong> message.</p>'
 
139
>>> msg = EmailMultiAlternatives('Message from Firstname Sürname', text_content, from_email, [to], headers=headers)
 
140
>>> msg.attach_alternative(html_content, "text/html")
 
141
>>> msg.encoding = 'iso-8859-1'
 
142
>>> msg.message()['To']
 
143
'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <to@example.com>'
 
144
>>> msg.message()['Subject'].encode()
 
145
u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?='
 
146
 
 
147
# Regression for #12791  - Encode body correctly with other encodings than utf-8
 
148
>>> email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'from@example.com', ['other@example.com'])
 
149
>>> email.encoding = 'iso-8859-1'
 
150
>>> message = email.message()
 
151
>>> message.as_string()
 
152
'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com\nDate: ...\nMessage-ID: <...>\n\nFirstname S=FCrname is a great guy.'
 
153
 
 
154
# Make sure MIME attachments also works correctly with other encodings than utf-8
 
155
>>> text_content = 'Firstname Sürname is a great guy.'
 
156
>>> html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>'
 
157
>>> msg = EmailMultiAlternatives('Subject', text_content, 'from@example.com', ['to@example.com'])
 
158
>>> msg.encoding = 'iso-8859-1'
 
159
>>> msg.attach_alternative(html_content, "text/html")
 
160
>>> msg.message().get_payload(0).as_string()
 
161
'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.'
 
162
>>> msg.message().get_payload(1).as_string()
 
163
'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>'
 
164
 
98
165
# Handle attachments within an multipart/alternative mail correctly (#9367)
99
166
# (test is not as precise/clear as it could be w.r.t. email tree structure,
100
167
#  but it's good enough.)
101
 
 
102
168
>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
103
169
>>> subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
104
170
>>> text_content = 'This is an important message.'
138
204
JVBERi0xLjQuJS4uLg==
139
205
...
140
206
 
 
207
# Make sure that the console backend writes to stdout by default
 
208
>>> connection = console.EmailBackend()
 
209
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
210
>>> connection.send_messages([email])
 
211
Content-Type: text/plain; charset="utf-8"
 
212
MIME-Version: 1.0
 
213
Content-Transfer-Encoding: quoted-printable
 
214
Subject: Subject
 
215
From: from@example.com
 
216
To: to@example.com
 
217
Date: ...
 
218
Message-ID: ...
 
219
 
 
220
Content
 
221
-------------------------------------------------------------------------------
 
222
1
 
223
 
 
224
# Test that the console backend can be pointed at an arbitrary stream
 
225
>>> s = StringIO()
 
226
>>> connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s)
 
227
>>> send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
 
228
1
 
229
>>> print s.getvalue()
 
230
Content-Type: text/plain; charset="utf-8"
 
231
MIME-Version: 1.0
 
232
Content-Transfer-Encoding: quoted-printable
 
233
Subject: Subject
 
234
From: from@example.com
 
235
To: to@example.com
 
236
Date: ...
 
237
Message-ID: ...
 
238
 
 
239
Content
 
240
-------------------------------------------------------------------------------
 
241
 
 
242
# Make sure that dummy backends returns correct number of sent messages
 
243
>>> connection = dummy.EmailBackend()
 
244
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
245
>>> connection.send_messages([email, email, email])
 
246
3
 
247
 
 
248
# Make sure that locmen backend populates the outbox
 
249
>>> mail.outbox = []
 
250
>>> connection = locmem.EmailBackend()
 
251
>>> email1 = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
252
>>> email2 = EmailMessage('Subject 2', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
253
>>> connection.send_messages([email1, email2])
 
254
2
 
255
>>> len(mail.outbox)
 
256
2
 
257
>>> mail.outbox[0].subject
 
258
'Subject'
 
259
>>> mail.outbox[1].subject
 
260
'Subject 2'
 
261
 
 
262
# Make sure that multiple locmem connections share mail.outbox
 
263
>>> mail.outbox = []
 
264
>>> connection1 = locmem.EmailBackend()
 
265
>>> connection2 = locmem.EmailBackend()
 
266
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
267
>>> connection1.send_messages([email])
 
268
1
 
269
>>> connection2.send_messages([email])
 
270
1
 
271
>>> len(mail.outbox)
 
272
2
 
273
 
 
274
# Make sure that the file backend write to the right location
 
275
>>> tmp_dir = tempfile.mkdtemp()
 
276
>>> connection = filebased.EmailBackend(file_path=tmp_dir)
 
277
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
278
>>> connection.send_messages([email])
 
279
1
 
280
>>> len(os.listdir(tmp_dir))
 
281
1
 
282
>>> print open(os.path.join(tmp_dir, os.listdir(tmp_dir)[0])).read()
 
283
Content-Type: text/plain; charset="utf-8"
 
284
MIME-Version: 1.0
 
285
Content-Transfer-Encoding: quoted-printable
 
286
Subject: Subject
 
287
From: from@example.com
 
288
To: to@example.com
 
289
Date: ...
 
290
Message-ID: ...
 
291
 
 
292
Content
 
293
-------------------------------------------------------------------------------
 
294
 
 
295
>>> connection2 = filebased.EmailBackend(file_path=tmp_dir)
 
296
>>> connection2.send_messages([email])
 
297
1
 
298
>>> len(os.listdir(tmp_dir))
 
299
2
 
300
>>> connection.send_messages([email])
 
301
1
 
302
>>> len(os.listdir(tmp_dir))
 
303
2
 
304
>>> email.connection = filebased.EmailBackend(file_path=tmp_dir)
 
305
>>> connection_created = connection.open()
 
306
>>> num_sent = email.send()
 
307
>>> len(os.listdir(tmp_dir))
 
308
3
 
309
>>> num_sent = email.send()
 
310
>>> len(os.listdir(tmp_dir))
 
311
3
 
312
>>> connection.close()
 
313
>>> shutil.rmtree(tmp_dir)
 
314
 
 
315
# Make sure that get_connection() accepts arbitrary keyword that might be
 
316
# used with custom backends.
 
317
>>> c = mail.get_connection(fail_silently=True, foo='bar')
 
318
>>> c.fail_silently
 
319
True
 
320
 
 
321
# Test custom backend defined in this suite.
 
322
>>> conn = mail.get_connection('regressiontests.mail.custombackend.EmailBackend')
 
323
>>> hasattr(conn, 'test_outbox')
 
324
True
 
325
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
 
326
>>> conn.send_messages([email])
 
327
1
 
328
>>> len(conn.test_outbox)
 
329
1
 
330
 
 
331
# Test backend argument of mail.get_connection()
 
332
>>> isinstance(mail.get_connection('django.core.mail.backends.smtp.EmailBackend'), smtp.EmailBackend)
 
333
True
 
334
>>> isinstance(mail.get_connection('django.core.mail.backends.locmem.EmailBackend'), locmem.EmailBackend)
 
335
True
 
336
>>> isinstance(mail.get_connection('django.core.mail.backends.dummy.EmailBackend'), dummy.EmailBackend)
 
337
True
 
338
>>> isinstance(mail.get_connection('django.core.mail.backends.console.EmailBackend'), console.EmailBackend)
 
339
True
 
340
>>> tmp_dir = tempfile.mkdtemp()
 
341
>>> isinstance(mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir), filebased.EmailBackend)
 
342
True
 
343
>>> shutil.rmtree(tmp_dir)
 
344
>>> isinstance(mail.get_connection(), locmem.EmailBackend)
 
345
True
 
346
 
 
347
# Test connection argument of send_mail() et al
 
348
>>> connection = mail.get_connection('django.core.mail.backends.console.EmailBackend')
 
349
>>> send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
 
350
Content-Type: text/plain; charset="utf-8"
 
351
MIME-Version: 1.0
 
352
Content-Transfer-Encoding: quoted-printable
 
353
Subject: Subject
 
354
From: from@example.com
 
355
To: to@example.com
 
356
Date: ...
 
357
Message-ID: ...
 
358
 
 
359
Content
 
360
-------------------------------------------------------------------------------
 
361
1
 
362
 
 
363
>>> send_mass_mail([
 
364
...         ('Subject1', 'Content1', 'from1@example.com', ['to1@example.com']),
 
365
...         ('Subject2', 'Content2', 'from2@example.com', ['to2@example.com'])
 
366
...     ], connection=connection)
 
367
Content-Type: text/plain; charset="utf-8"
 
368
MIME-Version: 1.0
 
369
Content-Transfer-Encoding: quoted-printable
 
370
Subject: Subject1
 
371
From: from1@example.com
 
372
To: to1@example.com
 
373
Date: ...
 
374
Message-ID: ...
 
375
 
 
376
Content1
 
377
-------------------------------------------------------------------------------
 
378
Content-Type: text/plain; charset="utf-8"
 
379
MIME-Version: 1.0
 
380
Content-Transfer-Encoding: quoted-printable
 
381
Subject: Subject2
 
382
From: from2@example.com
 
383
To: to2@example.com
 
384
Date: ...
 
385
Message-ID: ...
 
386
 
 
387
Content2
 
388
-------------------------------------------------------------------------------
 
389
2
 
390
 
 
391
>>> mail_admins('Subject', 'Content', connection=connection)
 
392
Content-Type: text/plain; charset="utf-8"
 
393
MIME-Version: 1.0
 
394
Content-Transfer-Encoding: quoted-printable
 
395
Subject: [Django] Subject
 
396
From: root@localhost
 
397
To: nobody@example.com
 
398
Date: ...
 
399
Message-ID: ...
 
400
 
 
401
Content
 
402
-------------------------------------------------------------------------------
 
403
 
 
404
>>> mail_managers('Subject', 'Content', connection=connection)
 
405
Content-Type: text/plain; charset="utf-8"
 
406
MIME-Version: 1.0
 
407
Content-Transfer-Encoding: quoted-printable
 
408
Subject: [Django] Subject
 
409
From: root@localhost
 
410
To: nobody@example.com
 
411
Date: ...
 
412
Message-ID: ...
 
413
 
 
414
Content
 
415
-------------------------------------------------------------------------------
 
416
 
 
417
>>> settings.ADMINS = old_admins
 
418
>>> settings.MANAGERS = old_managers
 
419
 
141
420
"""