Skip to content

Remove Type Hints from CspRenderingTestCase #2144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions tests/test_csp_rendering.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from __future__ import annotations

from typing import cast
from xml.etree.ElementTree import Element

from django.conf import settings
from django.http.response import HttpResponse
from django.test.utils import ContextList, override_settings
from django.test.utils import override_settings
from html5lib.constants import E
from html5lib.html5parser import HTMLParser

Expand All @@ -21,7 +17,7 @@
MIDDLEWARE_CSP_LAST = settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"]


def get_namespaces(element: Element) -> dict[str, str]:
def get_namespaces(element):
"""
Return the default `xmlns`. See
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
Expand All @@ -39,9 +35,7 @@ def setUp(self):
super().setUp()
self.parser = HTMLParser()

def _fail_if_missing(
self, root: Element, path: str, namespaces: dict[str, str], nonce: str
):
def _fail_if_missing(self, root, path, namespaces, nonce):
"""
Search elements, fail if a `nonce` attribute is missing on them.
"""
Expand All @@ -50,7 +44,7 @@ def _fail_if_missing(
if item.attrib.get("nonce") != nonce:
raise self.failureException(f"{item} has no nonce attribute.")

def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
def _fail_if_found(self, root, path, namespaces):
"""
Search elements, fail if a `nonce` attribute is found on them.
"""
Expand All @@ -59,7 +53,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
if "nonce" in item.attrib:
raise self.failureException(f"{item} has a nonce attribute.")

def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser):
def _fail_on_invalid_html(self, content, parser):
"""Fail if the passed HTML is invalid."""
if parser.errors:
default_msg = ["Content is invalid HTML:"]
Expand All @@ -74,10 +68,10 @@ def test_exists(self):
"""A `nonce` should exist when using the `CSPMiddleware`."""
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
with self.settings(MIDDLEWARE=middleware):
response = cast(HttpResponse, self.client.get(path="/csp_view/"))
response = self.client.get(path="/csp_view/")
self.assertEqual(response.status_code, 200)

html_root: Element = self.parser.parse(stream=response.content)
html_root = self.parser.parse(stream=response.content)
self._fail_on_invalid_html(content=response.content, parser=self.parser)
self.assertContains(response, "djDebug")

Expand All @@ -98,10 +92,10 @@ def test_does_not_exist_nonce_wasnt_used(self):
"""
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
with self.settings(MIDDLEWARE=middleware):
response = cast(HttpResponse, self.client.get(path="/regular/basic/"))
response = self.client.get(path="/regular/basic/")
self.assertEqual(response.status_code, 200)

html_root: Element = self.parser.parse(stream=response.content)
html_root = self.parser.parse(stream=response.content)
self._fail_on_invalid_html(content=response.content, parser=self.parser)
self.assertContains(response, "djDebug")

Expand All @@ -119,15 +113,15 @@ def test_does_not_exist_nonce_wasnt_used(self):
def test_redirects_exists(self):
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
with self.settings(MIDDLEWARE=middleware):
response = cast(HttpResponse, self.client.get(path="/csp_view/"))
response = self.client.get(path="/csp_view/")
self.assertEqual(response.status_code, 200)

html_root: Element = self.parser.parse(stream=response.content)
html_root = self.parser.parse(stream=response.content)
self._fail_on_invalid_html(content=response.content, parser=self.parser)
self.assertContains(response, "djDebug")

namespaces = get_namespaces(element=html_root)
context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue]
context = response.context
nonce = str(context["toolbar"].csp_nonce)
self._fail_if_missing(
root=html_root, path=".//link", namespaces=namespaces, nonce=nonce
Expand All @@ -139,14 +133,14 @@ def test_redirects_exists(self):
def test_panel_content_nonce_exists(self):
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
with self.settings(MIDDLEWARE=middleware):
response = cast(HttpResponse, self.client.get(path="/csp_view/"))
response = self.client.get(path="/csp_view/")
self.assertEqual(response.status_code, 200)

toolbar = list(DebugToolbar._store.values())[-1]
panels_to_check = ["HistoryPanel", "TimerPanel"]
for panel in panels_to_check:
content = toolbar.get_panel_by_id(panel).content
html_root: Element = self.parser.parse(stream=content)
html_root = self.parser.parse(stream=content)
namespaces = get_namespaces(element=html_root)
nonce = str(toolbar.csp_nonce)
self._fail_if_missing(
Expand All @@ -164,10 +158,10 @@ def test_panel_content_nonce_exists(self):

def test_missing(self):
"""A `nonce` should not exist when not using the `CSPMiddleware`."""
response = cast(HttpResponse, self.client.get(path="/regular/basic/"))
response = self.client.get(path="/regular/basic/")
self.assertEqual(response.status_code, 200)

html_root: Element = self.parser.parse(stream=response.content)
html_root = self.parser.parse(stream=response.content)
self._fail_on_invalid_html(content=response.content, parser=self.parser)
self.assertContains(response, "djDebug")

Expand Down