1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
import unittest
from django.http import QueryDict
from django.test import override_settings
import debug_toolbar.utils
from debug_toolbar.utils import (
get_name_from_obj,
get_stack,
get_stack_trace,
render_stacktrace,
sanitize_and_sort_request_vars,
tidy_stacktrace,
)
class GetNameFromObjTestCase(unittest.TestCase):
def test_func(self):
def x():
return 1
res = get_name_from_obj(x)
self.assertEqual(
res, "tests.test_utils.GetNameFromObjTestCase.test_func.<locals>.x"
)
def test_lambda(self):
res = get_name_from_obj(lambda: 1)
self.assertEqual(
res, "tests.test_utils.GetNameFromObjTestCase.test_lambda.<locals>.<lambda>"
)
def test_class(self):
class A:
pass
res = get_name_from_obj(A)
self.assertEqual(
res, "tests.test_utils.GetNameFromObjTestCase.test_class.<locals>.A"
)
class RenderStacktraceTestCase(unittest.TestCase):
def test_importlib_path_issue_1612(self):
trace = [
("/server/app.py", 1, "foo", ["code line 1", "code line 2"], {"foo": "bar"})
]
result = render_stacktrace(trace)
self.assertIn('<span class="djdt-path">/server/</span>', result)
self.assertIn('<span class="djdt-file">app.py</span> in', result)
trace = [
(
"<frozen importlib._bootstrap>",
1,
"foo",
["code line 1", "code line 2"],
{"foo": "bar"},
)
]
result = render_stacktrace(trace)
self.assertIn('<span class="djdt-path"></span>', result)
self.assertIn(
'<span class="djdt-file"><frozen importlib._bootstrap></span> in',
result,
)
class StackTraceTestCase(unittest.TestCase):
@override_settings(DEBUG_TOOLBAR_CONFIG={"HIDE_IN_STACKTRACES": []})
def test_get_stack_trace_skip(self):
stack_trace = get_stack_trace(skip=-1)
self.assertTrue(len(stack_trace) > 2)
self.assertEqual(stack_trace[-1][0], debug_toolbar.utils.__file__)
self.assertEqual(stack_trace[-1][2], "get_stack_trace")
self.assertEqual(stack_trace[-2][0], __file__)
self.assertEqual(stack_trace[-2][2], "test_get_stack_trace_skip")
stack_trace = get_stack_trace()
self.assertTrue(len(stack_trace) > 1)
self.assertEqual(stack_trace[-1][0], __file__)
self.assertEqual(stack_trace[-1][2], "test_get_stack_trace_skip")
def test_deprecated_functions(self):
with self.assertWarns(DeprecationWarning):
stack = get_stack()
self.assertEqual(stack[0][1], __file__)
with self.assertWarns(DeprecationWarning):
stack_trace = tidy_stacktrace(reversed(stack))
self.assertEqual(stack_trace[-1][0], __file__)
@override_settings(DEBUG_TOOLBAR_CONFIG={"ENABLE_STACKTRACES_LOCALS": True})
def test_locals(self):
# This wrapper class is necessary to mask the repr() of the list
# returned by get_stack_trace(); otherwise the 'test_locals_value_1'
# string will also be present in rendered_stack_2.
class HideRepr:
def __init__(self, value):
self.value = value
x = "test_locals_value_1"
stack_1_wrapper = HideRepr(get_stack_trace())
x = x.replace("1", "2")
stack_2_wrapper = HideRepr(get_stack_trace())
rendered_stack_1 = render_stacktrace(stack_1_wrapper.value)
self.assertIn("test_locals_value_1", rendered_stack_1)
self.assertNotIn("test_locals_value_2", rendered_stack_1)
rendered_stack_2 = render_stacktrace(stack_2_wrapper.value)
self.assertNotIn("test_locals_value_1", rendered_stack_2)
self.assertIn("test_locals_value_2", rendered_stack_2)
class SanitizeAndSortRequestVarsTestCase(unittest.TestCase):
"""Tests for the sanitize_and_sort_request_vars function."""
def test_dict_sanitization(self):
"""Test sanitization of a regular dictionary."""
test_dict = {
"username": "testuser",
"password": "secret123",
"api_key": "abc123",
}
result = sanitize_and_sort_request_vars(test_dict)
# Convert to dict for easier testing
result_dict = dict(result["list"])
self.assertEqual(result_dict["username"], "testuser")
self.assertEqual(result_dict["password"], "********************")
self.assertEqual(result_dict["api_key"], "********************")
def test_querydict_sanitization(self):
"""Test sanitization of a QueryDict."""
query_dict = QueryDict("username=testuser&password=secret123&api_key=abc123")
result = sanitize_and_sort_request_vars(query_dict)
# Convert to dict for easier testing
result_dict = dict(result["list"])
self.assertEqual(result_dict["username"], "testuser")
self.assertEqual(result_dict["password"], "********************")
self.assertEqual(result_dict["api_key"], "********************")
def test_non_sortable_dict_keys(self):
"""Test dictionary with keys that can't be sorted."""
test_dict = {
1: "one",
"2": "two",
None: "none",
}
result = sanitize_and_sort_request_vars(test_dict)
self.assertEqual(len(result["list"]), 3)
result_dict = dict(result["list"])
self.assertEqual(result_dict[1], "one")
self.assertEqual(result_dict["2"], "two")
self.assertEqual(result_dict[None], "none")
def test_querydict_multiple_values(self):
"""Test QueryDict with multiple values for the same key."""
query_dict = QueryDict("name=bar1&name=bar2&title=value")
result = sanitize_and_sort_request_vars(query_dict)
result_dict = dict(result["list"])
self.assertEqual(result_dict["name"], ["bar1", "bar2"])
self.assertEqual(result_dict["title"], "value")
def test_non_dict_input(self):
"""Test handling of non-dict input."""
test_input = ["not", "a", "dict"]
result = sanitize_and_sort_request_vars(test_input)
self.assertEqual(result["raw"], test_input)
|