# HG changeset patch # User Thomas De Schampheleire # Date 2018-05-20 22:23:52 # Node ID d612fd65356256a6319cf2fc7d04db3d02da4ab3 # Parent cefc3010baaf2afddcf8ca40fe5c07492e458404 admin: hooks: prevent creation of existing custom hook Trying to add a hook that already exists does not currently give an error but does not work. Detect the situation and report via a flash. diff --git a/kallithea/controllers/admin/settings.py b/kallithea/controllers/admin/settings.py --- a/kallithea/controllers/admin/settings.py +++ b/kallithea/controllers/admin/settings.py @@ -351,7 +351,9 @@ class SettingsController(BaseController) try: ui_key = ui_key and ui_key.strip() - if ui_value and ui_key: + if ui_key in (x.ui_key for x in Ui.get_custom_hooks()): + h.flash(_('Hook already exists'), category='error') + elif ui_value and ui_key: Ui.create_or_update_hook(ui_key, ui_value) h.flash(_('Added new hook'), category='success') elif hook_id: diff --git a/kallithea/tests/functional/test_admin_settings.py b/kallithea/tests/functional/test_admin_settings.py --- a/kallithea/tests/functional/test_admin_settings.py +++ b/kallithea/tests/functional/test_admin_settings.py @@ -56,6 +56,18 @@ class TestAdminSettingsController(TestCo response.mustcontain('test_hooks_1') response.mustcontain('new_value_of_hook_1') + def test_add_existing_custom_hook(self): + self.log_user() + response = self.app.post(url('admin_settings_hooks'), + params=dict(new_hook_ui_key='test_hooks_1', + new_hook_ui_value='attempted_new_value', + _authentication_token=self.authentication_token())) + + self.checkSessionFlash(response, 'Hook already exists') + response = response.follow() + response.mustcontain('test_hooks_1') + response.mustcontain('new_value_of_hook_1') + def test_create_custom_hook_delete(self): self.log_user() response = self.app.post(url('admin_settings_hooks'),