Mercurial > public > madeira
view madeira/email_list/tests/view_tests.py @ 51:13b2561c909d
For issue #7, create a mailing list application.
Still need to test that emails are being sent.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 28 Mar 2012 21:13:05 -0500 |
parents | |
children | 7f9e76e7eb4d |
line wrap: on
line source
""" View tests for the email_list application. """ from django.test import TestCase from django.core.urlresolvers import reverse 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': 'j.doe@example.com', '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()) # TODO: test email sent # post to confirm response = self.client.post( 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()) # TODO: test email sent # post to confirm unsubscribe response = self.client.post( 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'])