Merge pull request #19320 from edx/bmedx/remove_3rd_part_mailings_retirement

DEPR-8 - Remove retire_mailings endpoint and supporting code
This commit is contained in:
Brian Mesick
2018-11-28 14:16:54 -05:00
committed by GitHub
6 changed files with 0 additions and 224 deletions

View File

@@ -9,7 +9,6 @@ import crum
from celery.exceptions import TimeoutError
from django.conf import settings
from django.dispatch import receiver
from sailthru.sailthru_client import SailthruClient
from sailthru.sailthru_error import SailthruClientError
from six import text_type
@@ -19,7 +18,6 @@ from email_marketing.models import EmailMarketingConfiguration
from lms.djangoapps.email_marketing.tasks import get_email_cookies_via_sailthru, update_user, update_user_email
from openedx.core.djangoapps.user_authn.cookies import CREATE_LOGON_COOKIE
from openedx.core.djangoapps.lang_pref import LANGUAGE_KEY
from openedx.core.djangoapps.user_api.accounts.signals import USER_RETIRE_THIRD_PARTY_MAILINGS
from openedx.core.djangoapps.waffle_utils import WaffleSwitchNamespace
from student.signals import SAILTHRU_AUDIT_PURCHASE
from student.views import REGISTER_USER
@@ -261,58 +259,3 @@ def _log_sailthru_api_call_time(time_before_call):
time_before_call.isoformat(' '),
time_after_call.isoformat(' '),
delta_sailthru_api_call_time.microseconds / 1000)
@receiver(USER_RETIRE_THIRD_PARTY_MAILINGS)
def force_unsubscribe_all(sender, **kwargs): # pylint: disable=unused-argument
"""
Synchronously(!) unsubscribes the given user from all Sailthru email lists.
In the future this could be moved to a Celery task, however this is currently
only used as part of user retirement, where we need a very reliable indication
of success or failure.
Args:
email: Email address to unsubscribe
new_email (optional): Email address to change 3rd party services to for this user (used in retirement to clear
personal information from the service)
Returns:
None
"""
email = kwargs.get('email', None)
new_email = kwargs.get('new_email', None)
if not email:
raise TypeError('Expected an email address to unsubscribe, but received None.')
email_config = EmailMarketingConfiguration.current()
if not email_config.enabled:
return
sailthru_parms = {
"id": email,
"optout_email": "all",
"fields": {"optout_email": 1}
}
# If we have a new email address to change to, do that as well
if new_email:
sailthru_parms["keys"] = {
"email": new_email
}
sailthru_parms["fields"]["keys"] = 1
sailthru_parms["keysconflict"] = "merge"
try:
sailthru_client = SailthruClient(email_config.sailthru_key, email_config.sailthru_secret)
sailthru_response = sailthru_client.api_post("user", sailthru_parms)
except SailthruClientError as exc:
error_msg = "Exception attempting to opt-out user {} from Sailthru - {}".format(email, text_type(exc))
log.error(error_msg)
raise Exception(error_msg)
if not sailthru_response.is_ok():
error = sailthru_response.get_error()
error_msg = "Error attempting to opt-out user {} from Sailthru - {}".format(email, error.get_message())
log.error(error_msg)
raise Exception(error_msg)

View File

@@ -20,7 +20,6 @@ from email_marketing.signals import (
add_email_marketing_cookies,
email_marketing_register_user,
email_marketing_user_field_changed,
force_unsubscribe_all,
update_sailthru
)
from email_marketing.tasks import (
@@ -555,53 +554,6 @@ class EmailMarketingTests(TestCase):
email_marketing_user_field_changed(None, self.user, table='auth_user', setting='email', old_value='new@a.com')
self.assertFalse(mock_update_user.called)
@patch('email_marketing.tasks.log.error')
@patch('email_marketing.tasks.SailthruClient.api_post')
def test_sailthru_on_success(self, mock_sailthru, mock_log_error):
"""
Ensure that a successful unsubscribe does not trigger an error log
"""
mock_sailthru.return_value = SailthruResponse(JsonResponse({'ok': True}))
force_unsubscribe_all(sender=self.__class__, email=self.user.email)
self.assertTrue(mock_sailthru.called)
self.assertFalse(mock_log_error.called)
@patch('email_marketing.tasks.log.error')
@patch('email_marketing.tasks.SailthruClient.api_post')
def test_sailthru_on_connection_error(self, mock_sailthru, mock_log_error):
mock_sailthru.side_effect = SailthruClientError
with self.assertRaises(Exception):
force_unsubscribe_all(sender=self.__class__, email=self.user.email)
self.assertTrue(mock_log_error.called)
@patch('email_marketing.tasks.log.error')
@patch('email_marketing.tasks.SailthruClient.api_post')
def test_sailthru_on_bad_response(self, mock_sailthru, mock_log_error):
mock_sailthru.return_value = SailthruResponse(JsonResponse({'error': 100, 'errormsg': 'Got an error'}))
with self.assertRaises(Exception):
force_unsubscribe_all(sender=self.__class__, email=self.user.email)
self.assertTrue(mock_sailthru.called)
self.assertTrue(mock_log_error.called)
@patch('email_marketing.tasks.log.error')
@patch('email_marketing.tasks.SailthruClient.api_post')
def test_sailthru_with_email_changed(self, mock_sailthru, mock_log_error):
mock_sailthru.return_value = SailthruResponse(JsonResponse({'ok': True}))
force_unsubscribe_all(sender=self.__class__, email=self.user.email, new_email='email@somewhere.invalid')
mock_sailthru.assert_called_with("user", {
"id": self.user.email,
"optout_email": "all",
"keys": {
"email": "email@somewhere.invalid"
},
"fields": {
"optout_email": 1,
"keys": 1
},
"keysconflict": "merge"
})
self.assertFalse(mock_log_error.called)
class MockSailthruResponse(object):
"""