14132 fix api tests

This commit is contained in:
Arthur 2023-11-28 13:19:11 -08:00
parent 92d9e86ecc
commit 6549dce758
4 changed files with 91 additions and 0 deletions

View File

@ -10,6 +10,7 @@ __all__ = [
'NestedCustomFieldChoiceSetSerializer',
'NestedCustomFieldSerializer',
'NestedCustomLinkSerializer',
'NestedEventRuleSerializer',
'NestedExportTemplateSerializer',
'NestedImageAttachmentSerializer',
'NestedJournalEntrySerializer',
@ -19,6 +20,13 @@ __all__ = [
]
class NestedEventRuleSerializer(WritableNestedSerializer):
class Meta:
model = models.EventRule
fields = ['id', 'display', 'name']
class NestedWebhookSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='extras-api:webhook-detail')

View File

@ -68,6 +68,9 @@ class EventRuleSerializer(NetBoxModelSerializer):
many=True
)
action_type = ChoiceField(choices=EventRuleActionChoices)
action_object_type = ContentTypeField(
queryset=ContentType.objects.with_feature('event_rules'),
)
action_object = serializers.SerializerMethodField(read_only=True)
class Meta:

View File

@ -72,3 +72,9 @@ class ExtrasQuery(graphene.ObjectType):
def resolve_webhook_list(root, info, **kwargs):
return gql_query_optimizer(models.Webhook.objects.all(), info)
event_rule = ObjectField(EventRuleType)
event_rule_list = ObjectListField(EventRuleType)
def resolve_eventrule_list(root, info, **kwargs):
return gql_query_optimizer(models.EventRule.objects.all(), info)

View File

@ -8,6 +8,7 @@ from rest_framework import status
from core.choices import ManagedFileRootPathChoices
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Rack, Location, RackRole, Site
from extras.choices import *
from extras.models import *
from extras.reports import Report
from extras.scripts import BooleanVar, IntegerVar, Script, StringVar
@ -68,6 +69,79 @@ class WebhookTest(APIViewTestCases.APIViewTestCase):
Webhook.objects.bulk_create(webhooks)
class EventRuleTest(APIViewTestCases.APIViewTestCase):
model = EventRule
brief_fields = ['display', 'id', 'name',]
@classmethod
def setUpTestData(cls):
webhooks = (
Webhook(
name='Webhook 1',
payload_url='http://example.com/?1',
),
Webhook(
name='Webhook 2',
payload_url='http://example.com/?1',
),
Webhook(
name='Webhook 3',
payload_url='http://example.com/?1',
),
Webhook(
name='Webhook 4',
payload_url='http://example.com/?1',
),
Webhook(
name='Webhook 5',
payload_url='http://example.com/?1',
),
Webhook(
name='Webhook 6',
payload_url='http://example.com/?1',
),
)
Webhook.objects.bulk_create(webhooks)
def setUp(self):
super().setUp()
webhooks = Webhook.objects.all()
event_rules = (
EventRule(name='EventRule 1', action_object=webhooks[0]),
EventRule(name='EventRule 2', action_object=webhooks[1]),
EventRule(name='EventRule 3', action_object=webhooks[2]),
)
EventRule.objects.bulk_create(event_rules)
self.create_data = [
{
'name': 'EventRule 4',
'content_types': ['dcim.device', 'dcim.devicetype'],
'type_create': True,
'action_type': EventRuleActionChoices.WEBHOOK,
'action_object_type': 'extras.webhook',
'action_object_id': webhooks[3].pk,
},
{
'name': 'EventRule 5',
'content_types': ['dcim.device', 'dcim.devicetype'],
'type_create': True,
'action_type': EventRuleActionChoices.WEBHOOK,
'action_object_type': 'extras.webhook',
'action_object_id': webhooks[4].pk,
},
{
'name': 'EventRule 6',
'content_types': ['dcim.device', 'dcim.devicetype'],
'type_create': True,
'action_type': EventRuleActionChoices.WEBHOOK,
'action_object_type': 'extras.webhook',
'action_object_id': webhooks[5].pk,
},
]
class CustomFieldTest(APIViewTestCases.APIViewTestCase):
model = CustomField
brief_fields = ['display', 'id', 'name', 'url']