changeset 130:3062c547bb90

For Django 1.6: new test discovery plus reverse now does urlquote(). My base64 keys were padded with '=' and these got quoted when doing a reverse to generate the URL. So changed the test to look for a quoted version of the key. This will change the URLs sent to users, but I believe it will all be taken care of by Django.
author Brian Neal <bgneal@gmail.com>
date Tue, 24 Dec 2013 16:47:27 -0600
parents fa4ef22f3f24
children 9c74cd8668eb
files email_list/tests/__init__.py email_list/tests/model_tests.py email_list/tests/test_models.py email_list/tests/test_views.py email_list/tests/view_tests.py
diffstat 5 files changed, 293 insertions(+), 291 deletions(-) [+]
line wrap: on
line diff
--- a/email_list/tests/__init__.py	Tue Dec 24 15:04:26 2013 -0600
+++ b/email_list/tests/__init__.py	Tue Dec 24 16:47:27 2013 -0600
@@ -1,2 +0,0 @@
-from model_tests import *
-from view_tests import *
--- a/email_list/tests/model_tests.py	Tue Dec 24 15:04:26 2013 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-"""
-Model tests for the email_list application.
-
-"""
-import datetime
-
-from django.test import TestCase
-
-from email_list.models import Subscriber
-
-
-class SubscriberTestCase(TestCase):
-
-    def test_auto_save(self):
-
-        sub = Subscriber(name='', location='', email='test@example.com')
-        sub.save()
-
-        now = datetime.datetime.now()
-        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
-
-        self.assertTrue(sub.status == 'A')
-        self.assertTrue(sub.is_active())
-        self.failIf(sub.is_pending())
-        self.failIf(sub.is_leaving())
-
-    def test_set_pending(self):
-
-        sub = Subscriber(name='', location='', email='test@example.com')
-        sub.set_pending()
-
-        now = datetime.datetime.now()
-        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
-
-        self.assertTrue(sub.status == 'P')
-        self.failIf(sub.is_active())
-        self.assertTrue(sub.is_pending())
-        self.failIf(sub.is_leaving())
-
-        self.assertTrue(len(sub.key) == sub.key_length)
-
-    def test_set_active(self):
-
-        sub = Subscriber(name='', location='', email='test@example.com')
-        sub.set_active()
-
-        now = datetime.datetime.now()
-        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
-
-        self.assertTrue(sub.status == 'A')
-        self.assertTrue(sub.is_active())
-        self.failIf(sub.is_pending())
-        self.failIf(sub.is_leaving())
-
-    def test_set_leaving(self):
-
-        sub = Subscriber(name='', location='', email='test@example.com')
-        sub.set_leaving()
-
-        now = datetime.datetime.now()
-        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
-
-        self.assertTrue(sub.status == 'L')
-        self.failIf(sub.is_active())
-        self.failIf(sub.is_pending())
-        self.assertTrue(sub.is_leaving())
-
-        self.assertTrue(len(sub.key) == sub.key_length)
-
-    def test_gen_key(self):
-
-        sub = Subscriber(name='', location='', email='test@example.com')
-        sub.status_date = datetime.datetime.now()
-        sub.gen_key()
-        self.assertTrue(len(sub.key) == sub.key_length)
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/email_list/tests/test_models.py	Tue Dec 24 16:47:27 2013 -0600
@@ -0,0 +1,76 @@
+"""
+Model tests for the email_list application.
+
+"""
+import datetime
+
+from django.test import TestCase
+
+from email_list.models import Subscriber
+
+
+class SubscriberTestCase(TestCase):
+
+    def test_auto_save(self):
+
+        sub = Subscriber(name='', location='', email='test@example.com')
+        sub.save()
+
+        now = datetime.datetime.now()
+        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
+
+        self.assertTrue(sub.status == 'A')
+        self.assertTrue(sub.is_active())
+        self.failIf(sub.is_pending())
+        self.failIf(sub.is_leaving())
+
+    def test_set_pending(self):
+
+        sub = Subscriber(name='', location='', email='test@example.com')
+        sub.set_pending()
+
+        now = datetime.datetime.now()
+        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
+
+        self.assertTrue(sub.status == 'P')
+        self.failIf(sub.is_active())
+        self.assertTrue(sub.is_pending())
+        self.failIf(sub.is_leaving())
+
+        self.assertTrue(len(sub.key) == sub.key_length)
+
+    def test_set_active(self):
+
+        sub = Subscriber(name='', location='', email='test@example.com')
+        sub.set_active()
+
+        now = datetime.datetime.now()
+        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
+
+        self.assertTrue(sub.status == 'A')
+        self.assertTrue(sub.is_active())
+        self.failIf(sub.is_pending())
+        self.failIf(sub.is_leaving())
+
+    def test_set_leaving(self):
+
+        sub = Subscriber(name='', location='', email='test@example.com')
+        sub.set_leaving()
+
+        now = datetime.datetime.now()
+        self.assertTrue(now - sub.status_date < datetime.timedelta(seconds=2))
+
+        self.assertTrue(sub.status == 'L')
+        self.failIf(sub.is_active())
+        self.failIf(sub.is_pending())
+        self.assertTrue(sub.is_leaving())
+
+        self.assertTrue(len(sub.key) == sub.key_length)
+
+    def test_gen_key(self):
+
+        sub = Subscriber(name='', location='', email='test@example.com')
+        sub.status_date = datetime.datetime.now()
+        sub.gen_key()
+        self.assertTrue(len(sub.key) == sub.key_length)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/email_list/tests/test_views.py	Tue Dec 24 16:47:27 2013 -0600
@@ -0,0 +1,217 @@
+"""
+View tests for the email_list application.
+
+"""
+import urllib
+
+from django.test import TestCase
+from django.core.urlresolvers import reverse
+from django.core import mail
+
+from email_list.models import Subscriber
+import email_list.forms
+
+
+SUB_PARAMS = {
+    'name': 'John Doe',
+    'email': 'j.doe@example.com',
+    'location': 'USA',
+    'option': 'sub'
+}
+
+UNSUB_PARAMS = {
+    'name': '',
+    'email': SUB_PARAMS['email'],
+    'location': '',
+    'option': 'unsub'
+}
+
+class EmailListTestCase(TestCase):
+
+    def test_already_subscribed(self):
+        """
+        Test that subscribing twice fails with a form error.
+
+        """
+        sub = Subscriber(email=SUB_PARAMS['email'])
+        sub.set_active()
+        sub.save()
+
+        # Post a subscribe request
+
+        response = self.client.post(
+                reverse('email_list-main'),
+                SUB_PARAMS,
+                follow=True)
+
+        self.assertTrue(response.status_code, 200)
+        self.assertEqual(len(response.redirect_chain), 0)
+        self.assertContains(response, email_list.forms.ALREADY_SUBSCRIBED)
+
+    def test_not_subscribed(self):
+        """
+        Test that unsubscribing without being subscribed fails with a form error.
+
+        """
+        # Post a unsubscribe request
+
+        response = self.client.post(
+                reverse('email_list-main'),
+                UNSUB_PARAMS,
+                follow=True)
+
+        self.assertTrue(response.status_code, 200)
+        self.assertEqual(len(response.redirect_chain), 0)
+        self.assertContains(response, email_list.forms.NOT_SUBSCRIBED)
+
+    def test_normal_cycle(self):
+        """
+        Test a normal subscribe and unsubscribe cycle.
+
+        """
+        self.do_test_subscribe()
+        self.do_test_unsubscribe()
+
+    def test_subscribe_if_pending(self):
+        """
+        Ensure you can subscribe if you are already pending.
+
+        """
+        sub = Subscriber(email=SUB_PARAMS['email'])
+        sub.set_pending()
+        sub.save()
+        self.do_test_subscribe()
+
+    def test_subscribe_if_leaving(self):
+        """
+        Ensure you can subscribe if you are leaving.
+
+        """
+        sub = Subscriber(email=SUB_PARAMS['email'])
+        sub.set_leaving()
+        sub.save()
+        self.do_test_subscribe()
+
+    def test_unsubscribe_if_leaving(self):
+        """
+        Ensure you can unsubscribe if you are already leaving.
+
+        """
+        sub = Subscriber(email=SUB_PARAMS['email'])
+        sub.set_leaving()
+        sub.save()
+        self.do_test_unsubscribe()
+
+    def do_test_subscribe(self):
+        # Get the form view
+        response = self.client.get(reverse('email_list-main'))
+        self.assertEqual(response.status_code, 200)
+        self.assertTemplateUsed(response, 'email_list/subscribe_form.html')
+
+        # Post a subscribe request
+
+        response = self.client.post(
+                reverse('email_list-main'),
+                SUB_PARAMS,
+                follow=True)
+
+        self.assertTrue(response.status_code, 200)
+        self.assertEqual(len(response.redirect_chain), 1)
+        self.assertEqual(response.redirect_chain[0][0],
+                'http://testserver' + reverse('email_list-request_subscribe'))
+        self.assertEqual(response.redirect_chain[0][1], 302)
+
+        # verify subscriber is in pending state
+
+        try:
+            subscriber = Subscriber.objects.get(email=SUB_PARAMS['email'])
+        except Subscriber.DoesNotExist:
+            self.fail("No pending subscriber")
+
+        self.assertTrue(subscriber.is_pending())
+
+        # test email sent
+        self.do_test_email(subscriber)
+
+        # simulate a confirm click
+
+        response = self.client.get(
+                reverse('email_list-confirm', kwargs={'key': subscriber.key}),
+                follow=True)
+
+        self.assertTrue(response.status_code, 200)
+        self.assertEqual(len(response.redirect_chain), 1)
+        self.assertEqual(response.redirect_chain[0][0],
+                'http://testserver' + reverse('email_list-subscribed'))
+        self.assertEqual(response.redirect_chain[0][1], 302)
+
+        # verify active user
+        try:
+            subscriber = Subscriber.objects.get(email=SUB_PARAMS['email'])
+        except Subscriber.DoesNotExist:
+            self.fail("No active subscriber")
+
+        self.assertTrue(subscriber.is_active())
+
+    def do_test_unsubscribe(self):
+        # Get the form view
+        response = self.client.get(reverse('email_list-main'))
+        self.assertEqual(response.status_code, 200)
+        self.assertTemplateUsed(response, 'email_list/subscribe_form.html')
+
+        # Post a unsubscribe request
+
+        response = self.client.post(
+                reverse('email_list-main'),
+                UNSUB_PARAMS,
+                follow=True)
+
+        self.assertTrue(response.status_code, 200)
+        self.assertEqual(len(response.redirect_chain), 1)
+        self.assertEqual(response.redirect_chain[0][0],
+                'http://testserver' + reverse('email_list-request_unsubscribe'))
+        self.assertEqual(response.redirect_chain[0][1], 302)
+
+        # verify subscriber is in leaving state
+
+        try:
+            subscriber = Subscriber.objects.get(email=UNSUB_PARAMS['email'])
+        except Subscriber.DoesNotExist:
+            self.fail("No pending subscriber")
+
+        self.assertTrue(subscriber.is_leaving())
+
+        # test email sent
+        self.do_test_email(subscriber)
+
+        # simulate a click to unsubscribe
+
+        response = self.client.get(
+                reverse('email_list-confirm', kwargs={'key': subscriber.key}),
+                follow=True)
+
+        self.assertTrue(response.status_code, 200)
+        self.assertEqual(len(response.redirect_chain), 1)
+        self.assertEqual(response.redirect_chain[0][0],
+                'http://testserver' + reverse('email_list-unsubscribed'))
+        self.assertEqual(response.redirect_chain[0][1], 302)
+
+        # verify subscription has been removed
+
+        self.assertRaises(Subscriber.DoesNotExist, Subscriber.objects.get,
+                email=UNSUB_PARAMS['email'])
+
+    def do_test_email(self, subscriber):
+        """
+        Tests to see if the confirmation email was sent.
+
+        """
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(len(mail.outbox[0].to), 1)
+        self.assertEqual(mail.outbox[0].to[0], subscriber.email)
+
+        msg = str(mail.outbox[0].message())
+        self.assertTrue(msg.count(urllib.quote(subscriber.key)) > 0)
+
+        # clear outbox
+        mail.outbox = []
--- a/email_list/tests/view_tests.py	Tue Dec 24 15:04:26 2013 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,213 +0,0 @@
-"""
-View tests for the email_list application.
-
-"""
-from django.test import TestCase
-from django.core.urlresolvers import reverse
-from django.core import mail
-
-from email_list.models import Subscriber
-import email_list.forms
-
-
-SUB_PARAMS = {
-    'name': 'John Doe',
-    'email': 'j.doe@example.com',
-    'location': 'USA',
-    'option': 'sub'
-}
-
-UNSUB_PARAMS = {
-    'name': '',
-    'email': SUB_PARAMS['email'],
-    'location': '',
-    'option': 'unsub'
-}
-
-class EmailListTestCase(TestCase):
-
-    def test_already_subscribed(self):
-        """
-        Test that subscribing twice fails with a form error.
-
-        """
-        sub = Subscriber(email=SUB_PARAMS['email'])
-        sub.set_active()
-        sub.save()
-
-        # Post a subscribe request
-
-        response = self.client.post(
-                reverse('email_list-main'),
-                SUB_PARAMS,
-                follow=True)
-
-        self.assertTrue(response.status_code, 200)
-        self.assertEqual(len(response.redirect_chain), 0)
-        self.assertContains(response, email_list.forms.ALREADY_SUBSCRIBED)
-
-    def test_not_subscribed(self):
-        """
-        Test that unsubscribing without being subscribed fails with a form error.
-
-        """
-        # Post a unsubscribe request
-
-        response = self.client.post(
-                reverse('email_list-main'),
-                UNSUB_PARAMS,
-                follow=True)
-
-        self.assertTrue(response.status_code, 200)
-        self.assertEqual(len(response.redirect_chain), 0)
-        self.assertContains(response, email_list.forms.NOT_SUBSCRIBED)
-
-    def test_normal_cycle(self):
-        """
-        Test a normal subscribe and unsubscribe cycle.
-
-        """
-        self.do_test_subscribe()
-        self.do_test_unsubscribe()
-
-    def test_subscribe_if_pending(self):
-        """
-        Ensure you can subscribe if you are already pending.
-
-        """
-        sub = Subscriber(email=SUB_PARAMS['email'])
-        sub.set_pending()
-        sub.save()
-        self.do_test_subscribe()
-
-    def test_subscribe_if_leaving(self):
-        """
-        Ensure you can subscribe if you are leaving.
-
-        """
-        sub = Subscriber(email=SUB_PARAMS['email'])
-        sub.set_leaving()
-        sub.save()
-        self.do_test_subscribe()
-
-    def test_unsubscribe_if_leaving(self):
-        """
-        Ensure you can unsubscribe if you are already leaving.
-
-        """
-        sub = Subscriber(email=SUB_PARAMS['email'])
-        sub.set_leaving()
-        sub.save()
-        self.do_test_unsubscribe()
-
-    def do_test_subscribe(self):
-        # Get the form view
-        response = self.client.get(reverse('email_list-main'))
-        self.assertEqual(response.status_code, 200)
-        self.assertTemplateUsed(response, 'email_list/subscribe_form.html')
-
-        # Post a subscribe request
-
-        response = self.client.post(
-                reverse('email_list-main'),
-                SUB_PARAMS,
-                follow=True)
-
-        self.assertTrue(response.status_code, 200)
-        self.assertEqual(len(response.redirect_chain), 1)
-        self.assertEqual(response.redirect_chain[0][0],
-                'http://testserver' + reverse('email_list-request_subscribe'))
-        self.assertEqual(response.redirect_chain[0][1], 302)
-
-        # verify subscriber is in pending state
-
-        try:
-            subscriber = Subscriber.objects.get(email=SUB_PARAMS['email'])
-        except Subscriber.DoesNotExist:
-            self.fail("No pending subscriber")
-
-        self.assertTrue(subscriber.is_pending())
-
-        # test email sent
-        self.do_test_email(subscriber)
-
-        # simulate a confirm click
-
-        response = self.client.get(
-                reverse('email_list-confirm', kwargs={'key': subscriber.key}),
-                follow=True)
-
-        self.assertTrue(response.status_code, 200)
-        self.assertEqual(len(response.redirect_chain), 1)
-        self.assertEqual(response.redirect_chain[0][0],
-                'http://testserver' + reverse('email_list-subscribed'))
-        self.assertEqual(response.redirect_chain[0][1], 302)
-
-        # verify active user
-        try:
-            subscriber = Subscriber.objects.get(email=SUB_PARAMS['email'])
-        except Subscriber.DoesNotExist:
-            self.fail("No active subscriber")
-
-        self.assertTrue(subscriber.is_active())
-
-    def do_test_unsubscribe(self):
-        # Get the form view
-        response = self.client.get(reverse('email_list-main'))
-        self.assertEqual(response.status_code, 200)
-        self.assertTemplateUsed(response, 'email_list/subscribe_form.html')
-
-        # Post a unsubscribe request
-
-        response = self.client.post(
-                reverse('email_list-main'),
-                UNSUB_PARAMS,
-                follow=True)
-
-        self.assertTrue(response.status_code, 200)
-        self.assertEqual(len(response.redirect_chain), 1)
-        self.assertEqual(response.redirect_chain[0][0],
-                'http://testserver' + reverse('email_list-request_unsubscribe'))
-        self.assertEqual(response.redirect_chain[0][1], 302)
-
-        # verify subscriber is in leaving state
-
-        try:
-            subscriber = Subscriber.objects.get(email=UNSUB_PARAMS['email'])
-        except Subscriber.DoesNotExist:
-            self.fail("No pending subscriber")
-
-        self.assertTrue(subscriber.is_leaving())
-
-        # test email sent
-        self.do_test_email(subscriber)
-
-        # simulate a click to unsubscribe
-
-        response = self.client.get(
-                reverse('email_list-confirm', kwargs={'key': subscriber.key}),
-                follow=True)
-
-        self.assertTrue(response.status_code, 200)
-        self.assertEqual(len(response.redirect_chain), 1)
-        self.assertEqual(response.redirect_chain[0][0],
-                'http://testserver' + reverse('email_list-unsubscribed'))
-        self.assertEqual(response.redirect_chain[0][1], 302)
-
-        # verify subscription has been removed
-
-        self.assertRaises(Subscriber.DoesNotExist, Subscriber.objects.get,
-                email=UNSUB_PARAMS['email'])
-
-    def do_test_email(self, subscriber):
-        """
-        Tests to see if the confirmation email was sent.
-
-        """
-        self.assertEqual(len(mail.outbox), 1)
-        self.assertEqual(len(mail.outbox[0].to), 1)
-        self.assertEqual(mail.outbox[0].to[0], subscriber.email)
-        self.assertTrue(str(mail.outbox[0].message()).count(subscriber.key) > 0)
-
-        # clear outbox
-        mail.outbox = []