This commit is contained in:
@@ -0,0 +1,274 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from dav_base.tests.generic import ValidatorTestMixin
|
||||
|
||||
from ..validators import AlphanumericValidator, LowerAlphanumericValidator, IdStringValidator
|
||||
|
||||
|
||||
class AlphanumericValidatorTestCase(ValidatorTestMixin, SimpleTestCase):
|
||||
validator = AlphanumericValidator
|
||||
|
||||
def test_valid_data(self):
|
||||
data = (
|
||||
'', # Empty string is valid
|
||||
'A', # Single uppercase letter
|
||||
'a', # Single lowercase letter
|
||||
'0', # Single digit
|
||||
'abc', # Multiple lowercase letters
|
||||
'ABCD', # Multiple uppercase letters
|
||||
'AbCde', # Mixed case
|
||||
'123456', # Multiple digits
|
||||
'bcd2345', # Letters and digits
|
||||
'CDEF3456', # Uppercase and digits
|
||||
'AbcD123', # Mixed case and digits
|
||||
'a1B2c3', # Alternating case and digits
|
||||
'012345678909876543210', # Long digit sequence
|
||||
'abcdefghijklmnopqrstuvwxyz', # All lowercase letters
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', # All uppercase letters
|
||||
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789', # All valid chars
|
||||
'EventCode2024', # Real world example
|
||||
'USER123EVENT', # Real world example
|
||||
'A1', # Single letter and digit
|
||||
'1A', # Single digit and letter
|
||||
)
|
||||
self.assertValid(self.validator, data)
|
||||
|
||||
def test_invalid_data(self):
|
||||
"""Test invalid strings with special characters, spaces, umlauts"""
|
||||
data = (
|
||||
' ', # Space
|
||||
'abc ', # Trailing space
|
||||
' abc', # Leading space
|
||||
'a b c', # Space in the middle
|
||||
'-', # Hyphen
|
||||
'abc-def', # Letters with hyphen
|
||||
'_', # Underscore
|
||||
'abc_def', # Letters with underscore
|
||||
'.', # Dot
|
||||
'abc.def', # Letters with dot
|
||||
',', # Comma
|
||||
'abc,def', # Letters with comma
|
||||
'!', # Exclamation mark
|
||||
'abc!def', # Letters with exclamation
|
||||
'@', # At sign
|
||||
'abc@def', # Letters with at sign
|
||||
'#', # Hash
|
||||
'abc#def', # Letters with hash
|
||||
'$', # Dollar sign
|
||||
'abc$def', # Letters with dollar
|
||||
'%', # Percent
|
||||
'abc%def', # Letters with percent
|
||||
'&', # Ampersand
|
||||
'abc&def', # Letters with ampersand
|
||||
'*', # Asterisk
|
||||
'abc*def', # Letters with asterisk
|
||||
'+', # Plus
|
||||
'abc+def', # Letters with plus
|
||||
'=', # Equals
|
||||
'abc=def', # Letters with equals
|
||||
'/', # Slash
|
||||
'abc/def', # Letters with slash
|
||||
'\\', # Backslash
|
||||
'abc\\def', # Letters with backslash
|
||||
'?', # Question mark
|
||||
'abc?def', # Letters with question mark
|
||||
':', # Colon
|
||||
'abc:def', # Letters with colon
|
||||
';', # Semicolon
|
||||
'abc;def', # Letters with semicolon
|
||||
'"', # Double quote
|
||||
'abc"def', # Letters with double quote
|
||||
"'", # Single quote
|
||||
"abc'def", # Letters with single quote
|
||||
'<', # Less than
|
||||
'abc<def', # Letters with less than
|
||||
'>', # Greater than
|
||||
'abc>def', # Letters with greater than
|
||||
'(', # Opening parenthesis
|
||||
'abc(def)', # Letters with parentheses
|
||||
'[', # Opening bracket
|
||||
'abc[def]', # Letters with brackets
|
||||
'{', # Opening brace
|
||||
'abc{def}', # Letters with braces
|
||||
'ä', # Umlaut
|
||||
'äbc', # Letters with umlaut
|
||||
'ö', # Umlaut o
|
||||
'ö', # Umlaut O
|
||||
'ü', # Umlaut u
|
||||
'ü', # Umlaut U
|
||||
'ß', # German sharp s
|
||||
'Straße', # German word with umlaut
|
||||
'café', # Accented character
|
||||
'naïve', # Accented character
|
||||
'中文', # Chinese characters
|
||||
'русский', # Cyrillic characters
|
||||
'العربية', # Arabic characters
|
||||
)
|
||||
self.assertInvalid(self.validator, data)
|
||||
|
||||
|
||||
class LowerAlphanumericValidatorTestCase(ValidatorTestMixin, SimpleTestCase):
|
||||
validator = LowerAlphanumericValidator
|
||||
|
||||
def test_valid_data(self):
|
||||
data = (
|
||||
'', # Empty string is valid
|
||||
'a', # Single lowercase letter
|
||||
'0', # Single digit
|
||||
'abc', # Multiple lowercase letters
|
||||
'123456', # Multiple digits
|
||||
'bcd2345', # Letters and digits
|
||||
'a1b2c3', # Alternating lowercase and digits
|
||||
'012345678909876543210', # Long digit sequence
|
||||
'abcdefghijklmnopqrstuvwxyz', # All lowercase letters
|
||||
'eventcode2024', # Real world example (all lowercase)
|
||||
'user123event', # Real world example (all lowercase)
|
||||
'a1', # Single letter and digit
|
||||
'1a', # Single digit and letter
|
||||
)
|
||||
self.assertValid(self.validator, data)
|
||||
|
||||
def test_invalid_data(self):
|
||||
"""Test invalid strings with uppercase letters and other characters"""
|
||||
data = (
|
||||
'A', # Single uppercase letter
|
||||
'ABC', # Multiple uppercase letters
|
||||
'AbC', # Mixed case
|
||||
'ABC123', # Uppercase and digits
|
||||
'AbC123', # Mixed case (with uppercase)
|
||||
' ', # Space
|
||||
'abc ', # Trailing space
|
||||
' abc', # Leading space
|
||||
'a b c', # Space in the middle
|
||||
'-', # Hyphen
|
||||
'abc-def', # Letters with hyphen
|
||||
'_', # Underscore
|
||||
'abc_def', # Letters with underscore
|
||||
'.', # Dot
|
||||
'abc.def', # Letters with dot
|
||||
',', # Comma
|
||||
'abc,def', # Letters with comma
|
||||
'!', # Exclamation mark
|
||||
'abc!def', # Letters with exclamation
|
||||
'@', # At sign
|
||||
'abc@def', # Letters with at sign
|
||||
'#', # Hash
|
||||
'abc#def', # Letters with hash
|
||||
'ä', # Umlaut
|
||||
'äbc', # Letters with umlaut
|
||||
'café', # Accented character
|
||||
'naïve', # Accented character
|
||||
)
|
||||
self.assertInvalid(self.validator, data)
|
||||
|
||||
|
||||
class IdStringValidatorTestCase(ValidatorTestMixin, SimpleTestCase):
|
||||
validator = IdStringValidator
|
||||
|
||||
def test_valid_data(self):
|
||||
data = (
|
||||
'', # Empty string is valid
|
||||
'a', # Single lowercase letter
|
||||
'0', # Single digit
|
||||
'abc', # Multiple lowercase letters
|
||||
'123', # Multiple digits
|
||||
'_', # Single underscore
|
||||
'-', # Single hyphen
|
||||
'.', # Single dot
|
||||
'abc123', # Lowercase letters and digits
|
||||
'a_b', # Underscore separator
|
||||
'a-b', # Hyphen separator
|
||||
'a.b', # Dot separator
|
||||
'user_name', # Underscore in middle
|
||||
'user-name', # Hyphen in middle
|
||||
'user.name', # Dot in middle
|
||||
'user_name_123', # Multiple underscores
|
||||
'user-name-123', # Multiple hyphens
|
||||
'user.name.123', # Multiple dots
|
||||
'mixed_name-with.numbers123', # Mixed separators
|
||||
'example.com', # Domain-like format
|
||||
'v1.0.0', # Version string
|
||||
'2024-01-15', # Date-like format
|
||||
'snake_case', # Snake case convention
|
||||
'kebab-case', # Kebab case convention
|
||||
'dot.case', # Dot case convention
|
||||
'a1b2c3d4e5f6g7h8i9j0', # Alternating
|
||||
'test_123-abc.xyz', # Complex mixed format
|
||||
'_leading_underscore', # Leading underscore
|
||||
'-leading-hyphen', # Leading hyphen
|
||||
'.leading-dot', # Leading dot
|
||||
'trailing_underscore_', # Trailing underscore
|
||||
'trailing-hyphen-', # Trailing hyphen
|
||||
'trailing.dot.', # Trailing dot
|
||||
'___', # Multiple underscores only
|
||||
'---', # Multiple hyphens only
|
||||
'...', # Multiple dots only
|
||||
'a_b-c.d_e-f.g', # All separators mixed
|
||||
)
|
||||
self.assertValid(self.validator, data)
|
||||
|
||||
def test_invalid_data(self):
|
||||
data = (
|
||||
'A', # Uppercase letter
|
||||
'ABC', # Multiple uppercase letters
|
||||
'AbC', # Mixed case
|
||||
'ABC123', # Uppercase and digits
|
||||
'aBC123', # Mixed case (any uppercase)
|
||||
' ', # Space
|
||||
'abc ', # Trailing space
|
||||
' abc', # Leading space
|
||||
'a b c', # Space in the middle
|
||||
'a b-c', # Space mixed with valid chars
|
||||
'@', # At sign
|
||||
'abc@def', # At sign in the middle
|
||||
'#', # Hash
|
||||
'abc#def', # Hash in the middle
|
||||
'$', # Dollar sign
|
||||
'abc$def', # Dollar in the middle
|
||||
'%', # Percent
|
||||
'abc%def', # Percent in the middle
|
||||
'&', # Ampersand
|
||||
'abc&def', # Ampersand in the middle
|
||||
'*', # Asterisk
|
||||
'abc*def', # Asterisk in the middle
|
||||
'+', # Plus
|
||||
'abc+def', # Plus in the middle
|
||||
'=', # Equals
|
||||
'abc=def', # Equals in the middle
|
||||
'/', # Slash
|
||||
'abc/def', # Slash in the middle
|
||||
'\\', # Backslash
|
||||
'abc\\def', # Backslash in the middle
|
||||
'?', # Question mark
|
||||
'abc?def', # Question mark in the middle
|
||||
':', # Colon
|
||||
'abc:def', # Colon in the middle
|
||||
';', # Semicolon
|
||||
'abc;def', # Semicolon in the middle
|
||||
'"', # Double quote
|
||||
'abc"def', # Double quote in the middle
|
||||
"'", # Single quote
|
||||
"abc'def", # Single quote in the middle
|
||||
'<', # Less than
|
||||
'abc<def', # Less than in the middle
|
||||
'>', # Greater than
|
||||
'abc>def', # Greater than in the middle
|
||||
'(', # Opening parenthesis
|
||||
'abc(def)', # Parentheses in the middle
|
||||
'[', # Opening bracket
|
||||
'abc[def]', # Brackets in the middle
|
||||
'{', # Opening brace
|
||||
'abc{def}', # Braces in the middle
|
||||
'!', # Exclamation mark
|
||||
'abc!def', # Exclamation mark in the middle
|
||||
',', # Comma
|
||||
'abc,def', # Comma in the middle
|
||||
'ä', # Umlaut
|
||||
'äbc', # Letters with umlaut
|
||||
'café', # Accented character
|
||||
'naïve', # Accented character
|
||||
'中文', # Chinese characters
|
||||
'русский', # Cyrillic characters
|
||||
)
|
||||
self.assertInvalid(self.validator, data)
|
||||
Reference in New Issue
Block a user