def test_remove_sender_from_notification(self, get_mail_client, get_notif_subject, get_notif_html) -> None: """ Test sender is removed if they exist in recipients :return: """ with local_app.app_context(): mock_client = MockMailClient(status_code=HTTPStatus.OK) mock_client.send_email = unittest.mock.Mock() get_mail_client.return_value = mock_client mock_subject = 'Test Subject' get_notif_subject.return_value = mock_subject mock_html = '<div>test html</div>' get_notif_html.return_value = mock_html test_sender = '*****@*****.**' test_recipients = [test_sender, '*****@*****.**'] test_notification_type = NotificationType.OWNER_ADDED test_options = {} expected_recipients = ['*****@*****.**'] send_notification(notification_type=test_notification_type, options=test_options, recipients=test_recipients, sender=test_sender) mock_client.send_email.assert_called_with( recipients=expected_recipients, sender=test_sender, subject=mock_subject, html=mock_html, optional_data={'email_type': test_notification_type}, )
def notification() -> Response: """ Uses the instance of BaseMailClient client configured on the MAIL_CLIENT config variable to send a notification email based on data passed from the request """ try: data = request.get_json() notification_type = data.get('notificationType') if notification_type is None: message = 'Encountered exception: notificationType must be provided in the request payload' logging.exception(message) return make_response(jsonify({'msg': message}), HTTPStatus.BAD_REQUEST) sender = data.get('sender') if sender is None: sender = app.config['AUTH_USER_METHOD'](app).email options = data.get('options', {}) recipients = data.get('recipients', []) return send_notification(notification_type=notification_type, options=options, recipients=recipients, sender=sender) except Exception as e: message = 'Encountered exception: ' + str(e) logging.exception(message) return make_response(jsonify({'msg': message}), HTTPStatus.INTERNAL_SERVER_ERROR)
def test_send_notification_success(self, get_mail_client, get_notif_subject, get_notif_html) -> None: """ Test successful execution of send_notification :return: """ with local_app.app_context(): get_mail_client.return_value = MockMailClient( status_code=HTTPStatus.OK) get_notif_subject.return_value = 'Test Subject' get_notif_html.return_value = '<div>test html</div>' test_recipients = ['*****@*****.**'] test_sender = '*****@*****.**' test_notification_type = NotificationType.OWNER_ADDED test_options = {} response = send_notification( notification_type=test_notification_type, options=test_options, recipients=test_recipients, sender=test_sender) get_mail_client.assert_called 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.assertEqual(response.status_code, HTTPStatus.OK)
def test_send_notification_disabled(self) -> None: """ Test send_notification fails gracefully if notifications are not enabled :return: """ with local_no_notification_app.app_context(): response = send_notification( notification_type=NotificationType.OWNER_ADDED, options={}, recipients=['*****@*****.**'], sender='*****@*****.**') self.assertEqual(response.status_code, HTTPStatus.ACCEPTED)
def test_no_recipients_for_notification(self) -> None: """ Test 200 response with appropriate message if no recipients exist :return: """ with local_app.app_context(): response = send_notification( notification_type=NotificationType.OWNER_ADDED, options={}, recipients=[], sender='*****@*****.**' ) data = json.loads(response.data) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(data.get('msg'), 'No valid recipients exist for notification, notification was not sent.')
def test_send_notification_success(self, get_mail_client: Mock, get_notif_subject: Mock, get_notif_html: Mock) -> None: """ Test successful execution of send_notification :return: """ status_codes = [HTTPStatus.OK, HTTPStatus.ACCEPTED] for status_code in status_codes: with self.subTest(): with local_app.app_context(): get_mail_client.return_value = MockMailClient( status_code=status_code) get_notif_subject.return_value = 'Test Subject' get_notif_html.return_value = '<div>test html</div>' test_recipients = ['*****@*****.**'] test_sender = '*****@*****.**' test_notification_type = NotificationType.OWNER_ADDED test_options: Dict = {} response = send_notification( notification_type=test_notification_type, options=test_options, recipients=test_recipients, sender=test_sender) get_mail_client.assert_called 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)