def test_tags_values(self): url = get_opt_out_path('*****@*****.**', 'notifications:123', 'comments:owner') self.assertEqual( url, '/opt-out/?email=foo%40bar.com&tag=notifications%3A123&tag=comments%3Aowner' )
def test_not_confirmed(self): url = get_opt_out_path("*****@*****.**") response = test.Client().post(url, data={'email': '*****@*****.**'}) self.assertNoFormErrors(response) item = models.OptOut.objects.first() self.assertIsNotNone(item) self.assertIsNone(item.confirmed)
def test_feedback(self): questions = factories.OptOutFeedbackFactory.create_batch(10)[3:7] url = get_opt_out_path("*****@*****.**") response = test.Client().post(url, data={'email': '*****@*****.**', 'feedback': [q.pk for q in questions]}) self.assertNoFormErrors(response) item = models.OptOut.objects.first() self.assertEqual(set(questions), set(item.feedback.all()))
def test_valid_tag_value(self): factories.OptOutTagFactory(name='source') url = get_opt_out_path("*****@*****.**", "source:some") response = test.Client().post(url, data={'email': '*****@*****.**'}) self.assertNoFormErrors(response) item = models.OptOut.objects.first() self.assertEqual('some', item.tags.all().first().value)
def test_with_feedback(self): feedback = factories.OptOutFeedbackFactory.create_batch(10) url = get_opt_out_path("*****@*****.**") response = test.Client().get(url) self.assertEqual(200, response.status_code) self.assertEqual({}, response.context_data['form'].errors) choices = [pk for pk, label in response.context_data['form'].fields['feedback'].choices] self.assertEqual(set((f.pk for f in feedback)), set(choices))
def test_multiple_tags(self): factories.OptOutTagFactory(name='source') factories.OptOutTagFactory(name='flag') url = get_opt_out_path("*****@*****.**", "source:some", "flag") response = test.Client().post(url, data={'email': '*****@*****.**'}) self.assertNoFormErrors(response) item = models.OptOut.objects.first() self.assertEqual(['source', 'flag'], list(item.tags.all().values_list('tag__name', flat=True)))
def test_feedback_checked_by_default(self): factories.OptOutFeedbackFactory.create_batch(3) feedback = factories.OptOutFeedbackFactory.create_batch(3, default=True) url = get_opt_out_path("*****@*****.**") response = test.Client().get(url) self.assertEqual(200, response.status_code) self.assertEqual({}, response.context_data['form'].errors) initial = response.context_data['form'].fields['feedback'].initial self.assertEqual([f.pk for f in feedback], initial)
def test_feedback_based_on_tags(self): feedback = factories.OptOutFeedbackFactory.create_batch(3) tag1 = factories.OptOutTagFactory() feedback.extend(factories.OptOutFeedbackFactory.create_batch(3, tags=(tag1,))) tag2 = factories.OptOutTagFactory() factories.OptOutFeedbackFactory.create_batch(3, tags=(tag2,)) tag3 = factories.OptOutTagFactory() feedback.extend(factories.OptOutFeedbackFactory.create_batch(3, tags=(tag3,))) url = get_opt_out_path("*****@*****.**", tag1.name, tag3.name) response = test.Client().get(url) self.assertEqual(200, response.status_code) self.assertEqual({}, response.context_data['form'].errors) choices = [pk for pk, label in response.context_data['form'].fields['feedback'].choices] self.assertEqual(set((f.pk for f in feedback)), set(choices))
def test_plain_email_send(): from django_opt_out.utils import get_opt_out_path unsubscribe = get_opt_out_path("", 'some', 'tags', 'controlling', 'questionnaire') # unsubscribe link will not have a domain name and scheme # you can build prefix from request, but I prefer to set it in settings from django.conf import settings unsubscribe = settings.BASE_URL + unsubscribe body = 'Hello, Regards\n\nUnsubscribe: ' + unsubscribe from django.core import mail message = mail.EmailMultiAlternatives(body=body, to=['*****@*****.**']) message.extra_headers['List-Unsubscribe'] = "<{}>".format(unsubscribe) message.send()
def test_send_mail_template(mocker, settings): render_to_string = mocker.patch('django_opt_out.plugins.sparkpost.render_to_string') render_to_string.return_value = \ "Ojciec Wirgiliusz uczył dzieci swoje. " \ "Na głowie przy tym stojąc wiele lat. " \ "Rzekł jeden z synów: – Tak bardzo się boję. " \ "O ciebie ojcze, boś już stary dziad." to = settings.ADMINS[0][1] if settings.ADMINS else '*****@*****.**' from django_opt_out.utils import get_opt_out_path ctx = { 'unsubscribe': settings.BASE_URL + get_opt_out_path(to, 'testing') } settings.EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # To actually test sending an email, comment out mocking of EmailMultiAlternatives.send send = mocker.patch('django.core.mail.message.EmailMultiAlternatives.send') send_email(subject='Alicja w krainie czarów', to=to, template_html='notused.html', ctx=ctx) assert send.called
def test_named_email_parsing(self): url = get_opt_out_path('Artur Dent <*****@*****.**>') self.assertEqual(url, '/opt-out/?email=42%40galaxy.net')
def test_many_tags(self): url = get_opt_out_path('*****@*****.**', 'notifications', 'comments') self.assertEqual( url, '/opt-out/?email=foo%40bar.com&tag=notifications&tag=comments')
def test_one_tag(self): url = get_opt_out_path('*****@*****.**', 'notification') self.assertEqual(url, '/opt-out/?email=foo2%40bar.com&tag=notification')
def test_no_tags(self): url = get_opt_out_path('*****@*****.**') self.assertEqual(url, '/opt-out/?email=foo%40bar.com')
def test_simple(self): url = get_opt_out_path("*****@*****.**") response = test.Client().get(url) self.assertEqual(200, response.status_code) self.assertEqual({}, response.context_data['form'].errors) self.assertEqual("*****@*****.**", response.context_data['form']['email'].initial)