|
|
@@ -3,7 +3,7 @@ from unittest.mock import patch
|
|
|
import yaml
|
|
|
from django import forms
|
|
|
from django.template.loader import render_to_string
|
|
|
-from django.test import TestCase
|
|
|
+from django.test import TestCase, tag
|
|
|
|
|
|
from circuits.models import CircuitTermination
|
|
|
from dcim.choices import (
|
|
|
@@ -1887,3 +1887,41 @@ class SiteFormTestCase(TestCase):
|
|
|
self.assertEqual(site.asns.count(), M2MAddRemoveFields.THRESHOLD)
|
|
|
self.assertTrue(site.asns.filter(pk__in=add_pks).count() == 3)
|
|
|
self.assertFalse(site.asns.filter(pk__in=remove_pks).exists())
|
|
|
+
|
|
|
+
|
|
|
+class InventoryItemTemplateFormTestCase(TestCase):
|
|
|
+
|
|
|
+ @tag('regression') # Ref: #23748
|
|
|
+ def test_foreign_component_error_on_component_field(self):
|
|
|
+ """
|
|
|
+ The device type can't be changed when editing, so the error must point at the component to change.
|
|
|
+ """
|
|
|
+ manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
|
|
|
+ device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
|
|
|
+ other_device_type = DeviceType.objects.create(
|
|
|
+ manufacturer=manufacturer, model='Device Type 2', slug='device-type-2'
|
|
|
+ )
|
|
|
+ interface = InterfaceTemplate.objects.create(
|
|
|
+ device_type=other_device_type, name='eth1', type=InterfaceTypeChoices.TYPE_1GE_FIXED
|
|
|
+ )
|
|
|
+ # Saved without validation, as the REST API allowed before #23748
|
|
|
+ saved = InventoryItemTemplate.objects.create(device_type=device_type, name='Optic', component=interface)
|
|
|
+ data = {'device_type': device_type.pk, 'name': 'Optic', 'interfacetemplate': interface.pk}
|
|
|
+
|
|
|
+ for instance in (None, saved):
|
|
|
+ with self.subTest(editing=instance is not None):
|
|
|
+ form = InventoryItemTemplateForm(data=data, instance=instance)
|
|
|
+ self.assertFalse(form.is_valid())
|
|
|
+ self.assertNotIn('device_type', form.errors)
|
|
|
+ self.assertEqual(
|
|
|
+ form.errors['interfacetemplate'],
|
|
|
+ ['Component template (eth1) must belong to the same device type.']
|
|
|
+ )
|
|
|
+
|
|
|
+ # With two components selected, the saved component is still validated, so no selector gets its error
|
|
|
+ own_console_port = ConsolePortTemplate.objects.create(device_type=device_type, name='Console')
|
|
|
+ form = InventoryItemTemplateForm(
|
|
|
+ data={**data, 'consoleporttemplate': own_console_port.pk}, instance=saved
|
|
|
+ )
|
|
|
+ self.assertFalse(form.is_valid())
|
|
|
+ self.assertNotIn('consoleporttemplate', form.errors)
|