|
|
@@ -13,6 +13,7 @@ from django.test import RequestFactory, TestCase, override_settings, tag
|
|
|
from django.test.utils import CaptureQueriesContext
|
|
|
from django.utils.text import slugify
|
|
|
from jinja2 import TemplateSyntaxError
|
|
|
+from jinja2.sandbox import SecurityError
|
|
|
from netaddr import EUI
|
|
|
|
|
|
from circuits.models import *
|
|
|
@@ -27,7 +28,7 @@ from ipam.models import IPAddress, Prefix
|
|
|
from netbox.choices import DiameterUnitChoices, FlowRateUnitChoices, WeightUnitChoices
|
|
|
from netbox.context_managers import event_tracking
|
|
|
from tenancy.models import Tenant
|
|
|
-from users.models import User
|
|
|
+from users.models import ObjectPermission, User
|
|
|
from utilities.data import drange
|
|
|
from utilities.exceptions import AbortRequest
|
|
|
from utilities.testing import create_test_device, create_test_image
|
|
|
@@ -1692,6 +1693,85 @@ class DeviceRenderConfigTestCase(TestCase):
|
|
|
self.device.render_config(config_template)
|
|
|
|
|
|
|
|
|
+@override_settings(TEMPLATE_RENDERING={'config_templates': 'restricted'})
|
|
|
+class DeviceRestrictedRenderConfigTestCase(TestCase):
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def setUpTestData(cls):
|
|
|
+ cls.device = create_test_device('Device 1', local_context_data={'ntp': '192.0.2.1'})
|
|
|
+ Interface.objects.bulk_create([
|
|
|
+ Interface(device=cls.device, name='eth0', type='1000base-t'),
|
|
|
+ Interface(device=cls.device, name='eth1', type='1000base-t'),
|
|
|
+ ])
|
|
|
+ cls.superuser = User.objects.create_user(username='admin', is_superuser=True)
|
|
|
+
|
|
|
+ def render(self, template_code, user=None, **kwargs):
|
|
|
+ config_template = ConfigTemplate.objects.create(name='Template 1', template_code=template_code)
|
|
|
+ return self.device.render_config(config_template, user=user or self.superuser, **kwargs)
|
|
|
+
|
|
|
+ def test_context(self):
|
|
|
+ cases = (
|
|
|
+ ('{{ device.name }} {{ device.status.value }} {{ device.site.slug }}', 'Device 1 active site-1'),
|
|
|
+ ('{% for i in device.interfaces.all() %}{{ i.name }} {% endfor %}', 'eth0 eth1 '),
|
|
|
+ ('{{ ntp }} {{ device.config_context.ntp }}', '192.0.2.1 192.0.2.1'),
|
|
|
+ ('{{ query("dcim.device").count() }} {{ dcim is defined }}', '1 False'),
|
|
|
+ ('{{ extra.key }}', 'value'),
|
|
|
+ )
|
|
|
+ for template_code, expected in cases:
|
|
|
+ with self.subTest(template_code=template_code):
|
|
|
+ try:
|
|
|
+ self.assertEqual(self.render(template_code, extra_context={'extra': {'key': 'value'}}), expected)
|
|
|
+ finally:
|
|
|
+ ConfigTemplate.objects.all().delete()
|
|
|
+
|
|
|
+ def test_extra_context_objects(self):
|
|
|
+ extra_context = {
|
|
|
+ 'site': self.device.site,
|
|
|
+ 'interfaces': self.device.interfaces,
|
|
|
+ 'devices': [self.device],
|
|
|
+ 'sites': {self.device.site},
|
|
|
+ }
|
|
|
+ output = self.render(
|
|
|
+ '{{ site.slug }} {{ interfaces | map(attribute="name") | join(",") }} {{ devices[0].name }} '
|
|
|
+ '{{ sites[0].slug }}',
|
|
|
+ extra_context=extra_context,
|
|
|
+ )
|
|
|
+ self.assertEqual(output, 'site-1 eth0,eth1 Device 1 site-1')
|
|
|
+
|
|
|
+ def test_extra_context_sliced_queryset(self):
|
|
|
+ template_code = '{{ interfaces | map(attribute="name") | join(",") }}'
|
|
|
+ interfaces = self.device.interfaces.order_by('name')
|
|
|
+ self.assertEqual(self.render(template_code, extra_context={'interfaces': interfaces[1:]}), 'eth1')
|
|
|
+ ConfigTemplate.objects.all().delete()
|
|
|
+
|
|
|
+ # The slice is taken of the objects the user is permitted to view
|
|
|
+ user = User.objects.create_user(username='user')
|
|
|
+ permission = ObjectPermission.objects.create(name='Interfaces', actions=['view'], constraints={'name': 'eth1'})
|
|
|
+ permission.object_types.set([ObjectType.objects.get_for_model(Interface)])
|
|
|
+ permission.users.set([user])
|
|
|
+ self.assertEqual(self.render(template_code, user=user, extra_context={'interfaces': interfaces[:1]}), 'eth1')
|
|
|
+
|
|
|
+ def test_extra_context_unsupported_model(self):
|
|
|
+ with self.assertRaises(ValueError):
|
|
|
+ self.render('{{ paths | length }}', extra_context={'paths': CablePath.objects.all()})
|
|
|
+
|
|
|
+ def test_related_objects_are_restricted(self):
|
|
|
+ user = User.objects.create_user(username='user')
|
|
|
+ permission = ObjectPermission.objects.create(name='Interfaces', actions=['view'], constraints={'name': 'eth1'})
|
|
|
+ permission.object_types.set([ObjectType.objects.get_for_model(Interface)])
|
|
|
+ permission.users.set([user])
|
|
|
+ self.assertEqual(self.render('{{ device.interfaces | map(attribute="name") | join }}', user=user), 'eth1')
|
|
|
+
|
|
|
+ def test_restricted_access(self):
|
|
|
+ with self.assertRaises(SecurityError):
|
|
|
+ self.render('{{ device._model }}')
|
|
|
+
|
|
|
+ def test_requires_user(self):
|
|
|
+ config_template = ConfigTemplate.objects.create(name='Template 1', template_code='{{ device.name }}')
|
|
|
+ with self.assertRaises(ValueError):
|
|
|
+ self.device.render_config(config_template)
|
|
|
+
|
|
|
+
|
|
|
class DeviceBayTestCase(TestCase):
|
|
|
|
|
|
@classmethod
|