[FR]Unit test for Schema allowance (#3538)

* [FR]Unit test for Schema allowance
Fixes #3420

* move schema loding into a runtime obj
This commit is contained in:
Matthias Mair 2022-10-18 00:38:43 +02:00 committed by GitHub
parent c1064906d6
commit 9cfbe1061d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 4 deletions

View File

@ -4,7 +4,6 @@ import sys
from decimal import Decimal from decimal import Decimal
from django import forms from django import forms
from django.core import validators
from django.db import models as models from django.db import models as models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -15,7 +14,7 @@ from rest_framework.fields import URLField as RestURLField
import InvenTree.helpers import InvenTree.helpers
from .validators import allowable_url_schemes from .validators import AllowedURLValidator, allowable_url_schemes
class InvenTreeRestURLField(RestURLField): class InvenTreeRestURLField(RestURLField):
@ -34,7 +33,7 @@ class InvenTreeRestURLField(RestURLField):
class InvenTreeURLField(models.URLField): class InvenTreeURLField(models.URLField):
"""Custom URL field which has custom scheme validators.""" """Custom URL field which has custom scheme validators."""
default_validators = [validators.URLValidator(schemes=allowable_url_schemes())] default_validators = [AllowedURLValidator()]
def __init__(self, **kwargs): def __init__(self, **kwargs):
"""Initialization method for InvenTreeURLField""" """Initialization method for InvenTreeURLField"""

View File

@ -4,6 +4,7 @@ import re
from decimal import Decimal, InvalidOperation from decimal import Decimal, InvalidOperation
from django.conf import settings from django.conf import settings
from django.core import validators
from django.core.exceptions import FieldDoesNotExist, ValidationError from django.core.exceptions import FieldDoesNotExist, ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -37,6 +38,14 @@ def allowable_url_schemes():
return schemes return schemes
class AllowedURLValidator(validators.URLValidator):
"""Custom URL validator to allow for custom schemes."""
def __call__(self, value):
"""Validate the URL."""
self.schemes = allowable_url_schemes()
super().__call__(value)
def validate_part_name(value): def validate_part_name(value):
"""Prevent some illegal characters in part names.""" """Prevent some illegal characters in part names."""
for c in ['|', '#', '$', '{', '}']: for c in ['|', '#', '$', '{', '}']:

View File

@ -4,6 +4,7 @@ import datetime
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db.models import Sum from django.db.models import Sum
from django.test import override_settings
from build.models import Build from build.models import Build
from InvenTree.helpers import InvenTreeTestCase from InvenTree.helpers import InvenTreeTestCase
@ -140,7 +141,7 @@ class StockTest(StockTestBase):
item.save() item.save()
item.full_clean() item.full_clean()
# Check that valid URLs pass # Check that valid URLs pass - and check custon schemes
for good_url in [ for good_url in [
'https://test.com', 'https://test.com',
'https://digikey.com/datasheets?file=1010101010101.bin', 'https://digikey.com/datasheets?file=1010101010101.bin',
@ -163,6 +164,14 @@ class StockTest(StockTestBase):
item.link = long_url item.link = long_url
item.save() item.save()
@override_settings(EXTRA_URL_SCHEMES=['ssh'])
def test_exteneded_schema(self):
"""Test that extended URL schemes are allowed"""
item = StockItem.objects.get(pk=1)
item.link = 'ssh://user:pwd@deb.org:223'
item.save()
item.full_clean()
def test_expiry(self): def test_expiry(self):
"""Test expiry date functionality for StockItem model.""" """Test expiry date functionality for StockItem model."""
today = datetime.datetime.now().date() today = datetime.datetime.now().date()