Unverified Commit e9316649 authored by Jacob Scherffenberg's avatar Jacob Scherffenberg Committed by GitHub

fix: notification_utils.send_notification accept all 2xx status codes (#860)

* Accept all 2xx status codes from mail clients
Signed-off-by: 's avatarJacobSMoller <scherffenberg91@gmail.com>

* Updates test_send_notification_success to include an ACCEPTED status
Signed-off-by: 's avatarJacobSMoller <scherffenberg91@gmail.com>

* Also check for all 2xx codes for feedback emails
Signed-off-by: 's avatarJacobSMoller <scherffenberg91@gmail.com>
parent e3b83d32
...@@ -56,7 +56,7 @@ def feedback() -> Response: ...@@ -56,7 +56,7 @@ def feedback() -> Response:
response = mail_client.send_email(html=html_content, subject=subject, optional_data=options) response = mail_client.send_email(html=html_content, subject=subject, optional_data=options)
status_code = response.status_code status_code = response.status_code
if status_code == HTTPStatus.OK: if 200 <= status_code < 300:
message = 'Success' message = 'Success'
else: else:
message = 'Mail client failed with status code ' + str(status_code) message = 'Mail client failed with status code ' + str(status_code)
......
...@@ -218,7 +218,7 @@ def send_notification(*, notification_type: str, options: Dict, recipients: List ...@@ -218,7 +218,7 @@ def send_notification(*, notification_type: str, options: Dict, recipients: List
) )
status_code = response.status_code status_code = response.status_code
if status_code == HTTPStatus.OK: if 200 <= status_code < 300:
message = 'Success' message = 'Success'
else: else:
message = 'Mail client failed with status code ' + str(status_code) message = 'Mail client failed with status code ' + str(status_code)
......
...@@ -59,12 +59,15 @@ class MailTest(unittest.TestCase): ...@@ -59,12 +59,15 @@ class MailTest(unittest.TestCase):
Test mail client success Test mail client success
:return: :return:
""" """
local_app.config['MAIL_CLIENT'] = MockMailClient(status_code=200) status_codes = [HTTPStatus.OK, HTTPStatus.ACCEPTED]
with local_app.test_client() as test: for status_code in status_codes:
response = test.post('/api/mail/v0/feedback', json={ local_app.config['MAIL_CLIENT'] = MockMailClient(status_code=status_code)
'rating': '10', 'comment': 'test' with self.subTest():
}) with local_app.test_client() as test:
self.assertEqual(response.status_code, HTTPStatus.OK) response = test.post('/api/mail/v0/feedback', json={
'rating': '10', 'comment': 'test'
})
self.assertTrue(200 <= response.status_code <= 300)
def test_feedback_client_raise_exception(self) -> None: def test_feedback_client_raise_exception(self) -> None:
""" """
......
...@@ -319,29 +319,34 @@ class NotificationUtilsTest(unittest.TestCase): ...@@ -319,29 +319,34 @@ class NotificationUtilsTest(unittest.TestCase):
Test successful execution of send_notification Test successful execution of send_notification
:return: :return:
""" """
with local_app.app_context(): status_codes = [HTTPStatus.OK, HTTPStatus.ACCEPTED]
get_mail_client.return_value = MockMailClient(status_code=HTTPStatus.OK)
get_notif_subject.return_value = 'Test Subject' for status_code in status_codes:
get_notif_html.return_value = '<div>test html</div>' with self.subTest():
with local_app.app_context():
test_recipients = ['test@test.com'] get_mail_client.return_value = MockMailClient(status_code=status_code)
test_sender = 'test2@test.com' get_notif_subject.return_value = 'Test Subject'
test_notification_type = NotificationType.OWNER_ADDED get_notif_html.return_value = '<div>test html</div>'
test_options = {}
test_recipients = ['test@test.com']
response = send_notification( test_sender = 'test2@test.com'
notification_type=test_notification_type, test_notification_type = NotificationType.OWNER_ADDED
options=test_options, test_options = {}
recipients=test_recipients,
sender=test_sender response = send_notification(
) notification_type=test_notification_type,
options=test_options,
get_mail_client.assert_called recipients=test_recipients,
get_notif_subject.assert_called_with(notification_type=test_notification_type, options=test_options) sender=test_sender
get_notif_html.assert_called_with(notification_type=test_notification_type, )
options=test_options,
sender=test_sender) get_mail_client.assert_called
self.assertEqual(response.status_code, HTTPStatus.OK) get_notif_subject.assert_called_with(notification_type=test_notification_type,
options=test_options)
get_notif_html.assert_called_with(notification_type=test_notification_type,
options=test_options,
sender=test_sender)
self.assertTrue(200 <= response.status_code <= 300)
@unittest.mock.patch('amundsen_application.api.utils.notification_utils.get_notification_html') @unittest.mock.patch('amundsen_application.api.utils.notification_utils.get_notification_html')
@unittest.mock.patch('amundsen_application.api.utils.notification_utils.get_notification_subject') @unittest.mock.patch('amundsen_application.api.utils.notification_utils.get_notification_subject')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment