diff --git a/doc/configuration.rst b/doc/configuration.rst index 6af2ccc..1c3ec2e 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -948,6 +948,15 @@ instead of its default: case-insensitive. Defaults to: ``False`` +PROJECT_NAME_REGEX +~~~~~~~~~~~~~~~~~~ + +This configuration key allows to customize the regular expression used to +validate new project name. + +Defaults to: ``^[a-zA-z0-9_][a-zA-Z0-9-_]*$`` + + Deprecated configuration keys ----------------------------- diff --git a/pagure/forms.py b/pagure/forms.py index 2f35243..e784597 100644 --- a/pagure/forms.py +++ b/pagure/forms.py @@ -31,7 +31,6 @@ import pagure.lib STRICT_REGEX = '^[a-zA-Z0-9-_]+$' TAGS_REGEX = '^[a-zA-Z0-9-_, .]+$' -PROJECT_NAME_REGEX = '^[a-zA-z0-9_][a-zA-Z0-9-_]*$' FALSE_VALUES = ('false', '', False, 'False', 0, '0') @@ -149,10 +148,6 @@ class ProjectForm(ProjectFormSimplified): ''' Form to create or edit project. ''' name = wtforms.TextField( 'Project name *', - [ - wtforms.validators.Required(), - wtforms.validators.Regexp(PROJECT_NAME_REGEX, flags=re.IGNORECASE) - ] ) create_readme = wtforms.BooleanField( 'Create README', @@ -172,6 +167,14 @@ class ProjectForm(ProjectFormSimplified): drop-down list. """ super(ProjectForm, self).__init__(*args, **kwargs) + # set the name validator + regex = pagure.APP.config.get( + 'PROJECT_NAME_REGEX', '^[a-zA-z0-9_][a-zA-Z0-9-_]*$') + self.name.validators = [ + wtforms.validators.Required(), + wtforms.validators.Regexp(regex, flags=re.IGNORECASE) + ] + # Set the list of namespace if 'namespaces' in kwargs: self.namespace.choices = [ (namespace, namespace) for namespace in kwargs['namespaces'] diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py index c7adf7f..70faf68 100644 --- a/tests/test_pagure_flask_ui_app.py +++ b/tests/test_pagure_flask_ui_app.py @@ -315,6 +315,38 @@ class PagureFlaskApptests(tests.Modeltests): self.assertTrue(os.path.exists( os.path.join(self.path, 'requests', 'project-1.git'))) + @patch.dict('pagure.APP.config', {'PROJECT_NAME_REGEX': '^1[a-z]*$'}) + def test_new_project_diff_regex(self): + """ Test the new_project endpoint with a different regex. """ + # Before + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 0) + + user = tests.FakeUser(username='foo') + with tests.user_set(pagure.APP, user): + output = self.app.get('/new/') + self.assertEqual(output.status_code, 200) + self.assertIn( + u'Create new Project', output.data) + + csrf_token = self.get_csrf(output=output) + + data = { + 'description': 'Project #1', + 'name': 'project-1', + 'csrf_token': csrf_token, + } + + output = self.app.post('/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + u'New project - Pagure', output.data) + self.assertIn( + u'Create new Project', output.data) + self.assertIn( + u'\n Invalid input. \n' + u' ', output.data) + @patch.dict('pagure.APP.config', {'PRIVATE_PROJECTS': True}) def test_new_project_private(self): """ Test the new_project endpoint for a private project. """