|
|
@@ -1,10 +1,12 @@
|
|
|
import json
|
|
|
+from enum import Enum
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
from django.db import connection
|
|
|
-from django.test import TestCase
|
|
|
+from django.test import SimpleTestCase, TestCase
|
|
|
from django.test.utils import CaptureQueriesContext
|
|
|
+from django.utils.safestring import mark_safe
|
|
|
from jinja2 import TemplateRuntimeError, UndefinedError
|
|
|
from jinja2.sandbox import SecurityError
|
|
|
|
|
|
@@ -15,7 +17,7 @@ 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
|
|
|
+from utilities.template_objects import _load_objects, get_template_object, get_template_objects, to_template_data
|
|
|
from utilities.testing import create_test_device
|
|
|
|
|
|
|
|
|
@@ -473,3 +475,17 @@ class QueryTest(TestCase):
|
|
|
def test_internals_are_inaccessible(self):
|
|
|
with self.assertRaises(SecurityError):
|
|
|
self.render('{{ query.__closure__ }}')
|
|
|
+
|
|
|
+
|
|
|
+class ToTemplateDataTest(SimpleTestCase):
|
|
|
+
|
|
|
+ def test_str_subclasses(self):
|
|
|
+ # Unlike a StrEnum, str() of this member gives 'Status.ACTIVE'
|
|
|
+ class Status(str, Enum): # noqa: UP042
|
|
|
+ ACTIVE = 'active'
|
|
|
+
|
|
|
+ for value, expected in ((mark_safe('safe'), 'safe'), (Status.ACTIVE, 'active')):
|
|
|
+ with self.subTest(value=value):
|
|
|
+ result = to_template_data(value)
|
|
|
+ self.assertIs(type(result), str)
|
|
|
+ self.assertEqual(result, expected)
|