Skip to content

Commit adde0e0

Browse files
committed
added validate path tests and settings
1 parent 06ced33 commit adde0e0

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

dynamic_breadcrumbs/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ def validate_path(path):
3333
logger.warning("Invalid path type provided: %s", type(path))
3434
return ""
3535

36-
# Ensure the path contains only alphanumeric characters, dashes, underscores, and slashes
37-
if not re.match(r'^[a-zA-Z0-9_\-/]*$', path):
38-
logger.warning("Invalid path provided: %s", path)
39-
return ""
36+
if app_settings.PATH_ONLY_ALPHANUMERIC:
37+
# Ensure the path contains only alphanumeric characters, dashes, underscores, and slashes
38+
if not re.match(r'^[a-zA-Z0-9_\-/]*$', path):
39+
logger.warning("Invalid path provided: %s", path)
40+
return ""
4041

4142
components = path.split('/')
4243
# Check path depth

tests/tests.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from dynamic_breadcrumbs import app_settings
55
from django.conf import settings
66
from django.http import HttpResponse, HttpRequest
7+
from dynamic_breadcrumbs import app_settings
78

8-
@override_settings(ALLOWED_HOSTS="www.example.com")
9+
@override_settings(ALLOWED_HOSTS=["www.example.com"])
910
class SanitizeUrlTests(TestCase):
1011

1112
def test_sanitize_base_urls_host_in_allowed_hosts_keeps_url(self):
@@ -22,13 +23,29 @@ def test_sanitize_base_urls_host_not_in_allowed_hosts_returns_empty_string(self)
2223

2324
self.assertEqual(result, "")
2425

25-
# class ValidatePathTests(GenericModelTestCase):
26-
# def test_sanitize_base_urls_host_in_allowed_hosts_keeps_url(self):
27-
# path= f"https://{settings.ALLOWED_HOSTS[0]}"
26+
class ValidatePathTests(TestCase):
27+
def test_validate_path_not_string_returns_empty_string(self):
28+
path=3.1416
29+
30+
result = validate_path(path=path)
31+
32+
self.assertEqual(result, "")
33+
34+
def test_validate_path_check_alphanumeric_returns_empty_string(self):
35+
app_settings.PATH_ALPHANUMERIC=True
36+
path="*%#$@@#*(/%#%_)*"
37+
38+
result = validate_path(path=path)
39+
40+
self.assertEqual(result, "")
41+
42+
def test_validate_path_non_check_alphanumeric_returns_path(self):
43+
app_settings.PATH_ALPHANUMERIC=False
44+
path="*%#$@@#*(/%#%_)*"
2845

29-
# result = sanitize_url(url=base_url)
46+
result = validate_path(path=path)
3047

31-
# self.assertEqual(result, base_url)
48+
self.assertEqual(result, path)
3249

3350

3451
class BreadcrumbsTests(TestCase):

0 commit comments

Comments
 (0)