bgneal@51: """ bgneal@51: View tests for the email_list application. bgneal@51: bgneal@51: """ bgneal@130: import urllib bgneal@130: bgneal@51: from django.test import TestCase bgneal@51: from django.core.urlresolvers import reverse bgneal@52: from django.core import mail bgneal@51: bgneal@51: from email_list.models import Subscriber bgneal@51: import email_list.forms bgneal@51: bgneal@51: bgneal@51: SUB_PARAMS = { bgneal@51: 'name': 'John Doe', bgneal@51: 'email': 'j.doe@example.com', bgneal@51: 'location': 'USA', bgneal@51: 'option': 'sub' bgneal@51: } bgneal@51: bgneal@51: UNSUB_PARAMS = { bgneal@51: 'name': '', bgneal@52: 'email': SUB_PARAMS['email'], bgneal@51: 'location': '', bgneal@51: 'option': 'unsub' bgneal@51: } bgneal@51: bgneal@51: class EmailListTestCase(TestCase): bgneal@51: bgneal@51: def test_already_subscribed(self): bgneal@51: """ bgneal@51: Test that subscribing twice fails with a form error. bgneal@51: bgneal@51: """ bgneal@51: sub = Subscriber(email=SUB_PARAMS['email']) bgneal@51: sub.set_active() bgneal@51: sub.save() bgneal@51: bgneal@51: # Post a subscribe request bgneal@51: bgneal@51: response = self.client.post( bgneal@51: reverse('email_list-main'), bgneal@51: SUB_PARAMS, bgneal@51: follow=True) bgneal@51: bgneal@51: self.assertTrue(response.status_code, 200) bgneal@51: self.assertEqual(len(response.redirect_chain), 0) bgneal@51: self.assertContains(response, email_list.forms.ALREADY_SUBSCRIBED) bgneal@51: bgneal@51: def test_not_subscribed(self): bgneal@51: """ bgneal@51: Test that unsubscribing without being subscribed fails with a form error. bgneal@51: bgneal@51: """ bgneal@51: # Post a unsubscribe request bgneal@51: bgneal@51: response = self.client.post( bgneal@51: reverse('email_list-main'), bgneal@51: UNSUB_PARAMS, bgneal@51: follow=True) bgneal@51: bgneal@51: self.assertTrue(response.status_code, 200) bgneal@51: self.assertEqual(len(response.redirect_chain), 0) bgneal@51: self.assertContains(response, email_list.forms.NOT_SUBSCRIBED) bgneal@51: bgneal@51: def test_normal_cycle(self): bgneal@51: """ bgneal@51: Test a normal subscribe and unsubscribe cycle. bgneal@51: bgneal@51: """ bgneal@51: self.do_test_subscribe() bgneal@51: self.do_test_unsubscribe() bgneal@51: bgneal@51: def test_subscribe_if_pending(self): bgneal@51: """ bgneal@51: Ensure you can subscribe if you are already pending. bgneal@51: bgneal@51: """ bgneal@51: sub = Subscriber(email=SUB_PARAMS['email']) bgneal@51: sub.set_pending() bgneal@51: sub.save() bgneal@51: self.do_test_subscribe() bgneal@51: bgneal@51: def test_subscribe_if_leaving(self): bgneal@51: """ bgneal@51: Ensure you can subscribe if you are leaving. bgneal@51: bgneal@51: """ bgneal@51: sub = Subscriber(email=SUB_PARAMS['email']) bgneal@51: sub.set_leaving() bgneal@51: sub.save() bgneal@51: self.do_test_subscribe() bgneal@51: bgneal@51: def test_unsubscribe_if_leaving(self): bgneal@51: """ bgneal@51: Ensure you can unsubscribe if you are already leaving. bgneal@51: bgneal@51: """ bgneal@51: sub = Subscriber(email=SUB_PARAMS['email']) bgneal@51: sub.set_leaving() bgneal@51: sub.save() bgneal@51: self.do_test_unsubscribe() bgneal@51: bgneal@51: def do_test_subscribe(self): bgneal@51: # Get the form view bgneal@51: response = self.client.get(reverse('email_list-main')) bgneal@51: self.assertEqual(response.status_code, 200) bgneal@51: self.assertTemplateUsed(response, 'email_list/subscribe_form.html') bgneal@51: bgneal@51: # Post a subscribe request bgneal@51: bgneal@51: response = self.client.post( bgneal@51: reverse('email_list-main'), bgneal@51: SUB_PARAMS, bgneal@51: follow=True) bgneal@51: bgneal@51: self.assertTrue(response.status_code, 200) bgneal@51: self.assertEqual(len(response.redirect_chain), 1) bgneal@51: self.assertEqual(response.redirect_chain[0][0], bgneal@51: 'http://testserver' + reverse('email_list-request_subscribe')) bgneal@51: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@51: bgneal@51: # verify subscriber is in pending state bgneal@51: bgneal@51: try: bgneal@51: subscriber = Subscriber.objects.get(email=SUB_PARAMS['email']) bgneal@51: except Subscriber.DoesNotExist: bgneal@51: self.fail("No pending subscriber") bgneal@51: bgneal@51: self.assertTrue(subscriber.is_pending()) bgneal@51: bgneal@52: # test email sent bgneal@52: self.do_test_email(subscriber) bgneal@51: bgneal@52: # simulate a confirm click bgneal@51: bgneal@52: response = self.client.get( bgneal@51: reverse('email_list-confirm', kwargs={'key': subscriber.key}), bgneal@51: follow=True) bgneal@51: bgneal@51: self.assertTrue(response.status_code, 200) bgneal@51: self.assertEqual(len(response.redirect_chain), 1) bgneal@51: self.assertEqual(response.redirect_chain[0][0], bgneal@51: 'http://testserver' + reverse('email_list-subscribed')) bgneal@51: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@51: bgneal@51: # verify active user bgneal@51: try: bgneal@51: subscriber = Subscriber.objects.get(email=SUB_PARAMS['email']) bgneal@51: except Subscriber.DoesNotExist: bgneal@51: self.fail("No active subscriber") bgneal@51: bgneal@51: self.assertTrue(subscriber.is_active()) bgneal@51: bgneal@51: def do_test_unsubscribe(self): bgneal@51: # Get the form view bgneal@51: response = self.client.get(reverse('email_list-main')) bgneal@51: self.assertEqual(response.status_code, 200) bgneal@51: self.assertTemplateUsed(response, 'email_list/subscribe_form.html') bgneal@51: bgneal@51: # Post a unsubscribe request bgneal@51: bgneal@51: response = self.client.post( bgneal@51: reverse('email_list-main'), bgneal@51: UNSUB_PARAMS, bgneal@51: follow=True) bgneal@51: bgneal@51: self.assertTrue(response.status_code, 200) bgneal@51: self.assertEqual(len(response.redirect_chain), 1) bgneal@51: self.assertEqual(response.redirect_chain[0][0], bgneal@51: 'http://testserver' + reverse('email_list-request_unsubscribe')) bgneal@51: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@51: bgneal@51: # verify subscriber is in leaving state bgneal@51: bgneal@51: try: bgneal@51: subscriber = Subscriber.objects.get(email=UNSUB_PARAMS['email']) bgneal@51: except Subscriber.DoesNotExist: bgneal@51: self.fail("No pending subscriber") bgneal@51: bgneal@51: self.assertTrue(subscriber.is_leaving()) bgneal@51: bgneal@52: # test email sent bgneal@52: self.do_test_email(subscriber) bgneal@51: bgneal@52: # simulate a click to unsubscribe bgneal@51: bgneal@52: response = self.client.get( bgneal@51: reverse('email_list-confirm', kwargs={'key': subscriber.key}), bgneal@51: follow=True) bgneal@51: bgneal@51: self.assertTrue(response.status_code, 200) bgneal@51: self.assertEqual(len(response.redirect_chain), 1) bgneal@51: self.assertEqual(response.redirect_chain[0][0], bgneal@51: 'http://testserver' + reverse('email_list-unsubscribed')) bgneal@51: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@51: bgneal@51: # verify subscription has been removed bgneal@51: bgneal@51: self.assertRaises(Subscriber.DoesNotExist, Subscriber.objects.get, bgneal@51: email=UNSUB_PARAMS['email']) bgneal@52: bgneal@52: def do_test_email(self, subscriber): bgneal@52: """ bgneal@52: Tests to see if the confirmation email was sent. bgneal@52: bgneal@52: """ bgneal@52: self.assertEqual(len(mail.outbox), 1) bgneal@52: self.assertEqual(len(mail.outbox[0].to), 1) bgneal@52: self.assertEqual(mail.outbox[0].to[0], subscriber.email) bgneal@130: bgneal@130: msg = str(mail.outbox[0].message()) bgneal@130: self.assertTrue(msg.count(urllib.quote(subscriber.key)) > 0) bgneal@52: bgneal@52: # clear outbox bgneal@52: mail.outbox = []