Skip to content

Commit

Permalink
add app setting user_modifiable validation (#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jan 9, 2025
1 parent b50794c commit 2f02947
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Added
- App setting type constants (#1458)
- ``PluginAppSettingDef`` class for app setting definitions (#1456)
- Django check for unique app setting names within each plugin (#1456)
- App setting ``user_modifiable`` validation (#1536)

Changed
-------
Expand Down
23 changes: 22 additions & 1 deletion projectroles/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def __init__(
placeholder=None,
description=None,
options=None,
user_modifiable=True,
user_modifiable=None, # Set None here to validate PROJECT_USER value
global_edit=False,
project_types=None,
widget_attrs=None,
Expand Down Expand Up @@ -714,6 +714,12 @@ def __init__(
and not callable(options)
):
self.validate_default_in_options(default, options)
# Validate and set user_modifiable
self.validate_user_modifiable(scope, user_modifiable)
if user_modifiable is None:
user_modifiable = (
False if scope == APP_SETTING_SCOPE_PROJECT_USER else True
)
# Set members
self.name = name
self.scope = scope
Expand Down Expand Up @@ -830,6 +836,21 @@ def validate_value(cls, setting_type, setting_value):
'Please enter valid JSON ({})'.format(setting_value)
)

@classmethod
def validate_user_modifiable(cls, scope, user_modifiable):
"""
Validate user_modifiable against scope.
:param scope: String
:param user_modifiable: Bool or None
:raises: ValueError if user_modifiable can't be set for scope
"""
if user_modifiable and scope == APP_SETTING_SCOPE_PROJECT_USER:
raise ValueError(
'Argument user_modifiable can not be set True for PROJECT_USER '
'scope settings'
)


class PluginObjectLink:
"""
Expand Down
34 changes: 34 additions & 0 deletions projectroles/tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,37 @@ def test_init_default_not_in_options(self):
default=0,
options=[1, 2],
)

def test_init_project_user_modifiable_false(self):
"""Test init with PROJECT_USER scope and user_modifiable=False"""
s_def = PluginAppSettingDef(
name=DEF_NAME,
scope=APP_SETTING_SCOPE_PROJECT_USER,
type=APP_SETTING_TYPE_BOOLEAN,
default=True,
user_modifiable=False,
)
self.assertIsNotNone(s_def)
self.assertEqual(s_def.user_modifiable, False)

def test_init_project_user_modifiable_unset(self):
"""Test init with PROJECT_USER scope and user_modifiable unset"""
s_def = PluginAppSettingDef(
name=DEF_NAME,
scope=APP_SETTING_SCOPE_PROJECT_USER,
type=APP_SETTING_TYPE_BOOLEAN,
default=True,
)
self.assertIsNotNone(s_def)
self.assertEqual(s_def.user_modifiable, False)

def test_init_project_user_modifiable_true(self):
"""Test init with PROJECT_USER scope and user_modifiable=True"""
with self.assertRaises(ValueError):
PluginAppSettingDef(
name=DEF_NAME,
scope=APP_SETTING_SCOPE_PROJECT_USER,
type=APP_SETTING_TYPE_BOOLEAN,
default=True,
user_modifiable=True,
)

0 comments on commit 2f02947

Please sign in to comment.