Skip to content

Commit

Permalink
[#293] add whitespace like validation for tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
SonnyBA committed Dec 3, 2024
1 parent c64fae1 commit 0d2bf64
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/openklant/components/token/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
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


class TokenAuth(models.Model):
identifier = models.SlugField(
unique=True, help_text=_("A human-friendly label to refer to this token")
)

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

contact_person = models.CharField(
_("contact person"),
Expand Down
39 changes: 39 additions & 0 deletions src/openklant/components/token/tests/test_validators.py
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("")
24 changes: 24 additions & 0 deletions src/openklant/components/token/validators.py
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"
)
)

0 comments on commit 0d2bf64

Please sign in to comment.