mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
[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:
parent
c1064906d6
commit
9cfbe1061d
@ -4,7 +4,6 @@ import sys
|
||||
from decimal import Decimal
|
||||
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.db import models as models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@ -15,7 +14,7 @@ from rest_framework.fields import URLField as RestURLField
|
||||
|
||||
import InvenTree.helpers
|
||||
|
||||
from .validators import allowable_url_schemes
|
||||
from .validators import AllowedURLValidator, allowable_url_schemes
|
||||
|
||||
|
||||
class InvenTreeRestURLField(RestURLField):
|
||||
@ -34,7 +33,7 @@ class InvenTreeRestURLField(RestURLField):
|
||||
class InvenTreeURLField(models.URLField):
|
||||
"""Custom URL field which has custom scheme validators."""
|
||||
|
||||
default_validators = [validators.URLValidator(schemes=allowable_url_schemes())]
|
||||
default_validators = [AllowedURLValidator()]
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
"""Initialization method for InvenTreeURLField"""
|
||||
|
@ -4,6 +4,7 @@ import re
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import validators
|
||||
from django.core.exceptions import FieldDoesNotExist, ValidationError
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@ -37,6 +38,14 @@ def allowable_url_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):
|
||||
"""Prevent some illegal characters in part names."""
|
||||
for c in ['|', '#', '$', '{', '}']:
|
||||
|
@ -4,6 +4,7 @@ import datetime
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.models import Sum
|
||||
from django.test import override_settings
|
||||
|
||||
from build.models import Build
|
||||
from InvenTree.helpers import InvenTreeTestCase
|
||||
@ -140,7 +141,7 @@ class StockTest(StockTestBase):
|
||||
item.save()
|
||||
item.full_clean()
|
||||
|
||||
# Check that valid URLs pass
|
||||
# Check that valid URLs pass - and check custon schemes
|
||||
for good_url in [
|
||||
'https://test.com',
|
||||
'https://digikey.com/datasheets?file=1010101010101.bin',
|
||||
@ -163,6 +164,14 @@ class StockTest(StockTestBase):
|
||||
item.link = long_url
|
||||
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):
|
||||
"""Test expiry date functionality for StockItem model."""
|
||||
today = datetime.datetime.now().date()
|
||||
|
Loading…
Reference in New Issue
Block a user