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 16f74b8
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions tests/test_csp_rendering.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Dict
from typing import Dict, 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 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 @@ -65,7 +66,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/")
response = cast(
typ=HttpResponse, val=self.client.get(path="/regular/basic/"))
self.assertEqual(response.status_code, 200)

html_root: Element = self.parser.parse(stream=response.content)
Expand All @@ -87,15 +89,17 @@ def test_exists(self):
MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"],
)
def test_redirects_exists(self):
response = self.client.get("/redirect/")
response = cast(
typ=HttpResponse, val=self.client.get(path="/regular/basic/"))
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 @@ -108,7 +112,8 @@ def test_redirects_exists(self):
MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"]
)
def test_panel_content_nonce_exists(self):
response = self.client.get("/regular/basic/")
response = cast(
typ=HttpResponse, val=self.client.get(path="/regular/basic/"))
self.assertEqual(response.status_code, 200)

toolbar = list(DebugToolbar._store.values())[0]
Expand All @@ -127,7 +132,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/")
response = cast(
typ=HttpResponse, val=self.client.get(path="/regular/basic/"))
self.assertEqual(response.status_code, 200)

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

0 comments on commit 16f74b8

Please sign in to comment.