2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-17 18:26:32 +00:00

Protected settings fix (#5229)

* Hide protected setting in settings view

* Implement custom serializer for setting value

- Return '***' if the setting is protected

* Implement to_internal_value

* Stringify

* Add protected setting to sample plugin

* Unit tests for plugin settings API

* Update unit test
This commit is contained in:
Oliver
2023-07-12 16:29:08 +10:00
committed by GitHub
parent ee274739a6
commit 01f2aa5f74
4 changed files with 103 additions and 11 deletions

View File

@@ -193,3 +193,76 @@ class PluginDetailAPITest(PluginMixin, InvenTreeAPITestCase):
with self.assertRaises(NotFound) as exc:
check_plugin(plugin_slug=None, plugin_pk='123')
self.assertEqual(str(exc.exception.detail), "Plugin '123' not installed")
def test_plugin_settings(self):
"""Test plugin settings access via the API"""
# Ensure we have superuser permissions
self.user.is_superuser = True
self.user.save()
# Activate the 'sample' plugin via the API
cfg = PluginConfig.objects.filter(key='sample').first()
url = reverse('api-plugin-detail-activate', kwargs={'pk': cfg.pk})
self.client.patch(url, {}, expected_code=200)
# Valid plugin settings endpoints
valid_settings = [
'SELECT_PART',
'API_KEY',
'NUMERICAL_SETTING',
]
for key in valid_settings:
response = self.get(
reverse('api-plugin-setting-detail', kwargs={
'plugin': 'sample',
'key': key
}))
self.assertEqual(response.data['key'], key)
# Test that an invalid setting key raises a 404 error
response = self.get(
reverse('api-plugin-setting-detail', kwargs={
'plugin': 'sample',
'key': 'INVALID_SETTING'
}),
expected_code=404
)
# Test that a protected setting returns hidden value
response = self.get(
reverse('api-plugin-setting-detail', kwargs={
'plugin': 'sample',
'key': 'PROTECTED_SETTING'
}),
expected_code=200
)
self.assertEqual(response.data['value'], '***')
# Test that we can update a setting value
response = self.patch(
reverse('api-plugin-setting-detail', kwargs={
'plugin': 'sample',
'key': 'NUMERICAL_SETTING'
}),
{
'value': 456
},
expected_code=200
)
self.assertEqual(response.data['value'], '456')
# Retrieve the value again
response = self.get(
reverse('api-plugin-setting-detail', kwargs={
'plugin': 'sample',
'key': 'NUMERICAL_SETTING'
}),
expected_code=200
)
self.assertEqual(response.data['value'], '456')