chore: rename braze_client to email_client
This commit is contained in:
committed by
Muhammad Faraz Maqsood
parent
5858be028b
commit
b3f9903a4b
@@ -9,7 +9,7 @@ from django.core.mail.message import EmailMultiAlternatives
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.template.loader import get_template
|
||||
|
||||
from lms.djangoapps.utils import get_braze_client
|
||||
from lms.djangoapps.utils import get_email_client
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -69,12 +69,12 @@ class Command(BaseCommand):
|
||||
logger.info(f'Retrieving unsubscribed emails from {start_date} to {end_date}')
|
||||
|
||||
try:
|
||||
braze_client = get_braze_client()
|
||||
if not braze_client:
|
||||
logger.info('No Braze client found. Unable to retrieve unsubscribed emails.')
|
||||
email_client = get_email_client()
|
||||
if not email_client:
|
||||
logger.info('No Email client found. Unable to retrieve unsubscribed emails.')
|
||||
return
|
||||
|
||||
emails = braze_client.retrieve_unsubscribed_emails(
|
||||
emails = email_client.retrieve_unsubscribed_emails(
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@ import logging
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from lms.djangoapps.utils import get_braze_client
|
||||
from lms.djangoapps.utils import get_email_client
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
CHUNK_SIZE = 50
|
||||
@@ -63,10 +63,10 @@ class Command(BaseCommand):
|
||||
chunks = self._chunk_list(emails)
|
||||
|
||||
try:
|
||||
braze_client = get_braze_client()
|
||||
if braze_client:
|
||||
email_client = get_email_client()
|
||||
if email_client:
|
||||
for i, chunk in enumerate(chunks):
|
||||
braze_client.unsubscribe_user_email(
|
||||
email_client.unsubscribe_user_email(
|
||||
email=chunk,
|
||||
)
|
||||
logger.info(f"Successfully unsubscribed for chunk-{i + 1} consist of {len(chunk)} emails")
|
||||
|
||||
@@ -39,14 +39,14 @@ class RetrieveUnsubscribedEmailsTests(TestCase):
|
||||
BRAZE_UNSUBSCRIBED_EMAILS_RECIPIENT_EMAIL=['test@example.com']
|
||||
)
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.EmailMultiAlternatives.send')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_braze_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_email_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.logger.info')
|
||||
def test_retrieve_unsubscribed_emails_command(self, mock_logger_info, mock_get_braze_client, mock_send):
|
||||
def test_retrieve_unsubscribed_emails_command(self, mock_logger_info, mock_get_email_client, mock_send):
|
||||
"""
|
||||
Test the retrieve_unsubscribed_emails command
|
||||
"""
|
||||
mock_braze_client = mock_get_braze_client.return_value
|
||||
mock_braze_client.retrieve_unsubscribed_emails.return_value = [
|
||||
mock_email_client = mock_get_email_client.return_value
|
||||
mock_email_client.retrieve_unsubscribed_emails.return_value = [
|
||||
{'email': 'test1@example.com', 'unsubscribed_at': '2023-06-01 10:00:00'},
|
||||
{'email': 'test2@example.com', 'unsubscribed_at': '2023-06-02 12:00:00'},
|
||||
]
|
||||
@@ -81,14 +81,14 @@ class RetrieveUnsubscribedEmailsTests(TestCase):
|
||||
BRAZE_UNSUBSCRIBED_EMAILS_RECIPIENT_EMAIL=['test@example.com']
|
||||
)
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.EmailMultiAlternatives.send')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_braze_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_email_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.logger.info')
|
||||
def test_retrieve_unsubscribed_emails_command_with_dates(self, mock_logger_info, mock_get_braze_client, mock_send):
|
||||
def test_retrieve_unsubscribed_emails_command_with_dates(self, mock_logger_info, mock_get_email_client, mock_send):
|
||||
"""
|
||||
Test the retrieve_unsubscribed_emails command with custom start and end dates.
|
||||
"""
|
||||
mock_braze_client = mock_get_braze_client.return_value
|
||||
mock_braze_client.retrieve_unsubscribed_emails.return_value = [
|
||||
mock_email_client = mock_get_email_client.return_value
|
||||
mock_email_client.retrieve_unsubscribed_emails.return_value = [
|
||||
{'email': 'test3@example.com', 'unsubscribed_at': '2023-06-03 08:00:00'},
|
||||
{'email': 'test4@example.com', 'unsubscribed_at': '2023-06-04 14:00:00'},
|
||||
]
|
||||
@@ -123,15 +123,15 @@ class RetrieveUnsubscribedEmailsTests(TestCase):
|
||||
self.assertIn('test4@example.com,2023-06-04 14:00:00', csv_data)
|
||||
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.EmailMultiAlternatives.send')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_braze_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_email_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.logger.exception')
|
||||
def test_retrieve_unsubscribed_emails_command_braze_exception(self, mock_logger_exception, mock_get_braze_client,
|
||||
def test_retrieve_unsubscribed_emails_command_braze_exception(self, mock_logger_exception, mock_get_email_client,
|
||||
mock_send):
|
||||
"""
|
||||
Test the retrieve_unsubscribed_emails command when an exception is raised.
|
||||
"""
|
||||
mock_braze_client = mock_get_braze_client.return_value
|
||||
mock_braze_client.retrieve_unsubscribed_emails.side_effect = Exception('Braze API error')
|
||||
mock_email_client = mock_get_email_client.return_value
|
||||
mock_email_client.retrieve_unsubscribed_emails.side_effect = Exception('Braze API error')
|
||||
mock_send.return_value = MagicMock()
|
||||
|
||||
with self.assertRaises(CommandError):
|
||||
@@ -143,14 +143,14 @@ class RetrieveUnsubscribedEmailsTests(TestCase):
|
||||
mock_send.assert_not_called()
|
||||
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.EmailMultiAlternatives.send')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_braze_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_email_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.logger.info')
|
||||
def test_retrieve_unsubscribed_emails_command_no_data(self, mock_logger_info, mock_get_braze_client, mock_send):
|
||||
def test_retrieve_unsubscribed_emails_command_no_data(self, mock_logger_info, mock_get_email_client, mock_send):
|
||||
"""
|
||||
Test the retrieve_unsubscribed_emails command when no unsubscribed emails are returned.
|
||||
"""
|
||||
mock_braze_client = mock_get_braze_client.return_value
|
||||
mock_braze_client.retrieve_unsubscribed_emails.return_value = []
|
||||
mock_email_client = mock_get_email_client.return_value
|
||||
mock_email_client.retrieve_unsubscribed_emails.return_value = []
|
||||
mock_send.return_value = MagicMock()
|
||||
|
||||
call_command('retrieve_unsubscribed_emails')
|
||||
@@ -166,15 +166,15 @@ class RetrieveUnsubscribedEmailsTests(TestCase):
|
||||
BRAZE_UNSUBSCRIBED_EMAILS_RECIPIENT_EMAIL=['test@example.com']
|
||||
)
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.EmailMultiAlternatives.send')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_braze_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.get_email_client')
|
||||
@patch('common.djangoapps.student.management.commands.retrieve_unsubscribed_emails.logger.exception')
|
||||
def test_retrieve_unsubscribed_emails_command_error_sending_email(self, mock_logger_exception,
|
||||
mock_get_braze_client, mock_send):
|
||||
mock_get_email_client, mock_send):
|
||||
"""
|
||||
Test the retrieve_unsubscribed_emails command when an error occurs during email sending.
|
||||
"""
|
||||
mock_braze_client = mock_get_braze_client.return_value
|
||||
mock_braze_client.retrieve_unsubscribed_emails.return_value = [
|
||||
mock_email_client = mock_get_email_client.return_value
|
||||
mock_email_client.retrieve_unsubscribed_emails.return_value = [
|
||||
{'email': 'test1@example.com', 'unsubscribed_at': '2023-06-01 10:00:00'},
|
||||
]
|
||||
mock_send.side_effect = Exception('Email sending error')
|
||||
|
||||
@@ -37,8 +37,8 @@ class UnsubscribeUserEmailTests(TestCase):
|
||||
csv.seek(0)
|
||||
return csv
|
||||
|
||||
@patch("common.djangoapps.student.management.commands.unsubscribe_user_email.get_braze_client")
|
||||
def test_unsubscribe_user_email(self, mock_get_braze_client):
|
||||
@patch("common.djangoapps.student.management.commands.unsubscribe_user_email.get_email_client")
|
||||
def test_unsubscribe_user_email(self, mock_get_email_client):
|
||||
""" Test CSV file to unsubscribe user's email"""
|
||||
|
||||
with NamedTemporaryFile() as csv:
|
||||
@@ -50,7 +50,7 @@ class UnsubscribeUserEmailTests(TestCase):
|
||||
csv.name
|
||||
)
|
||||
|
||||
mock_get_braze_client.assert_called_once()
|
||||
mock_get_email_client.assert_called_once()
|
||||
|
||||
def test_command_error_for_csv_path(self):
|
||||
""" Test command error raised if csv_path is not valid"""
|
||||
|
||||
@@ -12,7 +12,7 @@ from django.db.models.signals import post_delete, post_save, pre_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
from lms.djangoapps.courseware.toggles import courseware_mfe_progress_milestones_are_active
|
||||
from lms.djangoapps.utils import get_braze_client
|
||||
from lms.djangoapps.utils import get_email_client
|
||||
from common.djangoapps.student.helpers import EMAIL_EXISTS_MSG_FMT, USERNAME_EXISTS_MSG_FMT, AccountValidationError
|
||||
from common.djangoapps.student.models import (
|
||||
CourseAccessRole,
|
||||
@@ -145,8 +145,8 @@ def _listen_for_user_email_changed(sender, user, request, **kwargs):
|
||||
attributes = [{'email': email, 'external_id': user_id}]
|
||||
|
||||
try:
|
||||
braze_client = get_braze_client()
|
||||
if braze_client:
|
||||
braze_client.track_user(attributes=attributes)
|
||||
email_client = get_email_client()
|
||||
if email_client:
|
||||
email_client.track_user(attributes=attributes)
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
logger.exception(f'Unable to sync new email [{email}] with Braze for user [{user_id}]')
|
||||
|
||||
@@ -14,7 +14,7 @@ from common.djangoapps.student.helpers import (
|
||||
get_course_dates_for_email,
|
||||
get_instructors,
|
||||
)
|
||||
from lms.djangoapps.utils import get_braze_client
|
||||
from lms.djangoapps.utils import get_email_client
|
||||
from openedx.core.djangoapps.catalog.utils import (
|
||||
get_course_uuid_for_course,
|
||||
get_owners_for_course,
|
||||
@@ -131,9 +131,9 @@ def send_course_enrollment_email(
|
||||
|
||||
try:
|
||||
recipients = [{"external_user_id": user_id}]
|
||||
braze_client = get_braze_client()
|
||||
if braze_client:
|
||||
braze_client.send_canvas_message(
|
||||
email_client = get_email_client()
|
||||
if email_client:
|
||||
email_client.send_canvas_message(
|
||||
canvas_id=settings.BRAZE_COURSE_ENROLLMENT_CANVAS_ID,
|
||||
recipients=recipients,
|
||||
canvas_entry_properties=canvas_entry_properties,
|
||||
|
||||
@@ -72,11 +72,11 @@ class ReceiversTest(SharedModuleStoreTestCase):
|
||||
assert profile.name == new_name
|
||||
|
||||
@skip_unless_lms
|
||||
@patch('common.djangoapps.student.signals.receivers.get_braze_client')
|
||||
def test_listen_for_user_email_changed(self, mock_get_braze_client):
|
||||
@patch('common.djangoapps.student.signals.receivers.get_email_client')
|
||||
def test_listen_for_user_email_changed(self, mock_get_email_client):
|
||||
"""
|
||||
Ensure that USER_EMAIL_CHANGED signal triggers correct calls to
|
||||
get_braze_client and update email in session.
|
||||
get_email_client and update email in session.
|
||||
"""
|
||||
user = UserFactory(email='email@test.com', username='jdoe')
|
||||
request = get_mock_request(user=user)
|
||||
@@ -88,5 +88,5 @@ class ReceiversTest(SharedModuleStoreTestCase):
|
||||
|
||||
USER_EMAIL_CHANGED.send(sender=None, user=user, request=request)
|
||||
|
||||
assert mock_get_braze_client.called
|
||||
assert mock_get_email_client.called
|
||||
assert request.session.get('email', None) == user.email
|
||||
|
||||
@@ -173,10 +173,10 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
@patch("common.djangoapps.student.tasks.get_owners_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_course_run_details")
|
||||
@patch("common.djangoapps.student.tasks.get_course_dates_for_email")
|
||||
@patch("common.djangoapps.student.tasks.get_braze_client")
|
||||
@patch("common.djangoapps.student.tasks.get_email_client")
|
||||
def test_success_calls_for_canvas_properties(
|
||||
self,
|
||||
mock_get_braze_client,
|
||||
mock_get_email_client,
|
||||
mock_get_course_dates_for_email,
|
||||
mock_get_course_run_details,
|
||||
mock_get_owners_for_course,
|
||||
@@ -194,7 +194,7 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
send_course_enrollment_email.apply_async(
|
||||
kwargs=self.send_course_enrollment_email_kwargs
|
||||
)
|
||||
mock_get_braze_client.return_value.send_canvas_message.assert_called_with(
|
||||
mock_get_email_client.return_value.send_canvas_message.assert_called_with(
|
||||
canvas_id=BRAZE_COURSE_ENROLLMENT_CANVAS_ID,
|
||||
recipients=[
|
||||
{
|
||||
@@ -207,14 +207,14 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
@patch("common.djangoapps.student.tasks.get_course_uuid_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_owners_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_course_run_details")
|
||||
@patch("common.djangoapps.student.tasks.get_braze_client")
|
||||
@patch("common.djangoapps.student.tasks.get_email_client")
|
||||
@patch(
|
||||
"common.djangoapps.student.tasks.get_course_dates_for_email",
|
||||
Mock(side_effect=Exception),
|
||||
)
|
||||
def test_canvas_properties_without_course_dates(
|
||||
self,
|
||||
mock_get_braze_client,
|
||||
mock_get_email_client,
|
||||
mock_get_course_run_details,
|
||||
mock_get_owners_for_course,
|
||||
mock_get_course_uuid_for_course,
|
||||
@@ -230,7 +230,7 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
send_course_enrollment_email.apply_async(
|
||||
kwargs=self.send_course_enrollment_email_kwargs
|
||||
)
|
||||
mock_get_braze_client.return_value.send_canvas_message.assert_called_with(
|
||||
mock_get_email_client.return_value.send_canvas_message.assert_called_with(
|
||||
canvas_id=BRAZE_COURSE_ENROLLMENT_CANVAS_ID,
|
||||
recipients=[
|
||||
{
|
||||
@@ -243,14 +243,14 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
@patch("common.djangoapps.student.tasks.get_course_uuid_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_owners_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_course_dates_for_email")
|
||||
@patch("common.djangoapps.student.tasks.get_braze_client")
|
||||
@patch("common.djangoapps.student.tasks.get_email_client")
|
||||
@patch(
|
||||
"common.djangoapps.student.tasks.get_course_run_details",
|
||||
Mock(side_effect=Exception),
|
||||
)
|
||||
def test_canvas_properties_on_get_course_run_details_failure(
|
||||
self,
|
||||
mock_get_braze_client,
|
||||
mock_get_email_client,
|
||||
mock_get_course_dates_for_email,
|
||||
mock_get_owners_for_course,
|
||||
mock_get_course_uuid_for_course,
|
||||
@@ -266,7 +266,7 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
send_course_enrollment_email.apply_async(
|
||||
kwargs=self.send_course_enrollment_email_kwargs
|
||||
)
|
||||
mock_get_braze_client.return_value.send_canvas_message.assert_called_with(
|
||||
mock_get_email_client.return_value.send_canvas_message.assert_called_with(
|
||||
canvas_id=BRAZE_COURSE_ENROLLMENT_CANVAS_ID,
|
||||
recipients=[
|
||||
{
|
||||
@@ -280,12 +280,12 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
|
||||
@patch("common.djangoapps.student.tasks.get_course_uuid_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_course_dates_for_email")
|
||||
@patch("common.djangoapps.student.tasks.get_braze_client")
|
||||
@patch("common.djangoapps.student.tasks.get_email_client")
|
||||
@patch(TASK_LOGGER)
|
||||
def test_email_task_when_course_uuid_is_missing(
|
||||
self,
|
||||
mocked_logger,
|
||||
mock_get_braze_client,
|
||||
mock_get_email_client,
|
||||
mock_get_course_dates_for_email,
|
||||
mock_get_course_uuid_for_course,
|
||||
):
|
||||
@@ -304,7 +304,7 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
f"[Course Enrollment] Course run call failed for "
|
||||
f"user: {self.user.id} course: {self.course.id} error: Missing course_uuid"
|
||||
)
|
||||
mock_get_braze_client.return_value.send_canvas_message.assert_called_with(
|
||||
mock_get_email_client.return_value.send_canvas_message.assert_called_with(
|
||||
canvas_id=BRAZE_COURSE_ENROLLMENT_CANVAS_ID,
|
||||
recipients=[
|
||||
{
|
||||
@@ -318,12 +318,12 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
@patch("common.djangoapps.student.tasks.get_owners_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_course_run_details")
|
||||
@patch("common.djangoapps.student.tasks.get_course_dates_for_email")
|
||||
@patch("common.djangoapps.student.tasks.get_braze_client")
|
||||
@patch("common.djangoapps.student.tasks.get_email_client")
|
||||
@patch(TASK_LOGGER)
|
||||
def test_email_task_when_course_run_is_missing(
|
||||
self,
|
||||
mocked_logger,
|
||||
mock_get_braze_client,
|
||||
mock_get_email_client,
|
||||
mock_get_course_dates_for_email,
|
||||
mock_get_course_run_details,
|
||||
mock_get_owners_for_course,
|
||||
@@ -346,7 +346,7 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
f"[Course Enrollment] Course run call failed for "
|
||||
f"user: {self.user.id} course: {self.course.id} error: Missing course_run"
|
||||
)
|
||||
mock_get_braze_client.return_value.send_canvas_message.assert_called_with(
|
||||
mock_get_email_client.return_value.send_canvas_message.assert_called_with(
|
||||
canvas_id=BRAZE_COURSE_ENROLLMENT_CANVAS_ID,
|
||||
recipients=[
|
||||
{
|
||||
@@ -360,7 +360,7 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
@patch("common.djangoapps.student.tasks.get_owners_for_course")
|
||||
@patch("common.djangoapps.student.tasks.get_course_run_details")
|
||||
@patch("common.djangoapps.student.tasks.get_course_dates_for_email")
|
||||
def test_retry_with_braze_client_exception(
|
||||
def test_retry_with_email_client_exception(
|
||||
self,
|
||||
mock_get_course_dates_for_email,
|
||||
mock_get_course_run_details,
|
||||
@@ -377,12 +377,12 @@ class TestCourseEnrollmentEmailTask(ModuleStoreTestCase):
|
||||
mock_get_course_dates_for_email.return_value = self._get_course_dates()
|
||||
|
||||
with patch(
|
||||
'common.djangoapps.student.tasks.get_braze_client',
|
||||
'common.djangoapps.student.tasks.get_email_client',
|
||||
new_callable=PropertyMock,
|
||||
side_effect=Exception('Braze Client Exception')
|
||||
) as mock_get_braze_client:
|
||||
) as mock_get_email_client:
|
||||
task = send_course_enrollment_email.apply_async(
|
||||
kwargs=self.send_course_enrollment_email_kwargs
|
||||
)
|
||||
pytest.raises(Exception, task.get)
|
||||
self.assertEqual(mock_get_braze_client.call_count, (MAX_RETRIES + 1))
|
||||
self.assertEqual(mock_get_email_client.call_count, (MAX_RETRIES + 1))
|
||||
|
||||
@@ -27,7 +27,7 @@ def _get_key(key_or_id, key_cls):
|
||||
)
|
||||
|
||||
|
||||
def get_braze_client():
|
||||
def get_email_client():
|
||||
""" Returns a Braze client. """
|
||||
if not BrazeClient:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user