From 0dea420ffe3bea51f9f68baaa4a40d72204690a6 Mon Sep 17 00:00:00 2001 From: Aakanksha Saini Date: Tue, 2 Jan 2018 17:01:05 +0530 Subject: [PATCH] groups: Consistent listing of groups - Remove conditional registration of groups on module initialization. - Create uninitialized groups on addition/update of groups for user. - Remove group creation on setup/installation of a module. Tested for: - Multi registration of groups. - Manual UI test to check visibility of groups irrespective of whether module is installed/enabled. Signed-off-by: Aakanksha Saini Reviewed-by: Sunil Mohan Adapa --- plinth/modules/deluge/__init__.py | 5 ++-- plinth/modules/ikiwiki/__init__.py | 5 ++-- plinth/modules/transmission/__init__.py | 5 ++-- plinth/modules/ttrss/__init__.py | 5 ++-- plinth/modules/users/__init__.py | 1 - plinth/modules/users/forms.py | 2 ++ plinth/modules/users/tests/test_group.py | 37 ++++++++++++++++++++++++ 7 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 plinth/modules/users/tests/test_group.py diff --git a/plinth/modules/deluge/__init__.py b/plinth/modules/deluge/__init__.py index 4144dee09..2286f1904 100644 --- a/plinth/modules/deluge/__init__.py +++ b/plinth/modules/deluge/__init__.py @@ -26,7 +26,7 @@ from plinth import action_utils from plinth import frontpage from plinth import service as service_module from plinth.menu import main_menu -from plinth.modules.users import create_group, register_group +from plinth.modules.users import register_group from .manifest import clients @@ -63,6 +63,7 @@ def init(): menu = main_menu.get('apps') menu.add_urlname(name, 'glyphicon-magnet', 'deluge:index', short_description) + register_group(group) global service setup_helper = globals()['setup_helper'] @@ -74,7 +75,6 @@ def init(): if is_enabled(): add_shortcut() - register_group(group) def setup(helper, old_version=None): @@ -89,7 +89,6 @@ def setup(helper, old_version=None): disable=disable) helper.call('post', service.notify_enabled, None, True) helper.call('post', add_shortcut) - create_group(group[0]) def add_shortcut(): diff --git a/plinth/modules/ikiwiki/__init__.py b/plinth/modules/ikiwiki/__init__.py index 14d920840..29cf1f127 100644 --- a/plinth/modules/ikiwiki/__init__.py +++ b/plinth/modules/ikiwiki/__init__.py @@ -28,7 +28,7 @@ from plinth import cfg from plinth import frontpage from plinth import service as service_module from plinth.menu import main_menu -from plinth.modules.users import create_group, register_group +from plinth.modules.users import register_group from .manifest import clients @@ -69,6 +69,7 @@ def init(): """Initialize the ikiwiki module.""" menu = main_menu.get('apps') menu.add_urlname(name, 'glyphicon-edit', 'ikiwiki:index', short_description) + register_group(group) global service setup_helper = globals()['setup_helper'] @@ -79,7 +80,6 @@ def init(): if is_enabled(): add_shortcuts() - register_group(group) def setup(helper, old_version=None): @@ -93,7 +93,6 @@ def setup(helper, old_version=None): is_enabled=is_enabled, enable=enable, disable=disable) helper.call('post', service.notify_enabled, None, True) helper.call('post', add_shortcuts) - create_group(group[0]) def add_shortcuts(): diff --git a/plinth/modules/transmission/__init__.py b/plinth/modules/transmission/__init__.py index 58d302b46..ae518ebb7 100644 --- a/plinth/modules/transmission/__init__.py +++ b/plinth/modules/transmission/__init__.py @@ -25,7 +25,7 @@ from django.utils.translation import ugettext_lazy as _ from plinth import service as service_module from plinth import action_utils, actions, frontpage from plinth.menu import main_menu -from plinth.modules.users import create_group, register_group +from plinth.modules.users import register_group from .manifest import clients version = 2 @@ -59,6 +59,7 @@ def init(): menu = main_menu.get('apps') menu.add_urlname(name, 'glyphicon-save', 'transmission:index', short_description) + register_group(group) global service setup_helper = globals()['setup_helper'] @@ -70,7 +71,6 @@ def init(): if is_enabled(): add_shortcut() - register_group(group) def setup(helper, old_version=None): @@ -94,7 +94,6 @@ def setup(helper, old_version=None): disable=disable) helper.call('post', service.notify_enabled, None, True) helper.call('post', add_shortcut) - create_group(group[0]) def add_shortcut(): diff --git a/plinth/modules/ttrss/__init__.py b/plinth/modules/ttrss/__init__.py index aa4b4e7b6..3c416f275 100644 --- a/plinth/modules/ttrss/__init__.py +++ b/plinth/modules/ttrss/__init__.py @@ -28,7 +28,7 @@ from plinth import cfg from plinth import frontpage from plinth import service as service_module from plinth.menu import main_menu -from plinth.modules.users import create_group, register_group +from plinth.modules.users import register_group from .manifest import clients @@ -71,6 +71,7 @@ def init(): menu = main_menu.get('apps') menu.add_urlname(name, 'glyphicon-envelope', 'ttrss:index', short_description) + register_group(group) global service setup_helper = globals()['setup_helper'] @@ -82,7 +83,6 @@ def init(): if is_enabled(): add_shortcut() - register_group(group) def setup(helper, old_version=None): @@ -98,7 +98,6 @@ def setup(helper, old_version=None): is_enabled=is_enabled, enable=enable, disable=disable) helper.call('post', service.notify_enabled, None, True) helper.call('post', add_shortcut) - create_group(group[0]) def add_shortcut(): diff --git a/plinth/modules/users/__init__.py b/plinth/modules/users/__init__.py index f38629149..f13ef06b8 100644 --- a/plinth/modules/users/__init__.py +++ b/plinth/modules/users/__init__.py @@ -96,7 +96,6 @@ def _diagnose_ldap_entry(search_item): def create_group(group): """Add an LDAP group.""" actions.superuser_run('users', options=['create-group', group]) - register_group(group) def remove_group(group): diff --git a/plinth/modules/users/forms.py b/plinth/modules/users/forms.py index 09ff81ed6..ca4ac4cb1 100644 --- a/plinth/modules/users/forms.py +++ b/plinth/modules/users/forms.py @@ -109,6 +109,7 @@ class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm): for group in self.cleaned_data['groups']: try: + users.create_group(group) actions.superuser_run('users', [ 'add-user-to-group', user.get_username(), group @@ -205,6 +206,7 @@ class UserUpdateForm(ValidNewUsernameCheckMixin, forms.ModelForm): for new_group in new_groups: if new_group not in old_groups: try: + users.create_group(new_group) actions.superuser_run('users', [ 'add-user-to-group', user.get_username(), new_group diff --git a/plinth/modules/users/tests/test_group.py b/plinth/modules/users/tests/test_group.py new file mode 100644 index 000000000..9978f89e5 --- /dev/null +++ b/plinth/modules/users/tests/test_group.py @@ -0,0 +1,37 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +""" +Test module to exercise group registration. + +It is recommended to run this module with root privileges in a virtual machine. +""" + +import unittest +from plinth.modules import users + + +class TestGroups(unittest.TestCase): + """Test groups behavior.""" + def test_register_group(self): + """Test for multi addition of same group""" + group = ('TestGroup', 'Group for testing') + users.register_group(group) + users.register_group(group) + self.assertEqual( + len(users.groups), 1, + 'Duplicate entries for same group generated!') + return users.groups