Skip to content

Commit

Permalink
update tests, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Oct 25, 2023
1 parent 10f8542 commit 26caf88
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 132 deletions.
37 changes: 21 additions & 16 deletions example_project_app/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
from example_project_app.urls import urlpatterns


# SODAR constants
PROJECT_TYPE_PROJECT = SODAR_CONSTANTS['PROJECT_TYPE_PROJECT']
PROJECT_TYPE_CATEGORY = SODAR_CONSTANTS['PROJECT_TYPE_CATEGORY']

# Local constants
EXAMPLE_MODIFY_API_MSG = (
'Example project app plugin API called from {project_type} {action}.'
)
PROJECT_TYPE_PROJECT = SODAR_CONSTANTS['PROJECT_TYPE_PROJECT']
PROJECT_TYPE_CATEGORY = SODAR_CONSTANTS['PROJECT_TYPE_CATEGORY']
# Invalid app setting value for testing custom validation
INVALID_SETTING_VALUE = 'ieDeequ6Eizohxi7Eebiliu6shou5aiT'
INVALID_SETTING_MSG = 'INVALID_SETTING_VALUE detected'


class ProjectAppPlugin(ProjectModifyPluginMixin, ProjectAppPluginPoint):
Expand Down Expand Up @@ -348,18 +354,17 @@ def perform_project_modify(
),
)

# TODO: Fix this and update tests
def validate_form_app_settings(self, app_settings, project=None, user=None):
"""Example implementation for app setting validation plugin API"""
errors = {}
for setting_name, setting_value in app_settings.items():
if (
setting_name == 'project_hidden_setting'
and setting_value == 'Example project hidden setting'
):
errors[
setting_name
] = 'Invalid value for a custom validation method'
if errors == {}:
return None
return errors
"""Example implementation for custom form app setting validation"""
ret = {}
if (
project
and app_settings.get('project_str_setting') == INVALID_SETTING_VALUE
):
ret['project_str_setting'] = INVALID_SETTING_MSG
if (
user
and app_settings.get('user_str_setting') == INVALID_SETTING_VALUE
):
ret['user_str_setting'] = INVALID_SETTING_MSG
return ret
4 changes: 2 additions & 2 deletions projectroles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
(PROJECT_TYPE_PROJECT, get_display_name(PROJECT_TYPE_PROJECT, title=True)),
]
SETTING_CUSTOM_VALIDATE_ERROR = (
'Exception in custom app setting validation '
'for plugin "{plugin}": {exception}'
'Exception in custom app setting validation for plugin "{plugin}": '
'{exception}'
)


Expand Down
6 changes: 2 additions & 4 deletions projectroles/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def validate_form_app_settings(self, app_settings, project=None, user=None):
:param app_settings: Dict of app settings
:param project: Project object or None
:param user: User object
:param user: User object or None
:return: dict in format of {setting_name: 'Error string'}
"""
# TODO: Implement this in your app plugin (optional)
Expand Down Expand Up @@ -626,10 +626,8 @@ def validate_form_app_settings(self, app_settings, user=None):
"""
Validate app settings form data and return a dict of errors.
Validate app settings form data and return a dict of errors.
:param app_settings: Dict of app settings
:param user: User object
:param user: User object or None
:return: dict in format of {setting_name: 'Error string'}
"""
# TODO: Implement this in your app plugin (optional)
Expand Down
43 changes: 32 additions & 11 deletions projectroles/tests/test_app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
AppSettingMixin,
)

# example_project_app dependency
from example_project_app.plugins import (
INVALID_SETTING_VALUE,
INVALID_SETTING_MSG,
)


app_settings = AppSettingAPI()

Expand Down Expand Up @@ -1039,22 +1045,37 @@ def test_delete_by_scope_param_user(self):
)

def test_validate_form_app_settings(self):
"""Test validate_form_app_settings() method on valid app_setting"""
"""Test validate_form_app_settings() with valid project setting value"""
app_plugin = get_app_plugin(EXAMPLE_APP_NAME)
valid_setting = {'valid_setting': True}
app_settings = {'project_str_setting': 'String'}
errors = app_plugin.validate_form_app_settings(
valid_setting, project=self.project, user=self.user
app_settings, project=self.project
)
self.assertEqual(errors, None)
self.assertEqual(errors, {})

def test_validate_form_app_settings_user_scope_error(self):
"""Test validate_form_app_settings() method on invalid app_setting"""
def test_validate_form_app_settings_invalid(self):
"""Test validate_form_app_settings() with invalid project setting value"""
app_plugin = get_app_plugin(EXAMPLE_APP_NAME)
settings = {'project_hidden_setting': 'Example project hidden setting'}
app_settings = {'project_str_setting': INVALID_SETTING_VALUE}
errors = app_plugin.validate_form_app_settings(
settings, project=self.project
app_settings, project=self.project
)
self.assertIsNotNone(errors)
self.assertIn(
'Invalid value for a custom validation method', errors.values()
self.assertEqual(errors, {'project_str_setting': INVALID_SETTING_MSG})

def test_validate_form_app_settings_user(self):
"""Test validate_form_app_settings() with valid user setting value"""
app_plugin = get_app_plugin(EXAMPLE_APP_NAME)
app_settings = {'user_str_setting': 'String'}
errors = app_plugin.validate_form_app_settings(
app_settings, user=self.user
)
self.assertEqual(errors, {})

def test_validate_form_app_settings_user_invalid(self):
"""Test validate_form_app_settings() with invalid user setting value"""
app_plugin = get_app_plugin(EXAMPLE_APP_NAME)
app_settings = {'user_str_setting': INVALID_SETTING_VALUE}
errors = app_plugin.validate_form_app_settings(
app_settings, user=self.user
)
self.assertEqual(errors, {'user_str_setting': INVALID_SETTING_MSG})
33 changes: 33 additions & 0 deletions projectroles/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

from test_plus.test import TestCase

# example_project_app dependency
from example_project_app.plugins import INVALID_SETTING_VALUE

# Timeline dependency
from timeline.models import ProjectEvent
from timeline.tests.test_models import (
Expand Down Expand Up @@ -1161,7 +1164,10 @@ def test_update_project_regular_user(self):

def test_update_project_title_delimiter(self):
"""Test Project updating with category delimiter in title (should fail)"""
# TODO: Add values getter as a helper
values = model_to_dict(self.project)
values['parent'] = self.category.sodar_uuid
values['owner'] = self.user.sodar_uuid
values['title'] = 'Project{}Title'.format(CAT_DELIMITER)
# Add settings values
values.update(
Expand All @@ -1179,6 +1185,33 @@ def test_update_project_title_delimiter(self):
self.project.refresh_from_db()
self.assertEqual(self.project.title, 'TestProject')

def test_update_project_custom_validation(self):
"""Test updating with custom validation and invalid value (should fail)"""
values = model_to_dict(self.project)
values['parent'] = self.category.sodar_uuid
values['owner'] = self.user.sodar_uuid
values.update(
app_settings.get_all(project=self.project, post_safe=True)
)
values[
'settings.example_project_app.project_str_setting'
] = INVALID_SETTING_VALUE
with self.login(self.user):
response = self.client.post(
reverse(
'projectroles:update',
kwargs={'project': self.project.sodar_uuid},
),
values,
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
app_settings.get(
EXAMPLE_APP_NAME, 'project_str_setting', project=self.project
),
'',
)

def test_render_category(self):
"""Test rendering with existing category"""
with self.login(self.user):
Expand Down
Loading

0 comments on commit 26caf88

Please sign in to comment.