mirror of
https://github.com/django-components/django-components.git
synced 2025-09-26 15:39:08 +00:00
Inline data for test_render_component_from_template.
This commit is contained in:
parent
e566d8ecbb
commit
678346313c
1 changed files with 19 additions and 14 deletions
|
@ -79,22 +79,11 @@ class MockInsecureComponentSlot(component.Component):
|
||||||
return self.render_to_response({}, {"test_slot": "<script>alert(1);</script>"})
|
return self.render_to_response({}, {"test_slot": "<script>alert(1);</script>"})
|
||||||
|
|
||||||
|
|
||||||
def render_template_view(request):
|
|
||||||
template = Template(
|
|
||||||
"""
|
|
||||||
{% load component_tags %}
|
|
||||||
{% component "testcomponent" variable="TEMPLATE" %}{% endcomponent %}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
return HttpResponse(template.render(Context({})))
|
|
||||||
|
|
||||||
|
|
||||||
components_urlpatterns = [
|
components_urlpatterns = [
|
||||||
path("test/", MockComponentRequest.as_view()),
|
path("test/", MockComponentRequest.as_view()),
|
||||||
path("test_slot/", MockComponentSlot.as_view()),
|
path("test_slot/", MockComponentSlot.as_view()),
|
||||||
path("test_context_insecure/", MockInsecureComponentContext.as_view()),
|
path("test_context_insecure/", MockInsecureComponentContext.as_view()),
|
||||||
path("test_slot_insecure/", MockInsecureComponentSlot.as_view()),
|
path("test_slot_insecure/", MockInsecureComponentSlot.as_view()),
|
||||||
path("test_template/", render_template_view),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -104,8 +93,14 @@ urlpatterns = [
|
||||||
|
|
||||||
|
|
||||||
class CustomClient(Client):
|
class CustomClient(Client):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, urlpatterns=None, *args, **kwargs):
|
||||||
settings.ROOT_URLCONF = __name__ # noqa
|
import types
|
||||||
|
if urlpatterns:
|
||||||
|
urls_module = types.ModuleType("urls")
|
||||||
|
urls_module.urlpatterns = urlpatterns # type: ignore
|
||||||
|
settings.ROOT_URLCONF = urls_module
|
||||||
|
else:
|
||||||
|
settings.ROOT_URLCONF = __name__
|
||||||
settings.SECRET_KEY = "secret" # noqa
|
settings.SECRET_KEY = "secret" # noqa
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -127,7 +122,17 @@ class TestComponentAsView(BaseTestCase):
|
||||||
self.client = CustomClient()
|
self.client = CustomClient()
|
||||||
|
|
||||||
def test_render_component_from_template(self):
|
def test_render_component_from_template(self):
|
||||||
response = self.client.get("/test_template/")
|
def render_template_view(request):
|
||||||
|
template = Template(
|
||||||
|
"""
|
||||||
|
{% load component_tags %}
|
||||||
|
{% component "testcomponent" variable="TEMPLATE" %}{% endcomponent %}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
return HttpResponse(template.render(Context({})))
|
||||||
|
|
||||||
|
client = CustomClient(urlpatterns=[path("test_template/", render_template_view)])
|
||||||
|
response = client.get("/test_template/")
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
b'<input type="text" name="variable" value="TEMPLATE">',
|
b'<input type="text" name="variable" value="TEMPLATE">',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue