annotate email_list/tests/test_views.py @ 184:6508aef37131

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