|
| 1 | +from itertools import product |
| 2 | + |
| 3 | +from asgiref.sync import iscoroutinefunction |
| 4 | + |
| 5 | +from django.http import HttpRequest, HttpResponse |
| 6 | +from django.test import SimpleTestCase |
| 7 | +from django.utils.csp import CSP |
| 8 | +from django.views.decorators.csp import csp_override, csp_report_only_override |
| 9 | + |
| 10 | +basic_config = { |
| 11 | + "default-src": [CSP.SELF], |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +class CSPOverrideDecoratorTest(SimpleTestCase): |
| 16 | + def test_wrapped_sync_function_is_not_coroutine_function(self): |
| 17 | + def sync_view(request): |
| 18 | + return HttpResponse() |
| 19 | + |
| 20 | + wrapped_view = csp_override({})(sync_view) |
| 21 | + self.assertIs(iscoroutinefunction(wrapped_view), False) |
| 22 | + |
| 23 | + def test_wrapped_async_function_is_coroutine_function(self): |
| 24 | + async def async_view(request): |
| 25 | + return HttpResponse() |
| 26 | + |
| 27 | + wrapped_view = csp_override({})(async_view) |
| 28 | + self.assertIs(iscoroutinefunction(wrapped_view), True) |
| 29 | + |
| 30 | + def test_decorator_requires_mapping(self): |
| 31 | + for config, decorator in product( |
| 32 | + [None, 0, False, [], [1, 2, 3], 42, {4, 5}], |
| 33 | + (csp_override, csp_report_only_override), |
| 34 | + ): |
| 35 | + with ( |
| 36 | + self.subTest(config=config, decorator=decorator), |
| 37 | + self.assertRaisesMessage(TypeError, "CSP config should be a mapping"), |
| 38 | + ): |
| 39 | + decorator(config) |
| 40 | + |
| 41 | + def test_csp_override(self): |
| 42 | + @csp_override(basic_config) |
| 43 | + def sync_view(request): |
| 44 | + return HttpResponse("OK") |
| 45 | + |
| 46 | + response = sync_view(HttpRequest()) |
| 47 | + self.assertEqual(response._csp_config, basic_config) |
| 48 | + self.assertIs(hasattr(response, "_csp_ro_config"), False) |
| 49 | + |
| 50 | + async def test_csp_override_async_view(self): |
| 51 | + @csp_override(basic_config) |
| 52 | + async def async_view(request): |
| 53 | + return HttpResponse("OK") |
| 54 | + |
| 55 | + response = await async_view(HttpRequest()) |
| 56 | + self.assertEqual(response._csp_config, basic_config) |
| 57 | + self.assertIs(hasattr(response, "_csp_ro_config"), False) |
| 58 | + |
| 59 | + def test_csp_report_only_override(self): |
| 60 | + @csp_report_only_override(basic_config) |
| 61 | + def sync_view(request): |
| 62 | + return HttpResponse("OK") |
| 63 | + |
| 64 | + response = sync_view(HttpRequest()) |
| 65 | + self.assertEqual(response._csp_ro_config, basic_config) |
| 66 | + self.assertIs(hasattr(response, "_csp_config"), False) |
| 67 | + |
| 68 | + async def test_csp_report_only_override_async_view(self): |
| 69 | + @csp_report_only_override(basic_config) |
| 70 | + async def async_view(request): |
| 71 | + return HttpResponse("OK") |
| 72 | + |
| 73 | + response = await async_view(HttpRequest()) |
| 74 | + self.assertEqual(response._csp_ro_config, basic_config) |
| 75 | + self.assertIs(hasattr(response, "_csp_config"), False) |
| 76 | + |
| 77 | + def test_csp_override_both(self): |
| 78 | + @csp_override(basic_config) |
| 79 | + @csp_report_only_override(basic_config) |
| 80 | + def sync_view(request): |
| 81 | + return HttpResponse("OK") |
| 82 | + |
| 83 | + response = sync_view(HttpRequest()) |
| 84 | + self.assertEqual(response._csp_config, basic_config) |
| 85 | + self.assertEqual(response._csp_ro_config, basic_config) |
| 86 | + |
| 87 | + async def test_csp_override_both_async_view(self): |
| 88 | + @csp_override(basic_config) |
| 89 | + @csp_report_only_override(basic_config) |
| 90 | + async def async_view(request): |
| 91 | + return HttpResponse("OK") |
| 92 | + |
| 93 | + response = await async_view(HttpRequest()) |
| 94 | + self.assertEqual(response._csp_config, basic_config) |
| 95 | + self.assertEqual(response._csp_ro_config, basic_config) |
0 commit comments