From 1d882ff18a5d208ed20dfbf4ae72fc2438ab1d2f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:48 +0000 Subject: [PATCH 1/20] Uploading sources requires the authentication --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index c7d6007..c91d147 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -681,6 +681,7 @@ def view_tags(repo, username=None): @APP.route('//upload', methods=('GET', 'POST')) @APP.route('/fork///upload/', methods=('GET', 'POST')) @APP.route('/fork///upload', methods=('GET', 'POST')) +@cla_required def new_release(repo, username=None): """ Upload a new release. """ From 1c965f382e36d57b1090918453c676835c3fec00 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:48 +0000 Subject: [PATCH 2/20] Create the releases folder for all the tests --- diff --git a/tests/__init__.py b/tests/__init__.py index 78181ee..fdbd60f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -94,7 +94,8 @@ class Modeltests(unittest.TestCase): if filename.endswith('.git') and os.path.isdir(filename): shutil.rmtree(filename) - for folder in ['tickets', 'repos', 'forks', 'docs', 'requests']: + for folder in ['tickets', 'repos', 'forks', 'docs', + 'requests' ,'releases']: folder = os.path.join(HERE, folder) if os.path.exists(folder): shutil.rmtree(folder) From ff69ce90809f356faf500640aaf6056181abbb79 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:48 +0000 Subject: [PATCH 3/20] Add unit-tests for the new_release endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 96d7e1e..7c5e52e 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -50,6 +50,8 @@ class PagureFlaskRepotests(tests.Modeltests): tests.HERE, 'tickets') pagure.APP.config['DOCS_FOLDER'] = os.path.join( tests.HERE, 'docs') + pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join( + tests.HERE, 'releases') self.app = pagure.APP.test_client() @patch('pagure.ui.repo.admin_session_timedout') @@ -1844,6 +1846,53 @@ index 0000000..fb7093d '
  • Default branch updated to master
  • ' in output.data) + def test_new_release(self): + """ Test the new_release endpoint. """ + + output = self.app.post('/foo/upload/') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/foo/upload/') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.post('/test/upload/') + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + img = os.path.join(tests.HERE, 'placebo.png') + + # Missing CSRF Token + data = {'filestream': open(img)} + output = self.app.post('/test/upload/', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn('

    Upload a new release

    ', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Upload Ok but No git repo + data = {'filestream': open(img), 'csrf_token': csrf_token} + output = self.app.post( + '/test/upload/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + self.assertIn('
  • File uploaded
  • ', output.data) + self.assertIn('
    No git repo found
    ', output.data) + + repo = tests.create_projects_git(tests.HERE) + + # Upload successful + data = {'filestream': open(img), 'csrf_token': csrf_token} + output = self.app.post( + '/test/upload/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn('
  • File uploaded
  • ', output.data) + self.assertIn('This project has not been tagged.', output.data) + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskRepotests) From 27a40148b8c6ccc394a09a4fba2e71cbd12f78f0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:48 +0000 Subject: [PATCH 4/20] Adjust redirect, we do not have the repo object at this point (yet) --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index c91d147..ad2be98 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -812,7 +812,7 @@ def update_project(repo, username=None): if admin_session_timedout(): flask.flash('Action canceled, try it again', 'error') url = flask.url_for( - 'view_settings', username=username, repo=repo.name) + 'view_settings', username=username, repo=repo) return flask.redirect( flask.url_for('auth_login', next=url)) From e2fa89175e71902bc0f519bbc9900e0a9ec01155 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:48 +0000 Subject: [PATCH 5/20] Improve unit-test for the update_project endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 7c5e52e..b6b49b1 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -260,14 +260,22 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertTrue( '
  • User removed
  • ' in output.data) - def test_update_project(self): + @patch('pagure.ui.repo.admin_session_timedout') + def test_update_project(self, ast): """ Test the update_project endpoint. """ + ast.return_value = True + output = self.app.post('/foo/update') self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.post('/foo/update') + self.assertEqual(output.status_code, 302) + + ast.return_value = False + + output = self.app.post('/foo/update') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) From c62a10d643c637c89bee799a85896ec2da25b561 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 6/20] Improve unit-test for the change_ref_head endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index b6b49b1..f329f67 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -1772,7 +1772,7 @@ index 0000000..fb7093d @patch('pagure.ui.repo.admin_session_timedout') def test_change_ref_head(self,ast): """ Test the change_ref_head endpoint. """ - ast.return_value = False + ast.return_value = True output = self.app.post('/foo/default/branch/') self.assertEqual(output.status_code, 302) @@ -1780,6 +1780,11 @@ index 0000000..fb7093d user = tests.FakeUser() with tests.user_set(pagure.APP, user): output = self.app.post('/foo/default/branch/') + self.assertEqual(output.status_code, 302) + + ast.return_value = False + + output = self.app.post('/foo/default/branch/') self.assertEqual(output.status_code, 404) tests.create_projects(self.session) From cff1c929c2913b1b1d87305ed097e19855e34b1a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 7/20] Let's ignore exceptions we cannot reproduce in the unit-tests --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index ad2be98..68fc3e1 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -883,7 +883,7 @@ def change_ref_head(repo, username=None): reference = repo_obj.lookup_reference('refs/heads/%s'%branchname).resolve() repo_obj.set_head(reference.name) flask.flash('Default branch updated to %s'%branchname) - except Exception as err: + except Exception as err: # pragma: no cover APP.logger.exception(err) return flask.redirect(flask.url_for( From 728eb18e52ca8d6de284904ee45fd2ca3ab2f3dc Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 8/20] Improve the unit-tests for the add_user endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index f329f67..bfe9d99 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -75,6 +75,17 @@ class PagureFlaskRepotests(tests.Modeltests): ast.return_value = True output = self.app.get('/test/adduser') self.assertEqual(output.status_code, 302) + + # Redirect also happens for POST request + output = self.app.post('/test/adduser') + self.assertEqual(output.status_code, 302) + # Check the message flashed during the redirect + output = self.app.get('/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
  • Action canceled, try it again
  • ', + output.data) + ast.return_value = False user.username = 'pingou' From 7de9f5819dce54ecdfc820c12b8f41e7b9aaa36d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 9/20] Improve the unit-tests for the add_group_project endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index bfe9d99..cf621ac 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -148,6 +148,17 @@ class PagureFlaskRepotests(tests.Modeltests): ast.return_value = True output = self.app.get('/test/addgroup') self.assertEqual(output.status_code, 302) + + # Redirect also happens for POST request + output = self.app.post('/test/addgroup') + self.assertEqual(output.status_code, 302) + # Check the message flashed during the redirect + output = self.app.get('/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
  • Action canceled, try it again
  • ', + output.data) + ast.return_value = False msg = pagure.lib.add_group( From 8e3bbb15a694b21d285a1aed28d3af0a3f42bd90 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 10/20] Create an issue and a request so that the test for regenerate_git has something to work on --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index cf621ac..8d6b0fd 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -1603,6 +1603,19 @@ index 0000000..fb7093d output = self.app.post('/test/regenerate', data=data) self.assertEqual(output.status_code, 400) + # Create an issue to play with + repo = pagure.lib.get_project(self.session, 'test') + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue', + content='We should work on this', + user='pingou', + ticketfolder=None + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + data['regenerate'] = 'tickets' tests.create_projects_git(tests.HERE) output = self.app.post( @@ -1612,6 +1625,21 @@ index 0000000..fb7093d '
  • Tickets git repo updated
  • ', output.data) + # Create a request to play with + repo = pagure.lib.get_project(self.session, 'test') + msg = pagure.lib.new_pull_request( + session=self.session, + repo_from=repo, + branch_from='branch', + repo_to=repo, + branch_to='master', + title='Test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(msg.title, 'Test pull-request') + data['regenerate'] = 'requests' output = self.app.post( '/test/regenerate', data=data, follow_redirects=True) From 973fb9567e732743c709673f3a0ea358802eaa57 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 11/20] Put the default ACLs for the token in the database for the tests --- diff --git a/tests/__init__.py b/tests/__init__.py index fdbd60f..d2ab4e7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -101,7 +101,8 @@ class Modeltests(unittest.TestCase): shutil.rmtree(folder) os.mkdir(folder) - self.session = pagure.lib.model.create_tables(DB_PATH) + self.session = pagure.lib.model.create_tables( + DB_PATH, acls=pagure.APP.config.get('ACLS', {})) # Create a couple of users item = pagure.lib.model.User( From a26be41c618b1b8640f390b329ad0d2f3c4e7017 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 12/20] Add unit-tests for the add_token endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 8d6b0fd..24a26b9 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -1956,6 +1956,55 @@ index 0000000..fb7093d self.assertIn('
  • File uploaded
  • ', output.data) self.assertIn('This project has not been tagged.', output.data) + def test_add_token(self): + """ Test the add_token endpoint. """ + + output = self.app.get('/foo/token/new/') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/foo/token/new/') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/token/new/') + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/token/new/') + self.assertEqual(output.status_code, 200) + self.assertIn('

    Create a new token

    ', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Missing acls + data = {'csrf_token': csrf_token} + output = self.app.post('/test/token/new/', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn('

    Create a new token

    ', output.data) + + data = {'csrf_token': csrf_token, 'acls': ['issue_create']} + output = self.app.post( + '/test/token/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + self.assertIn('
  • Token created
  • ', output.data) + self.assertIn('
    No git repo found
    ', output.data) + + repo = tests.create_projects_git(tests.HERE) + + # Upload successful + data = {'csrf_token': csrf_token, 'acls': ['issue_create']} + output = self.app.post( + '/test/token/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn('
  • Token created
  • ', output.data) + self.assertIn('

    Settings

    ', output.data) + self.assertIn('Valid until:', output.data) + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskRepotests) From e1ff5c61fbe68bd50924522c2a6a94ca711a87b3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 13/20] Drop the call to create_acls and update the acl identifier for the new ones --- diff --git a/tests/__init__.py b/tests/__init__.py index d2ab4e7..fcfbf6a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -258,22 +258,6 @@ def create_tokens(session, user_id=1): session.commit() -def create_acls(session): - """ Create some acls for the tokens. """ - for acl in [ - 'issue_create', 'pull_request_merge', 'pull_request_comment', - 'issue_change_status', 'issue_comment', 'pull_request_close', - 'pull_request_flag', - ]: - item = pagure.lib.model.ACL( - name=acl, - description=acl.replace('_', ' '), - ) - session.add(item) - - session.commit() - - def create_tokens_acl(session, token_id='aaabbbcccddd'): """ Create some acls for the tokens. """ for aclid in range(7): diff --git a/tests/test_progit_flask_api_auth.py b/tests/test_progit_flask_api_auth.py index 2850459..ef76281 100644 --- a/tests/test_progit_flask_api_auth.py +++ b/tests/test_progit_flask_api_auth.py @@ -139,7 +139,6 @@ class PagureFlaskApiAuthtests(tests.Modeltests): """ tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) output = self.app.post('/api/0/test/new_issue') diff --git a/tests/test_progit_flask_api_fork.py b/tests/test_progit_flask_api_fork.py index 7047d18..b0bf0fb 100644 --- a/tests/test_progit_flask_api_fork.py +++ b/tests/test_progit_flask_api_fork.py @@ -51,7 +51,6 @@ class PagureFlaskApiForktests(tests.Modeltests): tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) # Create a pull-request @@ -167,7 +166,6 @@ class PagureFlaskApiForktests(tests.Modeltests): tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) # Create a pull-request @@ -286,7 +284,6 @@ class PagureFlaskApiForktests(tests.Modeltests): tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) # Create the pull-request to close @@ -357,7 +354,7 @@ class PagureFlaskApiForktests(tests.Modeltests): self.session.commit() item = pagure.lib.model.TokenAcl( token_id='foobar_token', - acl_id=6, + acl_id=2, ) self.session.add(item) self.session.commit() @@ -399,7 +396,6 @@ class PagureFlaskApiForktests(tests.Modeltests): tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) # Create the pull-request to close @@ -470,7 +466,7 @@ class PagureFlaskApiForktests(tests.Modeltests): self.session.commit() item = pagure.lib.model.TokenAcl( token_id='foobar_token', - acl_id=2, + acl_id=3, ) self.session.add(item) self.session.commit() @@ -510,7 +506,6 @@ class PagureFlaskApiForktests(tests.Modeltests): tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) headers = {'Authorization': 'token aaabbbcccddd'} @@ -625,7 +620,6 @@ class PagureFlaskApiForktests(tests.Modeltests): tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) headers = {'Authorization': 'token aaabbbcccddd'} diff --git a/tests/test_progit_flask_api_issue.py b/tests/test_progit_flask_api_issue.py index 0c50434..9eefc5e 100644 --- a/tests/test_progit_flask_api_issue.py +++ b/tests/test_progit_flask_api_issue.py @@ -49,7 +49,6 @@ class PagureFlaskApiIssuetests(tests.Modeltests): tests.create_projects(self.session) tests.create_projects_git(os.path.join(tests.HERE, 'tickets')) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) headers = {'Authorization': 'token aaabbbcccddd'} @@ -553,7 +552,6 @@ class PagureFlaskApiIssuetests(tests.Modeltests): tests.create_projects(self.session) tests.create_projects_git(os.path.join(tests.HERE, 'tickets')) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) headers = {'Authorization': 'token aaabbbcccddd'} @@ -633,7 +631,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests): # Give `change_status_issue` to this token item = pagure.lib.model.TokenAcl( token_id='pingou_foo', - acl_id=4, + acl_id=6, ) self.session.add(item) self.session.commit() @@ -736,7 +734,6 @@ class PagureFlaskApiIssuetests(tests.Modeltests): tests.create_projects(self.session) tests.create_tokens(self.session) - tests.create_acls(self.session) tests.create_tokens_acl(self.session) headers = {'Authorization': 'token aaabbbcccddd'} @@ -863,7 +860,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests): # Give `change_status_issue` to this token item = pagure.lib.model.TokenAcl( token_id='pingou_foo', - acl_id=5, + acl_id=1, ) self.session.add(item) self.session.commit() From 58ebb2f92cb04a30457d927ca741f19ca6b5402b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 14/20] Expand the add_token unit-test to check the behavior when the session times-out --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 24a26b9..7e366b0 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -1956,8 +1956,10 @@ index 0000000..fb7093d self.assertIn('
  • File uploaded
  • ', output.data) self.assertIn('This project has not been tagged.', output.data) - def test_add_token(self): + @patch('pagure.ui.repo.admin_session_timedout') + def test_add_token(self, ast): """ Test the add_token endpoint. """ + ast.return_value = False output = self.app.get('/foo/token/new/') self.assertEqual(output.status_code, 302) @@ -1980,9 +1982,20 @@ index 0000000..fb7093d csrf_token = output.data.split( 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + data = {'csrf_token': csrf_token} + + ast.return_value = True + # Test when the session timed-out + output = self.app.post('/test/token/new/', data=data) + self.assertEqual(output.status_code, 302) + output = self.app.get('/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
  • Action canceled, try it again
  • ', + output.data) + ast.return_value = False # Missing acls - data = {'csrf_token': csrf_token} output = self.app.post('/test/token/new/', data=data) self.assertEqual(output.status_code, 200) self.assertIn('

    Create a new token

    ', output.data) From 66632f7fb9794f464b2ff3b31c9ba3bcb6c93c6a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 15/20] Add unit-tests for the revoke_api_token endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 7e366b0..20ab180 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -11,6 +11,7 @@ __requires__ = ['SQLAlchemy >= 0.8'] import pkg_resources +import datetime import json import unittest import shutil @@ -2018,6 +2019,82 @@ index 0000000..fb7093d self.assertIn('

    Settings

    ', output.data) self.assertIn('Valid until:', output.data) + @patch('pagure.ui.repo.admin_session_timedout') + def test_revoke_api_token(self, ast): + """ Test the revoke_api_token endpoint. """ + ast.return_value = False + + output = self.app.post('/foo/token/revoke/123') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/foo/token/revoke/123') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.post('/test/token/revoke/123') + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/token/new') + self.assertEqual(output.status_code, 200) + self.assertIn('

    Create a new token

    ', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + data = {'csrf_token': csrf_token} + + ast.return_value = True + # Test when the session timed-out + output = self.app.post('/test/token/revoke/123', data=data) + self.assertEqual(output.status_code, 302) + output = self.app.get('/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
  • Action canceled, try it again
  • ', + output.data) + ast.return_value = False + + output = self.app.post('/test/token/revoke/123', data=data) + self.assertEqual(output.status_code, 404) + self.assertIn('
    Token not found
    ', output.data) + + # Create a token to revoke + repo = tests.create_projects_git(tests.HERE) + data = {'csrf_token': csrf_token, 'acls': ['issue_create']} + output = self.app.post( + '/test/token/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn('
  • Token created
  • ', output.data) + + # Existing token will expire in 60 days + repo = pagure.lib.get_project(self.session, 'test') + self.assertEqual( + repo.tokens[0].expiration.date(), + datetime.datetime.utcnow().date() + datetime.timedelta(days=60)) + + token = repo.tokens[0].id + output = self.app.post( + '/test/token/revoke/%s' % token, + data=data, + follow_redirects=True) + self.assertIn( + 'Settings - test - Pagure', output.data) + self.assertIn( + '
  • Token revoked
  • ', output.data) + + # Existing token has been expired + repo = pagure.lib.get_project(self.session, 'test') + self.assertEqual( + repo.tokens[0].expiration.date(), + repo.tokens[0].created.date()) + self.assertEqual( + repo.tokens[0].expiration.date(), + datetime.datetime.utcnow().date()) + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskRepotests) From 39a3dadc9134274e0d7d1c758247228505846a05 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 16/20] Block early if the pagure instance isn't running the doc server --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 68fc3e1..2cde267 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -1446,6 +1446,9 @@ def view_docs(repo, username=None, filename=None): if not repo_obj: flask.abort(404, 'Project not found') + if not APP.config.get('DOC_APP_URL'): + flask.abort(404, 'This pagure instance has no doc server') + return flask.render_template( 'docs.html', repo=repo_obj, From 24b5ea4039782762f5aea10f16684e69a696ab6a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 17/20] Add basic unit-tests for the view_docs endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 20ab180..7e18db6 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -2095,6 +2095,23 @@ index 0000000..fb7093d repo.tokens[0].expiration.date(), datetime.datetime.utcnow().date()) + def test_view_docs(self): + """ Test the view_docs endpoint. """ + output = self.app.get('/docs/foo/') + # No project registered in the DB + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/docs/test/') + # No git repo associated + self.assertEqual(output.status_code, 404) + + tests.create_projects_git(tests.HERE, bare=True) + + output = self.app.get('/docs/test/') + self.assertEqual(output.status_code, 404) + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskRepotests) From 0a9fcbcce7ac3b7e25223c6967fa216f4b6f3048 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 18/20] Add unit-tests for the delete_branch endpoint --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 7e18db6..479dacb 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -2095,6 +2095,55 @@ index 0000000..fb7093d repo.tokens[0].expiration.date(), datetime.datetime.utcnow().date()) + def test_delete_branch(self): + """ Test the delete_branch endpoint. """ + output = self.app.post('/foo/master/delete') + # No project registered in the DB + self.assertEqual(output.status_code, 302) + + tests.create_projects(self.session) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/foo/master/delete') + # Unknown repo + self.assertEqual(output.status_code, 404) + + output = self.app.post('/test/master/delete') + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post('/test/master/delete') + self.assertEqual(output.status_code, 403) + self.assertIn( + '

    You are not allowed to delete the master branch

    ', + output.data) + + tests.create_projects_git(tests.HERE, bare=True) + + output = self.app.post('/test/bar/delete') + self.assertEqual(output.status_code, 404) + self.assertIn('
    Branch no found
    ', output.data) + + # Add a branch that we can delete + path = os.path.join(tests.HERE, 'test.git') + tests.add_content_git_repo(path) + repo = pygit2.Repository(path) + repo.create_branch('foo', repo.head.get_object()) + + # Check before deletion + output = self.app.post('/test', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn('', output.data) + self.assertIn('', output.data) + + # Delete the branch + output = self.app.post('/test/foo/delete', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertNotIn('', output.data) + self.assertIn('', output.data) + def test_view_docs(self): """ Test the view_docs endpoint. """ output = self.app.get('/docs/foo/') From d4ef3db2b0826e9909982ede35ba439625093a06 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 19/20] Avoid sending emails during the tests --- diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 479dacb..7821659 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -1562,12 +1562,14 @@ index 0000000..fb7093d repo = pagure.lib.get_project(self.session, 'test') self.assertNotEqual(repo.hook_token, 'aaabbbccc') + @patch('pagure.lib.notify.send_email') @patch('pagure.ui.repo.admin_session_timedout') @patch('pagure.lib.git.update_git') - def test_regenerate_git(self, upgit, ast): + def test_regenerate_git(self, upgit, ast, sendmail): """ Test the regenerate_git endpoint. """ ast.return_value = False upgit.return_value = True + sendmail.return_value = True tests.create_projects(self.session) user = tests.FakeUser() From 564e632ef3aab9d342b08d7955995d0287182fec Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 14 2015 21:19:49 +0000 Subject: [PATCH 20/20] Globally prevent sending email in the tests --- diff --git a/tests/__init__.py b/tests/__init__.py index fcfbf6a..3359767 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -135,6 +135,9 @@ class Modeltests(unittest.TestCase): self.session.commit() + # Prevent unit-tests to send email, globally + pagure.APP.config['EMAIL_SEND'] = False + # pylint: disable=C0103 def tearDown(self): """ Remove the test.db database if there is one. """