Skip to content

Commit

Permalink
Merge branch 'issue/django-setup-configuration-tokens' into issue/dja…
Browse files Browse the repository at this point in the history
…ngo-setup-configuration-mozilla-django-oidc-db
  • Loading branch information
danielmursa-dev committed Dec 13, 2024
2 parents d03910a + f3f44d2 commit 2eb8a0c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
10 changes: 6 additions & 4 deletions INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,14 @@ There are no specific commands for the project. See
.. _Django framework commands: https://docs.djangoproject.com/en/dev/ref/django-admin/#available-commands

Configuration (CLI)
========
===================

After deploying Objecttypes API, they need to be configured to be fully functional. The command line tool setup_configuration assist with this configuration:
After deploying Objecttypes API, they need to be configured to be fully functional.
The command line tool ``setup_configuration`` assist with this configuration.

You can get the full command documentation with:

.. code-block:: bash
See `Django Setup Configuration`_ for all documentation, or type
``python src/manage.py setup_configuration --help``.

python src/manage.py setup_configuration --help
.. _Django Setup Configuration: https://github.com/maykinmedia/django-setup-configuration
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Migration(migrations.Migration):
field=models.CharField(
max_length=40,
unique=True,
validators=[objecttypes.token.validators.validate_whitespace],
validators=[objecttypes.token.validators.validate_no_whitespace],
verbose_name="token",
),
),
Expand Down
4 changes: 2 additions & 2 deletions src/objecttypes/token/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from objecttypes.token.validators import validate_whitespace
from objecttypes.token.validators import validate_no_whitespace


class TokenAuth(models.Model):
Expand All @@ -16,7 +16,7 @@ class TokenAuth(models.Model):
_("token"),
max_length=40,
unique=True,
validators=[validate_whitespace],
validators=[validate_no_whitespace],
)

contact_person = models.CharField(
Expand Down
30 changes: 28 additions & 2 deletions src/objecttypes/token/tests/test_authenticaton.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,33 @@ def test_valid_token(self):
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_invalid_token(self):
def test_valid_token_with_no_spaces(self):
token_auth = TokenAuth.objects.create(
contact_person="contact_person",
email="[email protected]",
identifier="token-1",
token="1234-Token-5678",
)
response = self.client.get(
reverse("v2:objecttype-list"),
HTTP_AUTHORIZATION=f"Token {token_auth.token}",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_invalid_token_with_spaces(self):
token_auth = TokenAuth.objects.create(
contact_person="contact_person",
email="[email protected]",
identifier="token-1",
token="1234 Token 5678",
)
response = self.client.get(
reverse("v2:objecttype-list"),
HTTP_AUTHORIZATION=f"Token {token_auth.token}",
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_invalid_token_existing(self):
response = self.client.get(
reverse("v2:objecttype-list"),
HTTP_AUTHORIZATION="Token 1234-Token-5678",
Expand All @@ -28,7 +54,7 @@ def test_invalid_token(self):

def test_empty_token(self):
response = self.client.get(
reverse("v2:objecttype-list"), HTTP_AUTHORIZATION="Token"
reverse("v2:objecttype-list"), HTTP_AUTHORIZATION="Token "
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

Expand Down
24 changes: 10 additions & 14 deletions src/objecttypes/token/tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
from django.core.exceptions import ValidationError
from django.test import SimpleTestCase

from objecttypes.token.validators import validate_whitespace
from objecttypes.token.validators import validate_no_whitespace


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

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

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

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

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

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

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

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

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

def test_blank_value(self):
with self.assertRaises(ValidationError):
validate_whitespace("")
validate_no_whitespace("\t\t")
5 changes: 1 addition & 4 deletions src/objecttypes/token/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
WHITESPACE_PATTERN = re.compile(r".*\s.*")


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

def validate_no_whitespace(value: str) -> None:
if WHITESPACE_PATTERN.match(value):
raise ValidationError(
code="all-whitespace",
Expand Down

0 comments on commit 2eb8a0c

Please sign in to comment.