Skip to content

Commit

Permalink
Fix typing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karolyi committed Aug 2, 2024
1 parent ad4b463 commit bc2bae8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions tests/test_csp_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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 html5lib.constants import E
from html5lib.html5parser import HTMLParser
Expand Down Expand Up @@ -47,7 +48,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: Dict[str, str]):
elements = root.findall(path=path, namespaces=namespaces)
for item in elements:
if "nonce" in item.attrib:
raise self.failureException(f"{item} has no nonce attribute.")
raise self.failureException(f"{item} has a nonce attribute.")

def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser):
"""Fail if the passed HTML is invalid."""
Expand All @@ -66,6 +67,8 @@ def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser):
def test_exists(self):
"""A `nonce` should exist when using the `CSPMiddleware`."""
response = self.client.get(path="/regular/basic/")
if not isinstance(response, HttpResponse):
raise self.failureException(f'{response!r} is not a HttpResponse')
self.assertEqual(response.status_code, 200)

html_root: Element = self.parser.parse(stream=response.content)
Expand All @@ -88,14 +91,17 @@ def test_exists(self):
)
def test_redirects_exists(self):
response = self.client.get("/redirect/")
if not isinstance(response, HttpResponse):
raise self.failureException(f'{response!r} is not a HttpResponse')
self.assertEqual(response.status_code, 200)

html_root: Element = 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
context: ContextList = \
response.context # pyright: ignore[reportAttributeAccessIssue]
nonce = str(context["toolbar"].request.csp_nonce)
self._fail_if_missing(
root=html_root, path=".//link", namespaces=namespaces, nonce=nonce
Expand All @@ -109,6 +115,8 @@ def test_redirects_exists(self):
)
def test_panel_content_nonce_exists(self):
response = self.client.get("/regular/basic/")
if not isinstance(response, HttpResponse):
raise self.failureException(f'{response!r} is not a HttpResponse')
self.assertEqual(response.status_code, 200)

toolbar = list(DebugToolbar._store.values())[0]
Expand All @@ -128,6 +136,8 @@ def test_panel_content_nonce_exists(self):
def test_missing(self):
"""A `nonce` should not exist when not using the `CSPMiddleware`."""
response = self.client.get(path="/regular/basic/")
if not isinstance(response, HttpResponse):
raise self.failureException(f'{response!r} is not a HttpResponse')
self.assertEqual(response.status_code, 200)

html_root: Element = self.parser.parse(stream=response.content)
Expand Down

0 comments on commit bc2bae8

Please sign in to comment.