mirror of
https://github.com/ansible/awx.git
synced 2026-03-29 06:45:09 -02:30
Disallow DELETE on a notification_template
When there are pending notifications
This commit is contained in:
@@ -3487,6 +3487,15 @@ class NotificationTemplateDetail(RetrieveUpdateDestroyAPIView):
|
|||||||
serializer_class = NotificationTemplateSerializer
|
serializer_class = NotificationTemplateSerializer
|
||||||
new_in_300 = True
|
new_in_300 = True
|
||||||
|
|
||||||
|
def delete(self, request, *args, **kwargs):
|
||||||
|
obj = self.get_object()
|
||||||
|
if not request.user.can_access(self.model, 'delete', obj):
|
||||||
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||||
|
if obj.notifications.filter(status='pending').exists():
|
||||||
|
return Response({"error": "Delete not allowed while there are pending notifications"},
|
||||||
|
status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||||
|
return resp
|
||||||
|
|
||||||
class NotificationTemplateTest(GenericAPIView):
|
class NotificationTemplateTest(GenericAPIView):
|
||||||
|
|
||||||
view_name = 'NotificationTemplate Test'
|
view_name = 'NotificationTemplate Test'
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import mock
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from awx.main.models.notifications import NotificationTemplate
|
from awx.main.models.notifications import NotificationTemplate, Notification
|
||||||
from awx.main.models.inventory import Inventory, Group
|
from awx.main.models.inventory import Inventory, Group
|
||||||
from awx.main.models.jobs import JobTemplate
|
from awx.main.models.jobs import JobTemplate
|
||||||
|
|
||||||
@@ -112,3 +112,13 @@ def test_notification_template_simple_patch(patch, notification_template, admin)
|
|||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_notification_template_invalid_notification_type(patch, notification_template, admin):
|
def test_notification_template_invalid_notification_type(patch, notification_template, admin):
|
||||||
patch(reverse('api:notification_template_detail', args=(notification_template.id,)), { 'notification_type': 'invalid'}, admin, expect=400)
|
patch(reverse('api:notification_template_detail', args=(notification_template.id,)), { 'notification_type': 'invalid'}, admin, expect=400)
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_disallow_delete_when_notifications_pending(delete, user, notification_template):
|
||||||
|
u = user('superuser', True)
|
||||||
|
url = reverse('api:notification_template_detail', args=(notification_template.id,))
|
||||||
|
n = Notification.objects.create(notification_template=notification_template,
|
||||||
|
status='pending')
|
||||||
|
response = delete(url, user=u)
|
||||||
|
assert response.status_code == 405
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user