diff email_list/tests/test_views.py @ 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 email_list/tests/view_tests.py@e2868ad47a1e
children 312f198e8958
line wrap: on
line diff
--- /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 = []