From 5c0b166d949eddc312de70c493f0d26834f7ed90 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 15:48:10 +0000 Subject: [PATCH 1/10] Include the namespace in the callback URL to give to jenkins Otherwise, we end up not finding the project when jenkins calls us back that the build is finished. --- diff --git a/pagure/hooks/pagure_ci.py b/pagure/hooks/pagure_ci.py index 5ff4612..bd29740 100644 --- a/pagure/hooks/pagure_ci.py +++ b/pagure/hooks/pagure_ci.py @@ -74,7 +74,7 @@ The URL to be used to POST the results of your build is: {{ (config['APP_URL'][:-1] if config['APP_URL'].endswith('/') else config['APP_URL']) + url_for('api_ns.%s_ci_notification' % repo.ci_hook.ci_type, - repo=repo.name, username=username, + repo=repo.name, username=username, namespace=repo.namespace, pagure_ci_token=repo.ci_hook.pagure_ci_token) }} From fc5bca402295d627e5bef0de7182a42471968614 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 15:48:10 +0000 Subject: [PATCH 2/10] Fix removing the pagure-ci hook --- diff --git a/pagure/hooks/pagure_ci.py b/pagure/hooks/pagure_ci.py index bd29740..4279ba7 100644 --- a/pagure/hooks/pagure_ci.py +++ b/pagure/hooks/pagure_ci.py @@ -155,6 +155,5 @@ class PagureCi(BaseHook): ''' if project.ci_hook is not None: - for hook in project.ci_hook: - hook.pagure_ci_token = None + project.ci_hook.pagure_ci_token = None SESSION.commit() From 5bc2abdcdad34788e0c820711e6fd6eb12427b3d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 15:48:10 +0000 Subject: [PATCH 3/10] Use the fullname in the settings page --- diff --git a/pagure/templates/settings.html b/pagure/templates/settings.html index 1bc8fb7..89ed747 100644 --- a/pagure/templates/settings.html +++ b/pagure/templates/settings.html @@ -8,7 +8,7 @@ {% block repo %}
-

Settings for {{repo.name}}

+

Settings for {{ repo.fullname }}

From 7c90b51c45f2be288634de23aad984075bf2d36c Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 15:48:10 +0000 Subject: [PATCH 4/10] Ensure REDIS is always None when running the tests --- diff --git a/tests/__init__.py b/tests/__init__.py index 3f0782e..36811f4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -149,6 +149,8 @@ class Modeltests(unittest.TestCase): def setUp(self): # pylint: disable=invalid-name """ Set up the environnment, ran before every tests. """ # Clean up eventual git repo left in the present folder. + pagure.REDIS = None + pagure.lib.REDIS = None self.path = tempfile.mkdtemp(prefix='pagure-tests') for folder in ['tickets', 'repos', 'forks', 'docs', 'requests', 'releases', 'remotes']: From c684ebcc4489054a70916cdc75d0541d20da8eb3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 15:48:10 +0000 Subject: [PATCH 5/10] Ensure there are no handlers when running the tests on docs_server --- diff --git a/tests/test_pagure_flask_docs.py b/tests/test_pagure_flask_docs.py index d5e1731..bb2d419 100644 --- a/tests/test_pagure_flask_docs.py +++ b/tests/test_pagure_flask_docs.py @@ -37,6 +37,7 @@ class PagureFlaskDocstests(tests.Modeltests): """ Set up the environnment, ran before every tests. """ super(PagureFlaskDocstests, self).setUp() + pagure.docs_server.LOG.handlers = [] pagure.docs_server.APP.config['TESTING'] = True pagure.docs_server.SESSION = self.session From 984e2635f35183595632d94014c1489ffd36cc07 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 15:48:10 +0000 Subject: [PATCH 6/10] Drop the reload mechanism from the tests The mechanism relying on a configuration key to support or not old git commit URLs has been updated a while ago, so there is no need to keep on relying in this for the tests. --- diff --git a/tests/__init__.py b/tests/__init__.py index 36811f4..8800134 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -65,53 +65,6 @@ if os.environ.get('BUILD_ID')or os.environ.get('FAITOUT_URL'): # Remove the log handlers for the tests pagure.LOG.handlers = [] - -def reload_pagure(config_file=None): - ''' Reload the different component of the pagure app. - - You may want to use this for some tests that require a specific - configuration key to include or not a controller (for example the - old_commit endpoint that's included or not depending on a value in - the configuration file). - ''' - - # We need to reload pagure as otherwise the configuration file will - # not be taken into account - pagure.APP.view_functions = {} - if config_file: - os.environ['PAGURE_CONFIG'] = config_file - else: - if 'PAGURE_CONFIG' in os.environ: - del os.environ['PAGURE_CONFIG'] - - reload(pagure) - reload(pagure.lib) - reload(pagure.lib.model) - reload(pagure.hooks) - reload(pagure.hooks.mail) - reload(pagure.hooks.irc) - reload(pagure.hooks.fedmsg) - reload(pagure.hooks.pagure_force_commit) - reload(pagure.hooks.pagure_hook) - reload(pagure.hooks.pagure_request_hook) - reload(pagure.hooks.pagure_ticket_hook) - reload(pagure.hooks.pagure_ci) - reload(pagure.hooks.rtd) - reload(pagure.api) - reload(pagure.api.fork) - reload(pagure.api.issue) - reload(pagure.api.project) - reload(pagure.api.user) - reload(pagure.ui.admin) - reload(pagure.ui.app) - reload(pagure.ui.groups) - reload(pagure.ui.repo) - reload(pagure.ui.filters) - reload(pagure.ui.plugins) - reload(pagure.ui.issues) - reload(pagure.ui.fork) - - @contextmanager def user_set(APP, user): """ Set the provided user as fas_user in the provided application.""" diff --git a/tests/test_config b/tests/test_config index cac6d7d..d52090c 100644 --- a/tests/test_config +++ b/tests/test_config @@ -1 +1 @@ -OLD_VIEW_COMMIT_ENABLED = True +PAGURE_CI_SERVICES = ['jenkins'] diff --git a/tests/test_zzz_pagure_flask_ui_old_commit.py b/tests/test_zzz_pagure_flask_ui_old_commit.py index 18c7bc8..464ae8a 100644 --- a/tests/test_zzz_pagure_flask_ui_old_commit.py +++ b/tests/test_zzz_pagure_flask_ui_old_commit.py @@ -25,11 +25,6 @@ from mock import patch sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) -HERE = os.path.join(os.path.dirname(os.path.abspath(__file__))) -CONFIG = os.path.join(HERE, 'test_config') - -os.environ['PAGURE_CONFIG'] = CONFIG - import pagure.lib import tests from pagure.lib.repo import PagureRepo @@ -42,8 +37,6 @@ class PagureFlaskRepoOldUrltests(tests.Modeltests): """ Set up the environnment, ran before every tests. """ super(PagureFlaskRepoOldUrltests, self).setUp() - tests.reload_pagure(CONFIG) - pagure.APP.config['TESTING'] = True pagure.SESSION = self.session pagure.ui.SESSION = self.session @@ -68,8 +61,6 @@ class PagureFlaskRepoOldUrltests(tests.Modeltests): """ Tear down the environnment, after every tests. """ super(PagureFlaskRepoOldUrltests, self).tearDown() - tests.reload_pagure(CONFIG) - pagure.APP.config['EMAIL_SEND'] = False pagure.LOG.handlers = [] From 7f3b76c7e5f069c9ea6635ceff39768e4b5601c0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 15:48:10 +0000 Subject: [PATCH 7/10] Improve the unit-tests around pagure-ci hook --- diff --git a/runtests.sh b/runtests.sh index eb8063b..3ceec59 100755 --- a/runtests.sh +++ b/runtests.sh @@ -1,3 +1,5 @@ #!/bin/bash -PYTHONPATH=pagure ./nosetests --with-coverage --cover-erase --cover-package=pagure $* +PAGURE_CONFIG=`pwd`/tests/test_config \ +PYTHONPATH=pagure \ +./nosetests --with-coverage --cover-erase --cover-package=pagure $* diff --git a/tests/test_pagure_flask_docs.py b/tests/test_pagure_flask_docs.py index bb2d419..de232f8 100644 --- a/tests/test_pagure_flask_docs.py +++ b/tests/test_pagure_flask_docs.py @@ -237,7 +237,7 @@ class PagureFlaskDocstests(tests.Modeltests): self.assertEqual(output.status_code, 500) output = self.app.get('/test/folder1') - self.assertEqual(output.status_code, 500) + self.assertEqual(output.status_code, 200) if __name__ == '__main__': diff --git a/tests/test_pagure_flask_ui_plugins_pagure_ci.py b/tests/test_pagure_flask_ui_plugins_pagure_ci.py index b7589fa..e063420 100644 --- a/tests/test_pagure_flask_ui_plugins_pagure_ci.py +++ b/tests/test_pagure_flask_ui_plugins_pagure_ci.py @@ -12,6 +12,11 @@ import os import pygit2 from mock import patch +# Insert the PAGURE_CONFIG env variable before we do the imports +HERE = os.path.join(os.path.dirname(os.path.abspath(__file__))) +CONFIG = os.path.join(HERE, 'test_config') +os.environ['PAGURE_CONFIG'] = CONFIG + sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) @@ -103,9 +108,6 @@ class PagureFlaskPluginPagureCItests(tests.Modeltests): data['csrf_token'] = csrf_token - if not pagure.APP.config.get('PAGURE_CI_SERVICES'): - return - # Activate hook output = self.app.post( '/test/settings/Pagure CI', data=data, follow_redirects=True) @@ -132,6 +134,9 @@ class PagureFlaskPluginPagureCItests(tests.Modeltests): self.assertTrue( '' in output.data) + self.assertIn( + '
\nhttps://pagure.org/api/0/ci/jenkins/test/',
+                output.data)
 
             # De-activate the hook
             data = {
@@ -180,6 +185,68 @@ class PagureFlaskPluginPagureCItests(tests.Modeltests):
                 '', output.data)
 
+    def test_plugin_pagure_ci_namespaced(self):
+        """ Test the pagure ci plugin on/off endpoint. """
+
+        tests.create_projects(self.session)
+        tests.create_projects_git(self.path)
+
+        user = tests.FakeUser(username='pingou')
+        with tests.user_set(pagure.APP, user):
+            output = self.app.get('/somenamespace/test3/settings/Pagure CI')
+            self.assertEqual(output.status_code, 200)
+            self.assertIn(
+                '
\n' + 'namespaced test project
', output.data) + self.assertTrue('

Pagure CI settings

' in output.data) + self.assertIn( + '' , output.data) + self.assertIn( + '', + output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Activate hook + data = { + 'active': 'y', + 'ci_url': 'https://jenkins.fedoraproject.org', + 'ci_type': 'jenkins', + 'csrf_token': csrf_token, + } + + # Activate hook + output = self.app.post( + '/somenamespace/test3/settings/Pagure CI', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
\n' + 'namespaced test project
', output.data) + self.assertIn( + 'Settings - somenamespace/test3 - Pagure', output.data) + self.assertIn('

Settings for somenamespace/test3

', output.data) + self.assertIn( + '\n Hook Pagure CI activated', + output.data) + + output = self.app.get('/somenamespace/test3/settings/Pagure CI') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
\n' + 'namespaced test project
', output.data) + self.assertTrue('

Pagure CI settings

' in output.data) + self.assertIn( + '' , output.data) + self.assertTrue( + '' + in output.data) + self.assertIn( + '
\nhttps://pagure.org/api/0/ci/jenkins/somenamespace/test3/',
+                output.data)
+
 
 if __name__ == '__main__':
     SUITE = unittest.TestLoader().loadTestsFromTestCase(

From bfc8ce03235b01fdc563a2bee0cece01342e84cc Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon 
Date: Feb 20 2017 15:48:10 +0000
Subject: [PATCH 8/10] Adjust the run_ci_tests to use the expected config file


---

diff --git a/run_ci_tests.sh b/run_ci_tests.sh
index cfb7cc3..b8c68b0 100755
--- a/run_ci_tests.sh
+++ b/run_ci_tests.sh
@@ -47,7 +47,9 @@ hash -r
 
 python setup.py build
 
-PYTHONPATH=pagure ./nosetests -v --with-xcoverage --cover-erase --cover-package=pagure
+PAGURE_CONFIG=`pwd`/tests/test_config \
+PYTHONPATH=pagure \
+./nosetests -v --with-xcoverage --cover-erase --cover-package=pagure
 
 if [ "$?" = "0" ]; then
 

From 2ad5a8d3a6b0e008767d151d1107f7251169dc13 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon 
Date: Feb 20 2017 15:48:10 +0000
Subject: [PATCH 9/10] Rename the test_zzz_pagure_flask_ui_old_commit to test_pagure_flask_ui_old_commit


Since we do not need to reload the entire application to run the tests
there is no need to have this test ran at the end, we can just run it
as we do for all the others

---

diff --git a/tests/test_pagure_flask_ui_old_commit.py b/tests/test_pagure_flask_ui_old_commit.py
new file mode 100644
index 0000000..464ae8a
--- /dev/null
+++ b/tests/test_pagure_flask_ui_old_commit.py
@@ -0,0 +1,220 @@
+# -*- coding: utf-8 -*-
+
+"""
+ (c) 2016 - Copyright Red Hat Inc
+
+ Authors:
+   Pierre-Yves Chibon 
+
+"""
+
+__requires__ = ['SQLAlchemy >= 0.8']
+import pkg_resources
+
+import datetime
+import json
+import unittest
+import shutil
+import sys
+import tempfile
+import os
+
+import pygit2
+from mock import patch
+
+sys.path.insert(0, os.path.join(os.path.dirname(
+    os.path.abspath(__file__)), '..'))
+
+import pagure.lib
+import tests
+from pagure.lib.repo import PagureRepo
+
+
+class PagureFlaskRepoOldUrltests(tests.Modeltests):
+    """ Tests for flask app controller of pagure """
+
+    def setUp(self):
+        """ Set up the environnment, ran before every tests. """
+        super(PagureFlaskRepoOldUrltests, self).setUp()
+
+        pagure.APP.config['TESTING'] = True
+        pagure.SESSION = self.session
+        pagure.ui.SESSION = self.session
+        pagure.ui.app.SESSION = self.session
+        pagure.ui.filters.SESSION = self.session
+        pagure.ui.repo.SESSION = self.session
+
+        pagure.APP.config['OLD_VIEW_COMMIT_ENABLED'] = True
+        pagure.APP.config['EMAIL_SEND'] = False
+        pagure.APP.config['GIT_FOLDER'] = self.path
+        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
+            self.path, 'requests')
+        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
+            self.path, 'tickets')
+        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
+            self.path, 'docs')
+        pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join(
+            self.path, 'releases')
+        self.app = pagure.APP.test_client()
+
+    def tearDown(self):
+        """ Tear down the environnment, after every tests. """
+        super(PagureFlaskRepoOldUrltests, self).tearDown()
+
+        pagure.APP.config['EMAIL_SEND'] = False
+        pagure.LOG.handlers = []
+
+    def test_view_commit_old(self):
+        """ Test the view_commit_old endpoint. """
+
+        tests.create_projects(self.session)
+        tests.create_projects_git(self.path, bare=True)
+
+        # Add a README to the git repo - First commit
+        tests.add_readme_git_repo(os.path.join(self.path, 'test.git'))
+        repo = pygit2.Repository(os.path.join(self.path, 'test.git'))
+        commit = repo.revparse_single('HEAD')
+
+        # View first commit
+        output = self.app.get('/test/%s' % commit.oid.hex)
+        self.assertEqual(output.status_code, 302)
+
+        output = self.app.get(
+            '/test/%s' % commit.oid.hex, follow_redirects=True)
+        self.assertEqual(output.status_code, 200)
+        self.assertTrue(
+            '