|
|
@@ -17,7 +17,13 @@ from ipam.models import IPAddress
|
|
|
from tenancy.models import Tenant, TenantGroup
|
|
|
from users.models import ObjectPermission, User
|
|
|
from utilities.jinja2 import RestrictedRenderState, render_jinja2
|
|
|
-from utilities.template_objects import _load_objects, get_template_object, get_template_objects, to_template_data
|
|
|
+from utilities.template_objects import (
|
|
|
+ _load_objects,
|
|
|
+ get_template_context_data,
|
|
|
+ get_template_object,
|
|
|
+ get_template_objects,
|
|
|
+ to_template_data,
|
|
|
+)
|
|
|
from utilities.testing import create_test_device
|
|
|
|
|
|
|
|
|
@@ -65,6 +71,27 @@ class TemplateObjectTest(TestCase):
|
|
|
with self.subTest(template_code=template_code):
|
|
|
self.assertEqual(self.render(template_code, object=self.device), expected)
|
|
|
|
|
|
+ def test_context_data(self):
|
|
|
+ state = RestrictedRenderState(self.superuser)
|
|
|
+ device = get_template_object(self.device, state)
|
|
|
+ context = {
|
|
|
+ 'device': device,
|
|
|
+ 'interfaces': get_template_objects(Interface.objects.all(), state),
|
|
|
+ 'extra': {'items': [device, 1]},
|
|
|
+ }
|
|
|
+ # Resolving a related object doesn't change the representation shown
|
|
|
+ render_jinja2('{{ device.tenant.group.slug }}', context, restricted=state)
|
|
|
+ with self.assertNumQueries(0):
|
|
|
+ data = get_template_context_data(context)
|
|
|
+ self.assertEqual(data['device']['name'], 'Device 1')
|
|
|
+ self.assertNotIn('group', data['device']['tenant'])
|
|
|
+ self.assertEqual(data['interfaces'], '<TemplateObjectList dcim.Interface>')
|
|
|
+ self.assertEqual(data['extra']['items'][1], 1)
|
|
|
+ self.assertEqual(data['extra']['items'][0]['name'], 'Device 1')
|
|
|
+ # The data is a copy
|
|
|
+ data['device']['name'] = 'Changed'
|
|
|
+ self.assertEqual(device['name'], 'Device 1')
|
|
|
+
|
|
|
def test_urls(self):
|
|
|
site = self.device.site
|
|
|
tenant_group = self.tenant.group
|