22
33from django .test import TestCase , override_settings
44
5- from dynamic_breadcrumbs .utils import Breadcrumbs , BreadcrumbsItem , validate_path , sanitize_url
5+ from dynamic_breadcrumbs .utils import (
6+ Breadcrumbs ,
7+ validate_path ,
8+ sanitize_url ,
9+ )
610from dynamic_breadcrumbs import app_settings
711from django .conf import settings
8- from django .http import HttpResponse , HttpRequest
9- from dynamic_breadcrumbs import app_settings
12+
1013
1114@override_settings (ALLOWED_HOSTS = ["www.example.com" ])
1215class SanitizeUrlTests (TestCase ):
13-
1416 def test_sanitize_base_urls_host_in_allowed_hosts_keeps_url (self ):
15- base_url = f"https://{ settings .ALLOWED_HOSTS [0 ]} "
16-
17+ base_url = f"https://{ settings .ALLOWED_HOSTS [0 ]} "
18+
1719 result = sanitize_url (url = base_url )
18-
20+
1921 self .assertEqual (result , base_url )
2022
2123 def test_sanitize_base_urls_host_not_in_allowed_hosts_returns_empty_string (self ):
22- base_url = "https://anothersite.com"
23-
24+ base_url = "https://anothersite.com"
25+
2426 result = sanitize_url (url = base_url )
25-
27+
2628 self .assertEqual (result , "" )
2729
30+
2831class ValidatePathTests (TestCase ):
2932 def test_validate_path_not_string_returns_empty_string (self ):
30- path = 3.1416
31-
33+ path = 3.1416
34+
3235 result = validate_path (path = path )
33-
36+
3437 self .assertEqual (result , "" )
3538
3639 def test_validate_path_check_alphanumeric_returns_empty_string (self ):
37- app_settings .PATH_XSS_SAFE_MODE = True
38- path = "*%#$@@#*(/%#%_)*"
39-
40+ app_settings .PATH_XSS_SAFE_MODE = True
41+ path = "*%#$@@#*(/%#%_)*"
42+
4043 result = validate_path (path = path )
41-
44+
4245 self .assertEqual (result , "" )
43-
46+
4447 def test_validate_path_non_check_alphanumeric_returns_path (self ):
45- app_settings .PATH_XSS_SAFE_MODE = False
46- path = "*%#$@@#*(/%#%_)*"
47-
48+ app_settings .PATH_XSS_SAFE_MODE = False
49+ path = "*%#$@@#*(/%#%_)*"
50+
4851 result = validate_path (path = path )
49-
52+
5053 self .assertEqual (result , path )
51-
54+
5255
5356class BreadcrumbsTests (TestCase ):
5457 def setUp (self ):
5558 self .host = settings .ALLOWED_HOSTS [0 ]
5659
57- #response = self.client.get('/')
58- #request = HttpRequest().build_absolute_uri(location="/")
59- #print(request)
60+ # response = self.client.get('/')
61+ # request = HttpRequest().build_absolute_uri(location="/")
62+ # print(request)
6063
61- #self.breadcrumbs = Breadcrumbs(base_url=self.base_url)
64+ # self.breadcrumbs = Breadcrumbs(base_url=self.base_url)
6265
6366 def test_split_path_ending_in_slash (self ):
6467 path = "/scale/minor-scale/"
@@ -86,9 +89,9 @@ def test_split_path_returns_empty_list(self):
8689
8790 self .assertEqual (paths , expected_result )
8891
89- @patch (' dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata' )
92+ @patch (" dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata" )
9093 def test_process_all_paths_including_home (self , mock_resolve ):
91- mock_resolve = True
94+ mock_resolve = True # noqa
9295 base_url = "https://example.com"
9396 path = "/scale/minor-scale/c"
9497 breadcrumbs = Breadcrumbs (base_url = base_url , path = path )
@@ -97,9 +100,9 @@ def test_process_all_paths_including_home(self, mock_resolve):
97100
98101 self .assertEqual (len (result ), 4 )
99102
100- @patch (' dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata' )
103+ @patch (" dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata" )
101104 def test_as_list (self , mock_resolve ):
102- mock_resolve = True
105+ mock_resolve = True # noqa
103106 path = "/continent/some-continent/"
104107 breadcrumbs = Breadcrumbs (path = path )
105108
@@ -109,9 +112,9 @@ def test_as_list(self, mock_resolve):
109112 self .assertEqual (result [1 ]["name" ], "continent" )
110113 self .assertEqual (result [2 ]["name" ], "some-continent" )
111114
112- @patch (' dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata' )
115+ @patch (" dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata" )
113116 def test_as_list_only_home (self , mock_resolve ):
114- mock_resolve = True
117+ mock_resolve = True # noqa
115118 app_settings .SHOW_AT_BASE_PATH = True
116119 path = "/"
117120 breadcrumbs = Breadcrumbs (path = path )
@@ -120,9 +123,9 @@ def test_as_list_only_home(self, mock_resolve):
120123
121124 self .assertEqual (len (result ), 1 )
122125
123- @patch (' dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata' )
126+ @patch (" dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata" )
124127 def test_as_list_not_showing_at_home (self , mock_resolve ):
125- mock_resolve = True
128+ mock_resolve = True # noqa
126129 app_settings .SHOW_AT_BASE_PATH = False
127130 path = "/"
128131 breadcrumbs = Breadcrumbs (path = path )
@@ -131,9 +134,9 @@ def test_as_list_not_showing_at_home(self, mock_resolve):
131134
132135 self .assertEqual (len (result ), 0 )
133136
134- @patch (' dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata' )
137+ @patch (" dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata" )
135138 def test_show_home_at_base_url (self , mock_resolve ):
136- mock_resolve = True
139+ mock_resolve = True # noqa
137140 app_settings .SHOW_AT_BASE_PATH = True
138141 path = "/"
139142 breadcrumbs = Breadcrumbs (path = path )
@@ -142,9 +145,9 @@ def test_show_home_at_base_url(self, mock_resolve):
142145
143146 self .assertEqual (len (result ), 1 )
144147
145- @patch (' dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata' )
148+ @patch (" dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata" )
146149 def test_hide_home_at_base_url (self , mock_resolve ):
147- mock_resolve = True
150+ mock_resolve = True # noqa
148151 app_settings .SHOW_AT_BASE_PATH = False
149152 path = "/"
150153 breadcrumbs = Breadcrumbs (path = path )
@@ -153,27 +156,21 @@ def test_hide_home_at_base_url(self, mock_resolve):
153156
154157 self .assertEqual (len (result ), 0 )
155158
156-
157-
158- @patch ('dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata' )
159+ @patch ("dynamic_breadcrumbs.utils.BreadcrumbsItem._get_resolved_url_metadata" )
159160 def test_filter_xss_attacks (self , mock_resolve ):
160- mock_resolve = False
161+ mock_resolve = False # noqa
161162
162163 app_settings .SHOW_AT_BASE_PATH = False
163164 malicious_code = """
164165 jaVasCript:/*-/*`/*\`/*'/*"/**/(/* */oNcliCk=alert() )//%0D%0A%0d%0a//</stYle/</titLe/</teXtarEa/</scRipt/--!>\x3c sVg/<sVg/oNloAd=alert()//>\x3e
165- """
166+ """ # noqa
166167 path = f"/level/{ malicious_code } /leaf/"
167168 breadcrumbs = Breadcrumbs (path = path )
168169
169170 result = breadcrumbs .as_list ()
170171 self .assertEqual (len (result ), 0 )
171172
172173
173-
174-
175-
176-
177174# class BreadcrumbsItemTests(TestCase):
178175# def test_get_resolved_url_metadata_resolves_valid_path(self):
179176# item = BreadcrumbsItem(
0 commit comments