From cdb351493ea81d00cd91ae4cc9a8db7b9d3bc71f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 17 2018 12:23:43 +0000 Subject: Add a configuration key to disable mirroring in projects Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/templates/new_project.html b/pagure/templates/new_project.html index ed638b7..4be5995 100644 --- a/pagure/templates/new_project.html +++ b/pagure/templates/new_project.html @@ -23,7 +23,9 @@ {% if config.get('PRIVATE_PROJECTS', False) %} {{ render_bootstrap_field(form.private, field_description="To mark the repo private") }} {% endif %} + {% if not config.get('DISABLE_MIRROR_IN', False) %} {{ render_bootstrap_field(form.mirrored_from, field_description="Mirror this project from another git server") }} + {% endif %} {{ render_bootstrap_field(form.create_readme, field_description="Create a README file automatically") }} {% if form.repospanner_region %} {{ render_bootstrap_field(form.repospanner_region, field_description="repoSpanner region to create the project in") }} @@ -66,6 +68,7 @@ $('#private').change(function(){ $('#namespace').removeAttr("disabled"); } }); +{% if not config.get('DISABLE_MIRROR_IN', False) %} function update_if_mirror() { if ($('#mirrored_from').val()){ $('#create_readme').attr("disabled", "disabled"); @@ -80,6 +83,7 @@ $('#mirrored_from').keyup(function(){ update_if_mirror(); }); update_if_mirror(); +{% endif %} {% endblock %} {% endif %} diff --git a/pagure/ui/app.py b/pagure/ui/app.py index 0a16d69..374fd2b 100644 --- a/pagure/ui/app.py +++ b/pagure/ui/app.py @@ -1042,6 +1042,12 @@ def new_project(): ignore_existing_repos = False mirrored_from = form.mirrored_from.data + if mirrored_from and pagure_config.get("DISABLE_MIRROR_IN", False): + flask.flash( + "Mirroring in projects has been disabled in this instance", + "error", + ) + return flask.render_template("new_project.html", form=form) try: task = pagure.lib.query.new_project( diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py index f9abf00..f3d9924 100644 --- a/tests/test_pagure_flask_ui_app.py +++ b/tests/test_pagure_flask_ui_app.py @@ -340,6 +340,10 @@ class PagureFlaskApptests(tests.Modeltests): with tests.user_set(self.app.application, user): output = self.app.get('/new/') self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn( + '', output_text) csrf_token = self.get_csrf(output=output) @@ -360,6 +364,39 @@ class PagureFlaskApptests(tests.Modeltests): '

This repo is brand new and meant to be mirrored from ' 'https://example.com/foo/bar.git !

', output_text) + @patch.dict('pagure.config.config', {'DISABLE_MIRROR_IN': True}) + def test_new_project_mirrored_mirror_disabled(self): + """ Test the new_project with a mirrored repo when that feature is + disabled. + """ + + user = tests.FakeUser(username='foo') + with tests.user_set(self.app.application, user): + output = self.app.get('/new/') + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertNotIn( + '', output_text) + + csrf_token = self.get_csrf(output=output) + + data = { + 'description': 'Project #1', + 'name': 'project-1', + 'mirrored_from': 'https://example.com/foo/bar.git', + 'csrf_token': csrf_token, + } + + 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( + 'New project - Pagure', output_text) + self.assertIn( + ' Mirroring in projects has been disabled in ' + 'this instance', output_text) + def test_new_project(self): """ Test the new_project endpoint. """ # Before