-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#293] add whitespace like validation for tokens
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.test import SimpleTestCase | ||
|
||
from openklant.components.token.validators import validate_non_empty_chars | ||
|
||
|
||
class WhiteSpaceValidatorTestCase(SimpleTestCase): | ||
def test_characters_only(self): | ||
self.assertIsNone(validate_non_empty_chars("test123")) | ||
|
||
def test_trailing_whitespace(self): | ||
self.assertIsNone(validate_non_empty_chars("test123 ")) | ||
|
||
def test_leading_whitespace(self): | ||
self.assertIsNone(validate_non_empty_chars(" test123")) | ||
|
||
def test_whitespace_in_between(self): | ||
self.assertIsNone(validate_non_empty_chars("test 123")) | ||
|
||
def test_whitespace_only(self): | ||
with self.assertRaises(ValidationError): | ||
validate_non_empty_chars(" ") | ||
|
||
def test_trailing_tab_character(self): | ||
self.assertIsNone(validate_non_empty_chars("test123\t")) | ||
|
||
def test_leading_tab_character(self): | ||
self.assertIsNone(validate_non_empty_chars("\ttest123")) | ||
|
||
def test_tab_character_in_between(self): | ||
self.assertIsNone(validate_non_empty_chars("test\t123")) | ||
|
||
def test_tab_characters_only(self): | ||
with self.assertRaises(ValidationError): | ||
validate_non_empty_chars("\t\t") | ||
|
||
def test_blank_value(self): | ||
with self.assertRaises(ValidationError): | ||
validate_non_empty_chars("") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import re | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import gettext as _ | ||
|
||
|
||
# includes tabs, carriage returns, newlines, form-feeds and vertical whitespace characters | ||
ALL_WHITESPACE_PATTERN = re.compile(r"^\s*$") | ||
|
||
|
||
def validate_non_empty_chars(value: str) -> None: | ||
if not value: | ||
raise ValidationError( | ||
code="invalid", | ||
message=_("Blank values are not allowed") | ||
) | ||
|
||
if ALL_WHITESPACE_PATTERN.match(value): | ||
raise ValidationError( | ||
code="all-whitespace", | ||
message=_( | ||
"Tokens cannot consistent exclusively out of whitespace-like characters" | ||
) | ||
) |