This commit is contained in:
Arthur 2026-04-16 09:20:47 -07:00
parent 7f14434162
commit a2845d190e
4 changed files with 76 additions and 6 deletions

View File

@ -1647,7 +1647,7 @@ class DeviceTest(APIViewTestCases.APIViewTestCase):
device.config_template = default_template
device.save()
self.add_permissions('dcim.render_config_device', 'dcim.view_device')
self.add_permissions('dcim.render_config_device', 'dcim.view_device', 'extras.view_configtemplate')
url = reverse('dcim-api:device-render-config', kwargs={'pk': device.pk})
# Render with override template
@ -1655,10 +1655,19 @@ class DeviceTest(APIViewTestCases.APIViewTestCase):
self.assertHttpStatus(response, status.HTTP_200_OK)
self.assertEqual(response.data['content'], f'Override config for {device.name}')
# Render with invalid config_template_id
# Render with nonexistent config_template_id
response = self.client.post(url, {'config_template_id': 999999}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
# Render with non-integer config_template_id
response = self.client.post(url, {'config_template_id': 'abc'}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
# Without view_configtemplate permission, override template should not be accessible
self.remove_permissions('extras.view_configtemplate')
response = self.client.post(url, {'config_template_id': override_template.pk}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
class ModuleTest(APIViewTestCases.APIViewTestCase):
model = Module

View File

@ -2375,7 +2375,7 @@ class DeviceTestCase(ViewTestCases.PrimaryObjectViewTestCase):
device.config_template = default_template
device.save()
self.add_permissions('dcim.view_device', 'dcim.render_config_device')
self.add_permissions('dcim.view_device', 'dcim.render_config_device', 'extras.view_configtemplate')
url = reverse('dcim:device_render-config', kwargs={'pk': device.pk})
# Render with override config_template_id
@ -2383,11 +2383,22 @@ class DeviceTestCase(ViewTestCases.PrimaryObjectViewTestCase):
self.assertHttpStatus(response, 200)
self.assertIn(b'Override config for', response.content)
# Render with invalid config_template_id still returns 200 with error message
# Render with nonexistent config_template_id still returns 200 with error message
response = self.client.get(url, {'config_template_id': 999999})
self.assertHttpStatus(response, 200)
self.assertIn(b'Error rendering template', response.content)
# Render with non-integer config_template_id still returns 200 with error message
response = self.client.get(url, {'config_template_id': 'abc'})
self.assertHttpStatus(response, 200)
self.assertIn(b'Error rendering template', response.content)
# Without view_configtemplate permission, override template should not be accessible
self.remove_permissions('extras.view_configtemplate')
response = self.client.get(url, {'config_template_id': override_template.pk})
self.assertHttpStatus(response, 200)
self.assertIn(b'Error rendering template', response.content)
def test_device_role_display_colored(self):
parent_role = DeviceRole.objects.create(name='Parent Role', slug='parent-role', color='111111')
child_role = DeviceRole.objects.create(name='Child Role', slug='child-role', parent=parent_role, color='aa00bb')

View File

@ -358,7 +358,8 @@ class VirtualMachineTest(APIViewTestCases.APIViewTestCase):
vm.save()
self.add_permissions(
'virtualization.render_config_virtualmachine', 'virtualization.view_virtualmachine'
'virtualization.render_config_virtualmachine', 'virtualization.view_virtualmachine',
'extras.view_configtemplate'
)
url = reverse('virtualization-api:virtualmachine-render-config', kwargs={'pk': vm.pk})
@ -367,10 +368,19 @@ class VirtualMachineTest(APIViewTestCases.APIViewTestCase):
self.assertHttpStatus(response, status.HTTP_200_OK)
self.assertEqual(response.data['content'], f'Override config for {vm.name}')
# Render with invalid config_template_id
# Render with nonexistent config_template_id
response = self.client.post(url, {'config_template_id': 999999}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
# Render with non-integer config_template_id
response = self.client.post(url, {'config_template_id': 'abc'}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
# Without view_configtemplate permission, override template should not be accessible
self.remove_permissions('extras.view_configtemplate')
response = self.client.post(url, {'config_template_id': override_template.pk}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
class VMInterfaceTest(APIViewTestCases.APIViewTestCase):
model = VMInterface

View File

@ -357,6 +357,46 @@ class VirtualMachineTestCase(ViewTestCases.PrimaryObjectViewTestCase):
self.remove_permissions('virtualization.view_virtualmachine')
self.assertHttpStatus(self.client.get(url), 403)
def test_virtualmachine_renderconfig_with_config_template_id(self):
default_template = ConfigTemplate.objects.create(
name='Default Template',
template_code='Default config for {{ virtualmachine.name }}'
)
override_template = ConfigTemplate.objects.create(
name='Override Template',
template_code='Override config for {{ virtualmachine.name }}'
)
vm = VirtualMachine.objects.first()
vm.config_template = default_template
vm.save()
self.add_permissions(
'virtualization.view_virtualmachine', 'virtualization.render_config_virtualmachine',
'extras.view_configtemplate'
)
url = reverse('virtualization:virtualmachine_render-config', kwargs={'pk': vm.pk})
# Render with override config_template_id
response = self.client.get(url, {'config_template_id': override_template.pk})
self.assertHttpStatus(response, 200)
self.assertIn(b'Override config for', response.content)
# Render with nonexistent config_template_id still returns 200 with error message
response = self.client.get(url, {'config_template_id': 999999})
self.assertHttpStatus(response, 200)
self.assertIn(b'Error rendering template', response.content)
# Render with non-integer config_template_id still returns 200 with error message
response = self.client.get(url, {'config_template_id': 'abc'})
self.assertHttpStatus(response, 200)
self.assertIn(b'Error rendering template', response.content)
# Without view_configtemplate permission, override template should not be accessible
self.remove_permissions('extras.view_configtemplate')
response = self.client.get(url, {'config_template_id': override_template.pk})
self.assertHttpStatus(response, 200)
self.assertIn(b'Error rendering template', response.content)
class VMInterfaceTestCase(ViewTestCases.DeviceComponentViewTestCase):
model = VMInterface