Просмотр исходного кода

Fixes #18768: allow removing secondary MACAddress from interface

Jason Novinger 11 месяцев назад
Родитель
Сommit
e86dba8fc8
2 измененных файлов с 42 добавлено и 2 удалено
  1. 4 1
      netbox/dcim/models/devices.py
  2. 38 1
      netbox/dcim/tests/test_models.py

+ 4 - 1
netbox/dcim/models/devices.py

@@ -1550,7 +1550,10 @@ class MACAddress(PrimaryModel):
             ct = ObjectType.objects.get_for_id(self._original_assigned_object_type_id)
             original_assigned_object = ct.get_object_for_this_type(pk=self._original_assigned_object_id)
 
-            if original_assigned_object.primary_mac_address:
+            if (
+                original_assigned_object.primary_mac_address
+                and original_assigned_object.primary_mac_address.pk == self.pk
+            ):
                 if not assigned_object:
                     raise ValidationError(
                         _("Cannot unassign MAC Address while it is designated as the primary MAC for an object")

+ 38 - 1
netbox/dcim/tests/test_models.py

@@ -1,5 +1,5 @@
 from django.core.exceptions import ValidationError
-from django.test import TestCase
+from django.test import tag, TestCase
 
 from circuits.models import *
 from core.models import ObjectType
@@ -12,6 +12,43 @@ from utilities.data import drange
 from virtualization.models import Cluster, ClusterType
 
 
+class MACAddressTestCase(TestCase):
+    @classmethod
+    def setUpTestData(cls):
+        site = Site.objects.create(name='Test Site 1', slug='test-site-1')
+        manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
+        device_type = DeviceType.objects.create(
+            manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
+        )
+        device_role = DeviceRole.objects.create(name='Test Role 1', slug='test-role-1')
+        device = Device.objects.create(
+            name='Device 1', device_type=device_type, role=device_role, site=site,
+        )
+        cls.interface = Interface.objects.create(
+            device=device,
+            name='Interface 1',
+            type=InterfaceTypeChoices.TYPE_1GE_FIXED,
+            mgmt_only=True
+        )
+
+        cls.mac_a = MACAddress.objects.create(mac_address='1234567890ab', assigned_object=cls.interface)
+        cls.mac_b = MACAddress.objects.create(mac_address='1234567890ba', assigned_object=cls.interface)
+
+        cls.interface.primary_mac_address = cls.mac_a
+        cls.interface.save()
+
+    @tag('regression')
+    def test_clean_will_not_allow_removal_of_assigned_object_if_primary(self):
+        self.mac_a.assigned_object = None
+        with self.assertRaisesMessage(ValidationError, 'Cannot unassign MAC Address while'):
+            self.mac_a.clean()
+
+    @tag('regression')
+    def test_clean_will_allow_removal_of_assigned_object_if_not_primary(self):
+        self.mac_b.assigned_object = None
+        self.mac_b.clean()
+
+
 class LocationTestCase(TestCase):
 
     def test_change_location_site(self):