diff --git a/pagure/cli/admin.py b/pagure/cli/admin.py index 0d1482d..e28576b 100644 --- a/pagure/cli/admin.py +++ b/pagure/cli/admin.py @@ -23,6 +23,7 @@ if 'PAGURE_CONFIG' not in os.environ \ import pagure.exceptions # noqa: E402 import pagure.lib # noqa: E402 import pagure.lib.git # noqa: E402 +import pagure.lib.tasks # noqa: E402 from pagure import (SESSION, APP, generate_user_key_files) # noqa: E402 @@ -34,6 +35,16 @@ def _parser_refresh_gitolite(subparser): local_parser = subparser.add_parser( 'refresh-gitolite', help='Re-generate the gitolite config file') + local_parser.add_argument( + '--user', help="User of the project (to use only on forks)") + local_parser.add_argument( + '--project', help="Project to update (as namespace/project if there " + "is a namespace)") + local_parser.add_argument( + '--group', help="Group to refresh") + local_parser.add_argument( + '--all', dest="all_", default=False, action='store_true', + help="Refresh all the projects") local_parser.set_defaults(func=do_generate_acl) @@ -156,23 +167,54 @@ def _get_input(text): return raw_input(text) -def do_generate_acl(_): +def do_generate_acl(args): """ Regenerate the gitolite ACL file. - :arg _: the argparse object returned by ``parse_arguments()``, which is - ignored as there are no argument to pass to this action. + :arg args: the argparse object returned by ``parse_arguments()``. """ - cmd = pagure.lib.git._get_gitolite_command() - if not cmd: - raise pagure.exceptions.PagureException( - '/!\ un-able to generate the right gitolite command') + _log.debug('group: %s', args.group) + _log.debug('project: %s', args.project) + _log.debug('user: %s', args.user) + _log.debug('all: %s', args.all_) + + title = None + project = None + if args.project: + namespace = None + if '/' in args.project: + if args.project.count('/') > 1: + raise pagure.exceptions.PagureException( + 'Invalid project name, has more than one "/": %s' % + args.project) + namespace, name = args.project.split('/') + else: + name = args.project + project = pagure.lib._get_project( + SESSION, namespace=namespace, name=name, user=args.user) + title = project.fullname + if args.all_: + title = 'all' + project = -1 + + helper = pagure.lib.git_auth.get_git_auth_helper( + APP.config['GITOLITE_BACKEND']) + _log.debug('Got helper: %s', helper) + + group_obj = None + if args.group: + group_obj = pagure.lib.search_groups(SESSION, group_name=args.group) + _log.debug( + 'Calling helper: %s with arg: project=%s, group=%s', + helper, project, group_obj) + print( - 'Do you want to re-generate the gitolite.conf file then ' - 'calling: %s' % cmd) + 'Do you want to re-generate the gitolite.conf file for group: %s ' + 'and project: %s?' % (group_obj, title)) if _ask_confirmation(): - pagure.lib.git._generate_gitolite_acls() + helper.generate_acls(project=project, group=group_obj) + pagure.lib.tasks.gc_clean() print('Gitolite ACLs updated') diff --git a/tests/test_pagure_admin.py b/tests/test_pagure_admin.py index 3b33249..638744f 100644 --- a/tests/test_pagure_admin.py +++ b/tests/test_pagure_admin.py @@ -97,10 +97,16 @@ actions: cmd = ['python', PAGURE_ADMIN, 'refresh-gitolite', '--help'] self.assertEqual( _get_ouput(cmd)[0], - '''usage: admin.py refresh-gitolite [-h] + '''usage: admin.py refresh-gitolite [-h] [--user USER] [--project PROJECT] + [--group GROUP] [--all] optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit + --user USER User of the project (to use only on forks) + --project PROJECT Project to update (as namespace/project if there is a + namespace) + --group GROUP Group to refresh + --all Refresh all the projects ''') def test_parser_refresh_ssh_help(self): @@ -253,6 +259,154 @@ class PagureAdminAdminTokenEmptytests(tests.Modeltests): self.assertEqual(_get_ouput(cmd)[0], 'No admin tokens found\n') +class PagureAdminAdminRefreshGitolitetests(tests.Modeltests): + """ Tests for pagure-admin refresh-gitolite """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureAdminAdminRefreshGitolitetests, self).setUp() + + self.configfile = os.path.join(self.path, 'config') + self.dbpath = "sqlite:///%s/pagure_dev.sqlite" % self.path + with open(self.configfile, 'w') as stream: + stream.write('DB_URL="%s"\n' % self.dbpath) + + os.environ['PAGURE_CONFIG'] = self.configfile + + createdb = os.path.abspath( + os.path.join(tests.HERE, '..', 'createdb.py')) + cmd = ['python', createdb] + _get_ouput(cmd) + + self.session = pagure.lib.model.create_tables( + self.dbpath, acls=pagure.APP.config.get('ACLS', {})) + + # Create the user pingou + item = pagure.lib.model.User( + user='pingou', + fullname='PY C', + password='foo', + default_email='bar@pingou.com', + ) + self.session.add(item) + item = pagure.lib.model.UserEmail( + user_id=1, + email='bar@pingou.com') + self.session.add(item) + self.session.commit() + + # Create a couple of projects + tests.create_projects(self.session) + + # Add a group + msg = pagure.lib.add_group( + self.session, + group_name='foo', + display_name='foo group', + description=None, + group_type='bar', + user='pingou', + is_admin=False, + blacklist=[], + ) + self.session.commit() + self.assertEqual(msg, 'User `pingou` added to the group `foo`.') + + # Make the imported pagure use the correct db session + pagure.cli.admin.SESSION = self.session + + def tearDown(self): + """ Tear down the environnment after running the tests. """ + super(PagureAdminAdminRefreshGitolitetests, self).tearDown() + del(os.environ['PAGURE_CONFIG']) + + @patch('pagure.cli.admin._ask_confirmation') + @patch('pagure.lib.git_auth.get_git_auth_helper') + def test_do_refresh_gitolite_no_args(self, get_helper, conf): + """ Test the do_generate_acl function with no special args. """ + conf.return_value = True + helper = MagicMock() + get_helper.return_value = helper + + args = munch.Munch( + {'group': None, 'project': None, 'all_': False, 'user': None}) + pagure.cli.admin.do_generate_acl(args) + + get_helper.assert_called_with('gitolite3') + args = helper.generate_acls.call_args + self.assertIsNone(args[1].get('group')) + self.assertIsNone(args[1].get('project')) + + @patch('pagure.cli.admin._ask_confirmation') + @patch('pagure.lib.git_auth.get_git_auth_helper') + def test_do_refresh_gitolite_all_project(self, get_helper, conf): + """ Test the do_generate_acl function for all projects. """ + conf.return_value = True + helper = MagicMock() + get_helper.return_value = helper + + args = munch.Munch( + {'group': None, 'project': None, 'all_': True, 'user': None}) + pagure.cli.admin.do_generate_acl(args) + + get_helper.assert_called_with('gitolite3') + args = helper.generate_acls.call_args + self.assertIsNone(args[1].get('group')) + self.assertEqual(args[1].get('project'), -1) + + @patch('pagure.cli.admin._ask_confirmation') + @patch('pagure.lib.git_auth.get_git_auth_helper') + def test_do_refresh_gitolite_one_project(self, get_helper, conf): + """ Test the do_generate_acl function for a certain project. """ + conf.return_value = True + helper = MagicMock() + get_helper.return_value = helper + + args = munch.Munch( + {'group': None, 'project': 'test', 'all_': False, 'user': None}) + pagure.cli.admin.do_generate_acl(args) + + get_helper.assert_called_with('gitolite3') + args = helper.generate_acls.call_args + self.assertIsNone(args[1].get('group')) + self.assertEqual(args[1].get('project').fullname, 'test') + + @patch('pagure.cli.admin._ask_confirmation') + @patch('pagure.lib.git_auth.get_git_auth_helper') + def test_do_refresh_gitolite_one_project_and_all(self, get_helper, conf): + """ Test the do_generate_acl function for a certain project and all. + """ + conf.return_value = True + helper = MagicMock() + get_helper.return_value = helper + + args = munch.Munch( + {'group': None, 'project': 'test', 'all_': True, 'user': None}) + pagure.cli.admin.do_generate_acl(args) + + get_helper.assert_called_with('gitolite3') + args = helper.generate_acls.call_args + self.assertIsNone(args[1].get('group')) + self.assertEqual(args[1].get('project'), -1) + + @patch('pagure.cli.admin._ask_confirmation') + @patch('pagure.lib.git_auth.get_git_auth_helper') + def test_do_refresh_gitolite_one_group(self, get_helper, conf): + """ Test the do_generate_acl function for a certain group. """ + conf.return_value = True + helper = MagicMock() + get_helper.return_value = helper + + args = munch.Munch( + {'group': 'foo', 'project': None, 'all_': False, 'user': None}) + pagure.cli.admin.do_generate_acl(args) + + get_helper.assert_called_with('gitolite3') + args = helper.generate_acls.call_args + self.assertEqual(args[1].get('group').group_name, 'foo') + self.assertIsNone(args[1].get('project')) + + class PagureAdminAdminTokentests(tests.Modeltests): """ Tests for pagure-admin admin-token """