Skip to content

Commit b758826

Browse files
take into account url scheme and domain when checking for active (#126)
1 parent fef0efb commit b758826

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Fixed
22+
23+
- Checking whether a `NavItem` or `NavGroup` is active now takes into account the URL scheme and domain name for both the nav item and request.
24+
2125
## [0.10.0]
2226

2327
### Added

src/django_simple_nav/nav.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ def get_active(self, request: HttpRequest) -> bool:
140140
parsed_url = urlparse(url)
141141
parsed_request = urlparse(request.build_absolute_uri())
142142

143+
if (
144+
parsed_url.scheme != parsed_request.scheme
145+
or parsed_url.netloc != parsed_request.netloc
146+
):
147+
return False
148+
143149
url_path = parsed_url.path
144150
request_path = parsed_request.path
145151

tests/test_navgroup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,17 @@ def get_url(self):
200200
("/foo/", False),
201201
],
202202
)
203-
def test_get_active(url, expected, req):
203+
def test_get_active(url, expected, rf):
204204
group = NavGroup(
205205
title=...,
206-
url="/test/",
206+
url="http://testserver/test/",
207207
items=[
208-
NavItem(title=..., url="/test/active/"),
209-
NavItem(title=..., url="/test/not-active/"),
208+
NavItem(title=..., url="http://testserver/test/active/"),
209+
NavItem(title=..., url="http://testserver/test/not-active/"),
210210
],
211211
)
212212

213-
req.path = url
213+
req = rf.get(url)
214214

215215
assert group.get_active(req) is expected
216216

tests/test_navitem.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,34 @@ def get_url(self):
133133
],
134134
)
135135
def test_active(url, req_path, req_params, expected, rf):
136-
item = NavItem(title=..., url=url)
136+
item = NavItem(title=..., url=f"http://testserver/{url.lstrip('/')}")
137137

138138
req = rf.get(req_path, req_params)
139-
print(f"{req.path=}")
140139

141140
assert item.get_active(req) == expected
142141

143142

143+
def test_active_different_scheme(rf):
144+
item = NavItem(title=..., url="https://testserver/")
145+
146+
req = rf.get("/")
147+
148+
assert item.get_active(req) is False
149+
150+
151+
def test_active_different_domain(rf):
152+
item = NavItem(title=..., url="http://different-domain/")
153+
154+
req = rf.get("/")
155+
156+
assert item.get_active(req) is False
157+
158+
144159
@pytest.mark.parametrize("append_slash", [True, False])
145-
def test_active_append_slash_setting(append_slash, req):
146-
item = NavItem(title=..., url="/test")
160+
def test_active_append_slash_setting(append_slash, rf):
161+
item = NavItem(title=..., url="http://testserver/test")
147162

148-
req.path = "/test"
163+
req = rf.get("/test")
149164

150165
with override_settings(APPEND_SLASH=append_slash):
151166
assert item.get_active(req) is True

0 commit comments

Comments
 (0)