view email_list/tests/view_tests.py @ 126:c59287134f90

Don't make photo and lead-in paragraph a media object. Just flow the text for small devices.
author Brian Neal <bgneal@gmail.com>
date Tue, 29 Oct 2013 20:45:11 -0500
parents e2868ad47a1e
children
line wrap: on
line source
"""
View tests for the email_list application.

"""
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)
        self.assertTrue(str(mail.outbox[0].message()).count(subscriber.key) > 0)

        # clear outbox
        mail.outbox = []