Skip to content

Commit

Permalink
WIP Validate URL with Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
CrystalPea committed Oct 1, 2024
1 parent 5f07d26 commit f9b0d8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,10 @@ class UrlForm(StripWhitespaceForm):
"URL",
validators=[
DataRequired(message="Cannot be empty"),
Regexp(regex="^https.*", message="Must be a valid https URL"),
Regexp(
regex=r"^https:\/\/docs\.notifications\.service\.gov\.uk\/(python|ruby|node|net|java|php|rest-api)\.html#.+",
message="Must be a valid https URL, pointing to a section within the GOV.UK Notify API docs.",
),
],
)

Expand Down
21 changes: 21 additions & 0 deletions tests/app/main/views/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,24 @@ def test_POST_guidance_api_documentation_section(client_request):
section_tag="send-a-file-by-email",
),
)


@pytest.mark.parametrize(
"url",
[
"", # empty string
"https://docs.notifications.service.gov.uk/python.html", # no section
"https://docs.payments.service.gov.uk/making_payments/#creating-a-payment", # URL is notfor Notify's docs
],
)
def test_POST_guidance_api_documentation_section_with_incorrect_url(client_request, url):
page = client_request.post(
"main.guidance_api_documentation_section",
_data={"url": "https://docs.notifications.service.gov.uk/python.html#send-a-file-by-email"},
_expected_status=200,
)

assert (
"Must be a valid https URL, pointing to a section within the GOV.UK Notify API docs."
in page.select_one(".govuk-error-message").text
)

0 comments on commit f9b0d8a

Please sign in to comment.