From 738a5641fabaec40e1237126ea563b8a4f38c351 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 23 2017 09:50:41 +0000 Subject: Add the option to get only the issue created or assigned to an user Instead of always returning both. Adjust the tests accordingly. Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/api/user.py b/pagure/api/user.py index 4f0bc75..43a1a5e 100644 --- a/pagure/api/user.py +++ b/pagure/api/user.py @@ -195,6 +195,16 @@ def api_view_user_issues(username): | | | | Default: | | | | | ``date_created`` | +---------------+---------+--------------+---------------------------+ + | ``assignee`` | boolean | Optional | | A boolean of whether to | + | | | | return the issues | + | | | | assigned to this user | + | | | | or not. Defaults to True| + +---------------+---------+--------------+---------------------------+ + | ``author`` | boolean | Optional | | A boolean of whether to | + | | | | return the issues | + | | | | created by this user or | + | | | | not. Defaults to True | + +---------------+---------+--------------+---------------------------+ Sample response ^^^^^^^^^^^^^^^ @@ -273,8 +283,6 @@ def api_view_user_issues(username): """ - assignee = flask.request.args.get('assignee', None) - author = username milestone = flask.request.args.getlist('milestones', None) no_stones = flask.request.args.get('no_stones', None) if no_stones is not None: @@ -290,6 +298,11 @@ def api_view_user_issues(username): tags = [tag.strip() for tag in tags if tag.strip()] page = flask.request.args.get('page', 1) + assignee = flask.request.args.get('assignee', '').lower()\ + not in ['false', '0', 'f'] + author = flask.request.args.get('author', '').lower() \ + not in ['false', '0', 'f'] + try: page = int(page) if page <= 0: @@ -343,21 +356,29 @@ def api_view_user_issues(username): params.update({'updated_after': updated_after}) - # Issues authored by this user - params_created = params.copy() - params_created.update({"author": username}) - issues_created = pagure.lib.search_issues(**params_created) - params_created.update({"offset": None, 'limit': None, 'count': True}) - issues_created_cnt = pagure.lib.search_issues(**params_created) - issues_created_pages = int(ceil(issues_created_cnt / float(50))) or 1 - - # Issues assigned to this user - params_assigned = params.copy() - params_assigned.update({"assignee": username}) - issues_assigned = pagure.lib.search_issues(**params_assigned) - params_assigned.update({"offset": None, 'limit': None, 'count': True}) - issues_assigned_cnt = pagure.lib.search_issues(**params_assigned) - issues_assigned_pages = int(ceil(issues_assigned_cnt / float(50))) or 1 + issues_created = [] + issues_created_pages = 1 + if author: + # Issues authored by this user + params_created = params.copy() + params_created.update({"author": username}) + issues_created = pagure.lib.search_issues(**params_created) + params_created.update({"offset": None, 'limit': None, 'count': True}) + issues_created_cnt = pagure.lib.search_issues(**params_created) + issues_created_pages = int( + ceil(issues_created_cnt / float(50))) or 1 + + issues_assigned = [] + issues_assigned_pages = 1 + if assignee: + # Issues assigned to this user + params_assigned = params.copy() + params_assigned.update({"assignee": username}) + issues_assigned = pagure.lib.search_issues(**params_assigned) + params_assigned.update({"offset": None, 'limit': None, 'count': True}) + issues_assigned_cnt = pagure.lib.search_issues(**params_assigned) + issues_assigned_pages = int( + ceil(issues_assigned_cnt / float(50))) or 1 jsonout = flask.jsonify({ 'total_issues_created_pages': issues_created_pages, @@ -377,6 +398,8 @@ def api_view_user_issues(username): 'status': status, 'tags': tags, 'page': page, + 'assignee': assignee, + 'author': author, } }) return jsonout diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py index beb2177..0d5f15a 100644 --- a/tests/test_pagure_flask_api_issue.py +++ b/tests/test_pagure_flask_api_issue.py @@ -284,7 +284,7 @@ LCL_ISSUES = [ ] -class PagureFlaskApiIssuetests(tests.Modeltests): +class PagureFlaskApiIssuetests(tests.SimplePagureTest): """ Tests for the flask API of pagure for issue """ maxDiff = None @@ -3082,6 +3082,8 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.assertEqual(output.status_code, 200) data = json.loads(output.data) args = { + u"assignee": True, + u"author": True, "milestones": [], "no_stones": None, "order": None, @@ -3105,6 +3107,8 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.assertEqual(output.status_code, 200) data = json.loads(output.data) args = { + u"assignee": True, + u"author": True, "milestones": ['v1.0'], "no_stones": None, "order": None, @@ -3128,6 +3132,8 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.assertEqual(output.status_code, 200) data = json.loads(output.data) args = { + u"assignee": True, + u"author": True, "milestones": [], "no_stones": None, "order": None, @@ -3151,6 +3157,8 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.assertEqual(output.status_code, 200) data = json.loads(output.data) args = { + u"assignee": True, + u"author": True, "milestones": [], "no_stones": None, "order": None, @@ -3193,14 +3201,16 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.assertEqual(output.status_code, 200) data = json.loads(output.data) args = { - "milestones": [], - "no_stones": None, - "order": None, - "order_key": None, - "page": 1, - "since": None, - "status": None, - "tags": [], + u"assignee": True, + u"author": True, + u"milestones": [], + u"no_stones": None, + u"order": None, + u"order_key": None, + u"page": 1, + u"since": None, + u"status": None, + u"tags": [], } self.assertEqual(data['args'], args) @@ -3240,6 +3250,64 @@ class PagureFlaskApiIssuetests(tests.Modeltests): } ) + def test_api_view_user_issues_foo_no_assignee(self): + """ Test the api_view_user_issues method of the flask api for foo. + """ + self.test_api_new_issue() + + output = self.app.get('/api/0/user/foo/issues?assignee=0') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + args = { + u"assignee": False, + u"author": True, + u"milestones": [], + u"no_stones": None, + u"order": None, + u"order_key": None, + u"page": 1, + u"since": None, + u"status": None, + u"tags": [], + } + + self.assertEqual(data['args'], args) + self.assertEqual(data['issues_assigned'], []) + self.assertEqual(data['issues_created'], []) + self.assertEqual(data['total_issues_assigned'], 0) + self.assertEqual(data['total_issues_created'], 0) + self.assertEqual(data['total_issues_assigned_pages'], 1) + self.assertEqual(data['total_issues_created_pages'], 1) + + def test_api_view_user_issues_pingou_no_author(self): + """ Test the api_view_user_issues method of the flask api for pingou. + """ + self.test_api_new_issue() + + output = self.app.get('/api/0/user/pingou/issues?author=0') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + args = { + u"assignee": True, + u"author": False, + "milestones": [], + "no_stones": None, + "order": None, + "order_key": None, + "page": 1, + "since": None, + "status": None, + "tags": [] + } + + self.assertEqual(data['args'], args) + self.assertEqual(data['issues_assigned'], []) + self.assertEqual(data['issues_created'], []) + self.assertEqual(data['total_issues_assigned'], 0) + self.assertEqual(data['total_issues_created'], 0) + self.assertEqual(data['total_issues_assigned_pages'], 1) + self.assertEqual(data['total_issues_created_pages'], 1) + if __name__ == '__main__': unittest.main(verbosity=2)