· 7 years ago · Jul 31, 2018, 03:00 PM
1@pytest.mark.django_db
2@testutils.override_settings_dict('MAILCHIMP', 'LISTID_AGENCIES', '123456')
3@testutils.override_settings_dict('MAILCHIMP', 'LISTID_FREELANCERS', '12345')
4@testutils.override_settings_dict('MAILCHIMP', 'SECRET_KEY', MAILCHIMP_SECRET_KEY)
5def test_webhook__unsubscribe_freelancer__wrong_list():
6 freelancer = FreelancerProfileFactory()
7 freelancer.settings.receive_marketing_emails = True
8 freelancer.settings.save()
9 url = _get_correct_webhook_url()
10 wrong_list_id = '123456789'
11 payload = {
12 'type': ['unsubscribe'],
13 'data[list_id]': [wrong_list_id],
14 'data[email]': [freelancer.user.email]
15 }
16 request = RequestFactory().post(url, payload)
17 request.user_agent = 'MailChimp.com'
18
19 with mock.patch('yunojuno.apps.core.views.mailchimp.logger') as _logger:
20 mailchimp_views.webhook(request)
21 _logger.error.assert_called_once_with(
22 "MailChimp webhook called with unknown list_id ({0})".format(wrong_list_id)
23 )
24
25 freelancer = FreelancerProfile.objects.last()
26 assert freelancer.settings.receive_marketing_emails
27
28
29@pytest.mark.django_db
30@testutils.override_settings_dict('MAILCHIMP', 'LISTID_AGENCIES', '123456')
31@testutils.override_settings_dict('MAILCHIMP', 'LISTID_FREELANCERS', '12345')
32@testutils.override_settings_dict('MAILCHIMP', 'SECRET_KEY', MAILCHIMP_SECRET_KEY)
33def test_webhook__unsubscribe__wrong_user_agent():
34 wrong_user_agent = 'dummy_user_agent'
35 url = _get_correct_webhook_url()
36 payload = {
37 'type': ['unsubscribe'],
38 'data[list_id]': ['12345'],
39 'data[email]': 'dummy@email.com'
40 }
41 request = RequestFactory().post(url, payload)
42 request.user_agent = wrong_user_agent
43
44 with mock.patch('yunojuno.apps.core.views.mailchimp.logger') as _logger:
45 mailchimp_views.webhook(request)
46 _logger.error.assert_called_once_with(
47 "MailChimp webhook called non POST request with user_agent ({0})"
48 .format(wrong_user_agent)
49 )