From f0750904106ba68c9866bf9a3f15cef82a57b242 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 17 2017 09:19:26 +0000 Subject: [PATCH 1/2] Adjust the logic when filtering issues by priorities In the database priorities are stored in tickets as integer so we cannot query them as string directly. The approach taken with this commit allows to filter by priority given its rank (integer) or its label (name). Thanks to faitout for pointing it out --- 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. From c2250ff88054789ea6f11cbffd1980ab7039c21b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 17 2017 09:19:26 +0000 Subject: [PATCH 2/2] Make testing with an invalid priority a separate test --- diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py index f4a5d69..8ba4531 100644 --- a/tests/test_pagure_flask_api_issue.py +++ b/tests/test_pagure_flask_api_issue.py @@ -1297,6 +1297,16 @@ class PagureFlaskApiIssuetests(tests.Modeltests): } ) + def test_api_view_issues_priority_invalid(self): + """ Test the api_view_issues method of the flask api when filtering + for an invalid priority. + """ + tests.create_projects(self.session) + tests.create_projects_git( + os.path.join(self.path, 'tickets'), bare=True) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + # Try getting issues with an invalid priority output = self.app.get('/api/0/test/issues?priority=foobar') self.assertEqual(output.status_code, 400)