def test_initial_newsletters(self): """ Make sure a new form renders with the correct newsletters checked """ email = EmailAddress.objects.create(user=self.user) email.subscribe(self.newsletter1) form = SubscriptionForm(user=self.user) soup = BeautifulSoup(form.as_ul()) self.assertEqual(3, len(soup.findAll('input', type='checkbox')), soup) self.assertEqual(1, len(soup.findAll('input', checked=True)), soup)
def test_email_sanitization(self): """ Make sure email addresses get sanitized """ data = { 'email_address': ' [email protected] ', 'newsletters': [self.newsletter1.id] } form = SubscriptionForm(data=data) self.assertTrue(form.is_valid()) self.assertEqual('*****@*****.**', form.cleaned_data['email_address'])
def test_unsubscribe(self): """ Make sure removing previous subscriptions for a user works """ email = EmailAddress.objects.create(user=self.user) email.subscribe(self.newsletter1) data = {'email_address': self.user.email} form = SubscriptionForm(data=data, user=self.user) self.assertTrue(form.is_valid()) self.assertEqual(1, email.subscriptions.count()) form.save() self.assertEqual(0, email.subscriptions.count())
def test_inactive_newsletters(self): """ Make sure inactive newsletters don't get rendered """ inactive_newsletter = self.newsletter1 inactive_newsletter.active = False inactive_newsletter.save() form = SubscriptionForm(user=self.user) soup = BeautifulSoup(form.as_ul()) self.assertEqual(2, len(soup.findAll('input', attrs={'name': 'newsletters'})), soup) self.assertEqual(0, len(soup.findAll('input', attrs={'name': 'newsletters', 'value': inactive_newsletter.id})))
def test_multi_submit(self): """ Test submitting multiple newsletter subscription changes at once """ email = EmailAddress.objects.create(user=self.user) data = { 'email_address': self.user.email, 'newsletters': [n.id for n in Newsletter.objects.all()] } form = SubscriptionForm(data=data, user=self.user) self.assertTrue(form.is_valid(), form.errors) self.assertEqual(0, email.subscriptions.count()) form.save() self.assertEqual(3, email.subscriptions.count())
def test_unsubscribe(self): """ Make sure removing previous subscriptions for a user works """ email = EmailAddress.objects.create(user=self.user) email.subscribe(self.newsletter1) data = { 'email_address': self.user.email } form = SubscriptionForm(data=data, user=self.user) self.assertTrue(form.is_valid()) self.assertEqual(1, email.subscriptions.count()) form.save() self.assertEqual(0, email.subscriptions.count())
def test_old_user(self): """ Make sure specifying an existing user modifies that user's subscriptions """ data = { 'email_address': self.user.email, 'newsletters': [self.newsletter1.id] } form = SubscriptionForm(data=data, user=self.user) self.assertTrue(form.is_valid()) self.assertEqual(0, EmailAddress.objects.count()) form.save() self.assertEqual(1, EmailAddress.objects.count()) self.assertEqual(self.user, EmailAddress.objects.get().user) self.assertEqual(self.user.email, EmailAddress.objects.get().email)
def test_new_email(self): """ Make sure saving the form creates a new user """ test_email = '*****@*****.**' data = { 'email_address': test_email, 'newsletters': [self.newsletter1.id] } form = SubscriptionForm(data=data) self.assertTrue(form.is_valid()) self.assertEqual(0, EmailAddress.objects.count()) form.save() self.assertEqual(1, EmailAddress.objects.count()) self.assertEqual(test_email, EmailAddress.objects.get().email)
def update_subscriptions(request, template_name='nova/subscribe.html', redirect_url=None, extra_context=None): """ View that allows existing users (or even new users) to modify their subscriptions """ if request.method == "POST": form = SubscriptionForm(data=request.POST, user=request.user) if form.is_valid(): form.save() return redirect_to(request, redirect_url) else: form = SubscriptionForm(user=request.user) context = { 'form': form } if extra_context: context.update(extra_context) return render_to_response(template_name, context, RequestContext(request))
def test_inactive_newsletters(self): """ Make sure inactive newsletters don't get rendered """ inactive_newsletter = self.newsletter1 inactive_newsletter.active = False inactive_newsletter.save() form = SubscriptionForm(user=self.user) soup = BeautifulSoup(form.as_ul()) self.assertEqual( 2, len(soup.findAll('input', attrs={'name': 'newsletters'})), soup) self.assertEqual( 0, len( soup.findAll('input', attrs={ 'name': 'newsletters', 'value': inactive_newsletter.id })))
def update_subscriptions(request, template_name='nova/subscribe.html', redirect_url=None, extra_context=None): """ View that allows existing users (or even new users) to modify their subscriptions """ if request.method == "POST": form = SubscriptionForm(data=request.POST, user=request.user) if form.is_valid(): form.save() return redirect_to(request, redirect_url) else: form = SubscriptionForm(user=request.user) context = {'form': form} if extra_context: context.update(extra_context) return render_to_response(template_name, context, RequestContext(request))