diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index 4559b83..c05c644 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -83,6 +83,7 @@ class APIERROR(enum.Enum): EINVALIDISSUEFIELD = 'Invalid custom field submitted' EINVALIDISSUEFIELD_LINK = 'Invalid custom field submitted, the value '\ 'is not a link' + EINVALIDPRIORITY = 'Invalid priority submitted' def check_api_acls(acls, optional=False): diff --git a/pagure/api/issue.py b/pagure/api/issue.py index 474bbb7..10fc006 100644 --- a/pagure/api/issue.py +++ b/pagure/api/issue.py @@ -314,6 +314,23 @@ def api_view_issues(repo, username=None, namespace=None): tags = flask.request.args.getlist('tags') tags = [tag.strip() for tag in tags if tag.strip()] + priority_key = None + if priority: + found = False + if priority in repo.priorities: + found = True + priority_key = int(priority) + else: + for key, val in repo.priorities.items(): + if val.lower() == priority.lower(): + priority_key = key + found = True + break + + if not found: + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.EINVALIDPRIORITY) + # Hide private tickets private = False # If user is authenticated, show him/her his/her private tickets @@ -334,7 +351,7 @@ def api_view_issues(repo, username=None, namespace=None): 'author': author, 'private': private, 'milestones': milestone, - 'priority': priority, + 'priority': priority_key, 'no_milestones': no_stones, } diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py index 5902a0c..f4a5d69 100644 --- a/tests/test_pagure_flask_api_issue.py +++ b/tests/test_pagure_flask_api_issue.py @@ -1216,6 +1216,11 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.session.add(issue) self.session.commit() + # Set some priorities to the project + repo.priorities = {'1': 'High', '2': 'Normal'} + self.session.add(repo) + self.session.commit() + # List all opened issues output = self.app.get('/api/0/test/issues') self.assertEqual(output.status_code, 200) @@ -1224,7 +1229,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests): data['issues'][idx]['date_created'] = '1431414800' data['issues'][idx]['last_updated'] = '1431414800' lcl_issues = copy.deepcopy(LCL_ISSUES) - lcl_issues[0]['priority'] = 'high' + lcl_issues[0]['priority'] = 1 self.assertDictEqual( data, { @@ -1243,8 +1248,8 @@ class PagureFlaskApiIssuetests(tests.Modeltests): } ) - # List all issues of the milestone v1.0 - output = self.app.get('/api/0/test/issues?priority=1') + # List all issues of the priority high (ie: 1) + output = self.app.get('/api/0/test/issues?priority=high') self.assertEqual(output.status_code, 200) data = json.loads(output.data) for idx in range(len(data['issues'])): @@ -1268,6 +1273,42 @@ class PagureFlaskApiIssuetests(tests.Modeltests): } ) + output = self.app.get('/api/0/test/issues?priority=1') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + for idx in range(len(data['issues'])): + data['issues'][idx]['date_created'] = '1431414800' + data['issues'][idx]['last_updated'] = '1431414800' + self.assertDictEqual( + data, + { + "args": { + "assignee": None, + "author": None, + 'milestones': [], + 'no_stones': None, + 'priority': '1', + "since": None, + "status": None, + "tags": [], + }, + "issues": [lcl_issues[0]], + "total_issues": 1 + } + ) + + # Try getting issues with an invalid priority + output = self.app.get('/api/0/test/issues?priority=foobar') + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid priority submitted", + "error_code": "EINVALIDPRIORITY" + } + ) + def test_api_view_issues_no_stones(self): """ Test the api_view_issues method of the flask api when filtering with no_stones.