-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from lorin/django-mail-queue
Add Django email testing support
- Loading branch information
Showing
10 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
Email backend that sends mails to a multiprocessing queue | ||
""" | ||
from lettuce.django import mail | ||
from django.core.mail.backends.base import BaseEmailBackend | ||
|
||
|
||
class QueueEmailBackend(BaseEmailBackend): | ||
|
||
def send_messages(self, messages): | ||
for message in messages: | ||
mail.queue.put(message) | ||
return len(messages) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test: | ||
PYTHONPATH=`pwd` DJANGO_SETTINGS_MODULE=djangoapp django-admin.py harvest email.feature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" | ||
A minimal Django app, just one file. | ||
See: http://olifante.blogs.com/covil/2010/04/minimal-django.html | ||
""" | ||
import os | ||
from django.conf.urls.defaults import patterns | ||
from django.core.mail import send_mail | ||
|
||
from django.http import HttpResponse | ||
filepath, extension = os.path.splitext(__file__) | ||
ROOT_URLCONF = os.path.basename(filepath) | ||
INSTALLED_APPS = ( | ||
"lettuce.django" | ||
) | ||
|
||
|
||
def mail(request): | ||
send_mail('Subject here', 'Here is the message.', 'from@example.com', | ||
['to@example.com'], fail_silently=False) | ||
return HttpResponse('Mail has been sent') | ||
|
||
urlpatterns = patterns('', (r'^mail/$', mail)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Feature: Check email sent by Django server | ||
|
||
Scenario: Access a web page which triggers an email | ||
Given I visit "/mail/" | ||
Then I see "Mail has been sent" | ||
and an email is sent to "to@example.com" with subject "Subject here" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from lettuce import world, step | ||
from lettuce.django import mail | ||
from lettuce.django import django_url | ||
|
||
from nose.tools import assert_equals | ||
|
||
|
||
@step(u'I visit "([^"]*)"') | ||
def visit(step, url): | ||
world.browser.visit(django_url(url)) | ||
|
||
|
||
@step(u'I see "([^"]*)"') | ||
def see(step, text): | ||
assert world.browser.is_text_present(text) | ||
|
||
|
||
@step(u'an email is sent to "([^"]*?)" with subject "([^"]*)"') | ||
def email_sent(step, to, subject): | ||
message = mail.queue.get(True, timeout=5) | ||
assert_equals(message.subject, subject) | ||
assert_equals(message.recipients(), [to]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from splinter.browser import Browser | ||
from lettuce import before, after, world | ||
|
||
|
||
@before.harvest | ||
def setup(server): | ||
world.browser = Browser() | ||
|
||
|
||
@after.harvest | ||
def cleanup(server): | ||
world.browser.quit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
import sys | ||
import commands | ||
|
||
from tests.asserts import assert_not_equals | ||
from lettuce.fs import FileSystem | ||
|
||
current_directory = FileSystem.dirname(__file__) | ||
|
||
OLD_PYTHONPATH = os.getenv('PYTHONPATH', ':'.join(sys.path)) | ||
|
||
|
||
def teardown(): | ||
os.environ['PYTHONPATH'] = OLD_PYTHONPATH | ||
|
||
|
||
def test_email(): | ||
'lettuce should be able to receive emails sent from django server' | ||
os.environ['PYTHONPATH'] = current_directory | ||
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoapp' | ||
|
||
status, out = commands.getstatusoutput( | ||
"django-admin.py harvest email.feature --verbosity=2") | ||
|
||
assert_not_equals(status, 0) |