From b3f9903a4b9fe8c9b2eb95a0f1231cefadd77cec Mon Sep 17 00:00:00 2001 From: Muhammad Faraz Maqsood Date: Fri, 21 Mar 2025 19:01:51 +0500 Subject: [PATCH] chore: rename braze_client to email_client --- .../commands/retrieve_unsubscribed_emails.py | 10 ++--- .../commands/unsubscribe_user_email.py | 8 ++-- .../test_retrieve_unsubscribed_emails.py | 40 +++++++++---------- .../tests/test_unsubscribe_user_email.py | 6 +-- .../djangoapps/student/signals/receivers.py | 8 ++-- common/djangoapps/student/tasks.py | 8 ++-- .../student/tests/test_receivers.py | 8 ++-- common/djangoapps/student/tests/test_tasks.py | 38 +++++++++--------- lms/djangoapps/utils.py | 2 +- 9 files changed, 64 insertions(+), 64 deletions(-) diff --git a/common/djangoapps/student/management/commands/retrieve_unsubscribed_emails.py b/common/djangoapps/student/management/commands/retrieve_unsubscribed_emails.py index a9abd8831d..3c79d86879 100644 --- a/common/djangoapps/student/management/commands/retrieve_unsubscribed_emails.py +++ b/common/djangoapps/student/management/commands/retrieve_unsubscribed_emails.py @@ -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, ) diff --git a/common/djangoapps/student/management/commands/unsubscribe_user_email.py b/common/djangoapps/student/management/commands/unsubscribe_user_email.py index 23973b8475..f24db4cef8 100644 --- a/common/djangoapps/student/management/commands/unsubscribe_user_email.py +++ b/common/djangoapps/student/management/commands/unsubscribe_user_email.py @@ -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") diff --git a/common/djangoapps/student/management/tests/test_retrieve_unsubscribed_emails.py b/common/djangoapps/student/management/tests/test_retrieve_unsubscribed_emails.py index 5a2a74302e..e86badf885 100644 --- a/common/djangoapps/student/management/tests/test_retrieve_unsubscribed_emails.py +++ b/common/djangoapps/student/management/tests/test_retrieve_unsubscribed_emails.py @@ -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') diff --git a/common/djangoapps/student/management/tests/test_unsubscribe_user_email.py b/common/djangoapps/student/management/tests/test_unsubscribe_user_email.py index 66d0be8db3..0e9c9f0266 100644 --- a/common/djangoapps/student/management/tests/test_unsubscribe_user_email.py +++ b/common/djangoapps/student/management/tests/test_unsubscribe_user_email.py @@ -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""" diff --git a/common/djangoapps/student/signals/receivers.py b/common/djangoapps/student/signals/receivers.py index 82300f4e3a..529ac261b4 100644 --- a/common/djangoapps/student/signals/receivers.py +++ b/common/djangoapps/student/signals/receivers.py @@ -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}]') diff --git a/common/djangoapps/student/tasks.py b/common/djangoapps/student/tasks.py index c7e0842664..8f9746b1dd 100644 --- a/common/djangoapps/student/tasks.py +++ b/common/djangoapps/student/tasks.py @@ -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, diff --git a/common/djangoapps/student/tests/test_receivers.py b/common/djangoapps/student/tests/test_receivers.py index e987120a9d..424df4759d 100644 --- a/common/djangoapps/student/tests/test_receivers.py +++ b/common/djangoapps/student/tests/test_receivers.py @@ -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 diff --git a/common/djangoapps/student/tests/test_tasks.py b/common/djangoapps/student/tests/test_tasks.py index 99d24fed37..23b4cc19e6 100644 --- a/common/djangoapps/student/tests/test_tasks.py +++ b/common/djangoapps/student/tests/test_tasks.py @@ -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)) diff --git a/lms/djangoapps/utils.py b/lms/djangoapps/utils.py index 23245e9286..cabf13b84f 100644 --- a/lms/djangoapps/utils.py +++ b/lms/djangoapps/utils.py @@ -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