Skip to content

Commit

Permalink
Merge pull request #2427 from auslin-aot/bugfix/fwf-4077-allow-hyphen…
Browse files Browse the repository at this point in the history
…s-form-title-path-name

FWF-4077: [Bugfix] Allow hyphen in form title, path & name
  • Loading branch information
arun-s-aot authored Dec 12, 2024
2 parents d7cceac + 1272765 commit f65fcd2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
14 changes: 9 additions & 5 deletions forms-flow-api/src/formsflow_api/services/form_process_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,15 +739,18 @@ def is_valid_field(cls, field: str, pattern: str) -> bool:
@classmethod
def validate_title_name_path(cls, title: str, path: str, name: str):
"""Validates the title, path, and name fields."""
title_pattern = r"(?=.*[A-Za-z])[A-Za-z0-9 ]+"
path_name = r"(?=.*[A-Za-z])[A-Za-z0-9]+"
title_pattern = r"(?=.*[A-Za-z])^[A-Za-z0-9 ]+(-{1,}[A-Za-z0-9 ]+)*$"
path_name = r"(?=.*[A-Za-z])^[A-Za-z0-9]+(-{1,}[A-Za-z0-9]+)*$"

invalid_fields = []

error_messages = {
"title": "Title: Only contain alphanumeric characters and spaces, and must include at least one letter.",
"path": "Path: Only contain alphanumeric characters, no spaces, and must include at least one letter.",
"name": "Name: Only contain alphanumeric characters, no spaces, and must include at least one letter.",
"title": "Title: Only contain alphanumeric characters, hyphens(not at the start or end), spaces,"
"and must include at least one letter.",
"path": "Path: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,"
"and must include at least one letter.",
"name": "Name: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,"
"and must include at least one letter.",
}

# Validate title
Expand Down Expand Up @@ -794,6 +797,7 @@ def validate_form_name_path_title(request, **kwargs):
name = request.args.get("name")
path = request.args.get("path")
form_id = request.args.get("id")
current_app.logger.info(f"Title:{title}, Name:{name}, Path:{path}")

# Check if at least one query parameter is provided
if not (title or name or path):
Expand Down
24 changes: 12 additions & 12 deletions forms-flow-api/tests/unit/api/test_form_process_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,22 +573,22 @@ def test_form_name_invalid_form_title(app, client, session, jwt, mock_redis_clie
assert response.json is not None
assert (
response.json["message"]
== "Title: Only contain alphanumeric characters and spaces, and must include at least one letter."
== "Title: Only contain alphanumeric characters, hyphens(not at the start or end), spaces,and must include at least one letter."
)
# With special characters
response = client.get("/form/validate?title=$$", headers=headers)
assert response.status_code == 400
assert response.json is not None
assert (
response.json["message"]
== "Title: Only contain alphanumeric characters and spaces, and must include at least one letter."
== "Title: Only contain alphanumeric characters, hyphens(not at the start or end), spaces,and must include at least one letter."
)
response = client.get("/form/validate?title=1234$@@#test", headers=headers)
assert response.status_code == 400
assert response.json is not None
assert (
response.json["message"]
== "Title: Only contain alphanumeric characters and spaces, and must include at least one letter."
== "Title: Only contain alphanumeric characters, hyphens(not at the start or end), spaces,and must include at least one letter."
)


Expand All @@ -602,23 +602,23 @@ def test_form_name_invalid_form_name(app, client, session, jwt, mock_redis_clien
assert response.json is not None
assert (
response.json["message"]
== "Name: Only contain alphanumeric characters, no spaces, and must include at least one letter."
== "Name: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."
)
# With special characters
response = client.get("/form/validate?name=1234", headers=headers)
assert response.status_code == 400
assert response.json is not None
assert (
response.json["message"]
== "Name: Only contain alphanumeric characters, no spaces, and must include at least one letter."
== "Name: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."
)
# With spaces
response = client.get("/form/validate?name=test form", headers=headers)
assert response.status_code == 400
assert response.json is not None
assert (
response.json["message"]
== "Name: Only contain alphanumeric characters, no spaces, and must include at least one letter."
== "Name: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."
)


Expand All @@ -632,23 +632,23 @@ def test_form_name_invalid_form_path(app, client, session, jwt, mock_redis_clien
assert response.json is not None
assert (
response.json["message"]
== "Path: Only contain alphanumeric characters, no spaces, and must include at least one letter."
== "Path: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."
)
# With special characters
response = client.get("/form/validate?path=1234", headers=headers)
assert response.status_code == 400
assert response.json is not None
assert (
response.json["message"]
== "Path: Only contain alphanumeric characters, no spaces, and must include at least one letter."
== "Path: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."
)
# With spaces
response = client.get("/form/validate?path=test form", headers=headers)
assert response.status_code == 400
assert response.json is not None
assert (
response.json["message"]
== "Path: Only contain alphanumeric characters, no spaces, and must include at least one letter."
== "Path: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."
)


Expand All @@ -667,7 +667,7 @@ def test_form_name_invalid_form_name_title_path(
assert response.json is not None
assert (
response.json["message"]
== """Title: Only contain alphanumeric characters and spaces, and must include at least one letter.,\n Path: Only contain alphanumeric characters, no spaces, and must include at least one letter."""
== """Title: Only contain alphanumeric characters, hyphens(not at the start or end), spaces,and must include at least one letter.,\n Path: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."""
)
# Invalid name, title
response = client.get(
Expand All @@ -677,7 +677,7 @@ def test_form_name_invalid_form_name_title_path(
assert response.json is not None
assert (
response.json["message"]
== """Title: Only contain alphanumeric characters and spaces, and must include at least one letter.,\n Name: Only contain alphanumeric characters, no spaces, and must include at least one letter."""
== """Title: Only contain alphanumeric characters, hyphens(not at the start or end), spaces,and must include at least one letter.,\n Name: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."""
)
# Invalid path, name
response = client.get(
Expand All @@ -687,7 +687,7 @@ def test_form_name_invalid_form_name_title_path(
assert response.json is not None
assert (
response.json["message"]
== """Path: Only contain alphanumeric characters, no spaces, and must include at least one letter.,\n Name: Only contain alphanumeric characters, no spaces, and must include at least one letter."""
== """Path: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter.,\n Name: Only contain alphanumeric characters, hyphens(not at the start or end), no spaces,and must include at least one letter."""
)


Expand Down

0 comments on commit f65fcd2

Please sign in to comment.