Skip to content

Commit 9addf91

Browse files
committed
Validate URL - must be a link to Notify docs taht goes to a section
in those docs.
1 parent 5f07d26 commit 9addf91

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

app/main/forms.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from functools import partial
66
from itertools import chain
77
from numbers import Number
8+
from urllib.parse import urlparse
89

910
import pytz
1011
from flask import request
@@ -2290,7 +2291,16 @@ class UrlForm(StripWhitespaceForm):
22902291
)
22912292

22922293
def validate(self, *args, **kwargs):
2293-
return super().validate(*args, **kwargs) or self.url.data == ""
2294+
self.url.validators.append(self.check_url)
2295+
return super().validate(*args, **kwargs)
2296+
2297+
def check_url(self, *args, **kwargs):
2298+
parsed_url = urlparse(self.url.data)
2299+
2300+
if parsed_url.hostname == "docs.notifications.service.gov.uk" and parsed_url.fragment:
2301+
return parsed_url
2302+
else:
2303+
raise ValidationError("Must be a valid https URL, pointing to a section within the GOV.UK Notify API docs.")
22942304

22952305

22962306
class SMSPrefixForm(StripWhitespaceForm):

tests/app/main/views/test_index.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,31 @@ def test_POST_guidance_api_documentation_section(client_request):
402402
section_tag="send-a-file-by-email",
403403
),
404404
)
405+
406+
407+
@pytest.mark.parametrize(
408+
"url, expected_error_message",
409+
[
410+
["", "Cannot be empty"], # empty string
411+
[
412+
"https://docs.notifications.service.gov.uk/python.html",
413+
"Must be a valid https URL, pointing to a section within the GOV.UK Notify API docs.",
414+
], # no section
415+
[
416+
"https://docs.payments.service.gov.uk/making_payments/#creating-a-payment",
417+
"Must be a valid https URL, pointing to a section within the GOV.UK Notify API docs.",
418+
], # URL is notfor Notify's docs
419+
[
420+
"http://docs.notifications.service.gov.uk/python.html#send-a-file-by-email",
421+
"Must be a valid https URL",
422+
], # http instead of https
423+
],
424+
)
425+
def test_POST_guidance_api_documentation_section_with_incorrect_url(client_request, url, expected_error_message):
426+
page = client_request.post(
427+
"main.guidance_api_documentation_section",
428+
_data={"url": url},
429+
_expected_status=200,
430+
)
431+
432+
assert expected_error_message in page.select_one(".govuk-error-message").text

0 commit comments

Comments
 (0)