From 0096c93bfec4a0d677d9a7ad839988925beb3cc7 Mon Sep 17 00:00:00 2001 From: Fabian Arrotin Date: Oct 08 2018 14:00:07 +0000 Subject: Adding 'list-groups' function to pagure-admin Merges https://pagure.io/pagure/pull-request/3703 --- diff --git a/pagure/cli/admin.py b/pagure/cli/admin.py index fd957b8..11aeb32 100644 --- a/pagure/cli/admin.py +++ b/pagure/cli/admin.py @@ -320,6 +320,19 @@ def _parser_new_group(subparser): local_parser.set_defaults(func=do_new_group) +def _parser_list_groups(subparser): + """ Set up the CLI argument parser for the list-groups action. + + :arg subparser: an argparse subparser allowing to have action's specific + arguments + + """ + local_parser = subparser.add_parser( + "list-groups", help="Lists existing groups on this pagure instance" + ) + local_parser.set_defaults(func=do_list_groups) + + def _parser_block_user(subparser): """ Set up the CLI argument parser for the block-user action. @@ -416,6 +429,9 @@ def parse_arguments(args=None): # new-group _parser_new_group(subparser) + # list-groups + _parser_list_groups(subparser) + # block-user _parser_block_user(subparser) @@ -864,6 +880,22 @@ def do_new_group(args): print(msg) +def do_list_groups(args): + """ Lists existing groups in this pagure instance. + + :arg args: the argparse object returned by ``parse_arguments()``. + + """ + + msg = pagure.lib.search_groups(session=session) + if msg: + print("List of groups on this Pagure instance:") + for group in msg: + print(group) + else: + print("No groups found in this pagure instance.") + + def do_block_user(args): """ Block the specified user from all interactions with pagure until the specified date. diff --git a/tests/test_pagure_admin.py b/tests/test_pagure_admin.py index dcfce9d..553c0af 100644 --- a/tests/test_pagure_admin.py +++ b/tests/test_pagure_admin.py @@ -23,6 +23,7 @@ import unittest # noqa import munch # noqa from mock import patch, MagicMock # noqa +from six import StringIO # noqa sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) @@ -1354,6 +1355,117 @@ class PagureNewGroupTests(tests.Modeltests): self.assertEqual(len(groups), 0) +class PagureListGroupEmptyTests(tests.Modeltests): + """ Tests for pagure-admin list-groups """ + + populate_db = False + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureListGroupEmptyTests, self).setUp() + pagure.cli.admin.session = self.session + + # 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() + + # Make the imported pagure use the correct db session + pagure.cli.admin.session = self.session + + groups = pagure.lib.search_groups(self.session) + self.assertEqual(len(groups), 0) + + @patch('sys.stdout', new_callable=StringIO) + def test_no_groups(self, mock_stdout): + """ Test the list-groups function of pagure-admin when there are no + groups in the database + """ + + args = munch.Munch() + pagure.cli.admin.do_list_groups(args) + + self.assertEqual( + mock_stdout.getvalue(), + 'No groups found in this pagure instance.\n' + ) + + groups = pagure.lib.search_groups(self.session) + self.assertEqual(len(groups), 0) + + +class PagureListGroupTests(tests.Modeltests): + """ Tests for pagure-admin list-groups """ + + populate_db = False + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureListGroupTests, self).setUp() + pagure.cli.admin.session = self.session + + # 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) + + # Create a group + pagure.lib.add_group( + self.session, + group_name='JL', + display_name='Justice League', + description='Nope, it\'s not JLA anymore', + group_type='user', + user='pingou', + is_admin=False, + blacklist=[] + ) + + self.session.commit() + + # Make the imported pagure use the correct db session + pagure.cli.admin.session = self.session + + groups = pagure.lib.search_groups(self.session) + self.assertEqual(len(groups), 1) + + @patch('sys.stdout', new_callable=StringIO) + def test_list_groups(self, mock_stdout): + """ Test the list-groups function of pagure-admin when there is one + group in the database + """ + + args = munch.Munch() + pagure.cli.admin.do_list_groups(args) + + self.assertEqual( + mock_stdout.getvalue(), + 'List of groups on this Pagure instance:\n' + 'Group: 1 - name JL\n' + ) + + groups = pagure.lib.search_groups(self.session) + self.assertEqual(len(groups), 1) + + class PagureBlockUserTests(tests.Modeltests): """ Tests for pagure-admin block-user """