Prevent plugin URL routing if ENABLE_PLUGINS_URL setting is disabled (#5648)

* Prevent plugin URL routing if ENABLE_PLUGINS_URL setting is disabled

* Fix for unit test

* Improve unit testing
This commit is contained in:
Oliver 2023-10-03 12:58:39 +11:00 committed by GitHub
parent 30cf97d85b
commit 6521ce5216
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -11,8 +11,33 @@ class SampleIntegrationPluginTests(InvenTreeTestCase):
def test_view(self):
"""Check the function of the custom sample plugin."""
response = self.client.get('/plugin/sample/ho/he/')
from common.models import InvenTreeSetting
url = '/plugin/sample/ho/he/'
# First, check with custom URLs disabled
InvenTreeSetting.set_setting('ENABLE_PLUGINS_URL', False, None)
# Requires a full reload of the registry
registry.reload_plugins()
# URL should redirect to index page
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
# Now, check with custom URLs enabled
InvenTreeSetting.set_setting('ENABLE_PLUGINS_URL', True, None)
# Requires a full reload of the registry
registry.reload_plugins()
# And ensure that the plugin is enabled
registry.set_plugin_state('sample', True)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'Hi there testuser this works')
def test_settings(self):

View File

@ -7,12 +7,15 @@ PLUGIN_BASE = 'plugin' # Constant for links
def get_plugin_urls():
"""Returns a urlpattern that can be integrated into the global urls."""
from common.models import InvenTreeSetting
from plugin import registry
urls = []
for plugin in registry.plugins.values():
if plugin.mixin_enabled('urls'):
urls.append(plugin.urlpatterns)
# Only allow custom routing if the setting is enabled
if InvenTreeSetting.get_setting('ENABLE_PLUGINS_URL', False, create=False, cache=False):
for plugin in registry.plugins.values():
if plugin.mixin_enabled('urls'):
urls.append(plugin.urlpatterns)
return re_path(f'^{PLUGIN_BASE}/', include((urls, 'plugin')))