mirror of
https://github.com/ansible/awx.git
synced 2026-05-11 19:37:38 -02:30
Merge pull request #4473 from chrismeyersfsu/fix-3391
associate notifications with job before sending notifications
This commit is contained in:
@@ -109,8 +109,12 @@ def send_notifications(notification_list, job_id=None):
|
|||||||
raise TypeError("notification_list should be of type list")
|
raise TypeError("notification_list should be of type list")
|
||||||
if job_id is not None:
|
if job_id is not None:
|
||||||
job_actual = UnifiedJob.objects.get(id=job_id)
|
job_actual = UnifiedJob.objects.get(id=job_id)
|
||||||
for notification_id in notification_list:
|
|
||||||
notification = Notification.objects.get(id=notification_id)
|
notifications = Notification.objects.filter(id__in=notification_list)
|
||||||
|
if job_id is not None:
|
||||||
|
job_actual.notifications.add(*notifications)
|
||||||
|
|
||||||
|
for notification in notifications:
|
||||||
try:
|
try:
|
||||||
sent = notification.notification_template.send(notification.subject, notification.body)
|
sent = notification.notification_template.send(notification.subject, notification.body)
|
||||||
notification.status = "successful"
|
notification.status = "successful"
|
||||||
@@ -121,8 +125,6 @@ def send_notifications(notification_list, job_id=None):
|
|||||||
notification.error = smart_str(e)
|
notification.error = smart_str(e)
|
||||||
finally:
|
finally:
|
||||||
notification.save()
|
notification.save()
|
||||||
if job_id is not None:
|
|
||||||
job_actual.notifications.add(notification)
|
|
||||||
|
|
||||||
|
|
||||||
@task(bind=True, queue='default')
|
@task(bind=True, queue='default')
|
||||||
|
|||||||
@@ -38,17 +38,17 @@ def test_send_notifications_list(mocker):
|
|||||||
mock_job = mocker.MagicMock(spec=UnifiedJob)
|
mock_job = mocker.MagicMock(spec=UnifiedJob)
|
||||||
patches.append(mocker.patch('awx.main.models.UnifiedJob.objects.get', return_value=mock_job))
|
patches.append(mocker.patch('awx.main.models.UnifiedJob.objects.get', return_value=mock_job))
|
||||||
|
|
||||||
mock_notification = mocker.MagicMock(spec=Notification, subject="test", body={'hello': 'world'})
|
mock_notifications = [mocker.MagicMock(spec=Notification, subject="test", body={'hello': 'world'})]
|
||||||
patches.append(mocker.patch('awx.main.models.Notification.objects.get', return_value=mock_notification))
|
patches.append(mocker.patch('awx.main.models.Notification.objects.filter', return_value=mock_notifications))
|
||||||
|
|
||||||
with apply_patches(patches):
|
with apply_patches(patches):
|
||||||
send_notifications([1,2], job_id=1)
|
send_notifications([1,2], job_id=1)
|
||||||
assert Notification.objects.get.call_count == 2
|
assert Notification.objects.filter.call_count == 1
|
||||||
assert mock_notification.status == "successful"
|
assert mock_notifications[0].status == "successful"
|
||||||
assert mock_notification.save.called
|
assert mock_notifications[0].save.called
|
||||||
|
|
||||||
assert mock_job.notifications.add.called
|
assert mock_job.notifications.add.called
|
||||||
assert mock_job.notifications.add.called_with(mock_notification)
|
assert mock_job.notifications.add.called_with(*mock_notifications)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("current_instances,call_count", [(91, 2), (89,1)])
|
@pytest.mark.parametrize("current_instances,call_count", [(91, 2), (89,1)])
|
||||||
|
|||||||
Reference in New Issue
Block a user