Skip to content

Commit 260d2fb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 64041b6 commit 260d2fb

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

debug_toolbar/toolbar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
from django.urls import include, path, re_path, resolve
1717
from django.urls.exceptions import Resolver404
1818
from django.utils.module_loading import import_string
19-
from django.utils.translation import get_language
20-
from django.utils.translation import override as lang_override
19+
from django.utils.translation import get_language, override as lang_override
2120

22-
from debug_toolbar import APP_NAME
23-
from debug_toolbar import settings as dt_settings
21+
from debug_toolbar import APP_NAME, settings as dt_settings
2422
from debug_toolbar.panels import Panel
2523

2624

tests/panels/test_template.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def test_custom_context_processor(self):
9898
)
9999

100100
def test_disabled(self):
101-
config = {"DISABLE_PANELS": {
102-
"debug_toolbar.panels.templates.TemplatesPanel"}}
101+
config = {"DISABLE_PANELS": {"debug_toolbar.panels.templates.TemplatesPanel"}}
103102
self.assertTrue(self.panel.enabled)
104103
with self.settings(DEBUG_TOOLBAR_CONFIG=config):
105104
self.assertFalse(self.panel.enabled)

tests/test_csp_rendering.py

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,85 +15,81 @@ def _get_ns(element: Element) -> Dict[str, str]:
1515
Return the default `xmlns`. See
1616
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
1717
"""
18-
if not element.tag.startswith('{'):
18+
if not element.tag.startswith("{"):
1919
return {}
20-
return {'': element.tag[1:].split('}', maxsplit=1)[0]}
20+
return {"": element.tag[1:].split("}", maxsplit=1)[0]}
2121

2222

2323
class CspRenderingTestCase(BaseTestCase):
24-
'Testing if `csp-nonce` renders.'
24+
"Testing if `csp-nonce` renders."
25+
2526
panel_id = "StaticFilesPanel"
2627

2728
def _fail_if_missing(
28-
self, root: Element, path: str, namespaces: Dict[str, str],
29-
nonce: str):
29+
self, root: Element, path: str, namespaces: Dict[str, str], nonce: str
30+
):
3031
"""
3132
Search elements, fail if a `nonce` attribute is missing on them.
3233
"""
3334
elements = root.findall(path=path, namespaces=namespaces)
3435
for item in elements:
35-
if item.attrib.get('nonce') != nonce:
36-
raise self.failureException(f'{item} has no nonce attribute.')
36+
if item.attrib.get("nonce") != nonce:
37+
raise self.failureException(f"{item} has no nonce attribute.")
3738

38-
def _fail_if_found(
39-
self, root: Element, path: str, namespaces: Dict[str, str]):
39+
def _fail_if_found(self, root: Element, path: str, namespaces: Dict[str, str]):
4040
"""
4141
Search elements, fail if a `nonce` attribute is found on them.
4242
"""
4343
elements = root.findall(path=path, namespaces=namespaces)
4444
for item in elements:
45-
if 'nonce' in item.attrib:
46-
raise self.failureException(f'{item} has no nonce attribute.')
45+
if "nonce" in item.attrib:
46+
raise self.failureException(f"{item} has no nonce attribute.")
4747

4848
def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser):
49-
'Fail if the passed HTML is invalid.'
49+
"Fail if the passed HTML is invalid."
5050
if parser.errors:
51-
default_msg = ['Content is invalid HTML:']
52-
lines = content.split(b'\n')
51+
default_msg = ["Content is invalid HTML:"]
52+
lines = content.split(b"\n")
5353
for position, errorcode, datavars in parser.errors:
54-
default_msg.append(' %s' % E[errorcode] % datavars)
55-
default_msg.append(' %r' % lines[position[0] - 1])
56-
msg = self._formatMessage(None, '\n'.join(default_msg))
54+
default_msg.append(" %s" % E[errorcode] % datavars)
55+
default_msg.append(" %r" % lines[position[0] - 1])
56+
msg = self._formatMessage(None, "\n".join(default_msg))
5757
raise self.failureException(msg)
5858

5959
@override_settings(
60-
DEBUG=True, MIDDLEWARE=settings.MIDDLEWARE + [
61-
'csp.middleware.CSPMiddleware'
62-
])
60+
DEBUG=True, MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"]
61+
)
6362
def test_exists(self):
64-
'A `nonce` should exists when using the `CSPMiddleware`.'
65-
response = self.client.get(path='/regular/basic/')
63+
"A `nonce` should exists when using the `CSPMiddleware`."
64+
response = self.client.get(path="/regular/basic/")
6665
if not isinstance(response, HttpResponse):
67-
raise self.failureException(f'{response!r} is not a HttpResponse')
66+
raise self.failureException(f"{response!r} is not a HttpResponse")
6867
self.assertEqual(response.status_code, 200)
6968
parser = HTMLParser()
7069
el_htmlroot: Element = parser.parse(stream=response.content)
7170
self._fail_on_invalid_html(content=response.content, parser=parser)
72-
self.assertContains(response, 'djDebug')
71+
self.assertContains(response, "djDebug")
7372
namespaces = _get_ns(element=el_htmlroot)
74-
context: ContextList = \
75-
response.context # pyright: ignore[reportAttributeAccessIssue]
76-
nonce = str(context['toolbar'].request.csp_nonce)
73+
context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue]
74+
nonce = str(context["toolbar"].request.csp_nonce)
7775
self._fail_if_missing(
78-
root=el_htmlroot, path='.//link', namespaces=namespaces,
79-
nonce=nonce)
76+
root=el_htmlroot, path=".//link", namespaces=namespaces, nonce=nonce
77+
)
8078
self._fail_if_missing(
81-
root=el_htmlroot, path='.//script', namespaces=namespaces,
82-
nonce=nonce)
79+
root=el_htmlroot, path=".//script", namespaces=namespaces, nonce=nonce
80+
)
8381

8482
@override_settings(DEBUG=True)
8583
def test_missing(self):
86-
'A `nonce` should not exist when not using the `CSPMiddleware`.'
87-
response = self.client.get(path='/regular/basic/')
84+
"A `nonce` should not exist when not using the `CSPMiddleware`."
85+
response = self.client.get(path="/regular/basic/")
8886
if not isinstance(response, HttpResponse):
89-
raise self.failureException(f'{response!r} is not a HttpResponse')
87+
raise self.failureException(f"{response!r} is not a HttpResponse")
9088
self.assertEqual(response.status_code, 200)
9189
parser = HTMLParser()
9290
el_htmlroot: Element = parser.parse(stream=response.content)
9391
self._fail_on_invalid_html(content=response.content, parser=parser)
94-
self.assertContains(response, 'djDebug')
92+
self.assertContains(response, "djDebug")
9593
namespaces = _get_ns(element=el_htmlroot)
96-
self._fail_if_found(
97-
root=el_htmlroot, path='.//link', namespaces=namespaces)
98-
self._fail_if_found(
99-
root=el_htmlroot, path='.//script', namespaces=namespaces)
94+
self._fail_if_found(root=el_htmlroot, path=".//link", namespaces=namespaces)
95+
self._fail_if_found(root=el_htmlroot, path=".//script", namespaces=namespaces)

tests/test_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def title(self):
5353
def content(self):
5454
raise Exception
5555

56+
5657
@override_settings(DEBUG=True)
5758
class DebugToolbarTestCase(BaseTestCase):
5859
def test_show_toolbar(self):

0 commit comments

Comments
 (0)