Skip to content

Commit f9b0d8a

Browse files
committed
WIP Validate URL with Regex
1 parent 5f07d26 commit f9b0d8a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

app/main/forms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,10 @@ class UrlForm(StripWhitespaceForm):
22852285
"URL",
22862286
validators=[
22872287
DataRequired(message="Cannot be empty"),
2288-
Regexp(regex="^https.*", message="Must be a valid https URL"),
2288+
Regexp(
2289+
regex=r"^https:\/\/docs\.notifications\.service\.gov\.uk\/(python|ruby|node|net|java|php|rest-api)\.html#.+",
2290+
message="Must be a valid https URL, pointing to a section within the GOV.UK Notify API docs.",
2291+
),
22892292
],
22902293
)
22912294

tests/app/main/views/test_index.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,24 @@ 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",
409+
[
410+
"", # empty string
411+
"https://docs.notifications.service.gov.uk/python.html", # no section
412+
"https://docs.payments.service.gov.uk/making_payments/#creating-a-payment", # URL is notfor Notify's docs
413+
],
414+
)
415+
def test_POST_guidance_api_documentation_section_with_incorrect_url(client_request, url):
416+
page = client_request.post(
417+
"main.guidance_api_documentation_section",
418+
_data={"url": "https://docs.notifications.service.gov.uk/python.html#send-a-file-by-email"},
419+
_expected_status=200,
420+
)
421+
422+
assert (
423+
"Must be a valid https URL, pointing to a section within the GOV.UK Notify API docs."
424+
in page.select_one(".govuk-error-message").text
425+
)

0 commit comments

Comments
 (0)