mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #10355 -- Added an API for pluggable e-mail backends.
Thanks to Andi Albrecht for his work on this patch, and to everyone else that contributed during design and development. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11709 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -2,6 +2,7 @@ import sys, time, os
|
||||
from django.conf import settings
|
||||
from django.db import connection
|
||||
from django.core import mail
|
||||
from django.core.mail.backends import locmem
|
||||
from django.test import signals
|
||||
from django.template import Template
|
||||
from django.utils.translation import deactivate
|
||||
@@ -28,37 +29,22 @@ def instrumented_test_render(self, context):
|
||||
signals.template_rendered.send(sender=self, template=self, context=context)
|
||||
return self.nodelist.render(context)
|
||||
|
||||
class TestSMTPConnection(object):
|
||||
"""A substitute SMTP connection for use during test sessions.
|
||||
The test connection stores email messages in a dummy outbox,
|
||||
rather than sending them out on the wire.
|
||||
|
||||
"""
|
||||
def __init__(*args, **kwargs):
|
||||
pass
|
||||
def open(self):
|
||||
"Mock the SMTPConnection open() interface"
|
||||
pass
|
||||
def close(self):
|
||||
"Mock the SMTPConnection close() interface"
|
||||
pass
|
||||
def send_messages(self, messages):
|
||||
"Redirect messages to the dummy outbox"
|
||||
mail.outbox.extend(messages)
|
||||
return len(messages)
|
||||
|
||||
def setup_test_environment():
|
||||
"""Perform any global pre-test setup. This involves:
|
||||
|
||||
- Installing the instrumented test renderer
|
||||
- Diverting the email sending functions to a test buffer
|
||||
- Set the email backend to the locmem email backend.
|
||||
- Setting the active locale to match the LANGUAGE_CODE setting.
|
||||
"""
|
||||
Template.original_render = Template.render
|
||||
Template.render = instrumented_test_render
|
||||
|
||||
mail.original_SMTPConnection = mail.SMTPConnection
|
||||
mail.SMTPConnection = TestSMTPConnection
|
||||
mail.SMTPConnection = locmem.EmailBackend
|
||||
|
||||
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem'
|
||||
mail.original_email_backend = settings.EMAIL_BACKEND
|
||||
|
||||
mail.outbox = []
|
||||
|
||||
@@ -77,8 +63,10 @@ def teardown_test_environment():
|
||||
mail.SMTPConnection = mail.original_SMTPConnection
|
||||
del mail.original_SMTPConnection
|
||||
|
||||
del mail.outbox
|
||||
settings.EMAIL_BACKEND = mail.original_email_backend
|
||||
del mail.original_email_backend
|
||||
|
||||
del mail.outbox
|
||||
|
||||
def get_runner(settings):
|
||||
test_path = settings.TEST_RUNNER.split('.')
|
||||
|
||||
Reference in New Issue
Block a user