Skip to content

Commit

Permalink
Update auth_token and last_response columns' max lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
MrThearMan committed Nov 25, 2023
1 parent bf71637 commit 2b73036
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 37 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export DJANGO_SETTINGS_MODULE = tests.project.settings
.PHONY: tox
.PHONY: hook
.PHONY: lint
.PHONY: migrate
.PHONY: migrations
.PHONY: mypy
.PHONY: Makefile

Expand All @@ -30,6 +32,8 @@ define helptext
tox <args> Run all tests with tox.
hook Install pre-commit hook.
lint Run pre-commit hooks on all files.
migrate Run migrations.
migrations Create migrations.
mypy Run mypy on all files.

Use quotes (" ") if command contains flags (-h / --help)
Expand Down Expand Up @@ -61,5 +65,11 @@ hook:
lint:
@poetry run pre-commit run --all-files

migrate:
@poetry run python manage.py migrate

migrations:
@poetry run python manage.py makemigrations

mypy:
@poetry run mypy django_signal_webhooks/
4 changes: 2 additions & 2 deletions signal_webhooks/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Migration(migrations.Migration):
blank=True,
default="",
help_text="Authentication token to use in an Authorization header.",
max_length=10000,
max_length=8000,
validators=[signal_webhooks.utils.decode_cipher_key],
verbose_name="authentication token",
),
Expand Down Expand Up @@ -127,7 +127,7 @@ class Migration(migrations.Migration):
blank=True,
default="",
help_text="Latest response to this webhook.",
max_length=10000,
max_length=8000,
verbose_name="last response",
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.6 on 2023-11-25 08:58

from django.db import migrations, models

import signal_webhooks.fields
import signal_webhooks.utils


class Migration(migrations.Migration):
dependencies = [
("signal_webhooks", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="webhook",
name="auth_token",
field=signal_webhooks.fields.TokenField(
blank=True,
default="",
help_text="Authentication token to use in an Authorization header.",
max_length=8000,
validators=[signal_webhooks.utils.decode_cipher_key],
verbose_name="authentication token",
),
),
migrations.AlterField(
model_name="webhook",
name="last_response",
field=models.CharField(
blank=True,
default="",
help_text="Latest response to this webhook.",
max_length=8000,
verbose_name="last response",
),
),
]
6 changes: 3 additions & 3 deletions signal_webhooks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .fields import TokenField
from .settings import webhook_settings
from .typing import METHOD_SIGNALS, SignalChoices
from .typing import MAX_COL_SIZE, METHOD_SIGNALS, SignalChoices
from .utils import decode_cipher_key, is_dict, model_from_reference, reference_for_model

if TYPE_CHECKING:
Expand Down Expand Up @@ -73,7 +73,7 @@ class WebhookBase(models.Model):
auth_token: str = TokenField(
default="",
blank=True,
max_length=10_000,
max_length=MAX_COL_SIZE,
verbose_name="authentication token",
help_text="Authentication token to use in an Authorization header.",
validators=[decode_cipher_key],
Expand Down Expand Up @@ -101,7 +101,7 @@ class WebhookBase(models.Model):
last_response: str = models.CharField(
default="",
blank=True,
max_length=10_000,
max_length=MAX_COL_SIZE,
verbose_name="last response",
help_text="Latest response to this webhook.",
)
Expand Down
4 changes: 4 additions & 0 deletions signal_webhooks/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,7 @@ def delete_choises(cls) -> set["SignalChoices"]:
"UPDATE": SignalChoices.update_choises(),
"DELETE": SignalChoices.delete_choises(),
}

# Define a maximum column size based on MS SQL Server limitation:
# https://docs.microsoft.com/en-us/sql/sql-server/maximum-capacity-specifications-for-sql-server
MAX_COL_SIZE: int = 8_000
4 changes: 2 additions & 2 deletions signal_webhooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from .serializers import webhook_serializer
from .settings import webhook_settings
from .typing import ClientKwargs
from .typing import MAX_COL_SIZE, ClientKwargs

if TYPE_CHECKING:
from django.db.models import Model
Expand Down Expand Up @@ -56,7 +56,7 @@ def is_dict(value: str) -> None:
raise ValidationError(msg)


def truncate(string: str, limit: int = 10_000) -> str:
def truncate(string: str, limit: int = MAX_COL_SIZE) -> str:
if len(string) > limit:
string = string[: limit - 3] + "..."
return string
Expand Down
28 changes: 6 additions & 22 deletions tests/my_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by Django 4.1 on 2022-08-21 20:06
# Generated by Django 4.2.6 on 2023-11-25 09:03

from django.db import migrations, models

import signal_webhooks.fields
import signal_webhooks.utils

Expand All @@ -24,12 +23,7 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
(
"name",
models.CharField(
max_length=256,
),
),
("name", models.CharField(max_length=256)),
],
),
migrations.CreateModel(
Expand Down Expand Up @@ -104,7 +98,7 @@ class Migration(migrations.Migration):
blank=True,
default="",
help_text="Authentication token to use in an Authorization header.",
max_length=10000,
max_length=8000,
validators=[signal_webhooks.utils.decode_cipher_key],
verbose_name="authentication token",
),
Expand Down Expand Up @@ -147,7 +141,7 @@ class Migration(migrations.Migration):
blank=True,
default="",
help_text="Latest response to this webhook.",
max_length=10000,
max_length=8000,
verbose_name="last response",
),
),
Expand All @@ -169,12 +163,7 @@ class Migration(migrations.Migration):
verbose_name="last failure",
),
),
(
"code",
models.CharField(
max_length=256,
),
),
("code", models.CharField(max_length=256)),
],
options={
"abstract": False,
Expand All @@ -192,12 +181,7 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
(
"name",
models.CharField(
max_length=256,
),
),
("name", models.CharField(max_length=256)),
],
),
migrations.AddConstraint(
Expand Down
6 changes: 2 additions & 4 deletions tests/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "django" / "testdb",
"NAME": BASE_DIR / "project" / "testdb",
"OPTIONS": {
"timeout": 1,
},
Expand All @@ -70,9 +70,7 @@


AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
},
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
Expand Down
Binary file modified tests/project/testdb
Binary file not shown.
9 changes: 5 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.core.exceptions import ValidationError
from freezegun import freeze_time

from signal_webhooks.typing import MAX_COL_SIZE
from signal_webhooks.utils import (
decode_cipher_key,
default_serializer,
Expand Down Expand Up @@ -137,11 +138,11 @@ def test_is_dict__fail():


def test_truncate():
len_max = "".join(random.choice(string.ascii_letters) for _ in range(10_000))
len_over = "".join(random.choice(string.ascii_letters) for _ in range(10_001))
len_max = "".join(random.choice(string.ascii_letters) for _ in range(MAX_COL_SIZE))
len_over = "".join(random.choice(string.ascii_letters) for _ in range(MAX_COL_SIZE + 1))

assert len(truncate(len_max)) == 10_000
assert len(truncate(len_over)) == 10_000
assert len(truncate(len_max)) == MAX_COL_SIZE
assert len(truncate(len_over)) == MAX_COL_SIZE


@freeze_time("2022-01-01T00:00:00")
Expand Down

0 comments on commit 2b73036

Please sign in to comment.