From 929a2a220cce6b9f5ab7777e4288188473497e20 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: May 09 2017 02:13:15 +0000 Subject: Move the tests for the API to change the status of issues to their own file Signed-off-by: Pierre-Yves Chibon --- diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py index adbb43b..9269aa0 100644 --- a/tests/test_pagure_flask_api_issue.py +++ b/tests/test_pagure_flask_api_issue.py @@ -1845,180 +1845,6 @@ class PagureFlaskApiIssuetests(tests.Modeltests): } ) - 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(self.path, '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.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name, - data['error_code']) - self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error']) - - # No issue - 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.get_authorized_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=4, - 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.get_authorized_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.get_authorized_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", - "errors": {"status": ["Not a valid choice"]} - } - ) - - # No change - repo = pagure.get_authorized_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.get_authorized_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':[ - 'Issue status updated to: Closed (was: Open)', - 'Issue close_status updated to: Fixed' - ]} - ) - - 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, 401) - data = json.loads(output.data) - self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name, - data['error_code']) - self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error']) - def test_api_change_milestone_issue(self): """ Test the api_change_milestone_issue method of the flask api. """ tests.create_projects(self.session) @@ -2219,7 +2045,6 @@ class PagureFlaskApiIssuetests(tests.Modeltests): data['error_code']) self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error']) - @patch('pagure.lib.git.update_git') @patch('pagure.lib.notify.send_email') def test_api_comment_issue(self, p_send_email, p_ugt): diff --git a/tests/test_pagure_flask_api_issue_change_status.py b/tests/test_pagure_flask_api_issue_change_status.py new file mode 100644 index 0000000..536d85b --- /dev/null +++ b/tests/test_pagure_flask_api_issue_change_status.py @@ -0,0 +1,227 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015-2017 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import copy +import datetime +import unittest +import shutil +import sys +import time +import os + +import json +from mock import patch, MagicMock + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure +import pagure.lib +import tests + + +class PagureFlaskApiIssueChangeStatustests(tests.Modeltests): + """ Tests for the flask API of pagure for changing the status of an + issue + """ + + @patch('pagure.lib.notify.send_email', MagicMock(return_value=True)) + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApiIssueChangeStatustests, 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() + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'tickets')) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + # Create normal issue + repo = pagure.get_authorized_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 private issue + msg = pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue #2', + content='We should work on this', + user='foo', + ticketfolder=None, + private=True, + ) + self.session.commit() + self.assertEqual(msg.title, 'Test issue #2') + + def test_api_change_status_issue_invalid_project(self): + """ Test the api_change_status_issue method of the flask api. """ + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Invalid project + output = self.app.post( + '/api/0/foobar/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", + } + ) + + def test_api_change_status_issue_token_not_for_project(self): + """ Test the api_change_status_issue method of the flask api. """ + + headers = {'Authorization': 'token aaabbbcccddd'} + + # 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.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name, + data['error_code']) + self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error']) + + def test_api_change_status_issue_invalid_issue(self): + """ Test the api_change_status_issue method of the flask api. """ + + headers = {'Authorization': 'token aaabbbcccddd'} + + # No issue + output = self.app.post('/api/0/test/issue/42/status', headers=headers) + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Issue not found", + "error_code": "ENOISSUE", + } + ) + + def test_api_change_status_issue_incomplete(self): + """ Test the api_change_status_issue method of the flask api. """ + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Check status before + repo = pagure.get_authorized_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", + "errors": {"status": ["Not a valid choice"]} + } + ) + + # No change + repo = pagure.get_authorized_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.status, 'Open') + + def test_api_change_status_issue_no_change(self): + """ Test the api_change_status_issue method of the flask api. """ + + headers = {'Authorization': 'token aaabbbcccddd'} + + 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.get_authorized_project(self.session, 'test') + issue = pagure.lib.search_issues(self.session, repo, issueid=1) + self.assertEqual(issue.status, 'Open') + + @patch('pagure.lib.notify.send_email', MagicMock(return_value=True)) + def test_api_change_status_issue(self): + """ Test the api_change_status_issue method of the flask api. """ + + headers = {'Authorization': 'token aaabbbcccddd'} + + 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':[ + 'Issue status updated to: Closed (was: Open)', + 'Issue close_status updated to: Fixed' + ]} + ) + + 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, 401) + data = json.loads(output.data) + self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name, + data['error_code']) + self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error']) + + +if __name__ == '__main__': + unittest.main(verbosity=2)