Skip to content

Commit

Permalink
SS-1206 added validation and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anondo1969 committed Nov 26, 2024
1 parent ff925cd commit b51aad2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
15 changes: 7 additions & 8 deletions apps/models/app_types/shiny.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ def validate_shiny_site_dir(candidate):
It is up to the caller to handle the raised exception if desired.
"""
error_message = (
"Your shiny site directory subpath is not valid, please correct it. "
"It must be 1-53 characters long."
" It can contain only Unicode letters, digits, hyphens"
" ( - ), forward slashes ( / ), and underscores ( _ )."
" It cannot start or end with a hyphen ( - ) and "
"cannot start with a forward slash ( / )."
" It cannot contain consecutive forward slashes ( // )."
"Please provide a valid path. "
"It can be empty. "
"Otherwise, it must be 63 characters or less. "
" It must begin and end with an alphanumeric character (a-z, or 0-9, or A-Z)."
" It could contain dashes ( - ), underscores ( _ ), dots ( . ), "
"and alphanumerics."
)

pattern = r"^(?!-)(?!/)(?!.*//)[\p{Letter}\p{Mark}0-9-/_]{1,53}(?<!-)$|^$"
pattern = r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9._-]{0,61}[a-zA-Z0-9])?)?$"

if not re.match(pattern, candidate):
raise ValidationError(error_message)
Expand Down
69 changes: 69 additions & 0 deletions apps/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from apps.forms import CustomAppForm
from apps.models import Apps, AppStatus, Subdomain, VolumeInstance
from apps.models.app_types.custom.custom import validate_default_url_subpath
from apps.models.app_types.shiny import validate_shiny_site_dir
from projects.models import Flavor, Project

User = get_user_model()
Expand Down Expand Up @@ -240,3 +241,71 @@ def test_invalid_default_url_subpath(invalid_default_url_subpath):
valid_check = False

assert not valid_check


invalid_shiny_site_dir_list = [
"-invalidStart", # Starts with a non-alphanumeric character
"invalidEnd-", # Ends with a non-alphanumeric character
".dotStart", # Starts with a dot
"dotEnd.", # Ends with a dot
"_underscoreStart", # Starts with an underscore
"underscoreEnd_", # Ends with an underscore
"label with spaces", # Contains spaces
"label@value", # Contains an invalid character (@)
"too_long_label_with_more_than_sixty_three_characters__1234567890", # Exceeds 63 characters
"just-dashes-", # Ends with a dash
"123#", # Contains an invalid character (#)
".....", # Only contains dots
"-a", # Starts with a dash
"_a_", # Starts and ends with underscores
" ", # Contains only whitespace
]

valid_shiny_site_dir_list = [
"", # Empty string is allowed
"a", # Single alphanumeric character
"validLabel", # Alphanumeric characters only
"label-123", # Contains a dash
"label_with_underscores", # Contains underscores
"label.with.dots", # Contains dots
"abc-def_ghi.jkl", # Contains all allowed special characters
"label1", # Ends with a number
"1stLabel", # Starts with a number
"example-label", # Simple example with a dash
"nested.label.value", # Dots between words
"underscore_ending_label", # Underscore in the middle
"valid-value-0123", # Contains numbers and special characters
"long-valid-label-abcdefg-hijklmn-opqrstuv-wxyz", # Long but within 63 characters
"labelvalue123456789", # Combination of letters and numbers
"consecutive-dashes--allowed", # Contains consecutive dashes
"consecutive_underscores__allowed", # Contains consecutive underscores
"dots..in..between", # Contains consecutive dots
"mixed__--..label", # Contains a mix of consecutive allowed characters
"label---with---many---dashes", # Multiple consecutive dashes in different parts
"label..with..dots", # Multiple consecutive dots in between
"valid_--..mix_12", # Combination of numbers, letters, and allowed characters
"simple.label-value_1-2-3", # Mixed with numbers, dots, underscores, and dashes
"complex__label..value--mixed", # A complex mix with all allowed characters in a consecutive manner
]


@pytest.mark.parametrize("valid_shiny_site_dir", valid_shiny_site_dir_list)
def test_valid_shiny_site_dir(valid_shiny_site_dir):
valid_check = True
try:
validate_shiny_site_dir(valid_shiny_site_dir)
except ValidationError:
valid_check = False

assert valid_check


@pytest.mark.parametrize("invalid_shiny_site_dir", invalid_shiny_site_dir_list)
def test_invalid_shiny_site_dir(invalid_shiny_site_dir):
valid_check = True
try:
validate_shiny_site_dir(invalid_shiny_site_dir)
except ValidationError:
valid_check = False

assert not valid_check

0 comments on commit b51aad2

Please sign in to comment.