Repository: inventree/InvenTree

Issue:
[FR]Unit test for Schema allowance
**Is your feature request the result of a bug?**
#3414 #3411

**Problem**
Custom schema settings are not e2e or unit-tested in the API.

**Suggested solution**
Add tests

**Describe alternatives you've considered**
N/A

**Examples of other systems**
N/A

**Do you want to develop this?**
Yes



Discussion:

Resolving patch:
diff --git a/InvenTree/InvenTree/fields.py b/InvenTree/InvenTree/fields.py
--- a/InvenTree/InvenTree/fields.py
+++ b/InvenTree/InvenTree/fields.py
@@ -4,7 +4,6 @@
 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 @@
 
 import InvenTree.helpers
 
-from .validators import allowable_url_schemes
+from .validators import AllowedURLValidator, allowable_url_schemes
 
 
 class InvenTreeRestURLField(RestURLField):
@@ -34,7 +33,7 @@ def __init__(self, **kwargs):
 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"""
diff --git a/InvenTree/InvenTree/validators.py b/InvenTree/InvenTree/validators.py
--- a/InvenTree/InvenTree/validators.py
+++ b/InvenTree/InvenTree/validators.py
@@ -4,6 +4,7 @@
 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 ['|', '#', '$', '{', '}']:


Test patch:
diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py
--- a/InvenTree/stock/tests.py
+++ b/InvenTree/stock/tests.py
@@ -4,6 +4,7 @@
 
 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 @@ def test_link(self):
                 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 @@ def test_link(self):
         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()