From 1cb0526fd963acc8e7f51dbf2e466224e17910b7 Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Jan 18 2016 11:09:09 +0000 Subject: Rename tests from progit to pagure Signed-off-by: Patrick Uiterwijk --- diff --git a/tests/test_pagure_flask_api.py b/tests/test_pagure_flask_api.py new file mode 100644 index 0000000..6432d07 --- /dev/null +++ b/tests/test_pagure_flask_api.py @@ -0,0 +1,165 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import unittest +import shutil +import sys +import os + +import json +from mock import patch + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure.lib +import tests + + +class PagureFlaskApitests(tests.Modeltests): + """ Tests for flask API controller of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApitests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.api.SESSION = self.session + self.app = pagure.APP.test_client() + + def test_api_version(self): + """ Test the api_version function. """ + + output = self.app.get('/api/0/version') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(data['version'], pagure.__api_version__) + self.assertEqual(data.keys(), ['version']) + + def test_api_users(self): + """ Test the api_users function. """ + + output = self.app.get('/api/0/users') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(sorted(data['users']), ['foo', 'pingou']) + self.assertEqual(sorted(data.keys()), ['total_users', 'users']) + self.assertEqual(data['total_users'], 2) + + output = self.app.get('/api/0/users?pattern=p') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(data['users'], ['pingou']) + self.assertEqual(sorted(data.keys()), ['total_users', 'users']) + self.assertEqual(data['total_users'], 1) + + def test_api_project_tags(self): + """ Test the api_project_tags function. """ + tests.create_projects(self.session) + + output = self.app.get('/api/0/foo/tags/') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertEqual(data.keys(), ['output', 'error']) + self.assertEqual(data['output'], 'notok') + self.assertEqual(data['error'], 'Project not found') + + output = self.app.get('/api/0/test/tags/') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(sorted(data.keys()), ['tags', 'total_tags']) + self.assertEqual(data['tags'], []) + self.assertEqual(data['total_tags'], 0) + + # Add an issue and tag it so that we can list them + item = pagure.lib.model.Issue( + id=1, + uid='foobar', + project_id=1, + title='issue', + content='a bug report', + user_id=1, # pingou + ) + self.session.add(item) + self.session.commit() + item = pagure.lib.model.Tag( + tag='tag1', + ) + self.session.add(item) + self.session.commit() + item = pagure.lib.model.TagIssue( + tag='tag1', + issue_uid='foobar', + ) + self.session.add(item) + self.session.commit() + + output = self.app.get('/api/0/test/tags/') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(sorted(data.keys()), ['tags', 'total_tags']) + self.assertEqual(data['tags'], ['tag1']) + self.assertEqual(data['total_tags'], 1) + + output = self.app.get('/api/0/test/tags/?pattern=t') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(sorted(data.keys()), ['tags', 'total_tags']) + self.assertEqual(data['tags'], ['tag1']) + self.assertEqual(data['total_tags'], 1) + + output = self.app.get('/api/0/test/tags/?pattern=p') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(sorted(data.keys()), ['tags', 'total_tags']) + self.assertEqual(data['tags'], []) + self.assertEqual(data['total_tags'], 0) + + def test_api_groups(self): + """ Test the api_groups function. """ + + # Add a couple of groups so that we can list them + item = pagure.lib.model.PagureGroup( + group_name='group1', + group_type='user', + user_id=1, # pingou + ) + self.session.add(item) + + item = pagure.lib.model.PagureGroup( + group_name='rel-eng', + group_type='user', + user_id=1, # pingou + ) + self.session.add(item) + self.session.commit() + + output = self.app.get('/api/0/groups') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(data['groups'], ['group1', 'rel-eng']) + self.assertEqual(sorted(data.keys()), ['groups', 'total_groups']) + self.assertEqual(data['total_groups'], 2) + + output = self.app.get('/api/0/groups?pattern=re') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertEqual(data['groups'], ['rel-eng']) + self.assertEqual(sorted(data.keys()), ['groups', 'total_groups']) + self.assertEqual(data['total_groups'], 1) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskApitests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_api_auth.py b/tests/test_pagure_flask_api_auth.py new file mode 100644 index 0000000..ef76281 --- /dev/null +++ b/tests/test_pagure_flask_api_auth.py @@ -0,0 +1,173 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import unittest +import shutil +import sys +import os + +import json +from mock import patch + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure.lib +import tests + + +class PagureFlaskApiAuthtests(tests.Modeltests): + """ Tests for the authentication in the flask API of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApiAuthtests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.api.SESSION = self.session + pagure.api.issue.SESSION = self.session + pagure.lib.SESSION = self.session + self.app = pagure.APP.test_client() + + def test_auth_no_data(self): + """ Test the authentication when there is nothing in the database. + """ + + output = self.app.post('/api/0/foo/new_issue') + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + headers = {'Authorization': 'token aabbbccc'} + + output = self.app.post('/api/0/foo/new_issue', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + def test_auth_noacl(self): + """ Test the authentication when the token does not have any ACL. + """ + tests.create_projects(self.session) + tests.create_tokens(self.session) + + output = self.app.post('/api/0/test/new_issue') + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + output = self.app.post('/api/0/test/new_issue', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + def test_auth_expired(self): + """ Test the authentication when the token has expired. + """ + tests.create_projects(self.session) + tests.create_tokens(self.session) + + output = self.app.post('/api/0/test/new_issue') + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + headers = {'Authorization': 'token expired_token'} + + output = self.app.post('/api/0/test/new_issue', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + def test_auth(self): + """ Test the token based authentication. + """ + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + output = self.app.post('/api/0/test/new_issue') + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + output = self.app.post('/api/0/test/new_issue', headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase( + PagureFlaskApiAuthtests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_api_fork.py b/tests/test_pagure_flask_api_fork.py new file mode 100644 index 0000000..d4cee86 --- /dev/null +++ b/tests/test_pagure_flask_api_fork.py @@ -0,0 +1,776 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import datetime +import unittest +import shutil +import sys +import os + +import json +from mock import patch + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure.lib +import tests + + +class PagureFlaskApiForktests(tests.Modeltests): + """ Tests for the flask API of pagure for issue """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApiForktests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.api.SESSION = self.session + pagure.api.fork.SESSION = self.session + pagure.lib.SESSION = self.session + + pagure.APP.config['REQUESTS_FOLDER'] = None + + self.app = pagure.APP.test_client() + + @patch('pagure.lib.notify.send_email') + def test_api_pull_request_views(self, send_email): + """ Test the api_pull_request_views method of the flask api. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + # Create a pull-request + repo = pagure.lib.get_project(self.session, 'test') + forked_repo = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=forked_repo, + branch_from='master', + repo_to=repo, + branch_to='master', + title='test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'test pull-request') + + # Invalid repo + output = self.app.get('/api/0/foo/pull-requests') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # List pull-requests + output = self.app.get('/api/0/test/pull-requests') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['requests'][0]['date_created'] = '1431414800' + data['requests'][0]['updated_on'] = '1431414800' + data['requests'][0]['project']['date_created'] = '1431414800' + data['requests'][0]['repo_from']['date_created'] = '1431414800' + data['requests'][0]['uid'] = '1431414800' + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + "status": True + }, + "total_requests": 1, + "requests": [ + { + "assignee": None, + "branch": "master", + "branch_from": "master", + "closed_at": None, + "closed_by": None, + "comments": [], + "commit_start": None, + "commit_stop": None, + "date_created": "1431414800", + "id": 1, + "project": { + "date_created": "1431414800", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": None, + "tags": [], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "remote_git": None, + "repo_from": { + "date_created": "1431414800", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": None, + "tags": [], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "status": 'Open', + "title": "test pull-request", + "uid": "1431414800", + "updated_on": "1431414800", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Access Pull-Request authenticated + output = self.app.get('/api/0/test/pull-requests', headers=headers) + self.assertEqual(output.status_code, 200) + data2 = json.loads(output.data) + data2['requests'][0]['date_created'] = '1431414800' + data2['requests'][0]['updated_on'] = '1431414800' + data2['requests'][0]['project']['date_created'] = '1431414800' + data2['requests'][0]['repo_from']['date_created'] = '1431414800' + data2['requests'][0]['uid'] = '1431414800' + self.assertDictEqual(data, data2) + + @patch('pagure.lib.notify.send_email') + def test_api_pull_request_view(self, send_email): + """ Test the api_pull_request_view method of the flask api. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + # Create a pull-request + repo = pagure.lib.get_project(self.session, 'test') + forked_repo = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=forked_repo, + branch_from='master', + repo_to=repo, + branch_to='master', + title='test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'test pull-request') + + # Invalid repo + output = self.app.get('/api/0/foo/pull-request/1') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Invalid issue for this repo + output = self.app.get('/api/0/test2/pull-request/1') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Pull-Request not found", + "error_code": "ENOREQ", + } + ) + + # Valid issue + output = self.app.get('/api/0/test/pull-request/1') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1431414800' + data['updated_on'] = '1431414800' + data['project']['date_created'] = '1431414800' + data['repo_from']['date_created'] = '1431414800' + data['uid'] = '1431414800' + self.assertDictEqual( + data, + { + "assignee": None, + "branch": "master", + "branch_from": "master", + "closed_at": None, + "closed_by": None, + "comments": [], + "commit_start": None, + "commit_stop": None, + "date_created": "1431414800", + "id": 1, + "project": { + "date_created": "1431414800", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": None, + "tags": [], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "remote_git": None, + "repo_from": { + "date_created": "1431414800", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": None, + "tags": [], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "status": 'Open', + "title": "test pull-request", + "uid": "1431414800", + "updated_on": "1431414800", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Access Pull-Request authenticated + output = self.app.get('/api/0/test/pull-request/1', headers=headers) + self.assertEqual(output.status_code, 200) + data2 = json.loads(output.data) + data2['date_created'] = '1431414800' + data2['project']['date_created'] = '1431414800' + data2['repo_from']['date_created'] = '1431414800' + data2['uid'] = '1431414800' + data2['date_created'] = '1431414800' + data2['updated_on'] = '1431414800' + self.assertDictEqual(data, data2) + + @patch('pagure.lib.notify.send_email') + def test_api_pull_request_close(self, send_email): + """ Test the api_pull_request_close method of the flask api. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + # Create the pull-request to close + repo = pagure.lib.get_project(self.session, 'test') + forked_repo = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=forked_repo, + branch_from='master', + repo_to=repo, + branch_to='master', + title='test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'test pull-request') + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Invalid project + output = self.app.post( + '/api/0/foo/pull-request/1/close', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Valid token, wrong project + output = self.app.post( + '/api/0/test2/pull-request/1/close', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + # Invalid PR + output = self.app.post( + '/api/0/test/pull-request/2/close', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'error': 'Pull-Request not found', 'error_code': "ENOREQ"} + ) + + # Create a token for foo for this project + item = pagure.lib.model.Token( + id='foobar_token', + user_id=2, + project_id=1, + expiration=datetime.datetime.utcnow() + datetime.timedelta( + days=30) + ) + self.session.add(item) + self.session.commit() + item = pagure.lib.model.TokenAcl( + token_id='foobar_token', + acl_id=2, + ) + self.session.add(item) + self.session.commit() + + headers = {'Authorization': 'token foobar_token'} + + # User not admin + output = self.app.post( + '/api/0/test/pull-request/1/close', headers=headers) + self.assertEqual(output.status_code, 403) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + 'error': 'You are not allowed to merge/close pull-request ' + 'for this project', + 'error_code': "ENOPRCLOSE", + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Close PR + output = self.app.post( + '/api/0/test/pull-request/1/close', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {"message": "Pull-request closed!"} + ) + + @patch('pagure.lib.notify.send_email') + @patch('pagure.lib.git.merge_pull_request') + def test_api_pull_request_merge(self, mpr, send_email): + """ Test the api_pull_request_merge method of the flask api. """ + mpr.return_value = 'Changes merged!' + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + # Create the pull-request to close + repo = pagure.lib.get_project(self.session, 'test') + forked_repo = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=forked_repo, + branch_from='master', + repo_to=repo, + branch_to='master', + title='test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'test pull-request') + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Invalid project + output = self.app.post( + '/api/0/foo/pull-request/1/merge', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Valid token, wrong project + output = self.app.post( + '/api/0/test2/pull-request/1/merge', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + # Invalid PR + output = self.app.post( + '/api/0/test/pull-request/2/merge', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'error': 'Pull-Request not found', 'error_code': "ENOREQ"} + ) + + # Create a token for foo for this project + item = pagure.lib.model.Token( + id='foobar_token', + user_id=2, + project_id=1, + expiration=datetime.datetime.utcnow() + datetime.timedelta( + days=30) + ) + self.session.add(item) + self.session.commit() + item = pagure.lib.model.TokenAcl( + token_id='foobar_token', + acl_id=3, + ) + self.session.add(item) + self.session.commit() + + headers = {'Authorization': 'token foobar_token'} + + # User not admin + output = self.app.post( + '/api/0/test/pull-request/1/merge', headers=headers) + self.assertEqual(output.status_code, 403) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + 'error': 'You are not allowed to merge/close pull-request ' + 'for this project', + 'error_code': "ENOPRCLOSE", + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Merge PR + output = self.app.post( + '/api/0/test/pull-request/1/merge', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {"message": "Changes merged!"} + ) + + @patch('pagure.lib.notify.send_email') + def test_api_pull_request_add_comment(self, mockemail): + """ Test the api_pull_request_add_comment method of the flask api. """ + mockemail.return_value = True + + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Invalid project + output = self.app.post( + '/api/0/foo/pull-request/1/comment', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Valid token, wrong project + output = self.app.post( + '/api/0/test2/pull-request/1/comment', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + # No input + output = self.app.post( + '/api/0/test/pull-request/1/comment', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Pull-Request not found", + "error_code": "ENOREQ", + } + ) + + # Create a pull-request + repo = pagure.lib.get_project(self.session, 'test') + forked_repo = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=forked_repo, + branch_from='master', + repo_to=repo, + branch_to='master', + title='test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'test pull-request') + + # Check comments before + request = pagure.lib.search_pull_requests( + self.session, project_id=1, requestid=1) + self.assertEqual(len(request.comments), 0) + + data = { + 'title': 'test issue', + } + + # Incomplete request + output = self.app.post( + '/api/0/test/pull-request/1/comment', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + # No change + request = pagure.lib.search_pull_requests( + self.session, project_id=1, requestid=1) + self.assertEqual(len(request.comments), 0) + + data = { + 'comment': 'This is a very interesting question', + } + + # Valid request + output = self.app.post( + '/api/0/test/pull-request/1/comment', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'Comment added'} + ) + + # One comment added + request = pagure.lib.search_pull_requests( + self.session, project_id=1, requestid=1) + self.assertEqual(len(request.comments), 1) + + @patch('pagure.lib.notify.send_email') + def test_api_pull_request_add_flag(self, mockemail): + """ Test the api_pull_request_add_flag method of the flask api. """ + mockemail.return_value = True + + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Invalid project + output = self.app.post( + '/api/0/foo/pull-request/1/flag', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Valid token, wrong project + output = self.app.post( + '/api/0/test2/pull-request/1/flag', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + # No input + output = self.app.post( + '/api/0/test/pull-request/1/flag', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Pull-Request not found", + "error_code": "ENOREQ", + } + ) + + # Create a pull-request + repo = pagure.lib.get_project(self.session, 'test') + forked_repo = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=forked_repo, + branch_from='master', + repo_to=repo, + branch_to='master', + title='test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'test pull-request') + + # Check comments before + request = pagure.lib.search_pull_requests( + self.session, project_id=1, requestid=1) + self.assertEqual(len(request.flags), 0) + + data = { + 'username': 'Jenkins', + 'percent': 100, + 'url': 'http://jenkins.cloud.fedoraproject.org/', + 'uid': 'jenkins_build_pagure_100+seed', + } + + # Incomplete request + output = self.app.post( + '/api/0/test/pull-request/1/flag', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + # No change + request = pagure.lib.search_pull_requests( + self.session, project_id=1, requestid=1) + self.assertEqual(len(request.flags), 0) + + data = { + 'username': 'Jenkins', + 'percent': 0, + 'comment': 'Tests failed', + 'url': 'http://jenkins.cloud.fedoraproject.org/', + 'uid': 'jenkins_build_pagure_100+seed', + } + + # Valid request + output = self.app.post( + '/api/0/test/pull-request/1/flag', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'Flag added'} + ) + + # One flag added + request = pagure.lib.search_pull_requests( + self.session, project_id=1, requestid=1) + self.assertEqual(len(request.flags), 1) + self.assertEqual(request.flags[0].comment, 'Tests failed') + self.assertEqual(request.flags[0].percent, 0) + + # Update flag + data = { + 'username': 'Jenkins', + 'percent': 100, + 'comment': 'Tests passed', + 'url': 'http://jenkins.cloud.fedoraproject.org/', + 'uid': 'jenkins_build_pagure_100+seed', + } + + output = self.app.post( + '/api/0/test/pull-request/1/flag', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'Flag updated'} + ) + + # One flag added + request = pagure.lib.search_pull_requests( + self.session, project_id=1, requestid=1) + self.assertEqual(len(request.flags), 1) + self.assertEqual(request.flags[0].comment, 'Tests passed') + self.assertEqual(request.flags[0].percent, 100) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase( + PagureFlaskApiForktests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py new file mode 100644 index 0000000..794e230 --- /dev/null +++ b/tests/test_pagure_flask_api_issue.py @@ -0,0 +1,1053 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import datetime +import unittest +import shutil +import sys +import os + +import json +from mock import patch + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure.lib +import tests + + +class PagureFlaskApiIssuetests(tests.Modeltests): + """ Tests for the flask API of pagure for issue """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApiIssuetests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.api.SESSION = self.session + pagure.api.issue.SESSION = self.session + pagure.lib.SESSION = self.session + + pagure.APP.config['TICKETS_FOLDER'] = None + + self.app = pagure.APP.test_client() + + def test_api_new_issue(self): + """ Test the api_new_issue method of the flask api. """ + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(tests.HERE, 'tickets')) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Valid token, wrong project + output = self.app.post('/api/0/test2/new_issue', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + # No input + output = self.app.post('/api/0/test/new_issue', headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + data = { + 'title': 'test issue', + } + + # Invalid repo + output = self.app.post( + '/api/0/foo/new_issue', data=data, headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Incomplete request + output = self.app.post( + '/api/0/test/new_issue', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + data = { + 'title': 'test issue', + 'issue_content': 'This issue needs attention', + } + + # Valid request + output = self.app.post( + '/api/0/test/new_issue', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'Issue created'} + ) + + def test_api_view_issues(self): + """ Test the api_view_issues method of the flask api. """ + self.test_api_new_issue() + + # Invalid repo + output = self.app.get('/api/0/foo/issues') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # List all opened issues + output = self.app.get('/api/0/test/issues') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['issues'][0]['date_created'] = '1431414800' + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + "status": None, + "tags": [] + }, + "total_issues": 1, + "issues": [ + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "This issue needs attention", + "date_created": "1431414800", + "depends": [], + "id": 1, + "private": False, + "status": "Open", + "tags": [], + "title": "test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + + # Create private issue + 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, + private=True, + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + + # Access issues un-authenticated + output = self.app.get('/api/0/test/issues') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['issues'][0]['date_created'] = '1431414800' + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + "status": None, + "tags": [] + }, + "total_issues": 1, + "issues": [ + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "This issue needs attention", + "date_created": "1431414800", + "depends": [], + "id": 1, + "private": False, + "status": "Open", + "tags": [], + "title": "test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + headers = {'Authorization': 'token aaabbbccc'} + + # Access issues authenticated but non-existing token + output = self.app.get('/api/0/test/issues', headers=headers) + self.assertEqual(output.status_code, 401) + + # Create a new token for another user + item = pagure.lib.model.Token( + id='bar_token', + user_id=2, + project_id=1, + expiration=datetime.datetime.utcnow() + datetime.timedelta( + days=30) + ) + self.session.add(item) + + headers = {'Authorization': 'token bar_token'} + + # Access issues authenticated but wrong token + output = self.app.get('/api/0/test/issues', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['issues'][0]['date_created'] = '1431414800' + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + "status": None, + "tags": [] + }, + "total_issues": 1, + "issues": [ + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "This issue needs attention", + "date_created": "1431414800", + "depends": [], + "id": 1, + "private": False, + "status": "Open", + "tags": [], + "title": "test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Access issues authenticated correctly + output = self.app.get('/api/0/test/issues', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['issues'][0]['date_created'] = '1431414800' + data['issues'][1]['date_created'] = '1431414800' + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + "status": None, + "tags": [] + }, + "total_issues": 2, + "issues": [ + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "This issue needs attention", + "date_created": "1431414800", + "depends": [], + "id": 1, + "private": False, + "status": "Open", + "tags": [], + "title": "test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "We should work on this", + "date_created": "1431414800", + "depends": [], + "id": 2, + "private": True, + "status": "Open", + "tags": [], + "title": "Test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + + # List closed issue + output = self.app.get('/api/0/test/issues?status=Closed', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + "status": "Closed", + "tags": [] + }, + "total_issues": 0, + "issues": [] + } + ) + + # List closed issue + output = self.app.get('/api/0/test/issues?status=Invalid', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + "status": "Invalid", + "tags": [] + }, + "total_issues": 0, + "issues": [] + } + ) + + def test_api_view_issue(self): + """ Test the api_view_issue method of the flask api. """ + self.test_api_new_issue() + + # Invalid repo + output = self.app.get('/api/0/foo/issue/1') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Invalid issue for this repo + output = self.app.get('/api/0/test2/issue/1') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Issue not found", + "error_code": "ENOISSUE", + } + ) + + # Valid issue + output = self.app.get('/api/0/test/issue/1') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1431414800' + self.assertDictEqual( + data, + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "This issue needs attention", + "date_created": "1431414800", + "depends": [], + "id": 1, + "private": False, + "status": "Open", + "tags": [], + "title": "test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ) + + # Create private issue + 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, + private=True, + issue_uid='aaabbbccc', + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + + # Access private issue un-authenticated + output = self.app.get('/api/0/test/issue/2') + self.assertEqual(output.status_code, 403) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "You are not allowed to view this issue", + "error_code": "EISSUENOTALLOWED", + } + ) + + headers = {'Authorization': 'token aaabbbccc'} + + # Access private issue authenticated but non-existing token + output = self.app.get('/api/0/test/issue/2', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK" + } + ) + + # Create a new token for another user + item = pagure.lib.model.Token( + id='bar_token', + user_id=2, + project_id=1, + expiration=datetime.datetime.utcnow() + datetime.timedelta( + days=30) + ) + self.session.add(item) + + headers = {'Authorization': 'token bar_token'} + + # Access private issue authenticated but wrong token + output = self.app.get('/api/0/test/issue/2', headers=headers) + self.assertEqual(output.status_code, 403) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "You are not allowed to view this issue", + "error_code": "EISSUENOTALLOWED", + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Access private issue authenticated correctly + output = self.app.get('/api/0/test/issue/2', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1431414800' + self.assertDictEqual( + data, + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "We should work on this", + "date_created": "1431414800", + "depends": [], + "id": 2, + "private": True, + "status": "Open", + "tags": [], + "title": "Test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ) + + # Access private issue authenticated correctly using the issue's uid + output = self.app.get('/api/0/test/issue/aaabbbccc', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1431414800' + self.assertDictEqual( + data, + { + "assignee": None, + "blocks": [], + "comments": [], + "content": "We should work on this", + "date_created": "1431414800", + "depends": [], + "id": 2, + "private": True, + "status": "Open", + "tags": [], + "title": "Test issue", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ) + + def test_api_change_status_issue(self): + """ Test the api_change_status_issue method of the flask api. """ + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(tests.HERE, 'tickets')) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Invalid project + output = self.app.post('/api/0/foo/issue/1/status', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Valid token, wrong project + output = self.app.post('/api/0/test2/issue/1/status', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + # No input + output = self.app.post('/api/0/test/issue/1/status', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Issue not found", + "error_code": "ENOISSUE", + } + ) + + # Create normal issue + repo = pagure.lib.get_project(self.session, 'test') + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue #1', + content='We should work on this', + user='pingou', + ticketfolder=None, + private=False, + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue #1') + + # Create another project + item = pagure.lib.model.Project( + user_id=2, # pingou + name='foo', + description='test project #3', + hook_token='aaabbbdddeee', + ) + self.session.add(item) + self.session.commit() + + # Create a token for pingou for this project + item = pagure.lib.model.Token( + id='pingou_foo', + user_id=1, + project_id=3, + expiration=datetime.datetime.utcnow() + datetime.timedelta( + days=30) + ) + self.session.add(item) + self.session.commit() + + # Give `change_status_issue` to this token + item = pagure.lib.model.TokenAcl( + token_id='pingou_foo', + acl_id=6, + ) + self.session.add(item) + self.session.commit() + + repo = pagure.lib.get_project(self.session, 'foo') + # Create private issue + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue', + content='We should work on this', + user='foo', + ticketfolder=None, + private=True, + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + + # Check status before + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.status, 'Open') + + data = { + 'title': 'test issue', + } + + # Incomplete request + output = self.app.post( + '/api/0/test/issue/1/status', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + # No change + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.status, 'Open') + + data = { + 'status': 'Open', + } + + # Valid request but no change + output = self.app.post( + '/api/0/test/issue/1/status', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'No changes'} + ) + + # No change + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.status, 'Open') + + data = { + 'status': 'Fixed', + } + + # Valid request + output = self.app.post( + '/api/0/test/issue/1/status', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'Successfully edited issue #1'} + ) + + headers = {'Authorization': 'token pingou_foo'} + + # Un-authorized issue + output = self.app.post( + '/api/0/foo/issue/1/status', data=data, headers=headers) + self.assertEqual(output.status_code, 403) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "You are not allowed to view this issue", + "error_code": "EISSUENOTALLOWED", + } + ) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_api_comment_issue(self, p_send_email, p_ugt): + """ Test the api_comment_issue method of the flask api. """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Invalid project + output = self.app.post('/api/0/foo/issue/1/comment', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Project not found", + "error_code": "ENOPROJECT", + } + ) + + # Valid token, wrong project + output = self.app.post('/api/0/test2/issue/1/comment', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK", + } + ) + + # No input + output = self.app.post('/api/0/test/issue/1/comment', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Issue not found", + "error_code": "ENOISSUE", + } + ) + + # Create normal issue + repo = pagure.lib.get_project(self.session, 'test') + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue #1', + content='We should work on this', + user='pingou', + ticketfolder=None, + private=False, + issue_uid='aaabbbccc#1', + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue #1') + + # Check comments before + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 0) + + data = { + 'title': 'test issue', + } + + # Incomplete request + output = self.app.post( + '/api/0/test/issue/1/comment', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + # No change + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.status, 'Open') + + data = { + 'comment': 'This is a very interesting question', + } + + # Valid request + output = self.app.post( + '/api/0/test/issue/1/comment', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'Comment added'} + ) + + # One comment added + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 1) + + # Create another project + item = pagure.lib.model.Project( + user_id=2, # foo + name='foo', + description='test project #3', + hook_token='aaabbbdddeee', + ) + self.session.add(item) + self.session.commit() + + # Create a token for pingou for this project + item = pagure.lib.model.Token( + id='pingou_foo', + user_id=1, + project_id=3, + expiration=datetime.datetime.utcnow() + datetime.timedelta( + days=30) + ) + self.session.add(item) + self.session.commit() + + # Give `change_status_issue` to this token + item = pagure.lib.model.TokenAcl( + token_id='pingou_foo', + acl_id=1, + ) + self.session.add(item) + self.session.commit() + + repo = pagure.lib.get_project(self.session, 'foo') + # Create private issue + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue', + content='We should work on this', + user='foo', + ticketfolder=None, + private=True, + issue_uid='aaabbbccc#2', + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + + # Check before + repo = pagure.lib.get_project(self.session, 'foo') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 0) + + data = { + 'comment': 'This is a very interesting question', + } + headers = {'Authorization': 'token pingou_foo'} + + # Valid request but un-authorized + output = self.app.post( + '/api/0/foo/issue/1/comment', data=data, headers=headers) + self.assertEqual(output.status_code, 403) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "You are not allowed to view this issue", + "error_code": "EISSUENOTALLOWED", + } + ) + + # No comment added + repo = pagure.lib.get_project(self.session, 'foo') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 0) + + # Create token for user foo + item = pagure.lib.model.Token( + id='foo_token2', + user_id=2, + project_id=3, + expiration=datetime.datetime.utcnow() + datetime.timedelta(days=30) + ) + self.session.add(item) + self.session.commit() + tests.create_tokens_acl(self.session, token_id='foo_token2') + + data = { + 'comment': 'This is a very interesting question', + } + headers = {'Authorization': 'token foo_token2'} + + # Valid request and authorized + output = self.app.post( + '/api/0/foo/issue/1/comment', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'message': 'Comment added'} + ) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_api_view_issue_comment(self, p_send_email, p_ugt): + """ Test the api_view_issue_comment endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + self.test_api_comment_issue() + + # View a comment that does not exist + output = self.app.get('/api/0/foo/issue/100/comment/2') + self.assertEqual(output.status_code, 404) + + # Issue exists but not the comment + output = self.app.get('/api/0/test/issue/1/comment/2') + self.assertEqual(output.status_code, 404) + + # Issue and comment exists + output = self.app.get('/api/0/test/issue/1/comment/1') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1435821770' + data["comment_date"] = "2015-07-02 09:22" + data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..." + self.assertDictEqual( + data, + { + "avatar_url": "https://seccdn.libravatar.org/avatar/...", + "comment": "This is a very interesting question", + "comment_date": "2015-07-02 09:22", + "date_created": "1435821770", + "edited_on": None, + "editor": None, + "id": 1, + "parent": None, + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ) + + # Issue and comment exists, using UID + output = self.app.get('/api/0/test/issue/aaabbbccc#1/comment/1') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1435821770' + data["comment_date"] = "2015-07-02 09:22" + data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..." + self.assertDictEqual( + data, + { + "avatar_url": "https://seccdn.libravatar.org/avatar/...", + "comment": "This is a very interesting question", + "comment_date": "2015-07-02 09:22", + "date_created": "1435821770", + "edited_on": None, + "editor": None, + "id": 1, + "parent": None, + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ) + + # Private issue + output = self.app.get('/api/0/foo/issue/1/comment/2') + self.assertEqual(output.status_code, 403) + + # Private issue - Auth - wrong token + headers = {'Authorization': 'token pingou_foo'} + output = self.app.get('/api/0/foo/issue/1/comment/2', headers=headers) + self.assertEqual(output.status_code, 403) + + # Private issue - Auth - Invalid token + headers = {'Authorization': 'token aaabbbcccddd'} + output = self.app.get('/api/0/foo/issue/1/comment/2', headers=headers) + self.assertEqual(output.status_code, 401) + + # Private issue - Auth - valid token - unknown comment + headers = {'Authorization': 'token foo_token2'} + output = self.app.get('/api/0/foo/issue/1/comment/3', headers=headers) + self.assertEqual(output.status_code, 404) + + # Private issue - Auth - valid token - known comment + headers = {'Authorization': 'token foo_token2'} + output = self.app.get('/api/0/foo/issue/1/comment/2', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1435821770' + data["comment_date"] = "2015-07-02 09:22" + data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..." + self.assertDictEqual( + data, + { + "avatar_url": "https://seccdn.libravatar.org/avatar/...", + "comment": "This is a very interesting question", + "comment_date": "2015-07-02 09:22", + "date_created": "1435821770", + "edited_on": None, + "editor": None, + "id": 2, + "parent": None, + "user": { + "fullname": "foo bar", + "name": "foo" + } + } + ) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase( + PagureFlaskApiIssuetests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py new file mode 100644 index 0000000..f7abe34 --- /dev/null +++ b/tests/test_pagure_flask_api_project.py @@ -0,0 +1,225 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - 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 PagureFlaskApiProjecttests(tests.Modeltests): + """ Tests for the flask API of pagure for issue """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApiProjecttests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.api.SESSION = self.session + pagure.api.project.SESSION = self.session + pagure.lib.SESSION = self.session + + pagure.APP.config['REQUESTS_FOLDER'] = None + pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos') + + self.app = pagure.APP.test_client() + + def test_api_git_tags(self): + """ Test the api_git_tags method of the flask api. """ + tests.create_projects(self.session) + + # Create a git repo to play with + gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') + repo = pygit2.init_repository(gitrepo, bare=True) + + newpath = tempfile.mkdtemp(prefix='pagure-fork-test') + repopath = os.path.join(newpath, 'test') + clone_repo = pygit2.clone_repository(gitrepo, repopath) + + # Create a file in that git repo + with open(os.path.join(repopath, 'sources'), 'w') as stream: + stream.write('foo\n bar') + clone_repo.index.add('sources') + clone_repo.index.write() + + # Commits the files added + tree = clone_repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + clone_repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add sources file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [] + ) + refname = 'refs/heads/master:refs/heads/master' + ori_remote = clone_repo.remotes[0] + PagureRepo.push(ori_remote, refname) + + # Tag our first commit + first_commit = repo.revparse_single('HEAD') + tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0) + repo.create_tag( + "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, + "Release 0.0.1") + + # Check tags + output = self.app.get('/api/0/test/git/tags') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'tags': ['0.0.1'], 'total_tags': 1} + ) + + shutil.rmtree(newpath) + + def test_api_projects(self): + """ Test the api_projects method of the flask api. """ + tests.create_projects(self.session) + + # Check before adding + repo = pagure.lib.get_project(self.session, 'test') + self.assertEqual(repo.tags, []) + + # Adding a tag + output = pagure.lib.update_tags( + self.session, repo, 'infra', 'pingou', + ticketfolder=None) + self.assertEqual(output, ['Tag added: infra']) + + # Check after adding + repo = pagure.lib.get_project(self.session, 'test') + self.assertEqual(len(repo.tags), 1) + self.assertEqual(repo.tags_text, ['infra']) + + # Check the API + output = self.app.get('/api/0/projects?tags=inf') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'error_code': 'ENOPROJECTS', 'error': 'No projects found'} + ) + output = self.app.get('/api/0/projects?tags=infra') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['projects'][0]['date_created'] = "1436527638" + self.assertDictEqual( + data, + { + "total_projects": 1, + "projects": [ + { + "date_created": "1436527638", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": None, + "tags": ["infra"], + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + output = self.app.get('/api/0/projects?username=pingou') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['projects'][0]['date_created'] = "1436527638" + data['projects'][1]['date_created'] = "1436527638" + self.assertDictEqual( + data, + { + "total_projects": 2, + "projects": [ + { + "date_created": "1436527638", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": None, + "tags": ["infra"], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + { + "date_created": "1436527638", + "description": "test project #2", + "id": 2, + "name": "test2", + "parent": None, + "tags": [], + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + output = self.app.get('/api/0/projects?username=pingou&tags=infra') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['projects'][0]['date_created'] = "1436527638" + self.assertDictEqual( + data, + { + "total_projects": 1, + "projects": [ + { + "date_created": "1436527638", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": None, + "tags": ["infra"], + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase( + PagureFlaskApiProjecttests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_dump_load_ticket.py b/tests/test_pagure_flask_dump_load_ticket.py new file mode 100644 index 0000000..4b585b3 --- /dev/null +++ b/tests/test_pagure_flask_dump_load_ticket.py @@ -0,0 +1,236 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +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 + + +class PagureFlaskDumpLoadTicketTests(tests.Modeltests): + """ Tests for flask application for dumping and re-loading the JSON of + a ticket. + """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskDumpLoadTicketTests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.lib.SESSION = self.session + pagure.ui.app.SESSION = self.session + pagure.ui.filters.SESSION = self.session + pagure.ui.fork.SESSION = self.session + pagure.ui.repo.SESSION = self.session + + pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos') + pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks') + pagure.APP.config['TICKETS_FOLDER'] = os.path.join( + tests.HERE, 'tickets') + pagure.APP.config['DOCS_FOLDER'] = os.path.join( + tests.HERE, 'docs') + pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( + tests.HERE, 'requests') + self.app = pagure.APP.test_client() + + @patch('pagure.lib.notify.send_email') + def test_dumping_ticket(self, send_email): + """ Test dumping a ticket into a JSON blob. """ + send_email.return_value = True + + tests.create_projects(self.session) + + # Create repo + self.gitrepo = os.path.join(tests.HERE, 'tickets', 'test.git') + repopath = os.path.join(tests.HERE, 'tickets') + os.makedirs(self.gitrepo) + repo_obj = pygit2.init_repository(self.gitrepo, bare=True) + + repo = pagure.lib.get_project(self.session, 'test') + # Create an issue to play with + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue', + content='We should work on this', + user='pingou', + ticketfolder=repopath + ) + self.assertEqual(msg.title, 'Test issue') + + # Need another two issue to test the dependencie chain + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue #2', + content='Another bug', + user='pingou', + ticketfolder=repopath + ) + self.assertEqual(msg.title, 'Test issue #2') + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue #3', + content='That would be nice feature no?', + user='foo', + ticketfolder=repopath + ) + self.assertEqual(msg.title, 'Test issue #3') + + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + issue2 = pagure.lib.search_issues(self.session, repo, issueid=2) + issue3 = pagure.lib.search_issues(self.session, repo, issueid=3) + + # Add a couple of comment on the ticket + msg = pagure.lib.add_issue_comment( + session=self.session, + issue=issue, + comment='Hey look a comment!', + user='foo', + ticketfolder=repopath, + ) + self.session.commit() + self.assertEqual(msg, 'Comment added') + msg = pagure.lib.add_issue_comment( + session=self.session, + issue=issue, + comment='crazy right?', + user='pingou', + ticketfolder=repopath, + ) + self.session.commit() + self.assertEqual(msg, 'Comment added') + # Assign the ticket to someone + msg = pagure.lib.add_issue_assignee( + session=self.session, + issue=issue, + assignee='pingou', + user='pingou', + ticketfolder=repopath, + ) + self.session.commit() + self.assertEqual(msg, 'Issue assigned') + # Add a couple of tags on the ticket + msg = pagure.lib.add_tag_obj( + session=self.session, + obj=issue, + tags=[' feature ', 'future '], + user='pingou', + ticketfolder=repopath, + ) + self.session.commit() + self.assertEqual(msg, 'Tag added: feature, future') + # Add dependencies + msg = pagure.lib.add_issue_dependency( + session=self.session, + issue=issue, + issue_blocked=issue2, + user='pingou', + ticketfolder=repopath, + ) + self.session.commit() + self.assertEqual(msg, 'Dependency added') + msg = pagure.lib.add_issue_dependency( + session=self.session, + issue=issue3, + issue_blocked=issue, + user='foo', + ticketfolder=repopath, + ) + self.session.commit() + self.assertEqual(msg, 'Dependency added') + + # Dump the JSON + pagure.lib.git.update_git(issue, repo, repopath) + repo = pygit2.Repository(self.gitrepo) + cnt = len([commit + for commit in repo.walk( + repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)]) + self.assertEqual(cnt, 10) + + last_commit = repo.revparse_single('HEAD') + patch = pagure.lib.git.commit_to_patch(repo, last_commit) + for line in patch.split('\n'): + if line.startswith('--- a/'): + fileid = line.split('--- a/')[1] + break + + newpath = tempfile.mkdtemp(prefix='pagure-dump-load') + clone_repo = pygit2.clone_repository(self.gitrepo, newpath) + + self.assertEqual(len(os.listdir(newpath)), 4) + + ticket_json = os.path.join(tests.HERE, 'test_ticket.json') + self.assertFalse(os.path.exists(ticket_json)) + shutil.copyfile(os.path.join(newpath, fileid), ticket_json) + self.assertTrue(os.path.exists(ticket_json)) + + shutil.rmtree(newpath) + + @patch('pagure.lib.notify.send_email') + def test_reload_ticket(self, send_email): + """ Test reloading a ticket from a JSON blob. """ + send_email.return_value = True + ticket_json = os.path.join(tests.HERE, 'test_ticket.json') + self.assertTrue(os.path.exists(ticket_json)) + + tests.create_projects(self.session) + + jsondata = None + with open(ticket_json) as stream: + jsondata = json.load(stream) + self.assertNotEqual(jsondata, None) + + pagure.lib.git.update_ticket_from_git( + self.session, + reponame='test', + username=None, + issue_uid='foobar', + json_data=jsondata, + ) + + # Post loading + repo = pagure.lib.get_project(self.session, 'test') + self.assertEqual(len(repo.issues), 1) + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + + # Check after re-loading + self.assertEqual(len(issue.comments), 2) + self.assertEqual(len(issue.tags), 2) + self.assertEqual(issue.tags_text, ['future', 'feature']) + self.assertEqual(issue.assignee.username, 'pingou') + self.assertEqual(issue.children, []) + self.assertEqual(issue.parents, []) + + self.assertTrue(os.path.exists(ticket_json)) + os.unlink(ticket_json) + self.assertFalse(os.path.exists(ticket_json)) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase( + PagureFlaskDumpLoadTicketTests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_internal.py b/tests/test_pagure_flask_internal.py new file mode 100644 index 0000000..d812b13 --- /dev/null +++ b/tests/test_pagure_flask_internal.py @@ -0,0 +1,819 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import json +import unittest +import shutil +import sys +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 + + +class PagureFlaskInternaltests(tests.Modeltests): + """ Tests for flask Internal controller of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskInternaltests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.APP.config['IP_ALLOWED_INTERNAL'] = list(set( + pagure.APP.config['IP_ALLOWED_INTERNAL'] + [None])) + pagure.SESSION = self.session + pagure.internal.SESSION = self.session + pagure.ui.repo.SESSION = self.session + + pagure.APP.config['GIT_FOLDER'] = tests.HERE + pagure.APP.config['FORK_FOLDER'] = os.path.join( + tests.HERE, 'forks') + pagure.APP.config['REQUESTS_FOLDER'] = None + pagure.APP.config['TICKETS_FOLDER'] = None + pagure.APP.config['DOCS_FOLDER'] = None + self.app = pagure.APP.test_client() + + @patch('pagure.lib.notify.send_email') + def test_pull_request_add_comment(self, send_email): + """ Test the pull_request_add_comment function. """ + send_email.return_value = True + + tests.create_projects(self.session) + + repo = pagure.lib.get_project(self.session, 'test') + + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=repo, + branch_from='feature', + repo_to=repo, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + request = repo.requests[0] + self.assertEqual(len(request.comments), 0) + self.assertEqual(len(request.discussion), 0) + + data = { + 'objid': 'foo', + } + + # Wrong http request + output = self.app.post('/pv/pull-request/comment/', data=data) + self.assertEqual(output.status_code, 405) + + # Invalid request + output = self.app.put('/pv/pull-request/comment/', data=data) + self.assertEqual(output.status_code, 400) + + data = { + 'objid': 'foo', + 'useremail': 'foo@pingou.com', + } + + # Invalid objid + output = self.app.put('/pv/pull-request/comment/', data=data) + self.assertEqual(output.status_code, 404) + + data = { + 'objid': request.uid, + 'useremail': 'foo@pingou.com', + } + + # Valid objid, in-complete data for a comment + output = self.app.put('/pv/pull-request/comment/', data=data) + self.assertEqual(output.status_code, 400) + + data = { + 'objid': request.uid, + 'useremail': 'foo@pingou.com', + 'comment': 'Looks good to me!', + } + + # Add comment + output = self.app.put('/pv/pull-request/comment/', data=data) + self.assertEqual(output.status_code, 200) + js_data = json.loads(output.data) + self.assertDictEqual(js_data, {'message': 'Comment added'}) + + repo = pagure.lib.get_project(self.session, 'test') + request = repo.requests[0] + self.assertEqual(len(request.comments), 1) + self.assertEqual(len(request.discussion), 1) + + # Check the @localonly + pagure.APP.config['IP_ALLOWED_INTERNAL'].remove(None) + output = self.app.put('/pv/pull-request/comment/', data=data) + self.assertEqual(output.status_code, 403) + + @patch('pagure.lib.notify.send_email') + def test_ticket_add_comment(self, send_email): + """ Test the ticket_add_comment function. """ + send_email.return_value = True + + tests.create_projects(self.session) + + # Create issues 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') + + issue = repo.issues[0] + self.assertEqual(len(issue.comments), 0) + + data = { + 'objid': 'foo', + } + + # Wrong http request + output = self.app.post('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 405) + + # Invalid request + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 400) + + data = { + 'objid': 'foo', + 'useremail': 'foo@pingou.com', + } + + # Invalid objid + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 404) + + data = { + 'objid': issue.uid, + 'useremail': 'foo@pingou.com', + } + + # Valid objid, in-complete data for a comment + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 400) + + data = { + 'objid': issue.uid, + 'useremail': 'foo@pingou.com', + 'comment': 'Looks good to me!', + } + + # Add comment + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 200) + js_data = json.loads(output.data) + self.assertDictEqual(js_data, {'message': 'Comment added'}) + + repo = pagure.lib.get_project(self.session, 'test') + issue = repo.issues[0] + self.assertEqual(len(issue.comments), 1) + + # Check the @localonly + pagure.APP.config['IP_ALLOWED_INTERNAL'].remove(None) + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 403) + + @patch('pagure.lib.notify.send_email') + def test_private_ticket_add_comment(self, send_email): + """ Test the ticket_add_comment function on a private ticket. """ + send_email.return_value = True + + tests.create_projects(self.session) + + # Create issues 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, really', + user='pingou', + private=True, + ticketfolder=None + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + + issue = repo.issues[0] + self.assertEqual(len(issue.comments), 0) + + data = { + 'objid': 'foo', + } + + # Wrong http request + output = self.app.post('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 405) + + # Invalid request + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 400) + + data = { + 'objid': 'foo', + 'useremail': 'foo@pingou.com', + } + + # Invalid objid + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 404) + + data = { + 'objid': issue.uid, + 'useremail': 'foo@bar.com', + } + + # Valid objid, un-allowed user for this (private) ticket + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 403) + + data = { + 'objid': issue.uid, + 'useremail': 'foo@pingou.com', + } + + # Valid objid, un-allowed user for this (private) ticket + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 400) + + data = { + 'objid': issue.uid, + 'useremail': 'foo@pingou.com', + 'comment': 'Looks good to me!', + } + + # Add comment + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 200) + js_data = json.loads(output.data) + self.assertDictEqual(js_data, {'message': 'Comment added'}) + + repo = pagure.lib.get_project(self.session, 'test') + issue = repo.issues[0] + self.assertEqual(len(issue.comments), 1) + + # Check the @localonly + pagure.APP.config['IP_ALLOWED_INTERNAL'].remove(None) + output = self.app.put('/pv/ticket/comment/', data=data) + self.assertEqual(output.status_code, 403) + + @patch('pagure.lib.notify.send_email') + def test_mergeable_request_pull_FF(self, send_email): + """ Test the mergeable_request_pull endpoint with a fast-forward + merge. + """ + send_email.return_value = True + + # Create a git repo to play with + + gitrepo = os.path.join(tests.HERE, 'test.git') + self.assertFalse(os.path.exists(gitrepo)) + os.makedirs(gitrepo) + repo = pygit2.init_repository(gitrepo) + + # Create a file in that git repo + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add sources file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [] + ) + + first_commit = repo.revparse_single('HEAD') + + # Edit the sources file again + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/feature', # the name of the reference to update + author, + committer, + 'Add baz and boose to the sources\n\n There are more objects to ' + 'consider', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + + second_commit = repo.revparse_single('HEAD') + + # Create a PR for these changes + tests.create_projects(self.session) + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=project, + branch_from='feature', + repo_to=project, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + # Check if the PR can be merged + data = { + 'objid': 'blah', + } + + # Missing CSRF + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 400) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/adduser') + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Missing request identifier + data = { + 'csrf_token': csrf_token, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 404) + + # With all the desired information + project = pagure.lib.get_project(self.session, 'test') + data = { + 'csrf_token': csrf_token, + 'requestid': project.requests[0].uid, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 200) + exp = { + "code": "FFORWARD", + "message": "The pull-request can be merged and fast-forwarded", + "short_code": "Ok" + } + + js_data = json.loads(output.data) + self.assertDictEqual(js_data, exp) + + @patch('pagure.lib.notify.send_email') + def test_mergeable_request_pull_no_change(self, send_email): + """ Test the mergeable_request_pull endpoint when there are no + changes to merge. + """ + send_email.return_value = True + + # Create a git repo to play with + + gitrepo = os.path.join(tests.HERE, 'test.git') + self.assertFalse(os.path.exists(gitrepo)) + os.makedirs(gitrepo) + repo = pygit2.init_repository(gitrepo) + + # Create a file in that git repo + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add sources file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [] + ) + + first_commit = repo.revparse_single('HEAD') + + # Edit the sources file again + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add baz and boose to the sources\n\n There are more objects to ' + 'consider', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + + second_commit = repo.revparse_single('HEAD') + + # Create a PR for these changes + tests.create_projects(self.session) + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=project, + branch_from='master', + repo_to=project, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + # Check if the PR can be merged + data = { + 'objid': 'blah', + } + + # Missing CSRF + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 400) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/adduser') + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Missing request identifier + data = { + 'csrf_token': csrf_token, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 404) + + # With all the desired information + project = pagure.lib.get_project(self.session, 'test') + data = { + 'csrf_token': csrf_token, + 'requestid': project.requests[0].uid, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 200) + exp = { + "code": "NO_CHANGE", + "message": "Nothing to change, git is up to date", + "short_code": "No changes" + } + + js_data = json.loads(output.data) + self.assertDictEqual(js_data, exp) + + @patch('pagure.lib.notify.send_email') + def test_mergeable_request_pull_merge(self, send_email): + """ Test the mergeable_request_pull endpoint when the changes can + be merged with a merge commit. + """ + send_email.return_value = True + + # Create a git repo to play with + + gitrepo = os.path.join(tests.HERE, 'test.git') + self.assertFalse(os.path.exists(gitrepo)) + os.makedirs(gitrepo) + repo = pygit2.init_repository(gitrepo) + + # Create a file in that git repo + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add sources file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [] + ) + + first_commit = repo.revparse_single('HEAD') + + # Edit the sources file again + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/feature', # the name of the reference to update + author, + committer, + 'Add baz and boose to the sources\n\n There are more objects to ' + 'consider', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + + # Create another file in the master branch + with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream: + stream.write('*~') + repo.index.add('.gitignore') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add .gitignore file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + + # Create a PR for these changes + tests.create_projects(self.session) + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=project, + branch_from='feature', + repo_to=project, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + # Check if the PR can be merged + data = {} + + # Missing CSRF + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 400) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/adduser') + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Missing request identifier + data = { + 'csrf_token': csrf_token, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 404) + + # With all the desired information + project = pagure.lib.get_project(self.session, 'test') + data = { + 'csrf_token': csrf_token, + 'requestid': project.requests[0].uid, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 200) + exp = { + "code": "MERGE", + "message": "The pull-request can be merged with a merge commit", + "short_code": "With merge" + } + + js_data = json.loads(output.data) + self.assertDictEqual(js_data, exp) + + @patch('pagure.lib.notify.send_email') + def test_mergeable_request_pull_conflicts(self, send_email): + """ Test the mergeable_request_pull endpoint when the changes cannot + be merged due to conflicts. + """ + send_email.return_value = True + + # Create a git repo to play with + + gitrepo = os.path.join(tests.HERE, 'test.git') + self.assertFalse(os.path.exists(gitrepo)) + os.makedirs(gitrepo) + repo = pygit2.init_repository(gitrepo) + + # Create a file in that git repo + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add sources file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [] + ) + + first_commit = repo.revparse_single('HEAD') + + # Edit the sources file again + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/feature', # the name of the reference to update + author, + committer, + 'Add baz and boose to the sources\n\n There are more objects to ' + 'consider', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + + # Create another file in the master branch + with open(os.path.join(gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add .gitignore file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + + # Create a PR for these changes + tests.create_projects(self.session) + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=project, + branch_from='feature', + repo_to=project, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + # Check if the PR can be merged + data = {} + + # Missing CSRF + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 400) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/adduser') + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Missing request identifier + data = { + 'csrf_token': csrf_token, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 404) + + # With all the desired information + project = pagure.lib.get_project(self.session, 'test') + data = { + 'csrf_token': csrf_token, + 'requestid': project.requests[0].uid, + } + output = self.app.post('/pv/pull-request/merge', data=data) + self.assertEqual(output.status_code, 200) + exp = { + "code": "CONFLICTS", + "message": "The pull-request cannot be merged due to conflicts", + "short_code": "Conflicts" + } + + js_data = json.loads(output.data) + self.assertDictEqual(js_data, exp) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase( + PagureFlaskInternaltests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_ui_admin.py b/tests/test_pagure_flask_ui_admin.py new file mode 100644 index 0000000..377f95b --- /dev/null +++ b/tests/test_pagure_flask_ui_admin.py @@ -0,0 +1,256 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import json +import unittest +import shutil +import sys +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 + + +class PagureFlaskAdmintests(tests.Modeltests): + """ Tests for flask admin controller of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskAdmintests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.ui.SESSION = self.session + pagure.ui.app.SESSION = self.session + pagure.ui.repo.SESSION = self.session + pagure.ui.admin.SESSION = self.session + + pagure.APP.config['GIT_FOLDER'] = tests.HERE + pagure.APP.config['FORK_FOLDER'] = os.path.join( + tests.HERE, 'forks') + pagure.APP.config['TICKETS_FOLDER'] = os.path.join( + tests.HERE, 'tickets') + pagure.APP.config['DOCS_FOLDER'] = os.path.join( + tests.HERE, 'docs') + self.app = pagure.APP.test_client() + + def test_admin_index(self): + """ Test the admin_index endpoint. """ + + output = self.app.get('/admin') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin', follow_redirects=True) + self.assertEqual(output.status_code, 404) + self.assertIn( + '\n Access restricted', + output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.get('/admin', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Access restricted', + output.data) + + user = tests.FakeUser( + username='pingou', + groups=pagure.APP.config['ADMIN_GROUP']) + with tests.user_set(pagure.APP, user): + output = self.app.get('/admin', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

Admin section

' in output.data) + self.assertTrue('Re-generate gitolite ACLs file' in output.data) + self.assertTrue( + 'Re-generate user ssh key files' in output.data) + + @patch('pagure.lib.git.write_gitolite_acls') + def test_admin_generate_acl(self, wga): + """ Test the admin_generate_acl endpoint. """ + wga.return_value = True + + output = self.app.get('/admin/gitolite') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/admin/gitolite') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/gitolite', follow_redirects=True) + self.assertEqual(output.status_code, 404) + self.assertIn( + '\n Access restricted', + output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/gitolite', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Access restricted', + output.data) + + user = tests.FakeUser( + username='pingou', + groups=pagure.APP.config['ADMIN_GROUP']) + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/gitolite', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

Admin section

' in output.data) + self.assertTrue('Re-generate gitolite ACLs file' in output.data) + self.assertTrue( + 'Re-generate user ssh key files' in output.data) + self.assertFalse( + '
  • Gitolite ACLs updated
  • ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = {'csrf_token': csrf_token} + output = self.app.post( + '/admin/gitolite', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Admin section

    ' in output.data) + self.assertTrue('Re-generate gitolite ACLs file' in output.data) + self.assertTrue( + 'Re-generate user ssh key files' in output.data) + self.assertTrue( + '\n Gitolite ACLs updated' + in output.data) + + @patch('pagure.generate_user_key_files') + def test_admin_refresh_ssh(self, gakf): + """ Test the admin_refresh_ssh endpoint. """ + gakf.return_value = True + + output = self.app.get('/admin/ssh') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/admin/ssh') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/ssh', follow_redirects=True) + self.assertEqual(output.status_code, 404) + self.assertIn( + '\n Access restricted', + output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/ssh', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Access restricted', + output.data) + + user = tests.FakeUser( + username='pingou', + groups=pagure.APP.config['ADMIN_GROUP']) + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/ssh', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Admin section

    ' in output.data) + self.assertTrue('Re-generate gitolite ACLs file' in output.data) + self.assertTrue( + 'Re-generate user ssh key files' in output.data) + self.assertFalse( + '
  • Authorized file updated
  • ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = {'csrf_token': csrf_token} + output = self.app.post( + '/admin/ssh', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Admin section

    ' in output.data) + self.assertTrue('Re-generate gitolite ACLs file' in output.data) + self.assertTrue( + 'Re-generate user ssh key files' in output.data) + self.assertTrue( + '\n User key files regenerated' + in output.data) + + def test_admin_generate_hook_token(self): + """ Test the admin_generate_hook_token endpoint. """ + + output = self.app.get('/admin/hook_token') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/admin/hook_token') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/hook_token', follow_redirects=True) + self.assertEqual(output.status_code, 404) + self.assertIn( + '\n Access restricted', + output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/hook_token', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Access restricted', + output.data) + + user = tests.FakeUser( + username='pingou', + groups=pagure.APP.config['ADMIN_GROUP']) + with tests.user_set(pagure.APP, user): + output = self.app.post('/admin/hook_token', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Admin section

    ' in output.data) + self.assertTrue('Re-generate gitolite ACLs file' in output.data) + self.assertTrue( + 'Re-generate user ssh key files' in output.data) + self.assertTrue( + 'Re-generate hook-token for every projects' in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + data = {'csrf_token': csrf_token} + + output = self.app.post( + '/admin/hook_token', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Admin section

    ' in output.data) + self.assertTrue('Re-generate gitolite ACLs file' in output.data) + self.assertTrue( + 'Re-generate user ssh key files' in output.data) + self.assertTrue( + 'Re-generate hook-token for every projects' in output.data) + self.assertTrue( + '\n Hook token all re-generated' + in output.data) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskAdmintests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py new file mode 100644 index 0000000..b377a28 --- /dev/null +++ b/tests/test_pagure_flask_ui_app.py @@ -0,0 +1,640 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import unittest +import shutil +import sys +import os + +import json +from mock import patch + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure.lib +import tests + + +class PagureFlaskApptests(tests.Modeltests): + """ Tests for flask app controller of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApptests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.ui.SESSION = self.session + pagure.ui.app.SESSION = self.session + pagure.ui.repo.SESSION = self.session + + pagure.APP.config['GIT_FOLDER'] = tests.HERE + pagure.APP.config['FORK_FOLDER'] = os.path.join( + tests.HERE, 'forks') + pagure.APP.config['TICKETS_FOLDER'] = os.path.join( + tests.HERE, 'tickets') + pagure.APP.config['DOCS_FOLDER'] = os.path.join( + tests.HERE, 'docs') + pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( + tests.HERE, 'requests') + self.app = pagure.APP.test_client() + + def test_index(self): + """ Test the index endpoint. """ + + output = self.app.get('/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    All Projects ' + '0

    ', output.data) + + tests.create_projects(self.session) + + output = self.app.get('/?page=abc') + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    All Projects ' + '2

    ', output.data) + + # Add a 3rd project with a long description + item = pagure.lib.model.Project( + user_id=2, # foo + name='test3', + description='test project #3 with a very long description', + hook_token='aaabbbeee', + ) + self.session.add(item) + self.session.commit() + + user = tests.FakeUser(username='foo') + with tests.user_set(pagure.APP, user): + output = self.app.get('/?repopage=abc&forkpage=def') + self.assertIn( + 'Projects 1', + output.data) + self.assertIn( + 'Forks 0', + output.data) + self.assertEqual( + output.data.count('

    No group found

    '), 1) + self.assertEqual( + output.data.count('
    '), 3) + + def test_view_users(self): + """ Test the view_users endpoint. """ + + output = self.app.get('/users/?page=abc') + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    \n Users ' + '2

    ', output.data) + self.assertIn( + '', + output.data) + self.assertIn( + '', + output.data) + + def test_view_user(self): + """ Test the view_user endpoint. """ + + output = self.app.get('/user/pingou?repopage=abc&forkpage=def') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Projects 0', + output.data) + self.assertIn( + 'Forks 0', + output.data) + + tests.create_projects(self.session) + self.gitrepos = tests.create_projects_git( + pagure.APP.config['GIT_FOLDER']) + + output = self.app.get('/user/pingou?repopage=abc&forkpage=def') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Projects 2', + output.data) + self.assertIn( + 'Forks 0', output.data) + + def test_new_project(self): + """ Test the new_project endpoint. """ + # Before + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 0) + self.assertFalse(os.path.exists( + os.path.join(tests.HERE, 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(tests.HERE, 'tickets', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(tests.HERE, 'docs', 'project#1.git'))) + self.assertFalse(os.path.exists( + os.path.join(tests.HERE, 'requests', 'project#1.git'))) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/new/') + self.assertEqual(output.status_code, 200) + self.assertIn('Create new Project', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'description': 'Project #1', + } + + output = self.app.post('/new/', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn('Create new Project', output.data) + self.assertIn( + '\n This field is required.' + ' \n ', output.data) + + data['name'] = 'project-1' + output = self.app.post('/new/', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn('Create new Project', output.data) + self.assertNotIn( + '\n This field is required.' + ' \n ', output.data) + + data['csrf_token'] = csrf_token + output = self.app.post('/new/', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn('Create new Project', output.data) + self.assertIn( + '\n No user "username" found', + output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + data['csrf_token'] = csrf_token + output = self.app.post('/new/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Project #1

    ' in output.data) + self.assertIn( + '\n Project "project-1" created', + output.data) + + # After + projects = pagure.lib.search_projects(self.session) + self.assertEqual(len(projects), 1) + self.assertTrue(os.path.exists( + os.path.join(tests.HERE, 'project-1.git'))) + self.assertTrue(os.path.exists( + os.path.join(tests.HERE, 'tickets', 'project-1.git'))) + self.assertTrue(os.path.exists( + os.path.join(tests.HERE, 'docs', 'project-1.git'))) + self.assertTrue(os.path.exists( + os.path.join(tests.HERE, 'requests', 'project-1.git'))) + + @patch('pagure.ui.app.admin_session_timedout') + def test_user_settings(self, ast): + """ Test the user_settings endpoint. """ + ast.return_value = False + self.test_new_project() + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/settings/') + self.assertEqual(output.status_code, 404) + self.assertTrue('

    Page not found (404)

    ' in output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.get('/settings/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'ssh_key': 'this is my ssh key', + } + + output = self.app.post('/settings/', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + + data['csrf_token'] = csrf_token + + output = self.app.post( + '/settings/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '\n Public ssh key updated' + in output.data) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + + ast.return_value = True + output = self.app.get('/settings/') + self.assertEqual(output.status_code, 302) + + def test_markdown_preview(self): + """ Test the markdown_preview endpoint. """ + + data = { + 'content': 'test\n----\n\n * 1\n * item 2' + } + + # CSRF missing + output = self.app.post('/markdown/', data=data) + self.assertEqual(output.status_code, 400) + + user = tests.FakeUser() + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.get('/settings/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # With CSRF + data['csrf_token'] = csrf_token + output = self.app.post('/markdown/', data=data) + self.assertEqual(output.status_code, 200) + exp = """

    test

    +
      +
    • 1
    • +
    • item 2
    • +
    """ + self.assertEqual(output.data, exp) + + @patch('pagure.ui.app.admin_session_timedout') + def test_remove_user_email(self, ast): + """ Test the remove_user_email endpoint. """ + ast.return_value = False + self.test_new_project() + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/settings/email/drop') + self.assertEqual(output.status_code, 404) + self.assertTrue('

    Page not found (404)

    ' in output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/settings/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'email': 'foo@pingou.com', + } + + output = self.app.post( + '/settings/email/drop', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + self.assertIn( + '\n You must always have at least one email', + output.data) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post('/settings/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'email': 'foo@pingou.com', + } + + output = self.app.post( + '/settings/email/drop', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertEqual(output.data.count('foo@pingou.com'), 4) + + data = { + 'csrf_token': csrf_token, + 'email': 'foobar@pingou.com', + } + + output = self.app.post( + '/settings/email/drop', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '\n You do not have the ' + 'email: foobar@pingou.com, nothing to remove', output.data) + + data = { + 'csrf_token': csrf_token, + 'email': 'foo@pingou.com', + } + + output = self.app.post( + '/settings/email/drop', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertEqual(output.data.count('foo@pingou.com'), 0) + self.assertEqual(output.data.count('bar@pingou.com'), 3) + + output = self.app.post( + '/settings/email/drop', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertEqual(output.data.count('foo@pingou.com'), 0) + self.assertEqual(output.data.count('bar@pingou.com'), 3) + + ast.return_value = True + output = self.app.post('/settings/email/drop', data=data) + self.assertEqual(output.status_code, 302) + + @patch('pagure.lib.notify.send_email') + @patch('pagure.ui.app.admin_session_timedout') + def test_add_user_email(self, ast, send_email): + """ Test the add_user_email endpoint. """ + send_email.return_value = True + ast.return_value = False + self.test_new_project() + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/settings/email/add') + self.assertEqual(output.status_code, 404) + self.assertTrue('

    Page not found (404)

    ' in output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/settings/email/add') + self.assertEqual(output.status_code, 200) + + self.assertTrue("Add new email" in output.data) + self.assertIn( + '', output.data) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post('/settings/email/add') + self.assertEqual(output.status_code, 200) + self.assertTrue("Add new email" in output.data) + self.assertIn( + '', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'email': 'foo2@pingou.com', + } + + output = self.app.post( + '/settings/email/add', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue("Add new email" in output.data) + self.assertEqual(output.data.count('foo2@pingou.com'), 1) + + # New email + data = { + 'csrf_token': csrf_token, + 'email': 'foobar@pingou.com', + } + + output = self.app.post( + '/settings/email/add', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '\n Email pending validation', + output.data) + self.assertEqual(output.data.count('foo@pingou.com'), 4) + self.assertEqual(output.data.count('bar@pingou.com'), 4) + self.assertEqual(output.data.count('foobar@pingou.com'), 1) + + # User already has this email + data = { + 'csrf_token': csrf_token, + 'email': 'foo@pingou.com', + } + + output = self.app.post( + '/settings/email/add', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue("Add new email" in output.data) + self.assertIn( + 'Invalid value, can't be any of: bar@pingou.com, ' + 'foo@pingou.com. ', output.data) + self.assertEqual(output.data.count('foo@pingou.com'), 5) + self.assertEqual(output.data.count('bar@pingou.com'), 4) + self.assertEqual(output.data.count('foobar@pingou.com'), 0) + + # Email registered by someone else + data = { + 'csrf_token': csrf_token, + 'email': 'foo@bar.com', + } + + output = self.app.post( + '/settings/email/add', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue("Add new email" in output.data) + self.assertIn( + 'Invalid value, can't be any of: foo@bar.com. ', + output.data) + + ast.return_value = True + output = self.app.post('/settings/email/add', data=data) + self.assertEqual(output.status_code, 302) + + @patch('pagure.lib.notify.send_email') + @patch('pagure.ui.app.admin_session_timedout') + def test_set_default_email(self, ast, send_email): + """ Test the set_default_email endpoint. """ + send_email.return_value = True + ast.return_value = False + self.test_new_project() + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/settings/email/default') + self.assertEqual(output.status_code, 404) + self.assertTrue('

    Page not found (404)

    ' in output.data) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/settings/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'email': 'foo@pingou.com', + } + + output = self.app.post( + '/settings/email/default', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertEqual(output.data.count('foo@pingou.com'), 4) + + # Set invalid default email + data = { + 'csrf_token': csrf_token, + 'email': 'foobar@pingou.com', + } + + output = self.app.post( + '/settings/email/default', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertEqual(output.data.count('foo@pingou.com'), 4) + self.assertIn( + '\n You do not have the ' + 'email: foobar@pingou.com, nothing to set', + output.data) + + # Set default email + data = { + 'csrf_token': csrf_token, + 'email': 'foo@pingou.com', + } + + output = self.app.post( + '/settings/email/default', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertEqual(output.data.count('foo@pingou.com'), 4) + self.assertIn( + '\n Default email set to: ' + 'foo@pingou.com', output.data) + + ast.return_value = True + output = self.app.post('/settings/email/default', data=data) + self.assertEqual(output.status_code, 302) + + @patch('pagure.ui.app.admin_session_timedout') + def test_confirm_email(self, ast): + """ Test the confirm_email endpoint. """ + output = self.app.get('/settings/email/confirm/foobar') + self.assertEqual(output.status_code, 302) + + ast.return_value = False + + # Add a pending email to pingou + userobj = pagure.lib.search_user(self.session, username='pingou') + + self.assertEqual(len(userobj.emails), 2) + + email_pend = pagure.lib.model.UserEmailPending( + user_id=userobj.id, + email='foo@fp.o', + token='abcdef', + ) + self.session.add(email_pend) + self.session.commit() + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + # Wrong token + output = self.app.get( + '/settings/email/confirm/foobar', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '\n No email associated with this token.', + output.data) + + # Confirm email + output = self.app.get( + '/settings/email/confirm/abcdef', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '
    \n Basic Information\n' + '
    ', output.data) + self.assertIn( + '\n Email validated', + output.data) + + userobj = pagure.lib.search_user(self.session, username='pingou') + self.assertEqual(len(userobj.emails), 3) + + ast.return_value = True + output = self.app.get('/settings/email/confirm/foobar') + self.assertEqual(output.status_code, 302) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskApptests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_ui_fork.py b/tests/test_pagure_flask_ui_fork.py new file mode 100644 index 0000000..83ddef5 --- /dev/null +++ b/tests/test_pagure_flask_ui_fork.py @@ -0,0 +1,1681 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +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 PagureFlaskForktests(tests.Modeltests): + """ Tests for flask fork controller of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskForktests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.lib.SESSION = self.session + pagure.ui.SESSION = self.session + pagure.ui.app.SESSION = self.session + pagure.ui.filters.SESSION = self.session + pagure.ui.fork.SESSION = self.session + pagure.ui.repo.SESSION = self.session + pagure.ui.issues.SESSION = self.session + + pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos') + pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks') + pagure.APP.config['TICKETS_FOLDER'] = os.path.join( + tests.HERE, 'tickets') + pagure.APP.config['DOCS_FOLDER'] = os.path.join( + tests.HERE, 'docs') + pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( + tests.HERE, 'requests') + self.app = pagure.APP.test_client() + + def set_up_git_repo( + self, new_project=None, branch_from='feature', mtype='FF'): + """ Set up the git repo and create the corresponding PullRequest + object. + """ + + # Create a git repo to play with + gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') + repo = pygit2.init_repository(gitrepo, bare=True) + + newpath = tempfile.mkdtemp(prefix='pagure-fork-test') + repopath = os.path.join(newpath, 'test') + clone_repo = pygit2.clone_repository(gitrepo, repopath) + + # Create a file in that git repo + with open(os.path.join(repopath, 'sources'), 'w') as stream: + stream.write('foo\n bar') + clone_repo.index.add('sources') + clone_repo.index.write() + + # Commits the files added + tree = clone_repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + clone_repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add sources file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [] + ) + refname = 'refs/heads/master:refs/heads/master' + ori_remote = clone_repo.remotes[0] + PagureRepo.push(ori_remote, refname) + + first_commit = repo.revparse_single('HEAD') + + if mtype == 'merge': + with open(os.path.join(repopath, '.gitignore'), 'w') as stream: + stream.write('*~') + clone_repo.index.add('.gitignore') + clone_repo.index.write() + + # Commits the files added + tree = clone_repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + clone_repo.create_commit( + 'refs/heads/master', + author, + committer, + 'Add .gitignore file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + refname = 'refs/heads/master:refs/heads/master' + ori_remote = clone_repo.remotes[0] + PagureRepo.push(ori_remote, refname) + + if mtype == 'conflicts': + with open(os.path.join(repopath, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz') + clone_repo.index.add('sources') + clone_repo.index.write() + + # Commits the files added + tree = clone_repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + clone_repo.create_commit( + 'refs/heads/master', + author, + committer, + 'Add sources conflicting', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + refname = 'refs/heads/master:refs/heads/master' + ori_remote = clone_repo.remotes[0] + PagureRepo.push(ori_remote, refname) + + # Set the second repo + + new_gitrepo = repopath + if new_project: + # Create a new git repo to play with + new_gitrepo = os.path.join(newpath, new_project.fullname) + if not os.path.exists(new_gitrepo): + os.makedirs(new_gitrepo) + new_repo = pygit2.clone_repository(gitrepo, new_gitrepo) + + repo = pygit2.Repository(new_gitrepo) + + if mtype != 'nochanges': + # Edit the sources file again + with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/%s' % branch_from, + author, + committer, + 'A commit on branch %s' % branch_from, + tree, + [first_commit.oid.hex] + ) + refname = 'refs/heads/%s' % (branch_from) + ori_remote = repo.remotes[0] + PagureRepo.push(ori_remote, refname) + + # Create a PR for these changes + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=project, + branch_from=branch_from, + repo_to=project, + branch_to='master', + title='PR from the %s branch' % branch_from, + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the %s branch' % branch_from) + + shutil.rmtree(newpath) + + @patch('pagure.lib.notify.send_email') + def test_request_pull(self, send_email): + """ Test the request_pull endpoint. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + + # Non-existant project + output = self.app.get('/foobar/pull-request/1') + self.assertEqual(output.status_code, 404) + + # Project has no PR + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 404) + + self.set_up_git_repo(new_project=None, branch_from='feature') + + project = pagure.lib.get_project(self.session, 'test') + self.assertEqual(len(project.requests), 1) + + # View the pull-request + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertIn( + 'title="View file as of 2a552b">View
    ', output.data) + + @patch('pagure.lib.notify.send_email') + def test_merge_request_pull_FF(self, send_email): + """ Test the merge_request_pull endpoint with a FF PR. """ + send_email.return_value = True + + self.test_request_pull() + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # No CSRF + output = self.app.post( + '/test/pull-request/1/merge', data={}, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#1: PR from the feature branch - test\n - ' + 'Pagure', output.data) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertIn( + 'title="View file as of 2a552b">View', output.data) + + # Wrong project + data = { + 'csrf_token': csrf_token, + } + output = self.app.post( + '/foobar/pull-request/100/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Wrong project + data = { + 'csrf_token': csrf_token, + } + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + + # Wrong request id + data = { + 'csrf_token': csrf_token, + } + output = self.app.post( + '/test/pull-request/100/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + # Pull-request disabled + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Project w pull-request but only assignee can merge + settings['pull_requests'] = True + settings['Only_assignee_can_merge_pull-request'] = True + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#1: PR from the feature branch - test\n - ' + 'Pagure', output.data) + self.assertIn( + '

    PR#1 PR from ' + 'the feature branch ', + output.data) + self.assertIn( + '\n This request must be ' + 'assigned to be merged', output.data) + + # PR assigned but not to this user + repo = pagure.lib.get_project(self.session, 'test') + req = repo.requests[0] + req.assignee_id = 2 + self.session.add(req) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 PR from ' + 'the feature branch ', + output.data) + self.assertIn( + '\n Only the assignee can ' + 'merge this review', output.data) + + # Project w/ minimal PR score + settings['Only_assignee_can_merge_pull-request'] = False + settings['Minimum_score_to_merge_pull-request'] = 2 + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 PR from ' + 'the feature branch ', + output.data) + self.assertIn( + '\n This request does not ' + 'have the minimum review score necessary to be merged', + output.data) + + # Merge + settings['Minimum_score_to_merge_pull-request'] = -1 + repo.settings = settings + self.session.add(repo) + self.session.commit() + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Changes merged!', + output.data) + + @patch('pagure.lib.notify.send_email') + def test_merge_request_pull_merge(self, send_email): + """ Test the merge_request_pull endpoint with a merge PR. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + self.set_up_git_repo( + new_project=None, branch_from='feature', mtype='merge') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + } + + # Merge + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Changes merged!', + output.data) + + @patch('pagure.lib.notify.send_email') + def test_merge_request_pull_conflicts(self, send_email): + """ Test the merge_request_pull endpoint with a conflicting PR. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + self.set_up_git_repo( + new_project=None, branch_from='feature', mtype='conflicts') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + } + + # Merge conflicts + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 PR from ' + 'the feature branch ', + output.data) + self.assertIn( + '\n Merge conflicts!', + output.data) + + @patch('pagure.lib.notify.send_email') + def test_merge_request_pull_nochange(self, send_email): + """ Test the merge_request_pull endpoint. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + self.set_up_git_repo( + new_project=None, branch_from='master', mtype='nochanges') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + } + + # Nothing to merge + output = self.app.post( + '/test/pull-request/1/merge', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 ' + 'PR from the master branch

    ', output.data) + self.assertIn( + '\n Nothing to do, changes ' + 'were already merged', output.data) + + @patch('pagure.lib.notify.send_email') + def test_request_pull_close(self, send_email): + """ Test the request_pull endpoint with a closed PR. """ + send_email.return_value = True + + self.test_merge_request_pull_FF() + + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertIn( + 'Merged by', output.data) + self.assertIn( + 'title="View file as of 2a552b">View', output.data) + + @patch('pagure.lib.notify.send_email') + def test_request_pull_disabled(self, send_email): + """ Test the request_pull endpoint with PR disabled. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + self.set_up_git_repo(new_project=None, branch_from='feature') + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.notify.send_email') + def test_request_pull_empty_repo(self, send_email): + """ Test the request_pull endpoint against an empty repo. """ + send_email.return_value = True + + tests.create_projects(self.session) + item = pagure.lib.model.Project( + user_id=2, # foo + name='test', + description='test project #1', + hook_token='aaabbb', + parent_id=1, + ) + self.session.add(item) + self.session.commit() + + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + tests.create_projects_git( + os.path.join(tests.HERE, 'forks', 'foo'), bare=True) + + # Create a git repo to play with + gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') + self.assertFalse(os.path.exists(gitrepo)) + os.makedirs(gitrepo) + repo = pygit2.init_repository(gitrepo, bare=True) + + # Create a fork of this repo + newpath = tempfile.mkdtemp(prefix='pagure-fork-test') + gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') + new_repo = pygit2.clone_repository(gitrepo, newpath) + + # Edit the sources file again + with open(os.path.join(newpath, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + new_repo.index.add('sources') + new_repo.index.write() + + # Commits the files added + tree = new_repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + new_repo.create_commit( + 'refs/heads/feature', + author, + committer, + 'A commit on branch feature', + tree, + [] + ) + refname = 'refs/heads/feature:refs/heads/feature' + ori_remote = new_repo.remotes[0] + PagureRepo.push(ori_remote, refname) + + # Create a PR for these changes + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=item, + branch_from='feature', + repo_to=project, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertTrue( + output.data.count('Overview - test - Pagure', output.data) + self.assertIn( + '\n Fork is empty, there are no ' + 'commits to request pulling', output.data) + + shutil.rmtree(newpath) + + @patch('pagure.lib.notify.send_email') + def test_request_pulls(self, send_email): + """ Test the request_pulls endpoint. """ + send_email.return_value = True + + # No such project + output = self.app.get('/test/pull-requests') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/pull-requests') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Pull Requests 0', + output.data) + # Open is primary + self.assertIn( + 'Open', output.data) + self.assertIn( + 'Closed', output.data) + + self.set_up_git_repo(new_project=None, branch_from='feature') + + output = self.app.get('/test/pull-requests') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Pull Requests 1', + output.data) + # Open is primary + self.assertIn( + 'Open', output.data) + self.assertIn( + 'Closed', output.data) + + output = self.app.get('/test/pull-requests?status=Closed') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Closed Pull Requests 0', + output.data) + # Close is primary + self.assertIn( + 'Open', output.data) + self.assertIn( + 'Closed', output.data) + + output = self.app.get('/test/pull-requests?status=0') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Closed/Merged Pull Requests 0', + output.data) + # Close is primary + self.assertIn( + 'Open', output.data) + self.assertIn( + 'Closed', output.data) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.get('/test/pull-requests') + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.notify.send_email') + def test_request_pull_patch(self, send_email): + """ Test the request_pull_patch endpoint. """ + send_email.return_value = True + + output = self.app.get('/test/pull-request/1.patch') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + self.set_up_git_repo( + new_project=None, branch_from='feature', mtype='merge') + + output = self.app.get('/test/pull-request/100.patch') + self.assertEqual(output.status_code, 404) + + output = self.app.get('/test/pull-request/1.patch') + self.assertEqual(output.status_code, 200) + + npatch = [] + for row in output.data.split('\n'): + if row.startswith('Date:'): + continue + if row.startswith('From '): + row = row.split(' ', 2)[2] + npatch.append(row) + + exp = """Mon Sep 17 00:00:00 2001 +From: Alice Author +Subject: A commit on branch feature + + +--- + +diff --git a/.gitignore b/.gitignore +new file mode 100644 +index 0000000..e4e5f6c +--- /dev/null ++++ b/.gitignore +@@ -0,0 +1 @@ ++*~ +\ No newline at end of file +diff --git a/sources b/sources +index 9f44358..2a552bb 100644 +--- a/sources ++++ b/sources +@@ -1,2 +1,4 @@ + foo +- bar +\ No newline at end of file ++ bar ++baz ++ boose +\ No newline at end of file + +""" + + patch = '\n'.join(npatch) + #print patch + self.assertEqual(patch, exp) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.get('/test/pull-request/1.patch') + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.notify.send_email') + def test_request_pull_patch_close(self, send_email): + """ Test the request_pull_patch endpoint with a closed PR. """ + send_email.return_value = True + + self.test_merge_request_pull_FF() + + output = self.app.get('/test/pull-request/1.patch') + self.assertEqual(output.status_code, 200) + + npatch = [] + for row in output.data.split('\n'): + if row.startswith('Date:'): + continue + if row.startswith('From '): + row = row.split(' ', 2)[2] + npatch.append(row) + + exp = """Mon Sep 17 00:00:00 2001 +From: Alice Author +Subject: A commit on branch feature + + +--- + +diff --git a/sources b/sources +index 9f44358..2a552bb 100644 +--- a/sources ++++ b/sources +@@ -1,2 +1,4 @@ + foo +- bar +\ No newline at end of file ++ bar ++baz ++ boose +\ No newline at end of file + +""" + + patch = '\n'.join(npatch) + #print patch + self.assertEqual(patch, exp) + + @patch('pagure.lib.notify.send_email') + def test_request_pull_patch_empty_repo(self, send_email): + """ Test the request_pull_patch endpoint against an empty repo. """ + send_email.return_value = True + + tests.create_projects(self.session) + item = pagure.lib.model.Project( + user_id=2, # foo + name='test', + description='test project #1', + hook_token='aaabbb', + parent_id=1, + ) + self.session.add(item) + self.session.commit() + + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + tests.create_projects_git( + os.path.join(tests.HERE, 'forks', 'foo'), bare=True) + + # Create a git repo to play with + gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') + self.assertFalse(os.path.exists(gitrepo)) + os.makedirs(gitrepo) + repo = pygit2.init_repository(gitrepo, bare=True) + + # Create a fork of this repo + newpath = tempfile.mkdtemp(prefix='pagure-fork-test') + gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') + new_repo = pygit2.clone_repository(gitrepo, newpath) + + # Edit the sources file again + with open(os.path.join(newpath, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + new_repo.index.add('sources') + new_repo.index.write() + + # Commits the files added + tree = new_repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + new_repo.create_commit( + 'refs/heads/feature', + author, + committer, + 'A commit on branch feature', + tree, + [] + ) + refname = 'refs/heads/feature:refs/heads/feature' + ori_remote = new_repo.remotes[0] + PagureRepo.push(ori_remote, refname) + + # Create a PR for these "changes" (there are none, both repos are + # empty) + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=item, + branch_from='feature', + repo_to=project, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + output = self.app.get('/test/pull-request/1.patch', follow_redirects=True) + self.assertEqual(output.status_code, 200) + + npatch = [] + for row in output.data.split('\n'): + if row.startswith('Date:'): + continue + if row.startswith('From '): + row = row.split(' ', 2)[2] + npatch.append(row) + + exp = """Mon Sep 17 00:00:00 2001 +From: Alice Author +Subject: A commit on branch feature + + +--- + +diff --git a/sources b/sources +new file mode 100644 +index 0000000..2a552bb +--- /dev/null ++++ b/sources +@@ -0,0 +1,4 @@ ++foo ++ bar ++baz ++ boose +\ No newline at end of file + +""" + + patch = '\n'.join(npatch) + #print patch + self.assertEqual(patch, exp) + + shutil.rmtree(newpath) + + @patch('pagure.lib.notify.send_email') + def test_request_pull_patch_empty_fork(self, send_email): + """ Test the request_pull_patch endpoint from an empty fork. """ + send_email.return_value = True + + tests.create_projects(self.session) + item = pagure.lib.model.Project( + user_id=2, # foo + name='test', + description='test project #1', + hook_token='aaabbb', + parent_id=1, + ) + self.session.add(item) + self.session.commit() + + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + tests.create_projects_git( + os.path.join(tests.HERE, 'forks', 'foo'), bare=True) + + # Create a git repo to play with + gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') + self.assertFalse(os.path.exists(gitrepo)) + os.makedirs(gitrepo) + repo = pygit2.init_repository(gitrepo, bare=True) + + # Create a fork of this repo + newpath = tempfile.mkdtemp(prefix='pagure-fork-test') + gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') + new_repo = pygit2.clone_repository(gitrepo, newpath) + + # Create a PR for these "changes" (there are none, both repos are + # empty) + project = pagure.lib.get_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=item, + branch_from='feature', + repo_to=project, + branch_to='master', + title='PR from the feature branch', + user='pingou', + requestfolder=None, + + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'PR from the feature branch') + + output = self.app.get('/test/pull-request/1.patch', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Fork is empty, there are no ' + 'commits to request pulling', output.data) + + shutil.rmtree(newpath) + + @patch('pagure.lib.notify.send_email') + def test_cancel_request_pull(self, send_email): + """ Test the cancel_request_pull endpoint. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + self.set_up_git_repo( + new_project=None, branch_from='feature', mtype='merge') + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/test/pull-request/cancel/1') + self.assertEqual(output.status_code, 302) + + output = self.app.post( + '/test/pull-request/cancel/1', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Invalid input submitted', + output.data) + + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + } + + # Invalid project + output = self.app.post( + '/foo/pull-request/cancel/1', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Invalid PR id + output = self.app.post( + '/test/pull-request/cancel/100', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Invalid user for this project + output = self.app.post( + '/test/pull-request/cancel/1', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/cancel/1', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = True + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/cancel/1', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Request pull canceled!', + output.data) + + @patch('pagure.lib.notify.send_email') + def test_set_assignee_requests(self, send_email): + """ Test the set_assignee_requests endpoint. """ + send_email.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + self.set_up_git_repo(new_project=None, branch_from='feature') + + # No such project + user = tests.FakeUser() + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/foo/pull-request/1/assign') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/test/pull-request/100/assign') + self.assertEqual(output.status_code, 404) + + # Invalid input + output = self.app.post( + '/test/pull-request/1/assign', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#1: PR from the feature branch - test\n - ' + 'Pagure', output.data) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertNotIn( + '\n Request assigned', + output.data) + + output = self.app.get('/test/pull-request/1') + self.assertEqual(output.status_code, 200) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'user': 'pingou', + } + + # No CSRF + output = self.app.post( + '/test/pull-request/1/assign', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#1: PR from the feature branch - test\n - ' + 'Pagure', output.data) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertNotIn( + '\n Request assigned', + output.data) + + # Invalid assignee + data = { + 'csrf_token': csrf_token, + 'user': 'bar', + } + + output = self.app.post( + '/test/pull-request/1/assign', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#1: PR from the feature branch - test\n - ' + 'Pagure', output.data) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertIn( + '\n No user "bar" found', + output.data) + + # Assign the PR + data = { + 'csrf_token': csrf_token, + 'user': 'pingou', + } + + output = self.app.post( + '/test/pull-request/1/assign', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#1: PR from the feature branch - test\n - ' + 'Pagure', output.data) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + self.assertIn( + '\n Request assigned', + output.data) + + # Pull-Request closed + repo = pagure.lib.get_project(self.session, 'test') + req = repo.requests[0] + req.status = 'Closed' + req.closed_by_in = 1 + self.session.add(req) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/assign', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 403) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/assign', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.notify.send_email') + def test_fork_project(self, send_email): + """ Test the fork_project endpoint. """ + send_email.return_value = True + + tests.create_projects(self.session) + for folder in ['docs', 'tickets', 'requests', 'repos']: + tests.create_projects_git( + os.path.join(tests.HERE, folder), bare=True) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post('/do_fork/test') + self.assertEqual(output.status_code, 400) + + output = self.app.get('/new/') + self.assertEqual(output.status_code, 200) + self.assertIn('Create new Project', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + } + + output = self.app.post( + '/do_fork/foo', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + output = self.app.post( + '/do_fork/test', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n You may not fork your ' + 'own repo', output.data) + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/do_fork/test') + self.assertEqual(output.status_code, 400) + + data = { + 'csrf_token': csrf_token, + } + + output = self.app.post( + '/do_fork/test', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Repo "test" ' + 'cloned to "foo/test"', output.data) + + @patch('pagure.lib.notify.send_email') + def test_new_request_pull(self, send_email): + """ Test the new_request_pull endpoint. """ + send_email.return_value = True + + self.test_fork_project() + + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + + repo = pagure.lib.get_project(self.session, 'test') + fork = pagure.lib.get_project(self.session, 'test', user='foo') + + self.set_up_git_repo( + new_project=fork, branch_from='feature', mtype='FF') + + user = tests.FakeUser() + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.get('/foo/diff/master..feature') + self.assertEqual(output.status_code, 404) + + output = self.app.get('/test/diff/master..foo') + self.assertEqual(output.status_code, 400) + + output = self.app.get('/test/diff/foo..master') + self.assertEqual(output.status_code, 400) + + output = self.app.get('/test/diff/feature..master') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Diff from master to feature - test - Pagure', + output.data) + self.assertIn( + '

    No commits found

    ', output.data) + + output = self.app.get('/test/diff/master..feature') + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Diff from feature to master - test - Pagure', + output.data) + self.assertNotIn( + '', output.data) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/diff/master..feature') + self.assertEqual(output.status_code, 200) + self.assertIn( + ' Create new Pull Request for master - test - ' + 'Pagure', output.data) + self.assertIn( + '', + output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + 'title': 'foo bar PR', + } + + output = self.app.post( + '/test/diff/master..feature', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#2: foo bar PR - test\n - Pagure', + output.data) + self.assertIn( + '\n Request created', + output.data) + + @patch('pagure.lib.notify.send_email') + def test_new_request_pull_empty_repo(self, send_email): + """ Test the new_request_pull endpoint against an empty repo. """ + send_email.return_value = True + + self.test_fork_project() + + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + + repo = pagure.lib.get_project(self.session, 'test') + fork = pagure.lib.get_project(self.session, 'test', user='foo') + + # Create a git repo to play with + gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') + repo = pygit2.init_repository(gitrepo, bare=True) + + # Create a fork of this repo + newpath = tempfile.mkdtemp(prefix='pagure-fork-test') + gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') + new_repo = pygit2.clone_repository(gitrepo, newpath) + + user = tests.FakeUser() + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.get( + '/fork/foo/test/diff/master..feature', + follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Fork is empty, there are ' + 'no commits to request pulling', output.data) + + output = self.app.get('/test/new_issue') + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + 'title': 'foo bar PR', + } + + output = self.app.post( + '/test/diff/master..feature', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Fork is empty, there are ' + 'no commits to request pulling', output.data) + + shutil.rmtree(newpath) + + @patch('pagure.lib.notify.send_email') + def test_new_request_pull_empty_fork(self, send_email): + """ Test the new_request_pull endpoint against an empty repo. """ + send_email.return_value = True + + self.test_fork_project() + + tests.create_projects_git( + os.path.join(tests.HERE, 'requests'), bare=True) + + repo = pagure.lib.get_project(self.session, 'test') + fork = pagure.lib.get_project(self.session, 'test', user='foo') + + # Create a git repo to play with + gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') + repo = pygit2.init_repository(gitrepo, bare=True) + + # Create a fork of this repo + newpath = tempfile.mkdtemp(prefix='pagure-fork-test') + gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') + new_repo = pygit2.clone_repository(gitrepo, newpath) + + user = tests.FakeUser() + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.get( + '/fork/foo/test/diff/master..master', follow_redirects=True) + self.assertIn( + 'Overview - test - Pagure', output.data) + self.assertIn( + '\n Fork is empty, there are ' + 'no commits to request pulling', output.data) + + shutil.rmtree(newpath) + + @patch('pagure.lib.notify.send_email') + def test_pull_request_add_comment(self, send_email): + """ Test the pull_request_add_comment endpoint. """ + send_email.return_value = True + + self.test_request_pull() + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post('/foo/pull-request/1/comment') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/test/pull-request/100/comment') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/test/pull-request/1/comment') + self.assertEqual(output.status_code, 200) + self.assertTrue( + output.data.startswith('\n
    ')) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + 'comment': 'This look alright but we can do better', + } + output = self.app.post( + '/test/pull-request/1/comment', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'PR#1: PR from the feature branch - test\n - ' + 'Pagure', output.data) + self.assertIn( + '\n Comment added', + output.data) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/comment', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.notify.send_email') + def test_pull_request_drop_comment(self, send_email): + """ Test the pull_request_drop_comment endpoint. """ + send_email.return_value = True + + self.test_pull_request_add_comment() + # Project w/ pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = True + repo.settings = settings + self.session.add(repo) + self.session.commit() + + user = tests.FakeUser() + user.username = 'foo' + with tests.user_set(pagure.APP, user): + output = self.app.post('/foo/pull-request/1/comment/drop') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/test/pull-request/100/comment/drop') + self.assertEqual(output.status_code, 404) + + output = self.app.post( + '/test/pull-request/1/comment/drop', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 ' + 'PR from the feature branch

    ', output.data) + #self.assertIn('href="#comment-1">¶', output.data) + self.assertIn( + '

    This look alright but we can do better

    ', + output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Invalid comment id + data = { + 'csrf_token': csrf_token, + 'drop_comment': '10', + } + output = self.app.post( + '/test/pull-request/1/comment/drop', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 404) + + data['drop_comment'] = '1' + output = self.app.post( + '/test/pull-request/1/comment/drop', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + # Drop comment + output = self.app.post( + '/test/pull-request/1/comment/drop', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    PR#1 PR from ' + 'the feature branch ', + output.data) + self.assertIn( + '\n Comment removed', + output.data) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/comment/drop', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.notify.send_email') + def test_pull_request_edit_comment(self, send_email): + """ Test the pull request edit comment endpoint """ + send_email.return_value = True + + self.test_request_pull() + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + # Repo 'foo' does not exist so it is verifying that condition + output = self.app.post('/foo/pull-request/1/comment/1/edit') + self.assertEqual(output.status_code, 404) + + # Here no comment is present in the PR so its verifying that condition + output = self.app.post('/test/pull-request/100/comment/100/edit') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/test/pull-request/1/comment') + self.assertEqual(output.status_code, 200) + # Creating comment to play with + self.assertTrue( + output.data.startswith('\n
    ')) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + 'comment': 'This look alright but we can do better', + } + output = self.app.post( + '/test/pull-request/1/comment', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 200) + + self.assertIn( + '

    PR#1 PR from ' + 'the feature branch ', + output.data) + self.assertIn( + '\n Comment added', + output.data) + # Check if the comment is there + self.assertIn( + '

    This look alright but we can do better

    ', output.data) + output = self.app.get('/test/pull-request/1/comment/1/edit') + self.assertEqual(output.status_code, 200) + + self.assertIn('
    ', output.data) + # Checking if the comment is there in the update page + self.assertIn( + 'This look alright but we can do better', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'csrf_token': csrf_token, + 'update_comment': 'This look alright but we can do better than this.', + } + output = self.app.post( + '/test/pull-request/1/comment/1/edit', data=data, + follow_redirects=True) + # Checking if the comment is updated in the main page + self.assertIn( + '

    This look alright but we can do better than this.

    ', output.data) + self.assertIn( + '

    PR#1 PR from ' + 'the feature branch ', + output.data) + # Checking if Edited by User is there or not + self.assertIn( + 'Edited just now by pingou ', + output.data) + self.assertIn( + '\n Comment updated', output.data) + + # Project w/o pull-request + repo = pagure.lib.get_project(self.session, 'test') + settings = repo.settings + settings['pull_requests'] = False + repo.settings = settings + self.session.add(repo) + self.session.commit() + + output = self.app.post( + '/test/pull-request/1/comment/edit/1', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 404) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskForktests) + unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_flask_ui_groups.py b/tests/test_pagure_flask_ui_groups.py new file mode 100644 index 0000000..1a618fc --- /dev/null +++ b/tests/test_pagure_flask_ui_groups.py @@ -0,0 +1,412 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import unittest +import shutil +import sys +import os + +import json +from mock import patch + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure.lib +import tests + + +class PagureFlaskGroupstests(tests.Modeltests): + """ Tests for flask groups controller of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskGroupstests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.ui.SESSION = self.session + pagure.ui.groups.SESSION = self.session + pagure.ui.repo.SESSION = self.session + + pagure.APP.config['GIT_FOLDER'] = tests.HERE + pagure.APP.config['FORK_FOLDER'] = os.path.join( + tests.HERE, 'forks') + pagure.APP.config['TICKETS_FOLDER'] = os.path.join( + tests.HERE, 'tickets') + pagure.APP.config['DOCS_FOLDER'] = os.path.join( + tests.HERE, 'docs') + pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( + tests.HERE, 'requests') + self.app = pagure.APP.test_client() + + def test_group_lists(self): + """ Test the group_lists endpoint. """ + output = self.app.get('/groups') + self.assertIn( + '

    \n' + ' Groups 0', + output.data) + + def test_add_group(self): + """ Test the add_group endpoint. """ + output = self.app.get('/group/add') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/group/add') + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/group/add') + self.assertEqual(output.status_code, 200) + self.assertIn('

    Create group

    ', output.data) + self.assertNotIn( + '', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + } + + # Insufficient input + output = self.app.post('/group/add', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn('

    Create group

    ', output.data) + self.assertEqual(output.data.count( + 'This field is required.'), 1) + + data = { + 'group_name': 'test_group', + } + + # Missing CSRF + output = self.app.post('/group/add', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn('

    Create group

    ', output.data) + self.assertEqual(output.data.count( + 'This field is required.'), 0) + + data['csrf_token'] = csrf_token + + # All good + output = self.app.post( + '/group/add', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n User `pingou` added to ' + 'the group `test_group`.', output.data) + self.assertIn( + '\n Group `test_group` created.', + output.data) + self.assertIn( + '

    \n' + ' Groups 1', + output.data) + + user = tests.FakeUser( + username='pingou', + groups=pagure.APP.config['ADMIN_GROUP']) + with tests.user_set(pagure.APP, user): + output = self.app.get('/group/add') + self.assertEqual(output.status_code, 200) + self.assertIn('

    Create group

    ', output.data) + self.assertIn('', output.data) + + data = { + 'group_name': 'test_admin_group', + 'group_type': 'admin', + 'csrf_token': csrf_token, + } + + # All good + output = self.app.post( + '/group/add', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n User `pingou` added to ' + 'the group `test_admin_group`.', output.data) + self.assertIn( + '\n Group `test_admin_group` ' + 'created.',output.data) + self.assertIn( + '

    \n' + ' Groups 2', + output.data) + + def test_group_delete(self): + """ Test the group_delete endpoint. """ + output = self.app.post('/group/foo/delete') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/group/foo/delete', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    No groups have been created on this pagure instance ' + 'yet

    ', output.data) + self.assertIn( + '

    \n' + ' Groups 0', + output.data) + + self.test_add_group() + + with tests.user_set(pagure.APP, user): + output = self.app.post('/group/foo/delete', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '

    \n' + ' Groups 1', + output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + user.username = 'foo' + with tests.user_set(pagure.APP, user): + + data = { + 'csrf_token': csrf_token, + } + output = self.app.post( + '/group/bar/delete', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n No group `bar` found', + output.data) + self.assertIn( + '

    \n' + ' Groups 1', + output.data) + + output = self.app.post( + '/group/test_group/delete', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n You are not allowed to ' + 'delete the group test_group', output.data) + self.assertIn( + '

    \n' + ' Groups 1', + output.data) + + user.username = 'bar' + with tests.user_set(pagure.APP, user): + + output = self.app.post( + '/group/test_group/delete', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + + output = self.app.post( + '/group/test_group/delete', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Group `test_group` has ' + 'been deleted', output.data) + self.assertIn( + '

    \n' + ' Groups 0', + output.data) + + def test_view_group(self): + """ Test the view_group endpoint. """ + output = self.app.get('/group/foo') + self.assertEqual(output.status_code, 404) + + self.test_add_group() + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/group/test_group') + self.assertEqual(output.status_code, 200) + self.assertIn( + '  ' + 'test_group', output.data) + + output = self.app.get('/group/test_admin_group') + self.assertEqual(output.status_code, 404) + + user = tests.FakeUser( + username='pingou', + groups=pagure.APP.config['ADMIN_GROUP']) + with tests.user_set(pagure.APP, user): + # Admin can see group of type admins + output = self.app.get('/group/test_admin_group') + self.assertEqual(output.status_code, 200) + self.assertIn( + '  ' + 'test_admin_group', output.data) + self.assertEqual(output.data.count('')[0] + + # No CSRF + data = { + 'user': 'bar' + } + + output = self.app.post('/group/test_admin_group', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn( + '  ' + 'test_admin_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_admin_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_admin_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_group', output.data) + self.assertEqual(output.data.count('')[0] + + data = {'csrf_token': csrf_token} + + output = self.app.post( + '/group/test_group/bar/delete', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n No user `bar` found', + output.data) + self.assertIn( + '  ' + 'test_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_group', output.data) + self.assertEqual(output.data.count('  ' + 'test_group', output.data) + self.assertEqual(output.data.count(' + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import json +import unittest +import shutil +import sys +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 + + +class PagureFlaskIssuestests(tests.Modeltests): + """ Tests for flask issues controller of pagure """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskIssuestests, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.ui.SESSION = self.session + pagure.ui.app.SESSION = self.session + pagure.ui.issues.SESSION = self.session + pagure.ui.repo.SESSION = self.session + + pagure.APP.config['GIT_FOLDER'] = tests.HERE + pagure.APP.config['FORK_FOLDER'] = os.path.join( + tests.HERE, 'forks') + pagure.APP.config['TICKETS_FOLDER'] = os.path.join( + tests.HERE, 'tickets') + pagure.APP.config['DOCS_FOLDER'] = os.path.join( + tests.HERE, 'docs') + self.app = pagure.APP.test_client() + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_new_issue(self, p_send_email, p_ugt): + """ Test the new_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + output = self.app.get('/foo/new_issue') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/foo/new_issue') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/new_issue') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n New issue' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + } + + # Insufficient input + output = self.app.post('/test/new_issue', data=data) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n New issue' + in output.data) + self.assertEqual(output.data.count( + 'This field is required.'), 2) + + data['title'] = 'Test issue' + output = self.app.post('/test/new_issue', data=data) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n New issue' + in output.data) + self.assertEqual(output.data.count( + 'This field is required.'), 1) + + data['issue_content'] = 'We really should improve on this issue' + data['status'] = 'Open' + output = self.app.post('/test/new_issue', data=data) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n New issue' + in output.data) + self.assertEqual(output.data.count( + '\n This field is required.'), + 0) + + # Invalid user + data['csrf_token'] = csrf_token + output = self.app.post('/test/new_issue', data=data) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n New issue' + in output.data) + self.assertEqual(output.data.count( + 'This field is required.'), 0) + self.assertTrue( + '\n No user "username" found' + in output.data) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post( + '/test/new_issue', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Issue created', output.data) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post( + '/test/new_issue', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_new_issue_w_file(self, p_send_email, p_ugt): + """ Test the new_issue endpoint with a file. """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'tickets'), bare=True) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/new_issue') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n New issue' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + stream = open(os.path.join(tests.HERE, 'placebo.png'), 'r') + data = { + 'title': 'Test issue', + 'issue_content': 'We really should improve on this issue\n' + '', + 'status': 'Open', + 'filestream': stream, + 'enctype': 'multipart/form-data', + 'csrf_token': csrf_token, + } + + output = self.app.post( + '/test/new_issue', data=data, follow_redirects=True) + stream.close() + + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Issue created', output.data) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + stream = open(os.path.join(tests.HERE, 'placebo.png'), 'r') + data = { + 'title': 'Test issue', + 'issue_content': 'We really should improve on this issue', + 'status': 'Open', + 'filestream': stream, + 'enctype': 'multipart/form-data', + 'csrf_token': csrf_token, + } + + output = self.app.post( + '/test/new_issue', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_view_issues(self, p_send_email, p_ugt): + """ Test the view_issues endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + output = self.app.get('/foo/issues') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/issues') + self.assertEqual(output.status_code, 200) + self.assertTrue('

    test project #1

    ' in output.data) + self.assertTrue( + '

    \n 0 Open Issues' in output.data) + + # Create issues 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') + + # Whole list + output = self.app.get('/test/issues') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertTrue( + '

    \n 1 Open Issues' in output.data) + + # Status = closed + output = self.app.get('/test/issues?status=cloSED') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertTrue( + '

    \n 0 Closed Issues' in output.data) + + # Status = fixed + output = self.app.get('/test/issues?status=fixed') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertTrue( + '

    \n 0 Closed Issues' in output.data) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + output = self.app.get('/test/issues') + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_view_issue(self, p_send_email, p_ugt): + """ Test the view_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + output = self.app.get('/foo/issue/1') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 404) + + # Create issues 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') + + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertTrue( + '

    Login to comment on this ticket.

    ' + in output.data) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertFalse( + '

    Login to comment on this ticket.

    ' + in output.data) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Create private issue + 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, + private=True, + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + + # Not logged in + output = self.app.get('/test/issue/2') + self.assertEqual(output.status_code, 403) + + # Wrong user + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/2') + self.assertEqual(output.status_code, 403) + + # reporter + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/2') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_update_issue(self, p_send_email, p_ugt): + """ Test the update_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + output = self.app.get('/foo/issue/1/update') + self.assertEqual(output.status_code, 302) + + tests.create_projects(self.session) + + output = self.app.get('/test/issue/1/update') + self.assertEqual(output.status_code, 302) + + # Create issues 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') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'status': 'fixed' + } + + # Invalid repo + output = self.app.post('/bar/issue/1/update', data=data) + self.assertEqual(output.status_code, 404) + + # Non-existing issue + output = self.app.post('/test/issue/100/update', data=data) + self.assertEqual(output.status_code, 404) + + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertFalse( + '' + in output.data) + + data['status'] = 'Fixed' + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertFalse( + '' + in output.data) + + data['csrf_token'] = csrf_token + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Successfully edited issue #1', + output.data) + self.assertTrue( + '' + in output.data) + + # Add new comment + data = { + 'csrf_token': csrf_token, + 'status': 'Fixed', + 'comment': 'Woohoo a second comment !', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Comment added', + output.data) + self.assertNotIn( + '\n No changes to edit', + output.data) + self.assertTrue( + '

    Woohoo a second comment !

    ' in output.data) + self.assertEqual( + output.data.count('
    '), 2) + self.assertTrue( + '' + in output.data) + + # Add new tag + data = { + 'csrf_token': csrf_token, + 'status': 'Fixed', + 'tag': 'tag2', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Tag added: tag2', + output.data) + self.assertNotIn( + '\n No changes to edit', + output.data) + self.assertTrue( + '

    Woohoo a second comment !

    ' in output.data) + self.assertEqual( + output.data.count('
    '), 2) + self.assertTrue( + '' + in output.data) + + # Assign issue to an non-existent user + data = { + 'csrf_token': csrf_token, + 'status': 'Fixed', + 'assignee': 'ralph', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n No user "ralph" found', + output.data) + self.assertTrue( + '

    Woohoo a second comment !

    ' in output.data) + self.assertEqual( + output.data.count('
    '), 2) + self.assertTrue( + '' + in output.data) + + # Assign issue properly + data = { + 'csrf_token': csrf_token, + 'status': 'Fixed', + 'assignee': 'pingou', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Issue assigned', + output.data) + self.assertTrue( + '' in output.data) + self.assertTrue( + '

    Woohoo a second comment !

    ' in output.data) + self.assertEqual( + output.data.count('
    '), 2) + self.assertTrue( + '' + in output.data) + + # Create another issue with a dependency + 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') + + # Reset the status of the first issue + parent_issue = pagure.lib.search_issues( + self.session, repo, issueid=2) + parent_issue.status = 'Open' + # Add the dependency relationship + self.session.add(parent_issue) + issue = pagure.lib.search_issues(self.session, repo, issueid=2) + issue.parents.append(parent_issue) + self.session.add(issue) + self.session.commit() + + with tests.user_set(pagure.APP, user): + + data['csrf_token'] = csrf_token + output = self.app.post( + '/test/issue/2/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n You cannot close a ticket ' + 'that has ticket depending that are still open.', + output.data) + self.assertTrue( + '' + in output.data) + + # Create private issue + 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, + private=True, + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue') + + # Wrong user + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post( + '/test/issue/3/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 403) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1/update') + self.assertEqual(output.status_code, 302) + + # Repo not set-up for issue tracker + output = self.app.post('/test/issue/1/update', data=data) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_update_issue_drop_comment(self, p_send_email, p_ugt): + """ Test droping comment via the update_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + + # Create issues 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') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Add new comment + data = { + 'csrf_token': csrf_token, + 'comment': 'Woohoo a second comment !', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Comment added', + output.data) + self.assertTrue( + '

    Woohoo a second comment !

    ' in output.data) + self.assertEqual( + output.data.count('
    '), 2) + + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 1) + + data = { + 'csrf_token': csrf_token, + 'drop_comment': 1, + } + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + # Wrong issue id + output = self.app.post( + '/test/issue/3/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Wrong user + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 403) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + # Drop the new comment + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Comment removed', + output.data) + + # Drop non-existant comment + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 0) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_update_issue_depend(self, p_send_email, p_ugt): + """ Test adding dependency via the update_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + + # Create issues 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') + + repo = pagure.lib.get_project(self.session, 'test') + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue #2', + content='We should work on this again', + user='foo', + ticketfolder=None + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue #2') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Add a dependent ticket + data = { + 'csrf_token': csrf_token, + 'depends': '2', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Dependency added', + output.data) + + # Add an invalid dependent ticket + data = { + 'csrf_token': csrf_token, + 'depends': '2,abc', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertNotIn( + '\n Dependency added', output.data) + + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.depends_text, [2]) + self.assertEqual(issue.blocks_text, []) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_update_issue_block(self, p_send_email, p_ugt): + """ Test adding blocked issue via the update_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + + # Create issues 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') + + repo = pagure.lib.get_project(self.session, 'test') + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue #2', + content='We should work on this again', + user='foo', + ticketfolder=None + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue #2') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Add a dependent ticket + data = { + 'csrf_token': csrf_token, + 'blocks': '2', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Dependency added', + output.data) + + # Add an invalid dependent ticket + data = { + 'csrf_token': csrf_token, + 'blocks': '2,abc', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertNotIn( + '\n Dependency added', + output.data) + + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.depends_text, []) + self.assertEqual(issue.blocks_text, [2]) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_upload_issue(self, p_send_email, p_ugt): + """ Test the upload_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'tickets'), bare=True) + + # Create issues 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') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + output = self.app.post('/foo/issue/1/upload') + self.assertEqual(output.status_code, 404) + + output = self.app.post('/test/issue/100/upload') + self.assertEqual(output.status_code, 404) + + # Invalid upload + data = { + 'enctype': 'multipart/form-data', + } + output = self.app.post( + '/test/issue/1/upload', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + json_data = json.loads(output.data) + exp = {'output': 'notok'} + self.assertDictEqual(json_data, exp) + + # Attach a file to a ticket + stream = open(os.path.join(tests.HERE, 'placebo.png'), 'rb') + data = { + 'csrf_token': csrf_token, + 'filestream': stream, + 'enctype': 'multipart/form-data', + } + output = self.app.post( + '/test/issue/1/upload', data=data, follow_redirects=True) + stream.close() + self.assertEqual(output.status_code, 200) + json_data = json.loads(output.data) + + folder = os.path.dirname( + os.path.abspath(__file__))[1:].replace('/', '_') + exp = { + 'output': 'ok', + 'filelocation': '/test/issue/raw/files/8a06845923010b27bfd8' + 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a' + 'be-%s_placebo.png' % folder, + 'filename': '%s_placebo.png' % folder, + } + self.assertDictEqual(json_data, exp) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + with tests.user_set(pagure.APP, user): + output = self.app.post('/test/issue/1/upload') + self.assertEqual(output.status_code, 404) + + def test_view_issue_raw_file_empty(self): + """ Test the view_issue_raw_file endpoint. """ + # Create the project and git repos + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(tests.HERE, 'tickets'), bare=True) + + # Create issues 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') + + url = '/issue/raw/files/8a06845923010b27bfd8'\ + 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\ + 'be-home_pierrey_repos_gitrepo_pagure_tests'\ + '_placebo.png' + + output = self.app.get('/foo' + url) + self.assertEqual(output.status_code, 404) + + output = self.app.get('/test' + url) + self.assertEqual(output.status_code, 404) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + output = self.app.get('/test' + url) + self.assertEqual(output.status_code, 404) + + def test_view_issue_raw_file(self): + """ Test the view_issue_raw_file endpoint. """ + # Create the issue and upload to it + self.test_upload_issue() + + # Project w/ issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': True} + self.session.add(repo) + self.session.commit() + + url = '/issue/raw/files/8a06845923010b27bfd8'\ + 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\ + 'be-%s_placebo.png' % os.path.dirname( + os.path.abspath(__file__))[1:].replace('/', '_') + + output = self.app.get('/foo' + url) + self.assertEqual(output.status_code, 404) + + output = self.app.get('/test/issue/raw/files/test.png') + self.assertEqual(output.status_code, 404) + + # Access file by name + output = self.app.get('/test' + url) + self.assertEqual(output.status_code, 200) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + output = self.app.get('/test' + url) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_edit_issue(self, p_send_email, p_ugt): + """ Test the edit_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + output = self.app.get('/foo/issue/1/edit') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/foo/issue/1/edit') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/issue/1/edit') + self.assertEqual(output.status_code, 404) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1/edit') + self.assertEqual(output.status_code, 404) + + # Create issues 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') + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1/edit') + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1/edit') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n Edit ' + 'issue #1\n
    ' in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + 'issue_content': 'We should work on this!' + } + + output = self.app.post('/test/issue/1/edit', data=data) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n Edit ' + 'issue #1\n
    ' in output.data) + self.assertEqual(output.data.count( + '\n This field is required' + '. \n '), 1) + self.assertEqual(output.data.count( + '\n Not a valid choice' + ' \n '), 1) + + data['status'] = 'Open' + data['title'] = 'Test issue #1' + output = self.app.post('/test/issue/1/edit', data=data) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '
    \n Edit ' + 'issue #1\n
    ' in output.data) + self.assertEqual(output.data.count( + '\n This field is required' + '. \n '), 0) + self.assertEqual(output.data.count( + '\n Not a valid choice' + '. \n '), 0) + + data['csrf_token'] = csrf_token + output = self.app.post( + '/test/issue/1/edit', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + '\n Successfully edited issue #1', + output.data) + self.assertIn( + '#1\n' + ' Test issue #1', + output.data) + self.assertEqual(output.data.count( + ''), 1) + self.assertEqual(output.data.count( + '
    \n' + '

    We should work on this!

    '), 1) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post('/test/issue/1/edit', data=data) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_edit_tag(self, p_send_email, p_ugt): + """ Test the edit_tag endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + output = self.app.get('/foo/tag/foo/edit') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.get('/foo/tag/foo/edit') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/tag/foo/edit') + self.assertEqual(output.status_code, 403) + + # Create issues 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') + + # Add a tag to the issue + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + msg = pagure.lib.add_tag_obj( + session=self.session, + obj=issue, + tags='tag1', + user='pingou', + ticketfolder=None) + self.session.commit() + self.assertEqual(msg, 'Tag added: tag1') + + # Before edit, list tags + tags = pagure.lib.get_tags_of_project(self.session, repo) + self.assertEqual([tag.tag for tag in tags], ['tag1']) + + # Edit tag + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/tag/tag1/edit') + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Edit tag: tag1

    ' in output.data) + self.assertTrue( + '

    Enter in the field below the new name for the tag: ' + '"tag1"

    ' in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = {'tag': 'tag2'} + + output = self.app.post('/test/tag/tag1/edit', data=data) + self.assertEqual(output.status_code, 200) + self.assertTrue('

    Edit tag: tag1

    ' in output.data) + self.assertTrue( + '

    Enter in the field below the new name for the tag: ' + '"tag1"

    ' in output.data) + + data['csrf_token'] = csrf_token + tests.create_projects_git(tests.HERE) + output = self.app.post( + '/test/tag/tag1/edit', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Edited tag: tag1 to tag2', + output.data) + + # After edit, list tags + tags = pagure.lib.get_tags_of_project(self.session, repo) + self.assertEqual([tag.tag for tag in tags], ['tag2']) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_remove_tag(self, p_send_email, p_ugt): + """ Test the remove_tag endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + output = self.app.post('/foo/droptag/') + self.assertEqual(output.status_code, 302) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post('/foo/droptag/') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.post('/test/droptag/') + self.assertEqual(output.status_code, 403) + + # Create issues 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') + + # Add a tag to the issue + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + msg = pagure.lib.add_tag_obj( + session=self.session, + obj=issue, + tags='tag1', + user='pingou', + ticketfolder=None) + self.session.commit() + self.assertEqual(msg, 'Tag added: tag1') + + # Before edit, list tags + tags = pagure.lib.get_tags_of_project(self.session, repo) + self.assertEqual([tag.tag for tag in tags], ['tag1']) + + # Edit tag + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + tests.create_projects_git(tests.HERE) + output = self.app.post( + '/test/droptag/', data={}, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + 'Settings - test - Pagure' in output.data) + self.assertTrue("

    Settings for test

    " in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = {'tag': 'tag1'} + + output = self.app.post( + '/test/droptag/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue("

    Settings for test

    " in output.data) + + data['csrf_token'] = csrf_token + output = self.app.post( + '/test/droptag/', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue("

    Settings for test

    " in output.data) + self.assertIn( + '\n Removed tag: tag1', + output.data) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_delete_issue(self, p_send_email, p_ugt): + """ Test the delete_issue endpoint. """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + tests.create_projects_git(tests.HERE) + tests.create_projects_git(os.path.join(tests.HERE, 'tickets')) + + # Create issues 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') + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + output = self.app.post( + '/foo/issue/1/drop', follow_redirects=True) + self.assertEqual(output.status_code, 404) + + output = self.app.post( + '/test/issue/100/drop', follow_redirects=True) + self.assertEqual(output.status_code, 404) + + output = self.app.post( + '/test/issue/1/drop', follow_redirects=True) + self.assertEqual(output.status_code, 403) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post( + '/test/issue/1/drop', follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Issue #1 - test - Pagure', output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + data = { + } + + # No CSRF token + output = self.app.post( + '/test/issue/1/drop', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Issue #1 - test - Pagure', output.data) + + data['csrf_token'] = csrf_token + output = self.app.post( + '/test/issue/1/drop', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Issues - test - Pagure', output.data) + self.assertIn( + '\n Issue deleted', output.data) + + # Project w/o issue tracker + repo = pagure.lib.get_project(self.session, 'test') + repo.settings = {'issue_tracker': False} + self.session.add(repo) + self.session.commit() + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.post('/test/issue/1/drop', data=data) + self.assertEqual(output.status_code, 404) + + @patch('pagure.lib.git.update_git') + @patch('pagure.lib.notify.send_email') + def test_update_issue_edit_comment(self, p_send_email, p_ugt): + """ Test the issues edit comment endpoint """ + p_send_email.return_value = True + p_ugt.return_value = True + + tests.create_projects(self.session) + + # Create issues 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') + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1') + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Add new comment + data = { + 'csrf_token': csrf_token, + 'comment': 'Woohoo a second comment !', + } + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Comment added', + output.data) + self.assertTrue( + '

    Woohoo a second comment !

    ' in output.data) + self.assertEqual( + output.data.count('
    '), 2) + + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 1) + self.assertEqual(issue.comments[0].comment, 'Woohoo a second comment !') + + data = { + 'csrf_token': csrf_token, + 'edit_comment': 1, + 'update_comment': 'Updated comment', + } + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + # Wrong issue id + output = self.app.post( + '/test/issue/3/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 404) + + # Wrong user + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 403) + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + # Edit comment + output = self.app.post( + '/test/issue/1/update', data=data, follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertIn( + '\n Comment updated', + output.data) + + repo = pagure.lib.get_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(len(issue.comments), 1) + self.assertEqual(issue.comments[0].comment, 'Updated comment') + + with tests.user_set(pagure.APP, user): + output = self.app.get('/test/issue/1/comment/1/edit') + self.assertTrue( + '

    test project #1

    ' + in output.data) + self.assertTrue('
    ' in output.data) + self.assertTrue('
    ' in output.data) + self.assertTrue( + '', + output.data) + + csrf_token = output.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # View what's supposed to be an image + output = self.app.get('/test/edit/master/f/test.jpg') + self.assertEqual(output.status_code, 400) + self.assertIn('

    Cannot edit binary files

    ', output.data) + + # Check file before the commit: + output = self.app.get('/test/raw/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertEqual(output.data, 'foo\n bar') + + # No CSRF Token + data = { + 'content': 'foo\n bar\n baz', + 'commit_title': 'test commit', + 'commit_message': 'Online commits from the tests', + } + output = self.app.post('/test/edit/master/f/sources', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Edit - test - Pagure', output.data) + + # Check that nothing changed + output = self.app.get('/test/raw/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertEqual(output.data, 'foo\n bar') + + # Missing email + data['csrf_token'] = csrf_token + output = self.app.post('/test/edit/master/f/sources', data=data) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Edit - test - Pagure', output.data) + + # Invalid email + data['email'] = 'pingou@fp.o' + output = self.app.post('/test/edit/master/f/sources', data=data) + self.assertIn( + 'Edit - test - Pagure', output.data) + + # Works + data['email'] = 'bar@pingou.com' + data['branch'] = 'master' + output = self.app.post( + '/test/edit/master/f/sources', data=data, + follow_redirects=True) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Logs - test - Pagure', output.data) + self.assertIn( + '\n Changes committed', + output.data) + + # Check file after the commit: + output = self.app.get('/test/raw/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertEqual(output.data, 'foo\n bar\n baz') + + # Add a fork of a fork + item = pagure.lib.model.Project( + user_id=1, # pingou + name='test3', + description='test project #3', + parent_id=1, + hook_token='aaabbbppp', + ) + self.session.add(item) + self.session.commit() + + tests.add_content_git_repo( + os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) + tests.add_readme_git_repo( + os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) + tests.add_commit_git_repo( + os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'), + ncommits=10) + + output = self.app.get('/fork/pingou/test3/edit/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
  • ' + '  master' + '
  • ' + '  sources' + '
  • ', output.data) + self.assertIn( + '', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'ssh_key': 'this is my ssh key', - } - - output = self.app.post('/settings/', data=data) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '', output.data) - - data['csrf_token'] = csrf_token - - output = self.app.post( - '/settings/', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '\n Public ssh key updated' - in output.data) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '', output.data) - - ast.return_value = True - output = self.app.get('/settings/') - self.assertEqual(output.status_code, 302) - - def test_markdown_preview(self): - """ Test the markdown_preview endpoint. """ - - data = { - 'content': 'test\n----\n\n * 1\n * item 2' - } - - # CSRF missing - output = self.app.post('/markdown/', data=data) - self.assertEqual(output.status_code, 400) - - user = tests.FakeUser() - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.get('/settings/') - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # With CSRF - data['csrf_token'] = csrf_token - output = self.app.post('/markdown/', data=data) - self.assertEqual(output.status_code, 200) - exp = """

    test

    -
      -
    • 1
    • -
    • item 2
    • -
    """ - self.assertEqual(output.data, exp) - - @patch('pagure.ui.app.admin_session_timedout') - def test_remove_user_email(self, ast): - """ Test the remove_user_email endpoint. """ - ast.return_value = False - self.test_new_project() - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post('/settings/email/drop') - self.assertEqual(output.status_code, 404) - self.assertTrue('

    Page not found (404)

    ' in output.data) - - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.post('/settings/') - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'email': 'foo@pingou.com', - } - - output = self.app.post( - '/settings/email/drop', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '', output.data) - self.assertIn( - '\n You must always have at least one email', - output.data) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post('/settings/') - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'email': 'foo@pingou.com', - } - - output = self.app.post( - '/settings/email/drop', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertEqual(output.data.count('foo@pingou.com'), 4) - - data = { - 'csrf_token': csrf_token, - 'email': 'foobar@pingou.com', - } - - output = self.app.post( - '/settings/email/drop', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '\n You do not have the ' - 'email: foobar@pingou.com, nothing to remove', output.data) - - data = { - 'csrf_token': csrf_token, - 'email': 'foo@pingou.com', - } - - output = self.app.post( - '/settings/email/drop', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertEqual(output.data.count('foo@pingou.com'), 0) - self.assertEqual(output.data.count('bar@pingou.com'), 3) - - output = self.app.post( - '/settings/email/drop', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertEqual(output.data.count('foo@pingou.com'), 0) - self.assertEqual(output.data.count('bar@pingou.com'), 3) - - ast.return_value = True - output = self.app.post('/settings/email/drop', data=data) - self.assertEqual(output.status_code, 302) - - @patch('pagure.lib.notify.send_email') - @patch('pagure.ui.app.admin_session_timedout') - def test_add_user_email(self, ast, send_email): - """ Test the add_user_email endpoint. """ - send_email.return_value = True - ast.return_value = False - self.test_new_project() - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post('/settings/email/add') - self.assertEqual(output.status_code, 404) - self.assertTrue('

    Page not found (404)

    ' in output.data) - - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.post('/settings/email/add') - self.assertEqual(output.status_code, 200) - - self.assertTrue("Add new email" in output.data) - self.assertIn( - '', output.data) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post('/settings/email/add') - self.assertEqual(output.status_code, 200) - self.assertTrue("Add new email" in output.data) - self.assertIn( - '', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'email': 'foo2@pingou.com', - } - - output = self.app.post( - '/settings/email/add', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue("Add new email" in output.data) - self.assertEqual(output.data.count('foo2@pingou.com'), 1) - - # New email - data = { - 'csrf_token': csrf_token, - 'email': 'foobar@pingou.com', - } - - output = self.app.post( - '/settings/email/add', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '\n Email pending validation', - output.data) - self.assertEqual(output.data.count('foo@pingou.com'), 4) - self.assertEqual(output.data.count('bar@pingou.com'), 4) - self.assertEqual(output.data.count('foobar@pingou.com'), 1) - - # User already has this email - data = { - 'csrf_token': csrf_token, - 'email': 'foo@pingou.com', - } - - output = self.app.post( - '/settings/email/add', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue("Add new email" in output.data) - self.assertIn( - 'Invalid value, can't be any of: bar@pingou.com, ' - 'foo@pingou.com. ', output.data) - self.assertEqual(output.data.count('foo@pingou.com'), 5) - self.assertEqual(output.data.count('bar@pingou.com'), 4) - self.assertEqual(output.data.count('foobar@pingou.com'), 0) - - # Email registered by someone else - data = { - 'csrf_token': csrf_token, - 'email': 'foo@bar.com', - } - - output = self.app.post( - '/settings/email/add', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue("Add new email" in output.data) - self.assertIn( - 'Invalid value, can't be any of: foo@bar.com. ', - output.data) - - ast.return_value = True - output = self.app.post('/settings/email/add', data=data) - self.assertEqual(output.status_code, 302) - - @patch('pagure.lib.notify.send_email') - @patch('pagure.ui.app.admin_session_timedout') - def test_set_default_email(self, ast, send_email): - """ Test the set_default_email endpoint. """ - send_email.return_value = True - ast.return_value = False - self.test_new_project() - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post('/settings/email/default') - self.assertEqual(output.status_code, 404) - self.assertTrue('

    Page not found (404)

    ' in output.data) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/settings/') - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'email': 'foo@pingou.com', - } - - output = self.app.post( - '/settings/email/default', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertEqual(output.data.count('foo@pingou.com'), 4) - - # Set invalid default email - data = { - 'csrf_token': csrf_token, - 'email': 'foobar@pingou.com', - } - - output = self.app.post( - '/settings/email/default', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertEqual(output.data.count('foo@pingou.com'), 4) - self.assertIn( - '\n You do not have the ' - 'email: foobar@pingou.com, nothing to set', - output.data) - - # Set default email - data = { - 'csrf_token': csrf_token, - 'email': 'foo@pingou.com', - } - - output = self.app.post( - '/settings/email/default', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertEqual(output.data.count('foo@pingou.com'), 4) - self.assertIn( - '\n Default email set to: ' - 'foo@pingou.com', output.data) - - ast.return_value = True - output = self.app.post('/settings/email/default', data=data) - self.assertEqual(output.status_code, 302) - - @patch('pagure.ui.app.admin_session_timedout') - def test_confirm_email(self, ast): - """ Test the confirm_email endpoint. """ - output = self.app.get('/settings/email/confirm/foobar') - self.assertEqual(output.status_code, 302) - - ast.return_value = False - - # Add a pending email to pingou - userobj = pagure.lib.search_user(self.session, username='pingou') - - self.assertEqual(len(userobj.emails), 2) - - email_pend = pagure.lib.model.UserEmailPending( - user_id=userobj.id, - email='foo@fp.o', - token='abcdef', - ) - self.session.add(email_pend) - self.session.commit() - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - # Wrong token - output = self.app.get( - '/settings/email/confirm/foobar', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '\n No email associated with this token.', - output.data) - - # Confirm email - output = self.app.get( - '/settings/email/confirm/abcdef', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '
    \n Basic Information\n' - '
    ', output.data) - self.assertIn( - '\n Email validated', - output.data) - - userobj = pagure.lib.search_user(self.session, username='pingou') - self.assertEqual(len(userobj.emails), 3) - - ast.return_value = True - output = self.app.get('/settings/email/confirm/foobar') - self.assertEqual(output.status_code, 302) - - -if __name__ == '__main__': - SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskApptests) - unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_progit_flask_ui_fork.py b/tests/test_progit_flask_ui_fork.py deleted file mode 100644 index 83ddef5..0000000 --- a/tests/test_progit_flask_ui_fork.py +++ /dev/null @@ -1,1681 +0,0 @@ -# -*- coding: utf-8 -*- - -""" - (c) 2015 - Copyright Red Hat Inc - - Authors: - Pierre-Yves Chibon - -""" - -__requires__ = ['SQLAlchemy >= 0.8'] -import pkg_resources - -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 PagureFlaskForktests(tests.Modeltests): - """ Tests for flask fork controller of pagure """ - - def setUp(self): - """ Set up the environnment, ran before every tests. """ - super(PagureFlaskForktests, self).setUp() - - pagure.APP.config['TESTING'] = True - pagure.SESSION = self.session - pagure.lib.SESSION = self.session - pagure.ui.SESSION = self.session - pagure.ui.app.SESSION = self.session - pagure.ui.filters.SESSION = self.session - pagure.ui.fork.SESSION = self.session - pagure.ui.repo.SESSION = self.session - pagure.ui.issues.SESSION = self.session - - pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos') - pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks') - pagure.APP.config['TICKETS_FOLDER'] = os.path.join( - tests.HERE, 'tickets') - pagure.APP.config['DOCS_FOLDER'] = os.path.join( - tests.HERE, 'docs') - pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( - tests.HERE, 'requests') - self.app = pagure.APP.test_client() - - def set_up_git_repo( - self, new_project=None, branch_from='feature', mtype='FF'): - """ Set up the git repo and create the corresponding PullRequest - object. - """ - - # Create a git repo to play with - gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') - repo = pygit2.init_repository(gitrepo, bare=True) - - newpath = tempfile.mkdtemp(prefix='pagure-fork-test') - repopath = os.path.join(newpath, 'test') - clone_repo = pygit2.clone_repository(gitrepo, repopath) - - # Create a file in that git repo - with open(os.path.join(repopath, 'sources'), 'w') as stream: - stream.write('foo\n bar') - clone_repo.index.add('sources') - clone_repo.index.write() - - # Commits the files added - tree = clone_repo.index.write_tree() - author = pygit2.Signature( - 'Alice Author', 'alice@authors.tld') - committer = pygit2.Signature( - 'Cecil Committer', 'cecil@committers.tld') - clone_repo.create_commit( - 'refs/heads/master', # the name of the reference to update - author, - committer, - 'Add sources file for testing', - # binary string representing the tree object ID - tree, - # list of binary strings representing parents of the new commit - [] - ) - refname = 'refs/heads/master:refs/heads/master' - ori_remote = clone_repo.remotes[0] - PagureRepo.push(ori_remote, refname) - - first_commit = repo.revparse_single('HEAD') - - if mtype == 'merge': - with open(os.path.join(repopath, '.gitignore'), 'w') as stream: - stream.write('*~') - clone_repo.index.add('.gitignore') - clone_repo.index.write() - - # Commits the files added - tree = clone_repo.index.write_tree() - author = pygit2.Signature( - 'Alice Author', 'alice@authors.tld') - committer = pygit2.Signature( - 'Cecil Committer', 'cecil@committers.tld') - clone_repo.create_commit( - 'refs/heads/master', - author, - committer, - 'Add .gitignore file for testing', - # binary string representing the tree object ID - tree, - # list of binary strings representing parents of the new commit - [first_commit.oid.hex] - ) - refname = 'refs/heads/master:refs/heads/master' - ori_remote = clone_repo.remotes[0] - PagureRepo.push(ori_remote, refname) - - if mtype == 'conflicts': - with open(os.path.join(repopath, 'sources'), 'w') as stream: - stream.write('foo\n bar\nbaz') - clone_repo.index.add('sources') - clone_repo.index.write() - - # Commits the files added - tree = clone_repo.index.write_tree() - author = pygit2.Signature( - 'Alice Author', 'alice@authors.tld') - committer = pygit2.Signature( - 'Cecil Committer', 'cecil@committers.tld') - clone_repo.create_commit( - 'refs/heads/master', - author, - committer, - 'Add sources conflicting', - # binary string representing the tree object ID - tree, - # list of binary strings representing parents of the new commit - [first_commit.oid.hex] - ) - refname = 'refs/heads/master:refs/heads/master' - ori_remote = clone_repo.remotes[0] - PagureRepo.push(ori_remote, refname) - - # Set the second repo - - new_gitrepo = repopath - if new_project: - # Create a new git repo to play with - new_gitrepo = os.path.join(newpath, new_project.fullname) - if not os.path.exists(new_gitrepo): - os.makedirs(new_gitrepo) - new_repo = pygit2.clone_repository(gitrepo, new_gitrepo) - - repo = pygit2.Repository(new_gitrepo) - - if mtype != 'nochanges': - # Edit the sources file again - with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream: - stream.write('foo\n bar\nbaz\n boose') - repo.index.add('sources') - repo.index.write() - - # Commits the files added - tree = repo.index.write_tree() - author = pygit2.Signature( - 'Alice Author', 'alice@authors.tld') - committer = pygit2.Signature( - 'Cecil Committer', 'cecil@committers.tld') - repo.create_commit( - 'refs/heads/%s' % branch_from, - author, - committer, - 'A commit on branch %s' % branch_from, - tree, - [first_commit.oid.hex] - ) - refname = 'refs/heads/%s' % (branch_from) - ori_remote = repo.remotes[0] - PagureRepo.push(ori_remote, refname) - - # Create a PR for these changes - project = pagure.lib.get_project(self.session, 'test') - req = pagure.lib.new_pull_request( - session=self.session, - repo_from=project, - branch_from=branch_from, - repo_to=project, - branch_to='master', - title='PR from the %s branch' % branch_from, - user='pingou', - requestfolder=None, - ) - self.session.commit() - self.assertEqual(req.id, 1) - self.assertEqual(req.title, 'PR from the %s branch' % branch_from) - - shutil.rmtree(newpath) - - @patch('pagure.lib.notify.send_email') - def test_request_pull(self, send_email): - """ Test the request_pull endpoint. """ - send_email.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - - # Non-existant project - output = self.app.get('/foobar/pull-request/1') - self.assertEqual(output.status_code, 404) - - # Project has no PR - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 404) - - self.set_up_git_repo(new_project=None, branch_from='feature') - - project = pagure.lib.get_project(self.session, 'test') - self.assertEqual(len(project.requests), 1) - - # View the pull-request - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertIn( - 'title="View file as of 2a552b">View', output.data) - - @patch('pagure.lib.notify.send_email') - def test_merge_request_pull_FF(self, send_email): - """ Test the merge_request_pull endpoint with a FF PR. """ - send_email.return_value = True - - self.test_request_pull() - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # No CSRF - output = self.app.post( - '/test/pull-request/1/merge', data={}, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#1: PR from the feature branch - test\n - ' - 'Pagure', output.data) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertIn( - 'title="View file as of 2a552b">View', output.data) - - # Wrong project - data = { - 'csrf_token': csrf_token, - } - output = self.app.post( - '/foobar/pull-request/100/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Wrong project - data = { - 'csrf_token': csrf_token, - } - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 403) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - - # Wrong request id - data = { - 'csrf_token': csrf_token, - } - output = self.app.post( - '/test/pull-request/100/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - # Pull-request disabled - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Project w pull-request but only assignee can merge - settings['pull_requests'] = True - settings['Only_assignee_can_merge_pull-request'] = True - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#1: PR from the feature branch - test\n - ' - 'Pagure', output.data) - self.assertIn( - '

    PR#1 PR from ' - 'the feature branch ', - output.data) - self.assertIn( - '\n This request must be ' - 'assigned to be merged', output.data) - - # PR assigned but not to this user - repo = pagure.lib.get_project(self.session, 'test') - req = repo.requests[0] - req.assignee_id = 2 - self.session.add(req) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 PR from ' - 'the feature branch ', - output.data) - self.assertIn( - '\n Only the assignee can ' - 'merge this review', output.data) - - # Project w/ minimal PR score - settings['Only_assignee_can_merge_pull-request'] = False - settings['Minimum_score_to_merge_pull-request'] = 2 - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 PR from ' - 'the feature branch ', - output.data) - self.assertIn( - '\n This request does not ' - 'have the minimum review score necessary to be merged', - output.data) - - # Merge - settings['Minimum_score_to_merge_pull-request'] = -1 - repo.settings = settings - self.session.add(repo) - self.session.commit() - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Changes merged!', - output.data) - - @patch('pagure.lib.notify.send_email') - def test_merge_request_pull_merge(self, send_email): - """ Test the merge_request_pull endpoint with a merge PR. """ - send_email.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - self.set_up_git_repo( - new_project=None, branch_from='feature', mtype='merge') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - } - - # Merge - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Changes merged!', - output.data) - - @patch('pagure.lib.notify.send_email') - def test_merge_request_pull_conflicts(self, send_email): - """ Test the merge_request_pull endpoint with a conflicting PR. """ - send_email.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - self.set_up_git_repo( - new_project=None, branch_from='feature', mtype='conflicts') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - } - - # Merge conflicts - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 PR from ' - 'the feature branch ', - output.data) - self.assertIn( - '\n Merge conflicts!', - output.data) - - @patch('pagure.lib.notify.send_email') - def test_merge_request_pull_nochange(self, send_email): - """ Test the merge_request_pull endpoint. """ - send_email.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - self.set_up_git_repo( - new_project=None, branch_from='master', mtype='nochanges') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - } - - # Nothing to merge - output = self.app.post( - '/test/pull-request/1/merge', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 ' - 'PR from the master branch

    ', output.data) - self.assertIn( - '\n Nothing to do, changes ' - 'were already merged', output.data) - - @patch('pagure.lib.notify.send_email') - def test_request_pull_close(self, send_email): - """ Test the request_pull endpoint with a closed PR. """ - send_email.return_value = True - - self.test_merge_request_pull_FF() - - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertIn( - 'Merged by', output.data) - self.assertIn( - 'title="View file as of 2a552b">View', output.data) - - @patch('pagure.lib.notify.send_email') - def test_request_pull_disabled(self, send_email): - """ Test the request_pull endpoint with PR disabled. """ - send_email.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - self.set_up_git_repo(new_project=None, branch_from='feature') - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.notify.send_email') - def test_request_pull_empty_repo(self, send_email): - """ Test the request_pull endpoint against an empty repo. """ - send_email.return_value = True - - tests.create_projects(self.session) - item = pagure.lib.model.Project( - user_id=2, # foo - name='test', - description='test project #1', - hook_token='aaabbb', - parent_id=1, - ) - self.session.add(item) - self.session.commit() - - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - tests.create_projects_git( - os.path.join(tests.HERE, 'forks', 'foo'), bare=True) - - # Create a git repo to play with - gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') - self.assertFalse(os.path.exists(gitrepo)) - os.makedirs(gitrepo) - repo = pygit2.init_repository(gitrepo, bare=True) - - # Create a fork of this repo - newpath = tempfile.mkdtemp(prefix='pagure-fork-test') - gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') - new_repo = pygit2.clone_repository(gitrepo, newpath) - - # Edit the sources file again - with open(os.path.join(newpath, 'sources'), 'w') as stream: - stream.write('foo\n bar\nbaz\n boose') - new_repo.index.add('sources') - new_repo.index.write() - - # Commits the files added - tree = new_repo.index.write_tree() - author = pygit2.Signature( - 'Alice Author', 'alice@authors.tld') - committer = pygit2.Signature( - 'Cecil Committer', 'cecil@committers.tld') - new_repo.create_commit( - 'refs/heads/feature', - author, - committer, - 'A commit on branch feature', - tree, - [] - ) - refname = 'refs/heads/feature:refs/heads/feature' - ori_remote = new_repo.remotes[0] - PagureRepo.push(ori_remote, refname) - - # Create a PR for these changes - project = pagure.lib.get_project(self.session, 'test') - req = pagure.lib.new_pull_request( - session=self.session, - repo_from=item, - branch_from='feature', - repo_to=project, - branch_to='master', - title='PR from the feature branch', - user='pingou', - requestfolder=None, - - ) - self.session.commit() - self.assertEqual(req.id, 1) - self.assertEqual(req.title, 'PR from the feature branch') - - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertTrue( - output.data.count('Overview - test - Pagure', output.data) - self.assertIn( - '\n Fork is empty, there are no ' - 'commits to request pulling', output.data) - - shutil.rmtree(newpath) - - @patch('pagure.lib.notify.send_email') - def test_request_pulls(self, send_email): - """ Test the request_pulls endpoint. """ - send_email.return_value = True - - # No such project - output = self.app.get('/test/pull-requests') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - - output = self.app.get('/test/pull-requests') - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Pull Requests 0', - output.data) - # Open is primary - self.assertIn( - 'Open', output.data) - self.assertIn( - 'Closed', output.data) - - self.set_up_git_repo(new_project=None, branch_from='feature') - - output = self.app.get('/test/pull-requests') - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Pull Requests 1', - output.data) - # Open is primary - self.assertIn( - 'Open', output.data) - self.assertIn( - 'Closed', output.data) - - output = self.app.get('/test/pull-requests?status=Closed') - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Closed Pull Requests 0', - output.data) - # Close is primary - self.assertIn( - 'Open', output.data) - self.assertIn( - 'Closed', output.data) - - output = self.app.get('/test/pull-requests?status=0') - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Closed/Merged Pull Requests 0', - output.data) - # Close is primary - self.assertIn( - 'Open', output.data) - self.assertIn( - 'Closed', output.data) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.get('/test/pull-requests') - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.notify.send_email') - def test_request_pull_patch(self, send_email): - """ Test the request_pull_patch endpoint. """ - send_email.return_value = True - - output = self.app.get('/test/pull-request/1.patch') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - self.set_up_git_repo( - new_project=None, branch_from='feature', mtype='merge') - - output = self.app.get('/test/pull-request/100.patch') - self.assertEqual(output.status_code, 404) - - output = self.app.get('/test/pull-request/1.patch') - self.assertEqual(output.status_code, 200) - - npatch = [] - for row in output.data.split('\n'): - if row.startswith('Date:'): - continue - if row.startswith('From '): - row = row.split(' ', 2)[2] - npatch.append(row) - - exp = """Mon Sep 17 00:00:00 2001 -From: Alice Author -Subject: A commit on branch feature - - ---- - -diff --git a/.gitignore b/.gitignore -new file mode 100644 -index 0000000..e4e5f6c ---- /dev/null -+++ b/.gitignore -@@ -0,0 +1 @@ -+*~ -\ No newline at end of file -diff --git a/sources b/sources -index 9f44358..2a552bb 100644 ---- a/sources -+++ b/sources -@@ -1,2 +1,4 @@ - foo -- bar -\ No newline at end of file -+ bar -+baz -+ boose -\ No newline at end of file - -""" - - patch = '\n'.join(npatch) - #print patch - self.assertEqual(patch, exp) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.get('/test/pull-request/1.patch') - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.notify.send_email') - def test_request_pull_patch_close(self, send_email): - """ Test the request_pull_patch endpoint with a closed PR. """ - send_email.return_value = True - - self.test_merge_request_pull_FF() - - output = self.app.get('/test/pull-request/1.patch') - self.assertEqual(output.status_code, 200) - - npatch = [] - for row in output.data.split('\n'): - if row.startswith('Date:'): - continue - if row.startswith('From '): - row = row.split(' ', 2)[2] - npatch.append(row) - - exp = """Mon Sep 17 00:00:00 2001 -From: Alice Author -Subject: A commit on branch feature - - ---- - -diff --git a/sources b/sources -index 9f44358..2a552bb 100644 ---- a/sources -+++ b/sources -@@ -1,2 +1,4 @@ - foo -- bar -\ No newline at end of file -+ bar -+baz -+ boose -\ No newline at end of file - -""" - - patch = '\n'.join(npatch) - #print patch - self.assertEqual(patch, exp) - - @patch('pagure.lib.notify.send_email') - def test_request_pull_patch_empty_repo(self, send_email): - """ Test the request_pull_patch endpoint against an empty repo. """ - send_email.return_value = True - - tests.create_projects(self.session) - item = pagure.lib.model.Project( - user_id=2, # foo - name='test', - description='test project #1', - hook_token='aaabbb', - parent_id=1, - ) - self.session.add(item) - self.session.commit() - - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - tests.create_projects_git( - os.path.join(tests.HERE, 'forks', 'foo'), bare=True) - - # Create a git repo to play with - gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') - self.assertFalse(os.path.exists(gitrepo)) - os.makedirs(gitrepo) - repo = pygit2.init_repository(gitrepo, bare=True) - - # Create a fork of this repo - newpath = tempfile.mkdtemp(prefix='pagure-fork-test') - gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') - new_repo = pygit2.clone_repository(gitrepo, newpath) - - # Edit the sources file again - with open(os.path.join(newpath, 'sources'), 'w') as stream: - stream.write('foo\n bar\nbaz\n boose') - new_repo.index.add('sources') - new_repo.index.write() - - # Commits the files added - tree = new_repo.index.write_tree() - author = pygit2.Signature( - 'Alice Author', 'alice@authors.tld') - committer = pygit2.Signature( - 'Cecil Committer', 'cecil@committers.tld') - new_repo.create_commit( - 'refs/heads/feature', - author, - committer, - 'A commit on branch feature', - tree, - [] - ) - refname = 'refs/heads/feature:refs/heads/feature' - ori_remote = new_repo.remotes[0] - PagureRepo.push(ori_remote, refname) - - # Create a PR for these "changes" (there are none, both repos are - # empty) - project = pagure.lib.get_project(self.session, 'test') - req = pagure.lib.new_pull_request( - session=self.session, - repo_from=item, - branch_from='feature', - repo_to=project, - branch_to='master', - title='PR from the feature branch', - user='pingou', - requestfolder=None, - - ) - self.session.commit() - self.assertEqual(req.id, 1) - self.assertEqual(req.title, 'PR from the feature branch') - - output = self.app.get('/test/pull-request/1.patch', follow_redirects=True) - self.assertEqual(output.status_code, 200) - - npatch = [] - for row in output.data.split('\n'): - if row.startswith('Date:'): - continue - if row.startswith('From '): - row = row.split(' ', 2)[2] - npatch.append(row) - - exp = """Mon Sep 17 00:00:00 2001 -From: Alice Author -Subject: A commit on branch feature - - ---- - -diff --git a/sources b/sources -new file mode 100644 -index 0000000..2a552bb ---- /dev/null -+++ b/sources -@@ -0,0 +1,4 @@ -+foo -+ bar -+baz -+ boose -\ No newline at end of file - -""" - - patch = '\n'.join(npatch) - #print patch - self.assertEqual(patch, exp) - - shutil.rmtree(newpath) - - @patch('pagure.lib.notify.send_email') - def test_request_pull_patch_empty_fork(self, send_email): - """ Test the request_pull_patch endpoint from an empty fork. """ - send_email.return_value = True - - tests.create_projects(self.session) - item = pagure.lib.model.Project( - user_id=2, # foo - name='test', - description='test project #1', - hook_token='aaabbb', - parent_id=1, - ) - self.session.add(item) - self.session.commit() - - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - tests.create_projects_git( - os.path.join(tests.HERE, 'forks', 'foo'), bare=True) - - # Create a git repo to play with - gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') - self.assertFalse(os.path.exists(gitrepo)) - os.makedirs(gitrepo) - repo = pygit2.init_repository(gitrepo, bare=True) - - # Create a fork of this repo - newpath = tempfile.mkdtemp(prefix='pagure-fork-test') - gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') - new_repo = pygit2.clone_repository(gitrepo, newpath) - - # Create a PR for these "changes" (there are none, both repos are - # empty) - project = pagure.lib.get_project(self.session, 'test') - req = pagure.lib.new_pull_request( - session=self.session, - repo_from=item, - branch_from='feature', - repo_to=project, - branch_to='master', - title='PR from the feature branch', - user='pingou', - requestfolder=None, - - ) - self.session.commit() - self.assertEqual(req.id, 1) - self.assertEqual(req.title, 'PR from the feature branch') - - output = self.app.get('/test/pull-request/1.patch', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Fork is empty, there are no ' - 'commits to request pulling', output.data) - - shutil.rmtree(newpath) - - @patch('pagure.lib.notify.send_email') - def test_cancel_request_pull(self, send_email): - """ Test the cancel_request_pull endpoint. """ - send_email.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - self.set_up_git_repo( - new_project=None, branch_from='feature', mtype='merge') - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post('/test/pull-request/cancel/1') - self.assertEqual(output.status_code, 302) - - output = self.app.post( - '/test/pull-request/cancel/1', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Invalid input submitted', - output.data) - - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - } - - # Invalid project - output = self.app.post( - '/foo/pull-request/cancel/1', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Invalid PR id - output = self.app.post( - '/test/pull-request/cancel/100', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Invalid user for this project - output = self.app.post( - '/test/pull-request/cancel/1', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 403) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/cancel/1', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = True - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/cancel/1', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Request pull canceled!', - output.data) - - @patch('pagure.lib.notify.send_email') - def test_set_assignee_requests(self, send_email): - """ Test the set_assignee_requests endpoint. """ - send_email.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - self.set_up_git_repo(new_project=None, branch_from='feature') - - # No such project - user = tests.FakeUser() - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.post('/foo/pull-request/1/assign') - self.assertEqual(output.status_code, 404) - - output = self.app.post('/test/pull-request/100/assign') - self.assertEqual(output.status_code, 404) - - # Invalid input - output = self.app.post( - '/test/pull-request/1/assign', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#1: PR from the feature branch - test\n - ' - 'Pagure', output.data) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertNotIn( - '\n Request assigned', - output.data) - - output = self.app.get('/test/pull-request/1') - self.assertEqual(output.status_code, 200) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'user': 'pingou', - } - - # No CSRF - output = self.app.post( - '/test/pull-request/1/assign', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#1: PR from the feature branch - test\n - ' - 'Pagure', output.data) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertNotIn( - '\n Request assigned', - output.data) - - # Invalid assignee - data = { - 'csrf_token': csrf_token, - 'user': 'bar', - } - - output = self.app.post( - '/test/pull-request/1/assign', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#1: PR from the feature branch - test\n - ' - 'Pagure', output.data) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertIn( - '\n No user "bar" found', - output.data) - - # Assign the PR - data = { - 'csrf_token': csrf_token, - 'user': 'pingou', - } - - output = self.app.post( - '/test/pull-request/1/assign', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#1: PR from the feature branch - test\n - ' - 'Pagure', output.data) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - self.assertIn( - '\n Request assigned', - output.data) - - # Pull-Request closed - repo = pagure.lib.get_project(self.session, 'test') - req = repo.requests[0] - req.status = 'Closed' - req.closed_by_in = 1 - self.session.add(req) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/assign', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 403) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/assign', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.notify.send_email') - def test_fork_project(self, send_email): - """ Test the fork_project endpoint. """ - send_email.return_value = True - - tests.create_projects(self.session) - for folder in ['docs', 'tickets', 'requests', 'repos']: - tests.create_projects_git( - os.path.join(tests.HERE, folder), bare=True) - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post('/do_fork/test') - self.assertEqual(output.status_code, 400) - - output = self.app.get('/new/') - self.assertEqual(output.status_code, 200) - self.assertIn('Create new Project', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - } - - output = self.app.post( - '/do_fork/foo', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - output = self.app.post( - '/do_fork/test', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n You may not fork your ' - 'own repo', output.data) - - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.post('/do_fork/test') - self.assertEqual(output.status_code, 400) - - data = { - 'csrf_token': csrf_token, - } - - output = self.app.post( - '/do_fork/test', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n Repo "test" ' - 'cloned to "foo/test"', output.data) - - @patch('pagure.lib.notify.send_email') - def test_new_request_pull(self, send_email): - """ Test the new_request_pull endpoint. """ - send_email.return_value = True - - self.test_fork_project() - - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - - repo = pagure.lib.get_project(self.session, 'test') - fork = pagure.lib.get_project(self.session, 'test', user='foo') - - self.set_up_git_repo( - new_project=fork, branch_from='feature', mtype='FF') - - user = tests.FakeUser() - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.get('/foo/diff/master..feature') - self.assertEqual(output.status_code, 404) - - output = self.app.get('/test/diff/master..foo') - self.assertEqual(output.status_code, 400) - - output = self.app.get('/test/diff/foo..master') - self.assertEqual(output.status_code, 400) - - output = self.app.get('/test/diff/feature..master') - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Diff from master to feature - test - Pagure', - output.data) - self.assertIn( - '

    No commits found

    ', output.data) - - output = self.app.get('/test/diff/master..feature') - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Diff from feature to master - test - Pagure', - output.data) - self.assertNotIn( - '', output.data) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/diff/master..feature') - self.assertEqual(output.status_code, 200) - self.assertIn( - ' Create new Pull Request for master - test - ' - 'Pagure', output.data) - self.assertIn( - '', - output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - 'title': 'foo bar PR', - } - - output = self.app.post( - '/test/diff/master..feature', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#2: foo bar PR - test\n - Pagure', - output.data) - self.assertIn( - '\n Request created', - output.data) - - @patch('pagure.lib.notify.send_email') - def test_new_request_pull_empty_repo(self, send_email): - """ Test the new_request_pull endpoint against an empty repo. """ - send_email.return_value = True - - self.test_fork_project() - - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - - repo = pagure.lib.get_project(self.session, 'test') - fork = pagure.lib.get_project(self.session, 'test', user='foo') - - # Create a git repo to play with - gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') - repo = pygit2.init_repository(gitrepo, bare=True) - - # Create a fork of this repo - newpath = tempfile.mkdtemp(prefix='pagure-fork-test') - gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') - new_repo = pygit2.clone_repository(gitrepo, newpath) - - user = tests.FakeUser() - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.get( - '/fork/foo/test/diff/master..feature', - follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Fork is empty, there are ' - 'no commits to request pulling', output.data) - - output = self.app.get('/test/new_issue') - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - 'title': 'foo bar PR', - } - - output = self.app.post( - '/test/diff/master..feature', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Fork is empty, there are ' - 'no commits to request pulling', output.data) - - shutil.rmtree(newpath) - - @patch('pagure.lib.notify.send_email') - def test_new_request_pull_empty_fork(self, send_email): - """ Test the new_request_pull endpoint against an empty repo. """ - send_email.return_value = True - - self.test_fork_project() - - tests.create_projects_git( - os.path.join(tests.HERE, 'requests'), bare=True) - - repo = pagure.lib.get_project(self.session, 'test') - fork = pagure.lib.get_project(self.session, 'test', user='foo') - - # Create a git repo to play with - gitrepo = os.path.join(tests.HERE, 'repos', 'test.git') - repo = pygit2.init_repository(gitrepo, bare=True) - - # Create a fork of this repo - newpath = tempfile.mkdtemp(prefix='pagure-fork-test') - gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git') - new_repo = pygit2.clone_repository(gitrepo, newpath) - - user = tests.FakeUser() - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.get( - '/fork/foo/test/diff/master..master', follow_redirects=True) - self.assertIn( - 'Overview - test - Pagure', output.data) - self.assertIn( - '\n Fork is empty, there are ' - 'no commits to request pulling', output.data) - - shutil.rmtree(newpath) - - @patch('pagure.lib.notify.send_email') - def test_pull_request_add_comment(self, send_email): - """ Test the pull_request_add_comment endpoint. """ - send_email.return_value = True - - self.test_request_pull() - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post('/foo/pull-request/1/comment') - self.assertEqual(output.status_code, 404) - - output = self.app.post('/test/pull-request/100/comment') - self.assertEqual(output.status_code, 404) - - output = self.app.post('/test/pull-request/1/comment') - self.assertEqual(output.status_code, 200) - self.assertTrue( - output.data.startswith('\n
    ')) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - 'comment': 'This look alright but we can do better', - } - output = self.app.post( - '/test/pull-request/1/comment', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'PR#1: PR from the feature branch - test\n - ' - 'Pagure', output.data) - self.assertIn( - '\n Comment added', - output.data) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/comment', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.notify.send_email') - def test_pull_request_drop_comment(self, send_email): - """ Test the pull_request_drop_comment endpoint. """ - send_email.return_value = True - - self.test_pull_request_add_comment() - # Project w/ pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = True - repo.settings = settings - self.session.add(repo) - self.session.commit() - - user = tests.FakeUser() - user.username = 'foo' - with tests.user_set(pagure.APP, user): - output = self.app.post('/foo/pull-request/1/comment/drop') - self.assertEqual(output.status_code, 404) - - output = self.app.post('/test/pull-request/100/comment/drop') - self.assertEqual(output.status_code, 404) - - output = self.app.post( - '/test/pull-request/1/comment/drop', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 ' - 'PR from the feature branch

    ', output.data) - #self.assertIn('href="#comment-1">¶', output.data) - self.assertIn( - '

    This look alright but we can do better

    ', - output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # Invalid comment id - data = { - 'csrf_token': csrf_token, - 'drop_comment': '10', - } - output = self.app.post( - '/test/pull-request/1/comment/drop', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 404) - - data['drop_comment'] = '1' - output = self.app.post( - '/test/pull-request/1/comment/drop', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 403) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - # Drop comment - output = self.app.post( - '/test/pull-request/1/comment/drop', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    PR#1 PR from ' - 'the feature branch ', - output.data) - self.assertIn( - '\n Comment removed', - output.data) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/comment/drop', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.notify.send_email') - def test_pull_request_edit_comment(self, send_email): - """ Test the pull request edit comment endpoint """ - send_email.return_value = True - - self.test_request_pull() - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - # Repo 'foo' does not exist so it is verifying that condition - output = self.app.post('/foo/pull-request/1/comment/1/edit') - self.assertEqual(output.status_code, 404) - - # Here no comment is present in the PR so its verifying that condition - output = self.app.post('/test/pull-request/100/comment/100/edit') - self.assertEqual(output.status_code, 404) - - output = self.app.post('/test/pull-request/1/comment') - self.assertEqual(output.status_code, 200) - # Creating comment to play with - self.assertTrue( - output.data.startswith('\n
    ')) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - 'comment': 'This look alright but we can do better', - } - output = self.app.post( - '/test/pull-request/1/comment', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 200) - - self.assertIn( - '

    PR#1 PR from ' - 'the feature branch ', - output.data) - self.assertIn( - '\n Comment added', - output.data) - # Check if the comment is there - self.assertIn( - '

    This look alright but we can do better

    ', output.data) - output = self.app.get('/test/pull-request/1/comment/1/edit') - self.assertEqual(output.status_code, 200) - - self.assertIn('
    ', output.data) - # Checking if the comment is there in the update page - self.assertIn( - 'This look alright but we can do better', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'csrf_token': csrf_token, - 'update_comment': 'This look alright but we can do better than this.', - } - output = self.app.post( - '/test/pull-request/1/comment/1/edit', data=data, - follow_redirects=True) - # Checking if the comment is updated in the main page - self.assertIn( - '

    This look alright but we can do better than this.

    ', output.data) - self.assertIn( - '

    PR#1 PR from ' - 'the feature branch ', - output.data) - # Checking if Edited by User is there or not - self.assertIn( - 'Edited just now by pingou ', - output.data) - self.assertIn( - '\n Comment updated', output.data) - - # Project w/o pull-request - repo = pagure.lib.get_project(self.session, 'test') - settings = repo.settings - settings['pull_requests'] = False - repo.settings = settings - self.session.add(repo) - self.session.commit() - - output = self.app.post( - '/test/pull-request/1/comment/edit/1', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 404) - - -if __name__ == '__main__': - SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskForktests) - unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_progit_flask_ui_groups.py b/tests/test_progit_flask_ui_groups.py deleted file mode 100644 index 1a618fc..0000000 --- a/tests/test_progit_flask_ui_groups.py +++ /dev/null @@ -1,412 +0,0 @@ -# -*- coding: utf-8 -*- - -""" - (c) 2015 - Copyright Red Hat Inc - - Authors: - Pierre-Yves Chibon - -""" - -__requires__ = ['SQLAlchemy >= 0.8'] -import pkg_resources - -import unittest -import shutil -import sys -import os - -import json -from mock import patch - -sys.path.insert(0, os.path.join(os.path.dirname( - os.path.abspath(__file__)), '..')) - -import pagure.lib -import tests - - -class PagureFlaskGroupstests(tests.Modeltests): - """ Tests for flask groups controller of pagure """ - - def setUp(self): - """ Set up the environnment, ran before every tests. """ - super(PagureFlaskGroupstests, self).setUp() - - pagure.APP.config['TESTING'] = True - pagure.SESSION = self.session - pagure.ui.SESSION = self.session - pagure.ui.groups.SESSION = self.session - pagure.ui.repo.SESSION = self.session - - pagure.APP.config['GIT_FOLDER'] = tests.HERE - pagure.APP.config['FORK_FOLDER'] = os.path.join( - tests.HERE, 'forks') - pagure.APP.config['TICKETS_FOLDER'] = os.path.join( - tests.HERE, 'tickets') - pagure.APP.config['DOCS_FOLDER'] = os.path.join( - tests.HERE, 'docs') - pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( - tests.HERE, 'requests') - self.app = pagure.APP.test_client() - - def test_group_lists(self): - """ Test the group_lists endpoint. """ - output = self.app.get('/groups') - self.assertIn( - '

    \n' - ' Groups 0', - output.data) - - def test_add_group(self): - """ Test the add_group endpoint. """ - output = self.app.get('/group/add') - self.assertEqual(output.status_code, 302) - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/group/add') - self.assertEqual(output.status_code, 403) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/group/add') - self.assertEqual(output.status_code, 200) - self.assertIn('

    Create group

    ', output.data) - self.assertNotIn( - '', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - } - - # Insufficient input - output = self.app.post('/group/add', data=data) - self.assertEqual(output.status_code, 200) - self.assertIn('

    Create group

    ', output.data) - self.assertEqual(output.data.count( - 'This field is required.'), 1) - - data = { - 'group_name': 'test_group', - } - - # Missing CSRF - output = self.app.post('/group/add', data=data) - self.assertEqual(output.status_code, 200) - self.assertIn('

    Create group

    ', output.data) - self.assertEqual(output.data.count( - 'This field is required.'), 0) - - data['csrf_token'] = csrf_token - - # All good - output = self.app.post( - '/group/add', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n User `pingou` added to ' - 'the group `test_group`.', output.data) - self.assertIn( - '\n Group `test_group` created.', - output.data) - self.assertIn( - '

    \n' - ' Groups 1', - output.data) - - user = tests.FakeUser( - username='pingou', - groups=pagure.APP.config['ADMIN_GROUP']) - with tests.user_set(pagure.APP, user): - output = self.app.get('/group/add') - self.assertEqual(output.status_code, 200) - self.assertIn('

    Create group

    ', output.data) - self.assertIn('', output.data) - - data = { - 'group_name': 'test_admin_group', - 'group_type': 'admin', - 'csrf_token': csrf_token, - } - - # All good - output = self.app.post( - '/group/add', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n User `pingou` added to ' - 'the group `test_admin_group`.', output.data) - self.assertIn( - '\n Group `test_admin_group` ' - 'created.',output.data) - self.assertIn( - '

    \n' - ' Groups 2', - output.data) - - def test_group_delete(self): - """ Test the group_delete endpoint. """ - output = self.app.post('/group/foo/delete') - self.assertEqual(output.status_code, 302) - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post('/group/foo/delete', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    No groups have been created on this pagure instance ' - 'yet

    ', output.data) - self.assertIn( - '

    \n' - ' Groups 0', - output.data) - - self.test_add_group() - - with tests.user_set(pagure.APP, user): - output = self.app.post('/group/foo/delete', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '

    \n' - ' Groups 1', - output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - user.username = 'foo' - with tests.user_set(pagure.APP, user): - - data = { - 'csrf_token': csrf_token, - } - output = self.app.post( - '/group/bar/delete', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n No group `bar` found', - output.data) - self.assertIn( - '

    \n' - ' Groups 1', - output.data) - - output = self.app.post( - '/group/test_group/delete', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n You are not allowed to ' - 'delete the group test_group', output.data) - self.assertIn( - '

    \n' - ' Groups 1', - output.data) - - user.username = 'bar' - with tests.user_set(pagure.APP, user): - - output = self.app.post( - '/group/test_group/delete', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - - output = self.app.post( - '/group/test_group/delete', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n Group `test_group` has ' - 'been deleted', output.data) - self.assertIn( - '

    \n' - ' Groups 0', - output.data) - - def test_view_group(self): - """ Test the view_group endpoint. """ - output = self.app.get('/group/foo') - self.assertEqual(output.status_code, 404) - - self.test_add_group() - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/group/test_group') - self.assertEqual(output.status_code, 200) - self.assertIn( - '  ' - 'test_group', output.data) - - output = self.app.get('/group/test_admin_group') - self.assertEqual(output.status_code, 404) - - user = tests.FakeUser( - username='pingou', - groups=pagure.APP.config['ADMIN_GROUP']) - with tests.user_set(pagure.APP, user): - # Admin can see group of type admins - output = self.app.get('/group/test_admin_group') - self.assertEqual(output.status_code, 200) - self.assertIn( - '  ' - 'test_admin_group', output.data) - self.assertEqual(output.data.count('')[0] - - # No CSRF - data = { - 'user': 'bar' - } - - output = self.app.post('/group/test_admin_group', data=data) - self.assertEqual(output.status_code, 200) - self.assertIn( - '  ' - 'test_admin_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_admin_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_admin_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_group', output.data) - self.assertEqual(output.data.count('')[0] - - data = {'csrf_token': csrf_token} - - output = self.app.post( - '/group/test_group/bar/delete', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n No user `bar` found', - output.data) - self.assertIn( - '  ' - 'test_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_group', output.data) - self.assertEqual(output.data.count('  ' - 'test_group', output.data) - self.assertEqual(output.data.count(' - -""" - -__requires__ = ['SQLAlchemy >= 0.8'] -import pkg_resources - -import json -import unittest -import shutil -import sys -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 - - -class PagureFlaskIssuestests(tests.Modeltests): - """ Tests for flask issues controller of pagure """ - - def setUp(self): - """ Set up the environnment, ran before every tests. """ - super(PagureFlaskIssuestests, self).setUp() - - pagure.APP.config['TESTING'] = True - pagure.SESSION = self.session - pagure.ui.SESSION = self.session - pagure.ui.app.SESSION = self.session - pagure.ui.issues.SESSION = self.session - pagure.ui.repo.SESSION = self.session - - pagure.APP.config['GIT_FOLDER'] = tests.HERE - pagure.APP.config['FORK_FOLDER'] = os.path.join( - tests.HERE, 'forks') - pagure.APP.config['TICKETS_FOLDER'] = os.path.join( - tests.HERE, 'tickets') - pagure.APP.config['DOCS_FOLDER'] = os.path.join( - tests.HERE, 'docs') - self.app = pagure.APP.test_client() - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_new_issue(self, p_send_email, p_ugt): - """ Test the new_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - output = self.app.get('/foo/new_issue') - self.assertEqual(output.status_code, 302) - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/foo/new_issue') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - - output = self.app.get('/test/new_issue') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n New issue' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - } - - # Insufficient input - output = self.app.post('/test/new_issue', data=data) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n New issue' - in output.data) - self.assertEqual(output.data.count( - 'This field is required.'), 2) - - data['title'] = 'Test issue' - output = self.app.post('/test/new_issue', data=data) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n New issue' - in output.data) - self.assertEqual(output.data.count( - 'This field is required.'), 1) - - data['issue_content'] = 'We really should improve on this issue' - data['status'] = 'Open' - output = self.app.post('/test/new_issue', data=data) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n New issue' - in output.data) - self.assertEqual(output.data.count( - '\n This field is required.'), - 0) - - # Invalid user - data['csrf_token'] = csrf_token - output = self.app.post('/test/new_issue', data=data) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n New issue' - in output.data) - self.assertEqual(output.data.count( - 'This field is required.'), 0) - self.assertTrue( - '\n No user "username" found' - in output.data) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post( - '/test/new_issue', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n Issue created', output.data) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post( - '/test/new_issue', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_new_issue_w_file(self, p_send_email, p_ugt): - """ Test the new_issue endpoint with a file. """ - p_send_email.return_value = True - p_ugt.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'tickets'), bare=True) - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/new_issue') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n New issue' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - stream = open(os.path.join(tests.HERE, 'placebo.png'), 'r') - data = { - 'title': 'Test issue', - 'issue_content': 'We really should improve on this issue\n' - '', - 'status': 'Open', - 'filestream': stream, - 'enctype': 'multipart/form-data', - 'csrf_token': csrf_token, - } - - output = self.app.post( - '/test/new_issue', data=data, follow_redirects=True) - stream.close() - - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n Issue created', output.data) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - stream = open(os.path.join(tests.HERE, 'placebo.png'), 'r') - data = { - 'title': 'Test issue', - 'issue_content': 'We really should improve on this issue', - 'status': 'Open', - 'filestream': stream, - 'enctype': 'multipart/form-data', - 'csrf_token': csrf_token, - } - - output = self.app.post( - '/test/new_issue', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_view_issues(self, p_send_email, p_ugt): - """ Test the view_issues endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - output = self.app.get('/foo/issues') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - - output = self.app.get('/test/issues') - self.assertEqual(output.status_code, 200) - self.assertTrue('

    test project #1

    ' in output.data) - self.assertTrue( - '

    \n 0 Open Issues' in output.data) - - # Create issues 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') - - # Whole list - output = self.app.get('/test/issues') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertTrue( - '

    \n 1 Open Issues' in output.data) - - # Status = closed - output = self.app.get('/test/issues?status=cloSED') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertTrue( - '

    \n 0 Closed Issues' in output.data) - - # Status = fixed - output = self.app.get('/test/issues?status=fixed') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertTrue( - '

    \n 0 Closed Issues' in output.data) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - output = self.app.get('/test/issues') - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_view_issue(self, p_send_email, p_ugt): - """ Test the view_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - output = self.app.get('/foo/issue/1') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 404) - - # Create issues 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') - - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertTrue( - '

    Login to comment on this ticket.

    ' - in output.data) - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertFalse( - '

    Login to comment on this ticket.

    ' - in output.data) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # Create private issue - 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, - private=True, - ) - self.session.commit() - self.assertEqual(msg.title, 'Test issue') - - # Not logged in - output = self.app.get('/test/issue/2') - self.assertEqual(output.status_code, 403) - - # Wrong user - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/2') - self.assertEqual(output.status_code, 403) - - # reporter - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/2') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_update_issue(self, p_send_email, p_ugt): - """ Test the update_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - output = self.app.get('/foo/issue/1/update') - self.assertEqual(output.status_code, 302) - - tests.create_projects(self.session) - - output = self.app.get('/test/issue/1/update') - self.assertEqual(output.status_code, 302) - - # Create issues 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') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'status': 'fixed' - } - - # Invalid repo - output = self.app.post('/bar/issue/1/update', data=data) - self.assertEqual(output.status_code, 404) - - # Non-existing issue - output = self.app.post('/test/issue/100/update', data=data) - self.assertEqual(output.status_code, 404) - - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertFalse( - '' - in output.data) - - data['status'] = 'Fixed' - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertFalse( - '' - in output.data) - - data['csrf_token'] = csrf_token - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Successfully edited issue #1', - output.data) - self.assertTrue( - '' - in output.data) - - # Add new comment - data = { - 'csrf_token': csrf_token, - 'status': 'Fixed', - 'comment': 'Woohoo a second comment !', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Comment added', - output.data) - self.assertNotIn( - '\n No changes to edit', - output.data) - self.assertTrue( - '

    Woohoo a second comment !

    ' in output.data) - self.assertEqual( - output.data.count('
    '), 2) - self.assertTrue( - '' - in output.data) - - # Add new tag - data = { - 'csrf_token': csrf_token, - 'status': 'Fixed', - 'tag': 'tag2', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Tag added: tag2', - output.data) - self.assertNotIn( - '\n No changes to edit', - output.data) - self.assertTrue( - '

    Woohoo a second comment !

    ' in output.data) - self.assertEqual( - output.data.count('
    '), 2) - self.assertTrue( - '' - in output.data) - - # Assign issue to an non-existent user - data = { - 'csrf_token': csrf_token, - 'status': 'Fixed', - 'assignee': 'ralph', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n No user "ralph" found', - output.data) - self.assertTrue( - '

    Woohoo a second comment !

    ' in output.data) - self.assertEqual( - output.data.count('
    '), 2) - self.assertTrue( - '' - in output.data) - - # Assign issue properly - data = { - 'csrf_token': csrf_token, - 'status': 'Fixed', - 'assignee': 'pingou', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Issue assigned', - output.data) - self.assertTrue( - '' in output.data) - self.assertTrue( - '

    Woohoo a second comment !

    ' in output.data) - self.assertEqual( - output.data.count('
    '), 2) - self.assertTrue( - '' - in output.data) - - # Create another issue with a dependency - 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') - - # Reset the status of the first issue - parent_issue = pagure.lib.search_issues( - self.session, repo, issueid=2) - parent_issue.status = 'Open' - # Add the dependency relationship - self.session.add(parent_issue) - issue = pagure.lib.search_issues(self.session, repo, issueid=2) - issue.parents.append(parent_issue) - self.session.add(issue) - self.session.commit() - - with tests.user_set(pagure.APP, user): - - data['csrf_token'] = csrf_token - output = self.app.post( - '/test/issue/2/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n You cannot close a ticket ' - 'that has ticket depending that are still open.', - output.data) - self.assertTrue( - '' - in output.data) - - # Create private issue - 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, - private=True, - ) - self.session.commit() - self.assertEqual(msg.title, 'Test issue') - - # Wrong user - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post( - '/test/issue/3/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 403) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1/update') - self.assertEqual(output.status_code, 302) - - # Repo not set-up for issue tracker - output = self.app.post('/test/issue/1/update', data=data) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_update_issue_drop_comment(self, p_send_email, p_ugt): - """ Test droping comment via the update_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - tests.create_projects(self.session) - - # Create issues 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') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # Add new comment - data = { - 'csrf_token': csrf_token, - 'comment': 'Woohoo a second comment !', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Comment added', - output.data) - self.assertTrue( - '

    Woohoo a second comment !

    ' in output.data) - self.assertEqual( - output.data.count('
    '), 2) - - repo = pagure.lib.get_project(self.session, 'test') - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - self.assertEqual(len(issue.comments), 1) - - data = { - 'csrf_token': csrf_token, - 'drop_comment': 1, - } - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - # Wrong issue id - output = self.app.post( - '/test/issue/3/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Wrong user - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 403) - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - # Drop the new comment - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Comment removed', - output.data) - - # Drop non-existant comment - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - repo = pagure.lib.get_project(self.session, 'test') - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - self.assertEqual(len(issue.comments), 0) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_update_issue_depend(self, p_send_email, p_ugt): - """ Test adding dependency via the update_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - tests.create_projects(self.session) - - # Create issues 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') - - repo = pagure.lib.get_project(self.session, 'test') - msg = pagure.lib.new_issue( - session=self.session, - repo=repo, - title='Test issue #2', - content='We should work on this again', - user='foo', - ticketfolder=None - ) - self.session.commit() - self.assertEqual(msg.title, 'Test issue #2') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # Add a dependent ticket - data = { - 'csrf_token': csrf_token, - 'depends': '2', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Dependency added', - output.data) - - # Add an invalid dependent ticket - data = { - 'csrf_token': csrf_token, - 'depends': '2,abc', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertNotIn( - '\n Dependency added', output.data) - - repo = pagure.lib.get_project(self.session, 'test') - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - self.assertEqual(issue.depends_text, [2]) - self.assertEqual(issue.blocks_text, []) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_update_issue_block(self, p_send_email, p_ugt): - """ Test adding blocked issue via the update_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - tests.create_projects(self.session) - - # Create issues 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') - - repo = pagure.lib.get_project(self.session, 'test') - msg = pagure.lib.new_issue( - session=self.session, - repo=repo, - title='Test issue #2', - content='We should work on this again', - user='foo', - ticketfolder=None - ) - self.session.commit() - self.assertEqual(msg.title, 'Test issue #2') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # Add a dependent ticket - data = { - 'csrf_token': csrf_token, - 'blocks': '2', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Dependency added', - output.data) - - # Add an invalid dependent ticket - data = { - 'csrf_token': csrf_token, - 'blocks': '2,abc', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertNotIn( - '\n Dependency added', - output.data) - - repo = pagure.lib.get_project(self.session, 'test') - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - self.assertEqual(issue.depends_text, []) - self.assertEqual(issue.blocks_text, [2]) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_upload_issue(self, p_send_email, p_ugt): - """ Test the upload_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'tickets'), bare=True) - - # Create issues 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') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - output = self.app.post('/foo/issue/1/upload') - self.assertEqual(output.status_code, 404) - - output = self.app.post('/test/issue/100/upload') - self.assertEqual(output.status_code, 404) - - # Invalid upload - data = { - 'enctype': 'multipart/form-data', - } - output = self.app.post( - '/test/issue/1/upload', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - json_data = json.loads(output.data) - exp = {'output': 'notok'} - self.assertDictEqual(json_data, exp) - - # Attach a file to a ticket - stream = open(os.path.join(tests.HERE, 'placebo.png'), 'rb') - data = { - 'csrf_token': csrf_token, - 'filestream': stream, - 'enctype': 'multipart/form-data', - } - output = self.app.post( - '/test/issue/1/upload', data=data, follow_redirects=True) - stream.close() - self.assertEqual(output.status_code, 200) - json_data = json.loads(output.data) - - folder = os.path.dirname( - os.path.abspath(__file__))[1:].replace('/', '_') - exp = { - 'output': 'ok', - 'filelocation': '/test/issue/raw/files/8a06845923010b27bfd8' - 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a' - 'be-%s_placebo.png' % folder, - 'filename': '%s_placebo.png' % folder, - } - self.assertDictEqual(json_data, exp) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - with tests.user_set(pagure.APP, user): - output = self.app.post('/test/issue/1/upload') - self.assertEqual(output.status_code, 404) - - def test_view_issue_raw_file_empty(self): - """ Test the view_issue_raw_file endpoint. """ - # Create the project and git repos - tests.create_projects(self.session) - tests.create_projects_git( - os.path.join(tests.HERE, 'tickets'), bare=True) - - # Create issues 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') - - url = '/issue/raw/files/8a06845923010b27bfd8'\ - 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\ - 'be-home_pierrey_repos_gitrepo_pagure_tests'\ - '_placebo.png' - - output = self.app.get('/foo' + url) - self.assertEqual(output.status_code, 404) - - output = self.app.get('/test' + url) - self.assertEqual(output.status_code, 404) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - output = self.app.get('/test' + url) - self.assertEqual(output.status_code, 404) - - def test_view_issue_raw_file(self): - """ Test the view_issue_raw_file endpoint. """ - # Create the issue and upload to it - self.test_upload_issue() - - # Project w/ issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': True} - self.session.add(repo) - self.session.commit() - - url = '/issue/raw/files/8a06845923010b27bfd8'\ - 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\ - 'be-%s_placebo.png' % os.path.dirname( - os.path.abspath(__file__))[1:].replace('/', '_') - - output = self.app.get('/foo' + url) - self.assertEqual(output.status_code, 404) - - output = self.app.get('/test/issue/raw/files/test.png') - self.assertEqual(output.status_code, 404) - - # Access file by name - output = self.app.get('/test' + url) - self.assertEqual(output.status_code, 200) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - output = self.app.get('/test' + url) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_edit_issue(self, p_send_email, p_ugt): - """ Test the edit_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - output = self.app.get('/foo/issue/1/edit') - self.assertEqual(output.status_code, 302) - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/foo/issue/1/edit') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - - output = self.app.get('/test/issue/1/edit') - self.assertEqual(output.status_code, 404) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1/edit') - self.assertEqual(output.status_code, 404) - - # Create issues 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') - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1/edit') - self.assertEqual(output.status_code, 403) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1/edit') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n Edit ' - 'issue #1\n
    ' in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - 'issue_content': 'We should work on this!' - } - - output = self.app.post('/test/issue/1/edit', data=data) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n Edit ' - 'issue #1\n
    ' in output.data) - self.assertEqual(output.data.count( - '\n This field is required' - '. \n '), 1) - self.assertEqual(output.data.count( - '\n Not a valid choice' - ' \n '), 1) - - data['status'] = 'Open' - data['title'] = 'Test issue #1' - output = self.app.post('/test/issue/1/edit', data=data) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '
    \n Edit ' - 'issue #1\n
    ' in output.data) - self.assertEqual(output.data.count( - '\n This field is required' - '. \n '), 0) - self.assertEqual(output.data.count( - '\n Not a valid choice' - '. \n '), 0) - - data['csrf_token'] = csrf_token - output = self.app.post( - '/test/issue/1/edit', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - '\n Successfully edited issue #1', - output.data) - self.assertIn( - '#1\n' - ' Test issue #1', - output.data) - self.assertEqual(output.data.count( - ''), 1) - self.assertEqual(output.data.count( - '
    \n' - '

    We should work on this!

    '), 1) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post('/test/issue/1/edit', data=data) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_edit_tag(self, p_send_email, p_ugt): - """ Test the edit_tag endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - output = self.app.get('/foo/tag/foo/edit') - self.assertEqual(output.status_code, 302) - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.get('/foo/tag/foo/edit') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - - output = self.app.get('/test/tag/foo/edit') - self.assertEqual(output.status_code, 403) - - # Create issues 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') - - # Add a tag to the issue - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - msg = pagure.lib.add_tag_obj( - session=self.session, - obj=issue, - tags='tag1', - user='pingou', - ticketfolder=None) - self.session.commit() - self.assertEqual(msg, 'Tag added: tag1') - - # Before edit, list tags - tags = pagure.lib.get_tags_of_project(self.session, repo) - self.assertEqual([tag.tag for tag in tags], ['tag1']) - - # Edit tag - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/tag/tag1/edit') - self.assertEqual(output.status_code, 200) - self.assertTrue('

    Edit tag: tag1

    ' in output.data) - self.assertTrue( - '

    Enter in the field below the new name for the tag: ' - '"tag1"

    ' in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = {'tag': 'tag2'} - - output = self.app.post('/test/tag/tag1/edit', data=data) - self.assertEqual(output.status_code, 200) - self.assertTrue('

    Edit tag: tag1

    ' in output.data) - self.assertTrue( - '

    Enter in the field below the new name for the tag: ' - '"tag1"

    ' in output.data) - - data['csrf_token'] = csrf_token - tests.create_projects_git(tests.HERE) - output = self.app.post( - '/test/tag/tag1/edit', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Edited tag: tag1 to tag2', - output.data) - - # After edit, list tags - tags = pagure.lib.get_tags_of_project(self.session, repo) - self.assertEqual([tag.tag for tag in tags], ['tag2']) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_remove_tag(self, p_send_email, p_ugt): - """ Test the remove_tag endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - output = self.app.post('/foo/droptag/') - self.assertEqual(output.status_code, 302) - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post('/foo/droptag/') - self.assertEqual(output.status_code, 404) - - tests.create_projects(self.session) - - output = self.app.post('/test/droptag/') - self.assertEqual(output.status_code, 403) - - # Create issues 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') - - # Add a tag to the issue - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - msg = pagure.lib.add_tag_obj( - session=self.session, - obj=issue, - tags='tag1', - user='pingou', - ticketfolder=None) - self.session.commit() - self.assertEqual(msg, 'Tag added: tag1') - - # Before edit, list tags - tags = pagure.lib.get_tags_of_project(self.session, repo) - self.assertEqual([tag.tag for tag in tags], ['tag1']) - - # Edit tag - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - tests.create_projects_git(tests.HERE) - output = self.app.post( - '/test/droptag/', data={}, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - 'Settings - test - Pagure' in output.data) - self.assertTrue("

    Settings for test

    " in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = {'tag': 'tag1'} - - output = self.app.post( - '/test/droptag/', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue("

    Settings for test

    " in output.data) - - data['csrf_token'] = csrf_token - output = self.app.post( - '/test/droptag/', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue("

    Settings for test

    " in output.data) - self.assertIn( - '\n Removed tag: tag1', - output.data) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_delete_issue(self, p_send_email, p_ugt): - """ Test the delete_issue endpoint. """ - p_send_email.return_value = True - p_ugt.return_value = True - - tests.create_projects(self.session) - tests.create_projects_git(tests.HERE) - tests.create_projects_git(os.path.join(tests.HERE, 'tickets')) - - # Create issues 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') - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - output = self.app.post( - '/foo/issue/1/drop', follow_redirects=True) - self.assertEqual(output.status_code, 404) - - output = self.app.post( - '/test/issue/100/drop', follow_redirects=True) - self.assertEqual(output.status_code, 404) - - output = self.app.post( - '/test/issue/1/drop', follow_redirects=True) - self.assertEqual(output.status_code, 403) - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post( - '/test/issue/1/drop', follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Issue #1 - test - Pagure', output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - data = { - } - - # No CSRF token - output = self.app.post( - '/test/issue/1/drop', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Issue #1 - test - Pagure', output.data) - - data['csrf_token'] = csrf_token - output = self.app.post( - '/test/issue/1/drop', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Issues - test - Pagure', output.data) - self.assertIn( - '\n Issue deleted', output.data) - - # Project w/o issue tracker - repo = pagure.lib.get_project(self.session, 'test') - repo.settings = {'issue_tracker': False} - self.session.add(repo) - self.session.commit() - - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.post('/test/issue/1/drop', data=data) - self.assertEqual(output.status_code, 404) - - @patch('pagure.lib.git.update_git') - @patch('pagure.lib.notify.send_email') - def test_update_issue_edit_comment(self, p_send_email, p_ugt): - """ Test the issues edit comment endpoint """ - p_send_email.return_value = True - p_ugt.return_value = True - - tests.create_projects(self.session) - - # Create issues 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') - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1') - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # Add new comment - data = { - 'csrf_token': csrf_token, - 'comment': 'Woohoo a second comment !', - } - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Comment added', - output.data) - self.assertTrue( - '

    Woohoo a second comment !

    ' in output.data) - self.assertEqual( - output.data.count('
    '), 2) - - repo = pagure.lib.get_project(self.session, 'test') - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - self.assertEqual(len(issue.comments), 1) - self.assertEqual(issue.comments[0].comment, 'Woohoo a second comment !') - - data = { - 'csrf_token': csrf_token, - 'edit_comment': 1, - 'update_comment': 'Updated comment', - } - - user = tests.FakeUser() - with tests.user_set(pagure.APP, user): - # Wrong issue id - output = self.app.post( - '/test/issue/3/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 404) - - # Wrong user - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 403) - - user = tests.FakeUser() - user.username = 'pingou' - with tests.user_set(pagure.APP, user): - # Edit comment - output = self.app.post( - '/test/issue/1/update', data=data, follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertIn( - '\n Comment updated', - output.data) - - repo = pagure.lib.get_project(self.session, 'test') - issue = pagure.lib.search_issues(self.session, repo, issueid=1) - self.assertEqual(len(issue.comments), 1) - self.assertEqual(issue.comments[0].comment, 'Updated comment') - - with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1/comment/1/edit') - self.assertTrue( - '

    test project #1

    ' - in output.data) - self.assertTrue('
    ' in output.data) - self.assertTrue('
    ' in output.data) - self.assertTrue( - '', - output.data) - - csrf_token = output.data.split( - 'name="csrf_token" type="hidden" value="')[1].split('">')[0] - - # View what's supposed to be an image - output = self.app.get('/test/edit/master/f/test.jpg') - self.assertEqual(output.status_code, 400) - self.assertIn('

    Cannot edit binary files

    ', output.data) - - # Check file before the commit: - output = self.app.get('/test/raw/master/f/sources') - self.assertEqual(output.status_code, 200) - self.assertEqual(output.data, 'foo\n bar') - - # No CSRF Token - data = { - 'content': 'foo\n bar\n baz', - 'commit_title': 'test commit', - 'commit_message': 'Online commits from the tests', - } - output = self.app.post('/test/edit/master/f/sources', data=data) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Edit - test - Pagure', output.data) - - # Check that nothing changed - output = self.app.get('/test/raw/master/f/sources') - self.assertEqual(output.status_code, 200) - self.assertEqual(output.data, 'foo\n bar') - - # Missing email - data['csrf_token'] = csrf_token - output = self.app.post('/test/edit/master/f/sources', data=data) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Edit - test - Pagure', output.data) - - # Invalid email - data['email'] = 'pingou@fp.o' - output = self.app.post('/test/edit/master/f/sources', data=data) - self.assertIn( - 'Edit - test - Pagure', output.data) - - # Works - data['email'] = 'bar@pingou.com' - data['branch'] = 'master' - output = self.app.post( - '/test/edit/master/f/sources', data=data, - follow_redirects=True) - self.assertEqual(output.status_code, 200) - self.assertIn( - 'Logs - test - Pagure', output.data) - self.assertIn( - '\n Changes committed', - output.data) - - # Check file after the commit: - output = self.app.get('/test/raw/master/f/sources') - self.assertEqual(output.status_code, 200) - self.assertEqual(output.data, 'foo\n bar\n baz') - - # Add a fork of a fork - item = pagure.lib.model.Project( - user_id=1, # pingou - name='test3', - description='test project #3', - parent_id=1, - hook_token='aaabbbppp', - ) - self.session.add(item) - self.session.commit() - - tests.add_content_git_repo( - os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) - tests.add_readme_git_repo( - os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')) - tests.add_commit_git_repo( - os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'), - ncommits=10) - - output = self.app.get('/fork/pingou/test3/edit/master/f/sources') - self.assertEqual(output.status_code, 200) - self.assertIn( - '
  • ' - '  master' - '
  • ' - '  sources' - '
  • ', output.data) - self.assertIn( - '