From 678346313cb4d8b85e1ea23c0d3c2956c36f4768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Stenstr=C3=B6m?= Date: Thu, 2 May 2024 22:53:02 +0200 Subject: [PATCH 1/7] Inline data for test_render_component_from_template. --- tests/test_component_as_view.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/tests/test_component_as_view.py b/tests/test_component_as_view.py index beee3752..22a1f129 100644 --- a/tests/test_component_as_view.py +++ b/tests/test_component_as_view.py @@ -79,22 +79,11 @@ class MockInsecureComponentSlot(component.Component): return self.render_to_response({}, {"test_slot": ""}) -def render_template_view(request): - template = Template( - """ - {% load component_tags %} - {% component "testcomponent" variable="TEMPLATE" %}{% endcomponent %} - """ - ) - return HttpResponse(template.render(Context({}))) - - components_urlpatterns = [ path("test/", MockComponentRequest.as_view()), path("test_slot/", MockComponentSlot.as_view()), path("test_context_insecure/", MockInsecureComponentContext.as_view()), path("test_slot_insecure/", MockInsecureComponentSlot.as_view()), - path("test_template/", render_template_view), ] @@ -104,8 +93,14 @@ urlpatterns = [ class CustomClient(Client): - def __init__(self, *args, **kwargs): - settings.ROOT_URLCONF = __name__ # noqa + def __init__(self, urlpatterns=None, *args, **kwargs): + 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 super().__init__(*args, **kwargs) @@ -127,7 +122,17 @@ class TestComponentAsView(BaseTestCase): self.client = CustomClient() 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.assertIn( b'', From 8d8ae35c0e2bdf7b347ecc56f35e67ff43b781b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Stenstr=C3=B6m?= Date: Thu, 2 May 2024 22:57:39 +0200 Subject: [PATCH 2/7] Inline test_get_request & test_post_request. --- tests/test_component_as_view.py | 80 ++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/tests/test_component_as_view.py b/tests/test_component_as_view.py index 22a1f129..d23a906b 100644 --- a/tests/test_component_as_view.py +++ b/tests/test_component_as_view.py @@ -17,27 +17,6 @@ from django_components import component # COMPONENTS ######################### - -class MockComponentRequest(component.Component): - template = """ -
- {% csrf_token %} - - -
- """ - - def post(self, request, *args, **kwargs) -> HttpResponse: - variable = request.POST.get("variable") - return self.render_to_response({"variable": variable}) - - def get(self, request, *args, **kwargs) -> HttpResponse: - return self.render_to_response({"variable": "GET"}) - - def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]: - return {"variable": variable} - - class MockComponentSlot(component.Component): template = """ {% load component_tags %} @@ -80,7 +59,6 @@ class MockInsecureComponentSlot(component.Component): components_urlpatterns = [ - path("test/", MockComponentRequest.as_view()), path("test_slot/", MockComponentSlot.as_view()), path("test_context_insecure/", MockInsecureComponentContext.as_view()), path("test_slot_insecure/", MockInsecureComponentSlot.as_view()), @@ -113,7 +91,6 @@ class CustomClient(Client): class TestComponentAsView(BaseTestCase): @classmethod def setUpClass(self): - component.registry.register("testcomponent", MockComponentRequest) component.registry.register("testcomponent_slot", MockComponentSlot) component.registry.register("testcomponent_context_insecure", MockInsecureComponentContext) component.registry.register("testcomponent_slot_insecure", MockInsecureComponentSlot) @@ -122,6 +99,26 @@ class TestComponentAsView(BaseTestCase): self.client = CustomClient() def test_render_component_from_template(self): + @component.register("testcomponent") + class MockComponentRequest(component.Component): + template = """ +
+ {% csrf_token %} + + +
+ """ + + def post(self, request, *args, **kwargs) -> HttpResponse: + variable = request.POST.get("variable") + return self.render_to_response({"variable": variable}) + + def get(self, request, *args, **kwargs) -> HttpResponse: + return self.render_to_response({"variable": "GET"}) + + def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]: + return {"variable": variable} + def render_template_view(request): template = Template( """ @@ -140,7 +137,23 @@ class TestComponentAsView(BaseTestCase): ) def test_get_request(self): - response = self.client.get("/test/") + class MockComponentRequest(component.Component): + template = """ +
+ {% csrf_token %} + + +
+ """ + + def get(self, request, *args, **kwargs) -> HttpResponse: + return self.render_to_response({"variable": "GET"}) + + def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]: + return {"variable": variable} + + client = CustomClient(urlpatterns=[path("test/", MockComponentRequest.as_view())]) + response = client.get("/test/") self.assertEqual(response.status_code, 200) self.assertIn( b'', @@ -148,7 +161,24 @@ class TestComponentAsView(BaseTestCase): ) def test_post_request(self): - response = self.client.post("/test/", {"variable": "POST"}) + class MockComponentRequest(component.Component): + template = """ +
+ {% csrf_token %} + + +
+ """ + + def post(self, request, *args, **kwargs) -> HttpResponse: + variable = request.POST.get("variable") + return self.render_to_response({"variable": variable}) + + def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]: + return {"variable": variable} + + client = CustomClient(urlpatterns=[path("test/", MockComponentRequest.as_view())]) + response = client.post("/test/", {"variable": "POST"}) self.assertEqual(response.status_code, 200) self.assertIn( b'', From 2949969ea5a1c85ef4edf0d756853ba963c81611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Stenstr=C3=B6m?= Date: Thu, 2 May 2024 23:00:40 +0200 Subject: [PATCH 3/7] Inline test_replace_slot_in_view. --- tests/test_component_as_view.py | 36 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/tests/test_component_as_view.py b/tests/test_component_as_view.py index d23a906b..d471d99e 100644 --- a/tests/test_component_as_view.py +++ b/tests/test_component_as_view.py @@ -17,22 +17,6 @@ from django_components import component # COMPONENTS ######################### -class MockComponentSlot(component.Component): - template = """ - {% load component_tags %} -
- {% slot "first_slot" %} - Hey, I'm {{ name }} - {% endslot %} - {% slot "second_slot" %} - {% endslot %} -
- """ - - def get(self, request, *args, **kwargs) -> HttpResponse: - return self.render_to_response({"name": "Bob"}, {"second_slot": "Nice to meet you, Bob"}) - - class MockInsecureComponentContext(component.Component): template = """ {% load component_tags %} @@ -59,7 +43,6 @@ class MockInsecureComponentSlot(component.Component): components_urlpatterns = [ - path("test_slot/", MockComponentSlot.as_view()), path("test_context_insecure/", MockInsecureComponentContext.as_view()), path("test_slot_insecure/", MockInsecureComponentSlot.as_view()), ] @@ -91,7 +74,6 @@ class CustomClient(Client): class TestComponentAsView(BaseTestCase): @classmethod def setUpClass(self): - component.registry.register("testcomponent_slot", MockComponentSlot) component.registry.register("testcomponent_context_insecure", MockInsecureComponentContext) component.registry.register("testcomponent_slot_insecure", MockInsecureComponentSlot) @@ -186,7 +168,23 @@ class TestComponentAsView(BaseTestCase): ) def test_replace_slot_in_view(self): - response = self.client.get("/test_slot/") + class MockComponentSlot(component.Component): + template = """ + {% load component_tags %} +
+ {% slot "first_slot" %} + Hey, I'm {{ name }} + {% endslot %} + {% slot "second_slot" %} + {% endslot %} +
+ """ + + def get(self, request, *args, **kwargs) -> HttpResponse: + return self.render_to_response({"name": "Bob"}, {"second_slot": "Nice to meet you, Bob"}) + + client = CustomClient(urlpatterns=[path("test_slot/", MockComponentSlot.as_view())]) + response = client.get("/test_slot/") self.assertEqual(response.status_code, 200) self.assertIn( b"Hey, I'm Bob", From 61b08325251aa657ac9d0ee06a1d2561b49f8483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Stenstr=C3=B6m?= Date: Thu, 2 May 2024 23:09:17 +0200 Subject: [PATCH 4/7] Inline test_replace_slot_in_view_with_insecure_content. --- tests/test_component_as_view.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/test_component_as_view.py b/tests/test_component_as_view.py index d471d99e..bbed9d2a 100644 --- a/tests/test_component_as_view.py +++ b/tests/test_component_as_view.py @@ -29,22 +29,8 @@ class MockInsecureComponentContext(component.Component): return self.render_to_response({"variable": ""}) -class MockInsecureComponentSlot(component.Component): - template = """ - {% load component_tags %} -
- {% slot "test_slot" %} - {% endslot %} -
- """ - - def get(self, request, *args, **kwargs) -> HttpResponse: - return self.render_to_response({}, {"test_slot": ""}) - - components_urlpatterns = [ path("test_context_insecure/", MockInsecureComponentContext.as_view()), - path("test_slot_insecure/", MockInsecureComponentSlot.as_view()), ] @@ -75,7 +61,6 @@ class TestComponentAsView(BaseTestCase): @classmethod def setUpClass(self): component.registry.register("testcomponent_context_insecure", MockInsecureComponentContext) - component.registry.register("testcomponent_slot_insecure", MockInsecureComponentSlot) def setUp(self): self.client = CustomClient() @@ -196,7 +181,20 @@ class TestComponentAsView(BaseTestCase): ) def test_replace_slot_in_view_with_insecure_content(self): - response = self.client.get("/test_slot_insecure/") + class MockInsecureComponentSlot(component.Component): + template = """ + {% load component_tags %} +
+ {% slot "test_slot" %} + {% endslot %} +
+ """ + + def get(self, request, *args, **kwargs) -> HttpResponse: + return self.render_to_response({}, {"test_slot": ""}) + + client = CustomClient(urlpatterns=[path("test_slot_insecure/", MockInsecureComponentSlot.as_view())]) + response = client.get("/test_slot_insecure/") self.assertEqual(response.status_code, 200) self.assertNotIn( b""}) - - -components_urlpatterns = [ - path("test_context_insecure/", MockInsecureComponentContext.as_view()), -] - - -urlpatterns = [ - path("", include(components_urlpatterns)), -] - - class CustomClient(Client): def __init__(self, urlpatterns=None, *args, **kwargs): import types @@ -58,10 +32,6 @@ class CustomClient(Client): class TestComponentAsView(BaseTestCase): - @classmethod - def setUpClass(self): - component.registry.register("testcomponent_context_insecure", MockInsecureComponentContext) - def setUp(self): self.client = CustomClient() @@ -202,7 +172,19 @@ class TestComponentAsView(BaseTestCase): ) def test_replace_context_in_view_with_insecure_content(self): - response = self.client.get("/test_context_insecure/") + class MockInsecureComponentContext(component.Component): + template = """ + {% load component_tags %} +
+ {{ variable }} +
+ """ + + def get(self, request, *args, **kwargs) -> HttpResponse: + return self.render_to_response({"variable": ""}) + + client = CustomClient(urlpatterns=[path("test_context_insecure/", MockInsecureComponentContext.as_view())]) + response = client.get("/test_context_insecure/") self.assertEqual(response.status_code, 200) self.assertNotIn( b"