Skip to content

Commit

Permalink
Fix up more errors detected with tests. Add some final missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Sep 23, 2024
1 parent 094cdf1 commit 1d18e78
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 46 deletions.
2 changes: 0 additions & 2 deletions netbox_routing/fields/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def to_python(self, value):
raise ValidationError(e)

def get_prep_value(self, value):
if not value:
return None
if isinstance(value, list):
return [str(self.to_python(v)) for v in value]
return str(self.to_python(value))
Expand Down
5 changes: 5 additions & 0 deletions netbox_routing/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@

# OSPF
'OSPFAreaForm',
'OSPFAreaBulkEditForm',
'OSPFAreaImportForm',
'OSPFAreaFilterForm',

'OSPFInstanceForm',
'OSPFInstanceBulkEditForm',
'OSPFInstanceFilterForm',
'OSPFInstanceImportForm',

'OSPFInterfaceForm',
'OSPFInterfaceFilterForm',
'OSPFInterfaceBulkEditForm',
'OSPFInterfaceImportForm',

'BGPRouterForm',
'BGPScopeForm',
Expand Down
4 changes: 3 additions & 1 deletion netbox_routing/forms/bulk_edit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from .static import *
from .objects import *
from .ospf import OSPFInterfaceBulkEditForm
from .ospf import *


__all__ = (
# Staticroute
'StaticRouteBulkEditForm',

# OSPF
'OSPFInstanceBulkEditForm',
'OSPFInterfaceBulkEditForm',
'OSPFAreaBulkEditForm',

# Route Objects
'PrefixListEntryBulkEditForm',
Expand Down
52 changes: 51 additions & 1 deletion netbox_routing/forms/bulk_edit/ospf.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,62 @@
from django import forms
from django.utils.translation import gettext as _

from dcim.models import Device
from netbox.forms import NetBoxModelBulkEditForm
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, add_blank_choice
from utilities.forms.fields import DynamicModelChoiceField
from utilities.forms.fields import DynamicModelChoiceField, CommentField

from netbox_routing import choices
from netbox_routing.models import OSPFArea, OSPFInstance, OSPFInterface

__all__ = (
'OSPFInterfaceBulkEditForm',
'OSPFInstanceBulkEditForm',
'OSPFAreaBulkEditForm',
)

from utilities.forms.rendering import FieldSet


class OSPFInstanceBulkEditForm(NetBoxModelBulkEditForm):
device = DynamicModelChoiceField(
queryset=Device.objects.all(),
label=_('Device'),
required=False,
selector=True
)

description = forms.CharField(
label=_('Description'),
max_length=200,
required=False
)
comments = CommentField()

model = OSPFInstance
fieldsets = (
FieldSet('device', name='OSPF'),
FieldSet('description', ),
)
nullable_fields = ()


class OSPFAreaBulkEditForm(NetBoxModelBulkEditForm):

description = forms.CharField(
label=_('Description'),
max_length=200,
required=False
)
comments = CommentField()

model = OSPFArea
fieldsets = (
FieldSet('description'),
)
nullable_fields = ()


class OSPFInterfaceBulkEditForm(NetBoxModelBulkEditForm):
instance = DynamicModelChoiceField(
queryset=OSPFInstance.objects.all(),
Expand All @@ -37,9 +79,17 @@ class OSPFInterfaceBulkEditForm(NetBoxModelBulkEditForm):
)
passphrase = forms.CharField(label=_('Passphrase'), required=False)

description = forms.CharField(
label=_('Description'),
max_length=200,
required=False
)
comments = CommentField()

model = OSPFInterface
fieldsets = (
FieldSet('instance', 'area', name='OSPF'),
FieldSet('priority', 'bfd', 'authentication', 'passphrase', name='Attributes'),
FieldSet('description'),
)
nullable_fields = ()
11 changes: 9 additions & 2 deletions netbox_routing/forms/bulk_edit/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ipam.models import VRF
from netbox.forms import NetBoxModelBulkEditForm
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField, CommentField
from utilities.forms.rendering import FieldSet

from netbox_routing.models import StaticRoute
Expand All @@ -32,10 +32,17 @@ class StaticRouteBulkEditForm(NetBoxModelBulkEditForm):
metric = forms.IntegerField(label=_('Metric'), required=False)
permanent = forms.ChoiceField(label=_('Permanent'), choices=BOOLEAN_WITH_BLANK_CHOICES, required=False)

description = forms.CharField(
label=_('Description'),
max_length=200,
required=False
)
comments = CommentField()

model = StaticRoute
fieldsets = (
FieldSet('devices', 'vrf', 'prefix', 'next_hop', name='Route'),
FieldSet('metric', 'permanent', name='Attributes'),
FieldSet('description', 'comments')
FieldSet('description', )
)
nullable_fields = ('devices', 'vrf', 'metric', 'permanent', 'description', 'comments')
4 changes: 3 additions & 1 deletion netbox_routing/forms/bulk_import/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from .ospf import OSPFInterfaceImportForm
from .ospf import *


__all__ = (
# OSPF
'OSPFInstanceImportForm',
'OSPFAreaImportForm',
'OSPFInterfaceImportForm',
)

21 changes: 18 additions & 3 deletions netbox_routing/forms/ospf.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _

from dcim.models import Interface, Device
from netbox.forms import NetBoxModelForm
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField, CommentField

from netbox_routing.models import OSPFArea, OSPFInstance, OSPFInterface

Expand All @@ -23,23 +24,25 @@ class OSPFInstanceForm(NetBoxModelForm):
selector=True,
label=_('Device'),
)
comments = CommentField()

class Meta:
model = OSPFInstance
fields = ('name', 'router_id', 'process_id', 'device', 'description', 'comments', )


class OSPFAreaForm(NetBoxModelForm):
comments = CommentField()

class Meta:
model = OSPFArea
fields = ('area_id', 'description', 'comments', )
fields = ('area_id', 'description', 'comments', )


class OSPFInterfaceForm(NetBoxModelForm):
device = DynamicModelChoiceField(
queryset=Device.objects.all(),
required=True,
required=False,
selector=True,
label=_('Device'),
)
Expand Down Expand Up @@ -67,6 +70,7 @@ class OSPFInterfaceForm(NetBoxModelForm):
'device_id': '$device',
}
)
comments = CommentField()


class Meta:
Expand All @@ -84,3 +88,14 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
self.initial['device'] = self.instance.interface.device.pk

def clean(self):
super().clean()
if self.cleaned_data.get('instance') and self.cleaned_data.get('interface'):
if self.cleaned_data.get('instance').device != self.cleaned_data.get('interface').device:
raise ValidationError(
{
'instance': _('OSPF Instance Device and Interface Device must match'),
'interface': _('OSPF Instance Device and Interface Device must match')
}
)
3 changes: 2 additions & 1 deletion netbox_routing/forms/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ipam.models import VRF
from netbox.forms import NetBoxModelForm
from netbox_routing.models import StaticRoute
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField, CommentField


class StaticRouteForm(NetBoxModelForm):
Expand All @@ -14,6 +14,7 @@ class StaticRouteForm(NetBoxModelForm):
required=False,
label='VRF'
)
comments = CommentField()

class Meta:
model = StaticRoute
Expand Down
1 change: 0 additions & 1 deletion netbox_routing/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class OSPFAreaType(NetBoxObjectType):
)
class OSPFInterfaceType(NetBoxObjectType):

device: Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]
instance: Annotated["OSPFInstanceType", strawberry.lazy('netbox_routing.graphql.types')]
area: Annotated["OSPFAreaType", strawberry.lazy('netbox_routing.graphql.types')]
interface: Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')]
Expand Down
13 changes: 13 additions & 0 deletions netbox_routing/models/ospf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import netaddr
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -56,6 +58,17 @@ def __str__(self):
def get_absolute_url(self):
return reverse('plugins:netbox_routing:ospfarea', args=[self.pk])

def clean(self):
super().clean()
area_id = self.area_id
try:
int(area_id)
except ValueError:
try:
str(netaddr.IPAddress(area_id))
except netaddr.core.AddrFormatError:
raise ValidationError({'area_id': ['This field must be an integer or a valid net address']})


class OSPFInterface(PrimaryModel):
instance = models.ForeignKey(
Expand Down
3 changes: 3 additions & 0 deletions netbox_routing/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ def model_to_dict(self, instance, fields, api=False):
if api:
if type(value) is IPAddress:
model_dict[key] = str(value)
elif not api and key in ['router_id', ]:
if type(value) is IPAddress:
model_dict[key] = str(value)
return model_dict
18 changes: 10 additions & 8 deletions netbox_routing/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ def setUpTestData(cls):
]


class OSPFInstanceTest(IPAddressFieldMixin , APIViewTestCases.APIViewTestCase):
class OSPFInstanceTest(IPAddressFieldMixin, APIViewTestCases.APIViewTestCase):
model = OSPFInstance
view_namespace = 'plugins-api:netbox_routing'
brief_fields = ['device', 'display', 'id', 'name', 'process_id', 'router_id', 'url', ]

user_permissions = ('dcim.view_device', )

bulk_update_data = {
'description': "A test description"
Expand Down Expand Up @@ -115,7 +116,6 @@ class OSPFAreaTest(IPAddressFieldMixin , APIViewTestCases.APIViewTestCase):
view_namespace = 'plugins-api:netbox_routing'
brief_fields = ['area_id', 'display', 'id', 'url', ]


bulk_update_data = {
'description': "A test description"
}
Expand All @@ -140,8 +140,11 @@ def setUpTestData(cls):
class OSPFInterfaceTest(IPAddressFieldMixin, APIViewTestCases.APIViewTestCase):
model = OSPFInterface
view_namespace = 'plugins-api:netbox_routing'
brief_fields = ['device', 'display', 'id', 'name', 'process_id', 'router_id', 'url', ]
brief_fields = ['area', 'display', 'id', 'instance', 'interface', 'url', ]

user_permissions = (
'netbox_routing.view_ospfinstance', 'netbox_routing.view_ospfarea', 'dcim.view_device', 'dcim.view_interface',
)

bulk_update_data = {
'description': "A test description"
Expand All @@ -163,20 +166,19 @@ def setUpTestData(cls):
Interface.objects.bulk_create(interfaces)

data = (
cls.model(device=device, instance=instance, area=area, interface=interfaces[0], ),
cls.model(device=device, instance=instance, area=area, interface=interfaces[1], ),
cls.model(device=device, instance=instance, area=area, interface=interfaces[2], ),
cls.model(instance=instance, area=area, interface=interfaces[0], ),
cls.model(instance=instance, area=area, interface=interfaces[1], ),
cls.model(instance=instance, area=area, interface=interfaces[2], ),
)
cls.model.objects.bulk_create(data)

cls.create_data = [
{
'device': device.pk,
'instance': instance.pk,
'area': area.pk,
'interface': interfaces[3].pk,
'priority': 2,
'authentication': 'passphrase',
'authentication': 'message-digest',
'passphrase': 'test-password',
'bfd': True,
},
Expand Down
Loading

0 comments on commit 1d18e78

Please sign in to comment.