def assertViewOnSiteWorks(self, email): view_url = subscribers.get_adapter(email.__class__).get_view_url(email, self.subscriber1) # Test that is doesn't let you in if the email has not been sent. response = self.client.get(view_url) self.assertEqual(response.status_code, 404) # Test that an unsent email does not work. subscribers.dispatch_email(email, self.subscriber1) response = self.client.get(view_url) self.assertEqual(response.status_code, 404) # Test that the view URL is valid. subscribers.send_email_batch() response = self.client.get(view_url) self.assertEqual(response.status_code, 200) # Test that the txt version also works. response = self.client.get(view_url + "txt/") self.assertEqual(response.status_code, 200) self.assertEqual(response["Content-Type"], "text/plain; charset=utf-8")
def testUnsubscribedEmailsNotSent(self): # Unsubscribe a subscriber. self.subscriber2.is_subscribed = False self.subscriber2.save() # Send the emails. sent_emails = subscribers.send_email_batch() self.assertEqual(len([email for email in sent_emails if email.status == STATUS_SENT]), 2) self.assertEqual(len([email for email in sent_emails if email.status == STATUS_UNSUBSCRIBED]), 2) self.assertEqual(len(mail.outbox), 2) # Check individual emails. self.assertEqual(mail.outbox[0].subject, "Foo 1") self.assertEqual(mail.outbox[0].to, [unicode(self.subscriber1)]) self.assertEqual(mail.outbox[1].subject, "Foo 2") self.assertEqual(mail.outbox[1].to, [unicode(self.subscriber1)]) # Make sure they aren't sent twice. sent_emails = subscribers.send_email_batch() self.assertEqual(len(sent_emails), 0) self.assertEqual(len(mail.outbox), 2)
def testDispatchEmail(self): # Make sure that the emails exist. self.assertEqual(DispatchedEmail.objects.count(), 4) # Send the emails. sent_emails = subscribers.send_email_batch() self.assertEqual(len([email for email in sent_emails if email.status == STATUS_SENT]), 4) self.assertEqual(len(mail.outbox), 4) # Check individual emails. self.assertEqual(mail.outbox[0].subject, "Foo 1") self.assertEqual(mail.outbox[0].to, [unicode(self.subscriber1)]) self.assertEqual(mail.outbox[1].subject, "Foo 1") self.assertEqual(mail.outbox[1].to, [unicode(self.subscriber2)]) self.assertEqual(mail.outbox[2].subject, "Foo 2") self.assertEqual(mail.outbox[2].to, [unicode(self.subscriber1)]) self.assertEqual(mail.outbox[3].subject, "Foo 2") self.assertEqual(mail.outbox[3].to, [unicode(self.subscriber2)]) # Make sure they aren't sent twice. sent_emails = subscribers.send_email_batch() self.assertEqual(len(sent_emails), 0) self.assertEqual(len(mail.outbox), 4)
def assertUnsubscribeWorkflowWorks(self, email): self.assertTrue(Subscriber.objects.get(id=self.subscriber1.id).is_subscribed) # Get the unsubscribe URL. unsubscribe_url = subscribers.get_adapter(email.__class__).get_unsubscribe_url(email, self.subscriber1) self.assertTrue(unsubscribe_url) # Make sure the unsubscribe url is set. # Attempt to unsubscribe from an email that was never dispatched. self.assertEqual(self.client.get(unsubscribe_url).status_code, 404) # Dispatch the email. subscribers.dispatch_email(email, self.subscriber1) # Attempt to unsubscribe from an email that was never sent. self.assertEqual(self.client.get(unsubscribe_url).status_code, 404) # Send the emails. subscribers.send_email_batch() # Try to unsubscribe again. response = self.client.get(unsubscribe_url) self.assertEqual(response.status_code, 200) response = self.client.post(unsubscribe_url, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "subscribers/unsubscribe_success.html") # See if the unsubscribe worked. self.assertFalse(Subscriber.objects.get(id=self.subscriber1.id).is_subscribed) # Re-subscribe the user. self.subscriber1 = Subscriber.objects.subscribe(email="*****@*****.**")
def assertSaveAndSendWorks(self, model): model_slug = model.__name__.lower() # Create two subscribers. subscriber1 = Subscriber.objects.create( email = "*****@*****.**", is_subscribed = False, ) subscriber2 = Subscriber.objects.create( email = "*****@*****.**", ) # Create a mailing list. mailing_list = MailingList.objects.create( name = "Foo list", ) subscriber1.mailing_lists.add(mailing_list) # Create an email. email = model.objects.create( subject = "Foo bar 1", ) change_url = "/admin/auth/{model_slug}/{pk}/".format(model_slug=model_slug, pk=email.pk) # Send to nobody. response = self.client.post(change_url, { "subject": "Foo bar 1", "_saveandsend": "1", "_send_to": "_nobody", "_send_on_date": "", "_send_on_time": "", }) self.assertRedirects(response, change_url) self.assertEqual(len(subscribers.send_email_batch()), 0) self.assertEqual(len(mail.outbox), 0) # Send to a list with an unsubscribed person. response = self.client.post(change_url, { "subject": "Foo bar 1", "_saveandsend": "1", "_send_to": mailing_list.pk, "_send_on_date": "", "_send_on_time": "", }) self.assertRedirects(response, "/admin/auth/{model_slug}/".format(model_slug=model_slug)) self.assertEqual(len(subscribers.send_email_batch()), 0) self.assertEqual(len(mail.outbox), 0) # Subscribe the person again. subscriber1.is_subscribed = True subscriber1.save() # Send to a list. response = self.client.post(change_url, { "subject": "Foo bar 1", "_saveandsend": "1", "_send_to": mailing_list.pk, "_send_on_date": "", "_send_on_time": "", }) self.assertRedirects(response, "/admin/auth/{model_slug}/".format(model_slug=model_slug)) self.assertEqual(len(subscribers.send_email_batch()), 1) self.assertEqual(len(mail.outbox), 1) # Send to everyone, minus the people who have received it. response = self.client.post(change_url, { "subject": "Foo bar 1", "_saveandsend": "1", "_send_to": "_all", "_send_on_date": "", "_send_on_time": "", }) self.assertRedirects(response, "/admin/auth/{model_slug}/".format(model_slug=model_slug)) self.assertEqual(len(subscribers.send_email_batch()), 1) self.assertEqual(len(mail.outbox), 2)
def testSendPartialBatch(self): sent_emails = subscribers.send_email_batch(2) self.assertEqual(len([email for email in sent_emails if email.status == STATUS_SENT]), 2) self.assertEqual(len(mail.outbox), 2)