Skip to content

Commit 856ad85

Browse files
committed
Refactor URL generation in docs view and enhance test assertions
- Introduced a helper function `get_url` to streamline URL construction in the `docs` view. - Updated test cases to assert the presence of the `utm_referrer=amo` query parameter for 301 redirects. - Improved readability of the test case by unpacking the URL and status code directly in the loop.
1 parent c7c4df4 commit 856ad85

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/olympia/devhub/tests/test_views.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,11 +2066,14 @@ def test_doc_urls(self):
20662066

20672067
index = reverse('devhub.index')
20682068

2069-
for url in urls:
2070-
response = self.client.get(url[0])
2071-
assert response.status_code == url[1]
2069+
for [url, status_code] in urls:
2070+
response = self.client.get(url)
2071+
assert response.status_code == status_code
2072+
2073+
if status_code == 301:
2074+
assert 'utm_referrer=amo' in response.url
20722075

2073-
if url[1] == 302: # Redirect to the index page
2076+
if status_code == 302: # Redirect to the index page
20742077
self.assert3xx(response, index)
20752078

20762079

src/olympia/devhub/views.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import time
44
from copy import deepcopy
5-
from urllib.parse import quote
5+
from urllib.parse import quote, urlencode, urljoin
66
from uuid import UUID, uuid4
77

88
from django import forms as django_forms, http
@@ -1951,11 +1951,16 @@ def request_review(request, addon_id, addon):
19511951

19521952

19531953
def docs(request, doc_name=None):
1954+
def get_url(base, doc=None):
1955+
base_url = urljoin(base, doc)
1956+
query = urlencode({'utm_referrer': 'amo'})
1957+
return f'{base_url}?{query}'
1958+
19541959
def mdn_url(doc):
1955-
return MDN_BASE + doc
1960+
return get_url(MDN_BASE, doc)
19561961

19571962
def ext_url(doc):
1958-
return settings.EXTENSION_WORKSHOP_URL + doc
1963+
return get_url(settings.EXTENSION_WORKSHOP_URL, doc)
19591964

19601965
mdn_docs = {
19611966
None: mdn_url(''),

0 commit comments

Comments
 (0)