Skip to content

Commit

Permalink
fix: lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed May 8, 2024
1 parent d75f68b commit 6a3cbb7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 25 deletions.
15 changes: 12 additions & 3 deletions license_manager/apps/api/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from license_manager.apps.api_client.mailchimp import (
MailchimpTransactionalApiClient,
)
from license_manager.apps.subscriptions.constants import REMIND_EMAIL_ACTION_TYPE
from license_manager.apps.subscriptions.constants import (
REMIND_EMAIL_ACTION_TYPE,
)
from license_manager.apps.subscriptions.event_utils import (
get_license_tracking_properties,
)
Expand All @@ -31,7 +33,13 @@ def __init__(self) -> None:
else:
raise ValueError("Please set TRANSACTIONAL_MAIL_SERVICE setting to either 'braze' or 'mailchimp'.")

def send_assignment_or_reminder_email(self, pending_licenses, enterprise_customer, custom_template_text, action_type):
def send_assignment_or_reminder_email(
self,
pending_licenses,
enterprise_customer,
custom_template_text,
action_type
):
"""Helper function to send a assignment notification or reminder email.
Args:
Expand Down Expand Up @@ -90,7 +98,8 @@ def send_assignment_or_reminder_email(self, pending_licenses, enterprise_custome
}
)
if settings.TRANSACTIONAL_MAIL_SERVICE == 'braze':
campaign_id = settings.BRAZE_REMIND_EMAIL_CAMPAIGN if action_type == REMIND_EMAIL_ACTION_TYPE else settings.BRAZE_ASSIGNMENT_EMAIL_CAMPAIGN
campaign_id = settings.BRAZE_REMIND_EMAIL_CAMPAIGN if action_type == REMIND_EMAIL_ACTION_TYPE \
else settings.BRAZE_ASSIGNMENT_EMAIL_CAMPAIGN
self._braze_client.send_emails(
campaign_id,
recipients=messages,
Expand Down
4 changes: 2 additions & 2 deletions license_manager/apps/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
ENTERPRISE_BRAZE_ALIAS_LABEL,
LICENSE_UTILIZATION_THRESHOLDS,
NOTIFICATION_CHOICE_AND_CAMPAIGN_BY_THRESHOLD,
NOTIFY_EMAIL_ACTION_TYPE,
PENDING_ACCOUNT_CREATION_BATCH_SIZE,
REMIND_EMAIL_ACTION_TYPE,
REMINDER_EMAIL_BATCH_SIZE,
REVOCABLE_LICENSE_STATUSES,
TRACK_LICENSE_CHANGES_BATCH_SIZE,
NOTIFY_EMAIL_ACTION_TYPE,
REMIND_EMAIL_ACTION_TYPE,
NotificationChoices,
)
from license_manager.apps.subscriptions.event_utils import track_license_changes
Expand Down
64 changes: 50 additions & 14 deletions license_manager/apps/api/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
from django.conf import settings
from django.test import TestCase
from django.test.utils import override_settings
from mailchimp_transactional.api_client import ApiClientError as MailchimpClientError
from freezegun import freeze_time
from mailchimp_transactional.api_client import \
ApiClientError as MailchimpClientError
from requests import models

from license_manager.apps.api import tasks
Expand Down Expand Up @@ -97,9 +98,17 @@ def test_create_braze_aliases_task_reraises_braze_exceptions(self, _):
)

@mock.patch('license_manager.apps.api.tasks.EnterpriseApiClient', return_value=mock.MagicMock())
@mock.patch('license_manager.apps.api_client.braze.BrazeClient.send_campaign_message', return_value=mock.MagicMock())
@mock.patch(
'license_manager.apps.api_client.braze.BrazeClient.send_campaign_message',
return_value=mock.MagicMock()
)
@mock.patch('license_manager.apps.api_client.braze.BrazeClient.create_braze_alias', return_value=mock.MagicMock())
def test_braze_assignment_email_task(self, mock_create_braze_alias, mock_send_campaign_message, mock_enterprise_client):
def test_braze_assignment_email_task(
self,
mock_create_braze_alias,
mock_send_campaign_message,
mock_enterprise_client
):
"""
Assert the `send_assignment_email_task()` calls Braze API with the correct arguments.
"""
Expand Down Expand Up @@ -270,7 +279,10 @@ def test_braze_assignment_task_send_email_failure_logged(

# pylint: disable=unused-argument
@mock.patch('license_manager.apps.api_client.mailchimp.logger', return_value=mock.MagicMock())
@mock.patch('mailchimp_transactional.MessagesApi.send_template', side_effect=MailchimpClientError(text="error", status_code=400))
@mock.patch(
'mailchimp_transactional.MessagesApi.send_template',
side_effect=MailchimpClientError(text="error", status_code=400)
)
@mock.patch('license_manager.apps.api.tasks.EnterpriseApiClient', return_value=mock.MagicMock())
@override_settings(TRANSACTIONAL_MAIL_SERVICE='mailchimp')
def test_mailchimp_assignment_task_send_email_failure_logged(
Expand Down Expand Up @@ -305,7 +317,12 @@ def test_mailchimp_assignment_task_send_email_failure_logged(
)
@mock.patch('license_manager.apps.api_client.braze.BrazeClient.create_braze_alias', return_value=mock.MagicMock())
@mock.patch('license_manager.apps.api.tasks.EnterpriseApiClient', return_value=mock.MagicMock())
def test_braze_send_reminder_email_task(self, mock_enterprise_client, mock_create_alias, mock_send_campaign_message):
def test_braze_send_reminder_email_task(
self,
mock_enterprise_client,
mock_create_alias,
mock_send_campaign_message
):
"""
Assert send_reminder_email_task calls Braze API with the correct arguments
"""
Expand Down Expand Up @@ -425,7 +442,7 @@ def test_mailchimp_send_reminder_email_task(self, mock_enterprise_client, mock_s
template_context = [
{'name': 'TEMPLATE_GREETING', 'content': 'Hello'},
{'name': 'TEMPLATE_CLOSING', 'content': 'Goodbye'},
{'name': 'license_activation_key', 'content': str(expected_license.activation_key)},
{'name': 'license_activation_key', 'content': expected_license_key},
{'name': 'enterprise_customer_slug', 'content': self.enterprise_slug},
{'name': 'enterprise_customer_name', 'content': self.enterprise_name},
{'name': 'enterprise_sender_alias', 'content': self.enterprise_sender_alias},
Expand All @@ -441,14 +458,18 @@ def test_mailchimp_send_reminder_email_task(self, mock_enterprise_client, mock_s
expected_messages.append({'rcpt': user_email, 'vars': template_context})

# assert all recipients sent a campaign message in a single call
actual_emails = mock_send_template.call_args_list[0][1]['body']['message']['to']
actual_recipients_metadata = mock_send_template.call_args_list[0][1]['body']['message']['recipient_metadata']
actual_messages = mock_send_template.call_args_list[0][1]['body']['message']['merge_vars']
message_arg = mock_send_template.call_args_list[0][1]['body']['message']
actual_emails = message_arg['to']
actual_recipients_metadata = message_arg['recipient_metadata']
actual_messages = message_arg['merge_vars']

def sort_key(key='rcpt'):
return lambda x: x[key]

assert sorted(expected_recipient_metadata, key=sort_key()) == sorted(actual_recipients_metadata, key=sort_key())
assert sorted(
expected_recipient_metadata,
key=sort_key()
) == sorted(actual_recipients_metadata, key=sort_key())
assert sorted(expected_messages, key=sort_key()) == sorted(actual_messages, key=sort_key())
assert sorted(expected_emails, key=sort_key('email')) == sorted(actual_emails, key=sort_key('email'))

Expand Down Expand Up @@ -485,10 +506,17 @@ def test_braze_send_reminder_email_failure_no_remind_date_update(self, mock_ente
False
)

@mock.patch('mailchimp_transactional.MessagesApi.send_template', side_effect=MailchimpClientError(text="error", status_code=400))
@mock.patch(
'mailchimp_transactional.MessagesApi.send_template',
side_effect=MailchimpClientError(text="error", status_code=400)
)
@mock.patch('license_manager.apps.api.tasks.EnterpriseApiClient', return_value=mock.MagicMock())
@override_settings(TRANSACTIONAL_MAIL_SERVICE='mailchimp')
def test_mailchimp_send_reminder_email_failure_no_remind_date_update(self, mock_enterprise_client, mock_send_template):
def test_mailchimp_send_reminder_email_failure_no_remind_date_update(
self,
mock_enterprise_client,
mock_send_template
):
"""
Tests that when sending the remind email fails, last_remind_date is not updated
"""
Expand Down Expand Up @@ -629,7 +657,10 @@ def test_braze_send_post_activation_email_task_reraises_braze_exceptions(self, _
tasks.send_post_activation_email_task(self.enterprise_uuid, self.user_email)

@mock.patch('license_manager.apps.api.tasks.EnterpriseApiClient', return_value=mock.MagicMock())
@mock.patch('mailchimp_transactional.MessagesApi.send_template', side_effect=MailchimpClientError(text="error", status_code=400))
@mock.patch(
'mailchimp_transactional.MessagesApi.send_template',
side_effect=MailchimpClientError(text="error", status_code=400)
)
@override_settings(TRANSACTIONAL_MAIL_SERVICE='mailchimp')
def test_mailchimp_send_post_activation_email_task_reraises_braze_exceptions(self, _, mock_enterprise_client):
"""
Expand All @@ -651,7 +682,12 @@ def test_mailchimp_send_post_activation_email_task_reraises_braze_exceptions(sel
'license_manager.apps.api_client.braze.BrazeClient.send_campaign_message',
return_value=mock.MagicMock()
)
def test_braze_revocation_cap_email_task(self, mock_send_campaign_message, mock_create_alias, mock_enterprise_client):
def test_braze_revocation_cap_email_task(
self,
mock_send_campaign_message,
mock_create_alias,
mock_enterprise_client
):
"""
Tests that the email is sent with the right arguments
"""
Expand Down
2 changes: 0 additions & 2 deletions license_manager/apps/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,6 @@ def assign(self, request, subscription_uuid=None): # pylint: disable=unused-arg
]
}
"""
# __AUTO_GENERATED_PRINTF_START__
print(f"LicenseAdminViewSet#assign 1") # __AUTO_GENERATED_PRINTF_END__
subscription_plan = self._get_subscription_plan()
try:
lock_acquired = utils.acquire_subscription_plan_lock(
Expand Down
6 changes: 2 additions & 4 deletions license_manager/apps/api_client/mailchimp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from mailchimp_transactional import Client
from django.conf import settings
from mailchimp_transactional import Client
from mailchimp_transactional.api_client import ApiClientError


Expand Down Expand Up @@ -48,7 +48,7 @@ def send_message(
response object from mailchimp
"""
response = self.messages.send_template(
body = {
body={
'template_name': template_slug,
'template_content': [{}],
'message': {
Expand Down Expand Up @@ -129,7 +129,5 @@ def send_emails(
)
logger.info(success_msg)
except ApiClientError as ex:
# __AUTO_GENERATED_PRINT_VAR_START__
print(f"""======================================= MailchimpTransactionalApiClient#send_emails ex: {ex.text}""") # __AUTO_GENERATED_PRINT_VAR_END__
logger.exception(err_msg)
raise ex

0 comments on commit 6a3cbb7

Please sign in to comment.