annotate email_list/tests/test_views.py @ 153:abfc189f3877

Update requirements files for Django 1.6.6.
author Brian Neal <bgneal@gmail.com>
date Sat, 23 Aug 2014 14:06:47 -0500
parents 3062c547bb90
children 312f198e8958
rev   line source
bgneal@51 1 """
bgneal@51 2 View tests for the email_list application.
bgneal@51 3
bgneal@51 4 """
bgneal@130 5 import urllib
bgneal@130 6
bgneal@51 7 from django.test import TestCase
bgneal@51 8 from django.core.urlresolvers import reverse
bgneal@52 9 from django.core import mail
bgneal@51 10
bgneal@51 11 from email_list.models import Subscriber
bgneal@51 12 import email_list.forms
bgneal@51 13
bgneal@51 14
bgneal@51 15 SUB_PARAMS = {
bgneal@51 16 'name': 'John Doe',
bgneal@51 17 'email': 'j.doe@example.com',
bgneal@51 18 'location': 'USA',
bgneal@51 19 'option': 'sub'
bgneal@51 20 }
bgneal@51 21
bgneal@51 22 UNSUB_PARAMS = {
bgneal@51 23 'name': '',
bgneal@52 24 'email': SUB_PARAMS['email'],
bgneal@51 25 'location': '',
bgneal@51 26 'option': 'unsub'
bgneal@51 27 }
bgneal@51 28
bgneal@51 29 class EmailListTestCase(TestCase):
bgneal@51 30
bgneal@51 31 def test_already_subscribed(self):
bgneal@51 32 """
bgneal@51 33 Test that subscribing twice fails with a form error.
bgneal@51 34
bgneal@51 35 """
bgneal@51 36 sub = Subscriber(email=SUB_PARAMS['email'])
bgneal@51 37 sub.set_active()
bgneal@51 38 sub.save()
bgneal@51 39
bgneal@51 40 # Post a subscribe request
bgneal@51 41
bgneal@51 42 response = self.client.post(
bgneal@51 43 reverse('email_list-main'),
bgneal@51 44 SUB_PARAMS,
bgneal@51 45 follow=True)
bgneal@51 46
bgneal@51 47 self.assertTrue(response.status_code, 200)
bgneal@51 48 self.assertEqual(len(response.redirect_chain), 0)
bgneal@51 49 self.assertContains(response, email_list.forms.ALREADY_SUBSCRIBED)
bgneal@51 50
bgneal@51 51 def test_not_subscribed(self):
bgneal@51 52 """
bgneal@51 53 Test that unsubscribing without being subscribed fails with a form error.
bgneal@51 54
bgneal@51 55 """
bgneal@51 56 # Post a unsubscribe request
bgneal@51 57
bgneal@51 58 response = self.client.post(
bgneal@51 59 reverse('email_list-main'),
bgneal@51 60 UNSUB_PARAMS,
bgneal@51 61 follow=True)
bgneal@51 62
bgneal@51 63 self.assertTrue(response.status_code, 200)
bgneal@51 64 self.assertEqual(len(response.redirect_chain), 0)
bgneal@51 65 self.assertContains(response, email_list.forms.NOT_SUBSCRIBED)
bgneal@51 66
bgneal@51 67 def test_normal_cycle(self):
bgneal@51 68 """
bgneal@51 69 Test a normal subscribe and unsubscribe cycle.
bgneal@51 70
bgneal@51 71 """
bgneal@51 72 self.do_test_subscribe()
bgneal@51 73 self.do_test_unsubscribe()
bgneal@51 74
bgneal@51 75 def test_subscribe_if_pending(self):
bgneal@51 76 """
bgneal@51 77 Ensure you can subscribe if you are already pending.
bgneal@51 78
bgneal@51 79 """
bgneal@51 80 sub = Subscriber(email=SUB_PARAMS['email'])
bgneal@51 81 sub.set_pending()
bgneal@51 82 sub.save()
bgneal@51 83 self.do_test_subscribe()
bgneal@51 84
bgneal@51 85 def test_subscribe_if_leaving(self):
bgneal@51 86 """
bgneal@51 87 Ensure you can subscribe if you are leaving.
bgneal@51 88
bgneal@51 89 """
bgneal@51 90 sub = Subscriber(email=SUB_PARAMS['email'])
bgneal@51 91 sub.set_leaving()
bgneal@51 92 sub.save()
bgneal@51 93 self.do_test_subscribe()
bgneal@51 94
bgneal@51 95 def test_unsubscribe_if_leaving(self):
bgneal@51 96 """
bgneal@51 97 Ensure you can unsubscribe if you are already leaving.
bgneal@51 98
bgneal@51 99 """
bgneal@51 100 sub = Subscriber(email=SUB_PARAMS['email'])
bgneal@51 101 sub.set_leaving()
bgneal@51 102 sub.save()
bgneal@51 103 self.do_test_unsubscribe()
bgneal@51 104
bgneal@51 105 def do_test_subscribe(self):
bgneal@51 106 # Get the form view
bgneal@51 107 response = self.client.get(reverse('email_list-main'))
bgneal@51 108 self.assertEqual(response.status_code, 200)
bgneal@51 109 self.assertTemplateUsed(response, 'email_list/subscribe_form.html')
bgneal@51 110
bgneal@51 111 # Post a subscribe request
bgneal@51 112
bgneal@51 113 response = self.client.post(
bgneal@51 114 reverse('email_list-main'),
bgneal@51 115 SUB_PARAMS,
bgneal@51 116 follow=True)
bgneal@51 117
bgneal@51 118 self.assertTrue(response.status_code, 200)
bgneal@51 119 self.assertEqual(len(response.redirect_chain), 1)
bgneal@51 120 self.assertEqual(response.redirect_chain[0][0],
bgneal@51 121 'http://testserver' + reverse('email_list-request_subscribe'))
bgneal@51 122 self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@51 123
bgneal@51 124 # verify subscriber is in pending state
bgneal@51 125
bgneal@51 126 try:
bgneal@51 127 subscriber = Subscriber.objects.get(email=SUB_PARAMS['email'])
bgneal@51 128 except Subscriber.DoesNotExist:
bgneal@51 129 self.fail("No pending subscriber")
bgneal@51 130
bgneal@51 131 self.assertTrue(subscriber.is_pending())
bgneal@51 132
bgneal@52 133 # test email sent
bgneal@52 134 self.do_test_email(subscriber)
bgneal@51 135
bgneal@52 136 # simulate a confirm click
bgneal@51 137
bgneal@52 138 response = self.client.get(
bgneal@51 139 reverse('email_list-confirm', kwargs={'key': subscriber.key}),
bgneal@51 140 follow=True)
bgneal@51 141
bgneal@51 142 self.assertTrue(response.status_code, 200)
bgneal@51 143 self.assertEqual(len(response.redirect_chain), 1)
bgneal@51 144 self.assertEqual(response.redirect_chain[0][0],
bgneal@51 145 'http://testserver' + reverse('email_list-subscribed'))
bgneal@51 146 self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@51 147
bgneal@51 148 # verify active user
bgneal@51 149 try:
bgneal@51 150 subscriber = Subscriber.objects.get(email=SUB_PARAMS['email'])
bgneal@51 151 except Subscriber.DoesNotExist:
bgneal@51 152 self.fail("No active subscriber")
bgneal@51 153
bgneal@51 154 self.assertTrue(subscriber.is_active())
bgneal@51 155
bgneal@51 156 def do_test_unsubscribe(self):
bgneal@51 157 # Get the form view
bgneal@51 158 response = self.client.get(reverse('email_list-main'))
bgneal@51 159 self.assertEqual(response.status_code, 200)
bgneal@51 160 self.assertTemplateUsed(response, 'email_list/subscribe_form.html')
bgneal@51 161
bgneal@51 162 # Post a unsubscribe request
bgneal@51 163
bgneal@51 164 response = self.client.post(
bgneal@51 165 reverse('email_list-main'),
bgneal@51 166 UNSUB_PARAMS,
bgneal@51 167 follow=True)
bgneal@51 168
bgneal@51 169 self.assertTrue(response.status_code, 200)
bgneal@51 170 self.assertEqual(len(response.redirect_chain), 1)
bgneal@51 171 self.assertEqual(response.redirect_chain[0][0],
bgneal@51 172 'http://testserver' + reverse('email_list-request_unsubscribe'))
bgneal@51 173 self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@51 174
bgneal@51 175 # verify subscriber is in leaving state
bgneal@51 176
bgneal@51 177 try:
bgneal@51 178 subscriber = Subscriber.objects.get(email=UNSUB_PARAMS['email'])
bgneal@51 179 except Subscriber.DoesNotExist:
bgneal@51 180 self.fail("No pending subscriber")
bgneal@51 181
bgneal@51 182 self.assertTrue(subscriber.is_leaving())
bgneal@51 183
bgneal@52 184 # test email sent
bgneal@52 185 self.do_test_email(subscriber)
bgneal@51 186
bgneal@52 187 # simulate a click to unsubscribe
bgneal@51 188
bgneal@52 189 response = self.client.get(
bgneal@51 190 reverse('email_list-confirm', kwargs={'key': subscriber.key}),
bgneal@51 191 follow=True)
bgneal@51 192
bgneal@51 193 self.assertTrue(response.status_code, 200)
bgneal@51 194 self.assertEqual(len(response.redirect_chain), 1)
bgneal@51 195 self.assertEqual(response.redirect_chain[0][0],
bgneal@51 196 'http://testserver' + reverse('email_list-unsubscribed'))
bgneal@51 197 self.assertEqual(response.redirect_chain[0][1], 302)
bgneal@51 198
bgneal@51 199 # verify subscription has been removed
bgneal@51 200
bgneal@51 201 self.assertRaises(Subscriber.DoesNotExist, Subscriber.objects.get,
bgneal@51 202 email=UNSUB_PARAMS['email'])
bgneal@52 203
bgneal@52 204 def do_test_email(self, subscriber):
bgneal@52 205 """
bgneal@52 206 Tests to see if the confirmation email was sent.
bgneal@52 207
bgneal@52 208 """
bgneal@52 209 self.assertEqual(len(mail.outbox), 1)
bgneal@52 210 self.assertEqual(len(mail.outbox[0].to), 1)
bgneal@52 211 self.assertEqual(mail.outbox[0].to[0], subscriber.email)
bgneal@130 212
bgneal@130 213 msg = str(mail.outbox[0].message())
bgneal@130 214 self.assertTrue(msg.count(urllib.quote(subscriber.key)) > 0)
bgneal@52 215
bgneal@52 216 # clear outbox
bgneal@52 217 mail.outbox = []