Skip to content

Commit

Permalink
[#293] don't allow any whitespace for tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
SonnyBA committed Dec 4, 2024
1 parent ea6802c commit bacd852
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Migration(migrations.Migration):
max_length=40,
unique=True,
validators=[
openklant.components.token.validators.validate_non_empty_chars
openklant.components.token.validators.validate_whitespace
],
verbose_name="token",
),
Expand Down
4 changes: 2 additions & 2 deletions src/openklant/components/token/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils.translation import gettext_lazy as _

from openklant.components.token.utils import get_token
from openklant.components.token.validators import validate_non_empty_chars
from openklant.components.token.validators import validate_whitespace


class TokenAuth(models.Model):
Expand All @@ -11,7 +11,7 @@ class TokenAuth(models.Model):
)

token = models.CharField(
_("token"), max_length=40, unique=True, validators=[validate_non_empty_chars]
_("token"), max_length=40, unique=True, validators=[validate_whitespace]
)

contact_person = models.CharField(
Expand Down
28 changes: 17 additions & 11 deletions src/openklant/components/token/tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
from django.core.exceptions import ValidationError
from django.test import SimpleTestCase

from openklant.components.token.validators import validate_non_empty_chars
from openklant.components.token.validators import validate_whitespace


class WhiteSpaceValidatorTestCase(SimpleTestCase):
def test_characters_only(self):
self.assertIsNone(validate_non_empty_chars("test123"))
self.assertIsNone(validate_whitespace("test123"))

def test_trailing_whitespace(self):
self.assertIsNone(validate_non_empty_chars("test123 "))
with self.assertRaises(ValidationError):
validate_whitespace("test123 ")

def test_leading_whitespace(self):
self.assertIsNone(validate_non_empty_chars(" test123"))
with self.assertRaises(ValidationError):
validate_whitespace(" test123")

def test_whitespace_in_between(self):
self.assertIsNone(validate_non_empty_chars("test 123"))
with self.assertRaises(ValidationError):
validate_whitespace("test 123")

def test_whitespace_only(self):
with self.assertRaises(ValidationError):
validate_non_empty_chars(" ")
validate_whitespace(" ")

def test_trailing_tab_character(self):
self.assertIsNone(validate_non_empty_chars("test123\t"))
with self.assertRaises(ValidationError):
validate_whitespace("test123\t")

def test_leading_tab_character(self):
self.assertIsNone(validate_non_empty_chars("\ttest123"))
with self.assertRaises(ValidationError):
validate_whitespace("\ttest123")

def test_tab_character_in_between(self):
self.assertIsNone(validate_non_empty_chars("test\t123"))
with self.assertRaises(ValidationError):
validate_whitespace("test\t123")

def test_tab_characters_only(self):
with self.assertRaises(ValidationError):
validate_non_empty_chars("\t\t")
validate_whitespace("\t\t")

def test_blank_value(self):
with self.assertRaises(ValidationError):
validate_non_empty_chars("")
validate_whitespace("")
10 changes: 4 additions & 6 deletions src/openklant/components/token/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
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*$")
WHITESPACE_PATTERN = re.compile(r".*\s.*")


def validate_non_empty_chars(value: str) -> None:
def validate_whitespace(value: str) -> None:
if not value:
raise ValidationError(code="invalid", message=_("Blank values are not allowed"))

if ALL_WHITESPACE_PATTERN.match(value):
if WHITESPACE_PATTERN.match(value):
raise ValidationError(
code="all-whitespace",
message=_(
"Tokens cannot consistent exclusively out of whitespace-like characters"
),
message=_("Tokens cannot contain whitespace-like characters"),
)

0 comments on commit bacd852

Please sign in to comment.