Skip to content

Commit c3dc4bb

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

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,89 +15,85 @@ 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 setUp(self):
2829
# self.factory = RequestFactory()
2930
# self.async_factory = AsyncRequestFactory()
3031

3132
def _fail_if_missing(
32-
self, root: Element, path: str, namespaces: Dict[str, str],
33-
nonce: str):
33+
self, root: Element, path: str, namespaces: Dict[str, str], nonce: str
34+
):
3435
"""
3536
Search elements, fail if a `nonce` attribute is missing on them.
3637
"""
3738
elements = root.findall(path=path, namespaces=namespaces)
3839
for item in elements:
39-
if item.attrib.get('nonce') != nonce:
40-
raise self.failureException(f'{item} has no nonce attribute.')
40+
if item.attrib.get("nonce") != nonce:
41+
raise self.failureException(f"{item} has no nonce attribute.")
4142

42-
def _fail_if_found(
43-
self, root: Element, path: str, namespaces: Dict[str, str]):
43+
def _fail_if_found(self, root: Element, path: str, namespaces: Dict[str, str]):
4444
"""
4545
Search elements, fail if a `nonce` attribute is found on them.
4646
"""
4747
elements = root.findall(path=path, namespaces=namespaces)
4848
for item in elements:
49-
if 'nonce' in item.attrib:
50-
raise self.failureException(f'{item} has no nonce attribute.')
49+
if "nonce" in item.attrib:
50+
raise self.failureException(f"{item} has no nonce attribute.")
5151

5252
def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser):
53-
'Fail if the passed HTML is invalid.'
53+
"Fail if the passed HTML is invalid."
5454
if parser.errors:
55-
default_msg = ['Content is invalid HTML:']
56-
lines = content.split(b'\n')
55+
default_msg = ["Content is invalid HTML:"]
56+
lines = content.split(b"\n")
5757
for position, errorcode, datavars in parser.errors:
58-
default_msg.append(' %s' % E[errorcode] % datavars)
59-
default_msg.append(' %r' % lines[position[0] - 1])
60-
msg = self._formatMessage(None, '\n'.join(default_msg))
58+
default_msg.append(" %s" % E[errorcode] % datavars)
59+
default_msg.append(" %r" % lines[position[0] - 1])
60+
msg = self._formatMessage(None, "\n".join(default_msg))
6161
raise self.failureException(msg)
6262

6363
@override_settings(
64-
DEBUG=True, MIDDLEWARE=settings.MIDDLEWARE + [
65-
'csp.middleware.CSPMiddleware'
66-
])
64+
DEBUG=True, MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"]
65+
)
6766
def test_exists(self):
68-
'A `nonce` should exists when using the `CSPMiddleware`.'
69-
response = self.client.get(path='/regular/basic/')
67+
"A `nonce` should exists when using the `CSPMiddleware`."
68+
response = self.client.get(path="/regular/basic/")
7069
if not isinstance(response, HttpResponse):
71-
raise self.failureException(f'{response!r} is not a HttpResponse')
70+
raise self.failureException(f"{response!r} is not a HttpResponse")
7271
self.assertEqual(response.status_code, 200)
7372
parser = HTMLParser()
7473
el_htmlroot: Element = parser.parse(stream=response.content)
7574
self._fail_on_invalid_html(content=response.content, parser=parser)
76-
self.assertContains(response, 'djDebug')
75+
self.assertContains(response, "djDebug")
7776
namespaces = _get_ns(element=el_htmlroot)
78-
context: ContextList = \
79-
response.context # pyright: ignore[reportAttributeAccessIssue]
80-
nonce = str(context['toolbar'].request.csp_nonce)
77+
context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue]
78+
nonce = str(context["toolbar"].request.csp_nonce)
8179
self._fail_if_missing(
82-
root=el_htmlroot, path='.//link', namespaces=namespaces,
83-
nonce=nonce)
80+
root=el_htmlroot, path=".//link", namespaces=namespaces, nonce=nonce
81+
)
8482
self._fail_if_missing(
85-
root=el_htmlroot, path='.//script', namespaces=namespaces,
86-
nonce=nonce)
83+
root=el_htmlroot, path=".//script", namespaces=namespaces, nonce=nonce
84+
)
8785

8886
@override_settings(DEBUG=True)
8987
def test_missing(self):
90-
'A `nonce` should not exist when not using the `CSPMiddleware`.'
91-
response = self.client.get(path='/regular/basic/')
88+
"A `nonce` should not exist when not using the `CSPMiddleware`."
89+
response = self.client.get(path="/regular/basic/")
9290
if not isinstance(response, HttpResponse):
93-
raise self.failureException(f'{response!r} is not a HttpResponse')
91+
raise self.failureException(f"{response!r} is not a HttpResponse")
9492
self.assertEqual(response.status_code, 200)
9593
parser = HTMLParser()
9694
el_htmlroot: Element = parser.parse(stream=response.content)
9795
self._fail_on_invalid_html(content=response.content, parser=parser)
98-
self.assertContains(response, 'djDebug')
96+
self.assertContains(response, "djDebug")
9997
namespaces = _get_ns(element=el_htmlroot)
100-
self._fail_if_found(
101-
root=el_htmlroot, path='.//link', namespaces=namespaces)
102-
self._fail_if_found(
103-
root=el_htmlroot, path='.//script', namespaces=namespaces)
98+
self._fail_if_found(root=el_htmlroot, path=".//link", namespaces=namespaces)
99+
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)