#5034 Add an update-acls action to pagure-admin
Merged 3 years ago by pingou. Opened 3 years ago by pingou.

file modified
+37
@@ -31,6 +31,7 @@ 

  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 @@ 

      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 @@ 

      # set-default-branch

      _parser_set_default_branch(subparser)

  

+     # update-acls

+     _parser_update_acls(subparser)

+ 

      return parser.parse_args(args)

  

  
@@ -906,6 +927,22 @@ 

      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.

  

file modified
+111
@@ -2262,5 +2262,116 @@ 

          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)