mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Fixes #5446: Fix validation for plugin version and required settings
This commit is contained in:
@ -16,6 +16,7 @@
|
|||||||
* [#5410](https://github.com/netbox-community/netbox/issues/5410) - Restore tags field on cable connection forms
|
* [#5410](https://github.com/netbox-community/netbox/issues/5410) - Restore tags field on cable connection forms
|
||||||
* [#5433](https://github.com/netbox-community/netbox/issues/5433) - Exclude SVG files from front/rear image upload for device types (currently unsupported)
|
* [#5433](https://github.com/netbox-community/netbox/issues/5433) - Exclude SVG files from front/rear image upload for device types (currently unsupported)
|
||||||
* [#5436](https://github.com/netbox-community/netbox/issues/5436) - Show assigned IP addresses in interfaces list
|
* [#5436](https://github.com/netbox-community/netbox/issues/5436) - Show assigned IP addresses in interfaces list
|
||||||
|
* [#5446](https://github.com/netbox-community/netbox/issues/5446) - Fix validation for plugin version and required settings
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ import inspect
|
|||||||
from packaging import version
|
from packaging import version
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.conf import settings
|
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.template.loader import get_template
|
from django.template.loader import get_template
|
||||||
|
|
||||||
@ -71,10 +70,10 @@ class PluginConfig(AppConfig):
|
|||||||
register_menu_items(self.verbose_name, menu_items)
|
register_menu_items(self.verbose_name, menu_items)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate(cls, user_config):
|
def validate(cls, user_config, netbox_version):
|
||||||
|
|
||||||
# Enforce version constraints
|
# Enforce version constraints
|
||||||
current_version = version.parse(settings.VERSION)
|
current_version = version.parse(netbox_version)
|
||||||
if cls.min_version is not None:
|
if cls.min_version is not None:
|
||||||
min_version = version.parse(cls.min_version)
|
min_version = version.parse(cls.min_version)
|
||||||
if current_version < min_version:
|
if current_version < min_version:
|
||||||
|
@ -86,21 +86,19 @@ class PluginTest(TestCase):
|
|||||||
"""
|
"""
|
||||||
self.assertIn('extras.tests.dummy_plugin.*', settings.CACHEOPS)
|
self.assertIn('extras.tests.dummy_plugin.*', settings.CACHEOPS)
|
||||||
|
|
||||||
@override_settings(VERSION='0.9')
|
|
||||||
def test_min_version(self):
|
def test_min_version(self):
|
||||||
"""
|
"""
|
||||||
Check enforcement of minimum NetBox version.
|
Check enforcement of minimum NetBox version.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(ImproperlyConfigured):
|
with self.assertRaises(ImproperlyConfigured):
|
||||||
dummy_config.validate({})
|
dummy_config.validate({}, '0.9')
|
||||||
|
|
||||||
@override_settings(VERSION='10.0')
|
|
||||||
def test_max_version(self):
|
def test_max_version(self):
|
||||||
"""
|
"""
|
||||||
Check enforcement of maximum NetBox version.
|
Check enforcement of maximum NetBox version.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(ImproperlyConfigured):
|
with self.assertRaises(ImproperlyConfigured):
|
||||||
dummy_config.validate({})
|
dummy_config.validate({}, '10.0')
|
||||||
|
|
||||||
def test_required_settings(self):
|
def test_required_settings(self):
|
||||||
"""
|
"""
|
||||||
@ -110,11 +108,11 @@ class PluginTest(TestCase):
|
|||||||
required_settings = ['foo']
|
required_settings = ['foo']
|
||||||
|
|
||||||
# Validation should pass when all required settings are present
|
# Validation should pass when all required settings are present
|
||||||
DummyConfigWithRequiredSettings.validate({'foo': True})
|
DummyConfigWithRequiredSettings.validate({'foo': True}, settings.VERSION)
|
||||||
|
|
||||||
# Validation should fail when a required setting is missing
|
# Validation should fail when a required setting is missing
|
||||||
with self.assertRaises(ImproperlyConfigured):
|
with self.assertRaises(ImproperlyConfigured):
|
||||||
DummyConfigWithRequiredSettings.validate({})
|
DummyConfigWithRequiredSettings.validate({}, settings.VERSION)
|
||||||
|
|
||||||
def test_default_settings(self):
|
def test_default_settings(self):
|
||||||
"""
|
"""
|
||||||
@ -127,10 +125,10 @@ class PluginTest(TestCase):
|
|||||||
|
|
||||||
# Populate the default value if setting has not been specified
|
# Populate the default value if setting has not been specified
|
||||||
user_config = {}
|
user_config = {}
|
||||||
DummyConfigWithDefaultSettings.validate(user_config)
|
DummyConfigWithDefaultSettings.validate(user_config, settings.VERSION)
|
||||||
self.assertEqual(user_config['bar'], 123)
|
self.assertEqual(user_config['bar'], 123)
|
||||||
|
|
||||||
# Don't overwrite specified values
|
# Don't overwrite specified values
|
||||||
user_config = {'bar': 456}
|
user_config = {'bar': 456}
|
||||||
DummyConfigWithDefaultSettings.validate(user_config)
|
DummyConfigWithDefaultSettings.validate(user_config, settings.VERSION)
|
||||||
self.assertEqual(user_config['bar'], 456)
|
self.assertEqual(user_config['bar'], 456)
|
||||||
|
@ -621,7 +621,7 @@ for plugin_name in PLUGINS:
|
|||||||
# Validate user-provided configuration settings and assign defaults
|
# Validate user-provided configuration settings and assign defaults
|
||||||
if plugin_name not in PLUGINS_CONFIG:
|
if plugin_name not in PLUGINS_CONFIG:
|
||||||
PLUGINS_CONFIG[plugin_name] = {}
|
PLUGINS_CONFIG[plugin_name] = {}
|
||||||
plugin_config.validate(PLUGINS_CONFIG[plugin_name])
|
plugin_config.validate(PLUGINS_CONFIG[plugin_name], VERSION)
|
||||||
|
|
||||||
# Add middleware
|
# Add middleware
|
||||||
plugin_middleware = plugin_config.middleware
|
plugin_middleware = plugin_config.middleware
|
||||||
|
Reference in New Issue
Block a user