Login

Testing Email Registration

Author:
osborn.steven
Posted:
March 9, 2009
Language:
Python
Version:
1.0
Score:
0 (after 2 ratings)

http://steven.bitsetters.com/articles/2009/03/09/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.

 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
# 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()


# Put this in one of your apps under 
# <your app>/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)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

osborn.steven (on March 10, 2009):

carljm, thanks I wasn't aware of that.

I might still find this useful for manual testing. The email_reg command follows the first link in the email and opens it in your browser which is handy which works for registration and account recovery when I'm just poking around with new features on the site.

Thanks again for the tip.

#

Please login first before commenting.