Testing Email Registration Flows in Django

Testing email registration flows is typically a pain. Most of the time I just want to sign up with a test user, get the email link and finish the flow. I also want to be able to automate the whole process without having to write some SMTP code to check some mail box for the email.

The best way I’ve found to do this is just to write out your emails to some file instead of actually sending them via SMTP when your testing. Below is some code to do just that. I’ve also created a Django management script that will open the last email sent out from your application, find the first link in it and open it in your web browser. Quite handy for following email registration links without logging into your email and clicking on them manually.

Put this code somewhere like util/__init__.py and add the util app to your INSTALLED_APPS in settings.py. Then instead of using the send_mail function in django.core.mail, use the wrapper we created in util.

from django.core.mail import send_mail as send_real_mail
from django.conf import settings

def send_mail(subject, message, sender, to):
    """
    Send email
    """
    if settings.DEBUG:
        send_debug_mail(subject, message, sender, to)

    send_real_mail(subject, message, sender, to)

def send_debug_mail(subject, message, sender, to):
    """
    Save outgoing mail to a file for testing
    """
    if not os.path.exists(settings.DEBUG_MAILDIR):
        os.mkdir(settings.DEBUG_MAILDIR)
    fp = open('%s/%s' % (settings.DEBUG_MAILDIR, str(time.time())) , "w")
    fp.writelines([
        'From: %s\n' % sender,
        'To: %s\n' % ", ".join(to),
        'Subject: %s\n\n' % subject,
        message
    ])
    fp.close()

In one of your applications probably util if you followed the directions above, create a folder called management and with in that folder create another called commands. Then paste the code below in /management/commands/email_reg.py. Also don’t forget to add __init__.py files to both the management and command directories.

# Put this in one of your apps under
# /management/commands/email_reg.py

from glob import glob
import webbrowser
import re

from django.core.management.base import NoArgsCommand
from django.conf import settings

class Command(NoArgsCommand):
    help = "Follow first link in the latest debug email"

    requires_model_validation = False

    def handle_noargs(self, **options):

        latest = ''
        for x in glob(settings.DEBUG_MAILDIR + '/*'):
            if x > latest:
                latest = x

        f = open(latest).read()
        link = re.search('http:\/\/.*', f).group()
        webbrowser.open_new_tab(link)
Advertisements