From 82c33e5edcc1cc451d7eb78b2ec9380db289b17a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 20 2020 08:18:48 +0000 Subject: Add an update-acls action to pagure-admin Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/cli/admin.py b/pagure/cli/admin.py index 99651fc..b5d3a6e 100644 --- a/pagure/cli/admin.py +++ b/pagure/cli/admin.py @@ -31,6 +31,7 @@ if "PAGURE_CONFIG" not in os.environ and os.path.exists( import pagure.config # noqa: E402 import pagure.exceptions # noqa: E402 import pagure.lib.git # noqa: E402 +import pagure.lib.model # noqa: E402 import pagure.lib.model_base # noqa: E402 import pagure.lib.query # noqa: E402 import pagure.lib.tasks_utils # noqa: E402 @@ -503,6 +504,23 @@ def _parser_set_default_branch(subparser): local_parser.set_defaults(func=do_set_default_branch) +def _parser_update_acls(subparser): + """ Set up the CLI argument parser for the update-acls action. + + :arg subparser: an argparse subparser allowing to have action's specific + arguments + + """ + + local_parser = subparser.add_parser( + "update-acls", + help="Update the ACLs stored in the database with the ones defined " + "in the configuration file (addition only, no ACLs are removed from " + "the database).", + ) + local_parser.set_defaults(func=do_update_acls) + + def parse_arguments(args=None): """ Set-up the argument parsing. """ parser = argparse.ArgumentParser( @@ -567,6 +585,9 @@ def parse_arguments(args=None): # set-default-branch _parser_set_default_branch(subparser) + # update-acls + _parser_update_acls(subparser) + return parser.parse_args(args) @@ -906,6 +927,22 @@ def do_delete_project(args): print("Project deleted") +def do_update_acls(args): + """ Update the ACLs in the database from the list present in the + configuration file. + + :arg args: the argparse object returned by ``parse_arguments()``. + + """ + acls = _config.get("ACLS", {}) + _log.debug("ACLS: %s", acls) + + pagure.lib.model.create_default_status(session, acls=acls) + print( + "ACLS in the database synced with the list in the configuration file" + ) + + def do_get_watch_status(args): """ Get the watch status of an user on a project. diff --git a/tests/test_pagure_admin.py b/tests/test_pagure_admin.py index 3e6c20e..b7de246 100644 --- a/tests/test_pagure_admin.py +++ b/tests/test_pagure_admin.py @@ -2262,5 +2262,116 @@ class PagureSetDefaultBranchTests(tests.Modeltests): self.assertEqual(gitrepo.head.shorthand, "dev") +class PagureAdminUpdateAclsTests(tests.Modeltests): + """ Tests for pagure-admin update-acls """ + + populate_db = False + maxDiff = None + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureAdminUpdateAclsTests, self).setUp() + pagure.cli.admin.session = self.session + + # Make the imported pagure use the correct db session + pagure.cli.admin.session = self.session + + def test_update_acls(self): + """ Test the update-acls function of pagure-admin. + """ + args = munch.Munch() + with tests.capture_output() as output: + pagure.cli.admin.do_update_acls(args) + + output = output.getvalue() + self.assertEqual( + "ACLS in the database synced with the list in the configuration file\n", + output, + ) + + acls = pagure.lib.query.get_acls(self.session) + self.assertEqual( + [a.name for a in acls], + [ + "commit", + "commit_flag", + "create_branch", + "create_project", + "fork_project", + "generate_acls_project", + "internal_access", + "issue_assign", + "issue_change_status", + "issue_comment", + "issue_create", + "issue_subscribe", + "issue_update", + "issue_update_custom_fields", + "issue_update_milestone", + "modify_project", + "pull_request_assign", + "pull_request_close", + "pull_request_comment", + "pull_request_create", + "pull_request_flag", + "pull_request_merge", + "pull_request_rebase", + "pull_request_subscribe", + "pull_request_update", + "tag_project", + "update_watch_status", + ], + ) + + @patch.dict( + "pagure.cli.admin._config", + {"ACLS": {"dummy_acls": "Dummy ACL for testing"}}, + ) + def test_update_acls_new_acls(self): + args = munch.Munch({"debug": True}) + with tests.capture_output() as output: + pagure.cli.admin.do_update_acls(args) + + output = output.getvalue() + self.assertEqual( + "ACLS in the database synced with the list in the configuration file\n", + output, + ) + acls = pagure.lib.query.get_acls(self.session) + self.assertEqual( + [a.name for a in acls], + [ + "commit", + "commit_flag", + "create_branch", + "create_project", + "dummy_acls", + "fork_project", + "generate_acls_project", + "internal_access", + "issue_assign", + "issue_change_status", + "issue_comment", + "issue_create", + "issue_subscribe", + "issue_update", + "issue_update_custom_fields", + "issue_update_milestone", + "modify_project", + "pull_request_assign", + "pull_request_close", + "pull_request_comment", + "pull_request_create", + "pull_request_flag", + "pull_request_merge", + "pull_request_rebase", + "pull_request_subscribe", + "pull_request_update", + "tag_project", + "update_watch_status", + ], + ) + + if __name__ == "__main__": unittest.main(verbosity=2)