Exemple #1
0
 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'
     )
Exemple #2
0
 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)
Exemple #3
0
 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()))
Exemple #4
0
 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)
Exemple #5
0
 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))
Exemple #6
0
 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)))
Exemple #7
0
 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)
Exemple #8
0
 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))
Exemple #9
0
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()
Exemple #10
0
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
Exemple #11
0
 def test_named_email_parsing(self):
     url = get_opt_out_path('Artur Dent <*****@*****.**>')
     self.assertEqual(url, '/opt-out/?email=42%40galaxy.net')
Exemple #12
0
 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')
Exemple #13
0
 def test_one_tag(self):
     url = get_opt_out_path('*****@*****.**', 'notification')
     self.assertEqual(url,
                      '/opt-out/?email=foo2%40bar.com&tag=notification')
Exemple #14
0
 def test_no_tags(self):
     url = get_opt_out_path('*****@*****.**')
     self.assertEqual(url, '/opt-out/?email=foo%40bar.com')
Exemple #15
0
 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)