diff --git a/seqr/utils/communication_utils.py b/seqr/utils/communication_utils.py index 5df9d32825..10bc7caa3d 100644 --- a/seqr/utils/communication_utils.py +++ b/seqr/utils/communication_utils.py @@ -70,10 +70,9 @@ def send_project_notification(project, notification, subject, email_template=Non email_body=BASE_EMAIL_TEMPLATE.format(email), to=list(users.values_list('email', flat=True)), subject=subject, - process_message=_set_bulk_notification_stream, ) try: - send_html_email(**email_kwargs) + send_html_email(**email_kwargs, process_message=_set_bulk_notification_stream) except Exception as e: logger.error(f'Error sending project email for {project.guid}: {e}', extra={'detail': email_kwargs}) diff --git a/seqr/views/apis/dataset_api_tests.py b/seqr/views/apis/dataset_api_tests.py index a3a88e6ad8..5843c6aa0f 100644 --- a/seqr/views/apis/dataset_api_tests.py +++ b/seqr/views/apis/dataset_api_tests.py @@ -46,11 +46,10 @@ class DatasetAPITest(object): @mock.patch('seqr.models.random.randint') - @mock.patch('seqr.utils.communication_utils.logger') @mock.patch('seqr.utils.communication_utils.send_html_email') @mock.patch('seqr.utils.communication_utils.BASE_URL', 'https://seqr.broadinstitute.org/') @urllib3_responses.activate - def test_add_variants_dataset(self, mock_send_email, mock_logger, mock_random): + def test_add_variants_dataset(self, mock_send_email, mock_random): url = reverse(add_variants_dataset_handler, args=[PROJECT_GUID]) self.check_data_manager_login(url) @@ -247,6 +246,7 @@ def test_add_variants_dataset(self, mock_send_email, mock_logger, mock_random): 'sample_ids': {'buckets': [{'key': 'NA21234'}]} }}, method=urllib3_responses.POST) + self.reset_logs() mock_send_email.reset_mock() mock_send_email.side_effect = Exception('Email server is not configured') response = self.client.post(url, content_type='application/json', data=json.dumps({ @@ -259,11 +259,15 @@ def test_add_variants_dataset(self, mock_send_email, mock_logger, mock_random): mock_send_email, sample_type='WES', count=1, project_guid=NON_ANALYST_PROJECT_GUID, project_name='Non-Analyst Project', recipient='test_user_collaborator@test.com', ) - mock_logger.error.assert_called_with( - 'Error sending project email for R0004_non_analyst_project: Email server is not configured', extra={'detail': { - 'email_body': mock.ANY, 'process_message': mock.ANY, + self.assert_json_logs(user=None, offset=6, expected=[( + 'Error sending project email for R0004_non_analyst_project: Email server is not configured', {'detail': { + 'email_body': mock.ANY, 'subject': 'New data available in seqr', 'to': ['test_user_collaborator@test.com'], - }}) + }, + '@type': 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent', + 'severity': 'ERROR', + }) + ]) def _assert_expected_notification(self, mock_send_email, sample_type, count, email_content=None, project_guid=PROJECT_GUID, project_name='1kg project nåme with uniçøde', diff --git a/seqr/views/utils/test_utils.py b/seqr/views/utils/test_utils.py index e3e4eb1d01..1b6ce0225c 100644 --- a/seqr/views/utils/test_utils.py +++ b/seqr/views/utils/test_utils.py @@ -248,15 +248,19 @@ def reset_logs(self): self._log_stream.truncate(0) self._log_stream.seek(0) - def assert_json_logs(self, user, expected): + def assert_json_logs(self, user, expected, offset=0): logs = self._log_stream.getvalue().split('\n') + if offset: + logs = logs[offset:] for i, (message, extra) in enumerate(expected): extra = extra or {} validate = extra.pop('validate', None) log_value = json.loads(logs[i]) expected_log = { - 'timestamp': mock.ANY, 'severity': 'INFO', 'user': user.email, **extra, + 'timestamp': mock.ANY, 'severity': 'INFO', **extra, } + if user: + expected_log['user'] = user.email if message is not None: expected_log['message'] = message self.assertDictEqual(log_value, expected_log)