From 8b2c5ccda349b9abd7a29e7438a11ea40eb701db Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 19 2018 14:59:13 +0000 Subject: Ensure the DOCS_FOLDER and TICKETS_FOLDER really are optional Add corresponding unit-tests ensuring that the corresponding repositories don't get created if TICKETS or DOCS are disabled. Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/api/project.py b/pagure/api/project.py index 5e4e9c6..b5bba89 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -853,8 +853,8 @@ def api_new_project(): blacklist=pagure_config['BLACKLISTED_PROJECTS'], allowed_prefix=pagure_config['ALLOWED_PREFIX'], gitfolder=pagure_config['GIT_FOLDER'], - docfolder=pagure_config['DOCS_FOLDER'], - ticketfolder=pagure_config['TICKETS_FOLDER'], + docfolder=pagure_config.get('DOCS_FOLDER'), + ticketfolder=pagure_config.get('TICKETS_FOLDER'), requestfolder=pagure_config['REQUESTS_FOLDER'], add_readme=create_readme, userobj=user, @@ -1108,8 +1108,8 @@ def api_fork_project(): user=flask.g.fas_user.username, repo=repo, gitfolder=pagure_config['GIT_FOLDER'], - docfolder=pagure_config['DOCS_FOLDER'], - ticketfolder=pagure_config['TICKETS_FOLDER'], + docfolder=pagure_config.get('DOCS_FOLDER'), + ticketfolder=pagure_config.get('TICKETS_FOLDER'), requestfolder=pagure_config['REQUESTS_FOLDER'], ) flask.g.session.commit() diff --git a/pagure/lib/git.py b/pagure/lib/git.py index bf893ec..b15e8ee 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -397,8 +397,8 @@ def get_project_from_json(session, jsondata): session=session, repo=parent, gitfolder=pagure_config['GIT_FOLDER'], - docfolder=pagure_config['DOCS_FOLDER'], - ticketfolder=pagure_config['TICKETS_FOLDER'], + docfolder=pagure_config.get('DOCS_FOLDER'), + ticketfolder=pagure_config.get('TICKETS_FOLDER'), requestfolder=pagure_config['REQUESTS_FOLDER'], user=user.username) @@ -416,8 +416,8 @@ def get_project_from_json(session, jsondata): blacklist=pagure_config.get('BLACKLISTED_PROJECTS', []), allowed_prefix=pagure_config.get('ALLOWED_PREFIX', []), gitfolder=gitfolder, - docfolder=pagure_config['DOCS_FOLDER'], - ticketfolder=pagure_config['TICKETS_FOLDER'], + docfolder=pagure_config.get('DOCS_FOLDER'), + ticketfolder=pagure_config.get('TICKETS_FOLDER'), requestfolder=pagure_config['REQUESTS_FOLDER'], prevent_40_chars=pagure_config.get( 'OLD_VIEW_COMMIT_ENABLED', False), diff --git a/pagure/lib/tasks.py b/pagure/lib/tasks.py index 81bed6b..ce70af4 100644 --- a/pagure/lib/tasks.py +++ b/pagure/lib/tasks.py @@ -292,7 +292,7 @@ def create_project(self, session, username, namespace, name, add_readme, with open(http_clone_file, 'w') as stream: pass - if pagure_config['DOCS_FOLDER']: + if pagure_config.get('DOCS_FOLDER'): docrepo = os.path.join( pagure_config['DOCS_FOLDER'], project.path) if os.path.exists(docrepo): @@ -305,7 +305,7 @@ def create_project(self, session, username, namespace, name, add_readme, else: pygit2.init_repository(docrepo, bare=True) - if pagure_config['TICKETS_FOLDER']: + if pagure_config.get('TICKETS_FOLDER'): ticketrepo = os.path.join( pagure_config['TICKETS_FOLDER'], project.path) if os.path.exists(ticketrepo): diff --git a/pagure/ui/app.py b/pagure/ui/app.py index 2b4a65c..f8d3476 100644 --- a/pagure/ui/app.py +++ b/pagure/ui/app.py @@ -525,8 +525,8 @@ def new_project(): blacklist=pagure_config['BLACKLISTED_PROJECTS'], allowed_prefix=pagure_config['ALLOWED_PREFIX'], gitfolder=pagure_config['GIT_FOLDER'], - docfolder=pagure_config['DOCS_FOLDER'], - ticketfolder=pagure_config['TICKETS_FOLDER'], + docfolder=pagure_config.get('DOCS_FOLDER'), + ticketfolder=pagure_config.get('TICKETS_FOLDER'), requestfolder=pagure_config['REQUESTS_FOLDER'], add_readme=create_readme, userobj=user, diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index 06bdd02..63a112f 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -1039,8 +1039,8 @@ def fork_project(repo, username=None, namespace=None): session=flask.g.session, repo=repo, gitfolder=pagure_config['GIT_FOLDER'], - docfolder=pagure_config['DOCS_FOLDER'], - ticketfolder=pagure_config['TICKETS_FOLDER'], + docfolder=pagure_config.get('DOCS_FOLDER'), + ticketfolder=pagure_config.get('TICKETS_FOLDER'), requestfolder=pagure_config['REQUESTS_FOLDER'], user=flask.g.fas_user.username) diff --git a/tests/__init__.py b/tests/__init__.py index e211653..b79b5ad 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -262,12 +262,21 @@ class SimplePagureTest(unittest.TestCase): perfrepo.reset_stats() perfrepo.REQUESTS = [] + def _set_db_path(self): + if DB_PATH: + self.dbpath = DB_PATH + _create_db_entities(self.dbpath) + else: + self.dbpath = 'sqlite:///%s' % os.path.join( + self.path, 'db.sqlite') + shutil.copyfile(dbfile, self.dbpath[len('sqlite://'):]) + def setUp(self): self.perfReset() - if self.path is not None: - raise Exception('Double init?!') - self.path = tempfile.mkdtemp(prefix='pagure-tests-path-') + if not self.path: + self.path = tempfile.mkdtemp(prefix='pagure-tests-path-') + LOG.debug('Testdir: %s', self.path) for folder in ['repos', 'forks', 'releases', 'remotes', 'attachments']: os.mkdir(os.path.join(self.path, folder)) @@ -279,28 +288,20 @@ class SimplePagureTest(unittest.TestCase): pagure.lib.REDIS.connection_pool.disconnect() pagure.lib.REDIS = None - if DB_PATH: - self.dbpath = DB_PATH - _create_db_entities(self.dbpath) - else: - self.dbpath = 'sqlite:///%s' % os.path.join( - self.path, 'db.sqlite') - shutil.copyfile(dbfile, self.dbpath[len('sqlite://'):]) + self._set_db_path() # Write a config file config_values = {'path': self.path, 'dburl': self.dbpath} - with open(os.path.join(self.path, 'config'), 'w') as f: - f.write(CONFIG_TEMPLATE % config_values) + config_path = os.path.join(self.path, 'config') + if not os.path.exists(config_path): + with open(config_path, 'w') as f: + f.write(CONFIG_TEMPLATE % config_values) # Prevent unit-tests to send email, globally pagure_config['EMAIL_SEND'] = False pagure_config['TESTING'] = True pagure_config['GIT_FOLDER'] = gf = os.path.join( self.path, 'repos') - pagure_config['TICKETS_FOLDER'] = os.path.join( - gf, 'tickets') - pagure_config['DOCS_FOLDER'] = os.path.join( - gf, 'docs') pagure_config['REQUESTS_FOLDER'] = os.path.join( gf, 'requests') pagure_config['ATTACHMENTS_FOLDER'] = os.path.join( @@ -395,6 +396,7 @@ class Modeltests(SimplePagureTest): self.worker.poll() if self.worker.returncode is not None: raise Exception('Worker failed to start') + # We could do the ping below in-process: # pagure.lib.tasks.conn.control.ping(timeout=0.1) # but if we try it, Python starts raising OSError diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py index d346d78..cf52446 100644 --- a/tests/test_pagure_flask_ui_app.py +++ b/tests/test_pagure_flask_ui_app.py @@ -15,6 +15,7 @@ import datetime import unittest import shutil import sys +import tempfile import os import six @@ -1669,5 +1670,149 @@ class PagureFlaskApptests(tests.Modeltests): self.assertEqual(output.status_code, 302) +class PagureFlaskAppNoDocstests(tests.Modeltests): + """ Tests for flask app controller of pagure """ + + def setUp(self): + pagure.config.config['DOCS_FOLDER'] = None + pagure.config.config['ENABLE_DOCS'] = False + + self.path = tempfile.mkdtemp(prefix='pagure-tests-path-') + + self._set_db_path() + + config_path = os.path.join(self.path, 'config') + config_values = {'path': self.path, 'dburl': self.dbpath} + config_content = tests.CONFIG_TEMPLATE % config_values + config_content += 'DOCS_FOLDER = None\nENABLE_DOCS = False' + + with open(config_path, 'w') as f: + f.write(config_content) + + super(PagureFlaskAppNoDocstests, self).setUp() + + def tearDown(self): + pagure.config.config['ENABLE_DOCS'] = True + + @patch.dict('pagure.config.config', {'DOCS_FOLDER': None}) + def test_new_project_no_docs_folder(self): + """ Test the new_project endpoint with DOCS_FOLDER is None. """ + # Before + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 0) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'tickets', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'docs', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'requests', 'project#1.git'))) + + user = tests.FakeUser(username='foo') + with tests.user_set(self.app.application, user): + + csrf_token = self.get_csrf() + + 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'
\nProject #1
', + output.data) + self.assertIn(u'

This repo is brand new!

', output.data) + self.assertIn( + u'Overview - project-1 - Pagure', output.data) + + # After + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 1) + self.assertTrue(os.path.exists( + os.path.join(self.path, 'repos', 'project-1.git'))) + self.assertTrue(os.path.exists( + os.path.join(self.path, 'repos', 'tickets', 'project-1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'docs', 'project-1.git'))) + self.assertTrue(os.path.exists( + os.path.join(self.path, 'repos', 'requests', 'project-1.git'))) + + +class PagureFlaskAppNoTicketstests(tests.Modeltests): + """ Tests for flask app controller of pagure """ + + def setUp(self): + pagure.config.config['TICKETS_FOLDER'] = None + pagure.config.config['ENABLE_TICKETS'] = False + + self.path = tempfile.mkdtemp(prefix='pagure-tests-path-') + + self._set_db_path() + + config_path = os.path.join(self.path, 'config') + config_values = {'path': self.path, 'dburl': self.dbpath} + config_content = tests.CONFIG_TEMPLATE % config_values + config_content += 'TICKETS_FOLDER = None\nENABLE_TICKETS = False' + + with open(config_path, 'w') as f: + f.write(config_content) + + super(PagureFlaskAppNoTicketstests, self).setUp() + + def tearDown(self): + pagure.config.config['ENABLE_TICKETS'] = True + + @patch.dict('pagure.config.config', {'TICKETS_FOLDER': None}) + def test_new_project_no_tickets_folder(self): + """ Test the new_project endpoint with DOCS_FOLDER is None. """ + # Before + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 0) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'tickets', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'docs', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'requests', 'project#1.git'))) + + user = tests.FakeUser(username='foo') + with tests.user_set(self.app.application, user): + + csrf_token = self.get_csrf() + + 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'
\nProject #1
', + output.data) + self.assertIn(u'

This repo is brand new!

', output.data) + self.assertIn( + u'Overview - project-1 - Pagure', output.data) + + # After + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 1) + self.assertTrue(os.path.exists( + os.path.join(self.path, 'repos', 'project-1.git'))) + self.assertFalse(os.path.exists( + os.path.join(self.path, 'repos', 'tickets', 'project-1.git'))) + self.assertTrue(os.path.exists( + os.path.join(self.path, 'repos', 'docs', 'project-1.git'))) + self.assertTrue(os.path.exists( + os.path.join(self.path, 'repos', 'requests', 'project-1.git'))) + + if __name__ == '__main__': unittest.main(verbosity=2)