From 9638fb73001c290cd9df9c5395a663bd23dc4fbd Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Oct 11 2018 08:10:20 +0000 Subject: Allow a specific list of users to create a project ignoring existing repo Signed-off-by: Patrick Uiterwijk --- diff --git a/doc/configuration.rst b/doc/configuration.rst index e9fb409..ef0f5e0 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -1073,6 +1073,14 @@ This could be used to assume responsibility of existing repositories. Defaults to: ``False``. +USERS_IGNORE_EXISTING_REPOS +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +List of users who can al create a project while ignoring existing repositories. + +Defaults to: ``[]``. + + LOCAL_SSH_KEY ~~~~~~~~~~~~~ diff --git a/pagure/default_config.py b/pagure/default_config.py index acb4f8e..db7c308 100644 --- a/pagure/default_config.py +++ b/pagure/default_config.py @@ -81,6 +81,9 @@ ALLOW_DELETE_BRANCH = True # Allow admins to ignore existing repos when creating a new project ALLOW_ADMIN_IGNORE_EXISTING_REPOS = False +# List of users that can ignore existing repos when creating a new project +USERS_IGNORE_EXISTING_REPOS = [] + # Enable / Disable having pagure manage the user's ssh keys LOCAL_SSH_KEY = True diff --git a/pagure/forms.py b/pagure/forms.py index 5e5ff3b..9160b4b 100644 --- a/pagure/forms.py +++ b/pagure/forms.py @@ -211,6 +211,9 @@ class ProjectForm(ProjectFormSimplified): if not ( is_admin() and pagure_config.get("ALLOW_ADMIN_IGNORE_EXISTING_REPOS") + ) and ( + flask.g.fas_user.username + not in pagure_config["USERS_IGNORE_EXISTING_REPOS"] ): self.ignore_existing_repos = None diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py index 8f3b4fa..8315a20 100644 --- a/tests/test_pagure_flask_ui_app.py +++ b/tests/test_pagure_flask_ui_app.py @@ -393,6 +393,64 @@ class PagureFlaskApptests(tests.Modeltests): output_text = output.get_data(as_text=True) self.assertIn("Alice Author", output_text) + @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': [], + 'USERS_IGNORE_EXISTING_REPOS': ['pingou']}) + def test_adopt_repos_non_admin(self): + """ Test the new_project endpoint with existing git repo for non-admins. """ + # Before + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 0) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + tests.add_content_git_repo(os.path.join(self.path, 'repos', 'test.git')) + + user = tests.FakeUser(username='pingou') + with tests.user_set(self.app.application, user): + data = { + 'csrf_token': self.get_csrf(), + 'name': 'test', + 'description': 'Project #1', + } + + output = self.app.post('/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn('The main repo test.git already exists', output_text) + + data['ignore_existing_repos'] = 'y' + output = self.app.post('/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn("Alice Author", output_text) + + @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': [], + 'USERS_IGNORE_EXISTING_REPOS': []}) + def test_adopt_repos_not_allowed(self): + """ Test the new_project endpoint with existing git repo for no access. """ + # Before + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 0) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + tests.add_content_git_repo(os.path.join(self.path, 'repos', 'test.git')) + + user = tests.FakeUser(username='pingou') + with tests.user_set(self.app.application, user): + data = { + 'csrf_token': self.get_csrf(), + 'name': 'test', + 'description': 'Project #1', + } + + output = self.app.post('/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn('The main repo test.git already exists', output_text) + + data['ignore_existing_repos'] = 'y' + output = self.app.post('/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn('The main repo test.git already exists', output_text) + @patch.dict('pagure.config.config', {'PROJECT_NAME_REGEX': '^1[a-z]*$'}) def test_new_project_diff_regex(self): """ Test the new_project endpoint with a different regex. """