From 0b399a3cea968876a8d261018096d77e353cf1ad Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 05 2020 10:20:46 +0000 Subject: Make the API endpoint to update project's options accept JSON Up until now that API endpoint only accepted HTTP arguments but it is sometime useful to send the update as a JSON blob instead of different arguments. This API endpoint will now support both approaches. Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/api/project.py b/pagure/api/project.py index 64f413b..ed50f42 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -2865,6 +2865,19 @@ def api_modify_project_options(repo, username=None, namespace=None): do not specify in the request values that have been changed before they will go back to their default value. + The fields and values can be specified either as a regular HTTP form or as + a JSON blob. + + Sample request body + ^^^^^^^^^^^^^^^^^^^ + + :: + + { + 'issue_tracker': false, + 'disable_non_fast-forward_merges': true + } + Sample response ^^^^^^^^^^^^^^^ @@ -2880,9 +2893,10 @@ def api_modify_project_options(repo, username=None, namespace=None): _check_token(project, project_token=False) settings = {} - for key in flask.request.form: + request_data = get_request_data() + for key in request_data: - settings[key] = _check_value(flask.request.form[key]) + settings[key] = _check_value(request_data[key]) try: message = pagure.lib.query.update_project_settings( diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index de40d0f..cf297c1 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -4413,6 +4413,67 @@ class PagureFlaskApiProjectOptionsTests(tests.Modeltests): before["settings"]["issue_tracker"] = False self.assertEqual(after, before) + def test_api_modify_project_options_json(self): + """ Test accessing api_modify_project_options w/ auth header and + input submitted as JSON instead of HTML arguments. """ + + # check before + headers = {"Authorization": "token aaabbbcccddd"} + output = self.app.get("/api/0/test/options", headers=headers) + self.assertEqual(output.status_code, 200) + before = json.loads(output.get_data(as_text=True)) + self.assertEqual( + before, + { + "settings": { + "Enforce_signed-off_commits_in_pull-request": False, + "Minimum_score_to_merge_pull-request": -1, + "Only_assignee_can_merge_pull-request": False, + "Web-hooks": None, + "always_merge": False, + "disable_non_fast-forward_merges": False, + "fedmsg_notifications": True, + "issue_tracker": True, + "issue_tracker_read_only": False, + "issues_default_to_private": False, + "mqtt_notifications": True, + "notify_on_commit_flag": False, + "notify_on_pull-request_flag": False, + "open_metadata_access_to_all": False, + "project_documentation": False, + "pull_request_access_only": False, + "pull_requests": True, + "stomp_notifications": True, + }, + "status": "ok", + }, + ) + + # Update: `issue_tracker`. + data = json.dumps({"issue_tracker": False}) + headers["Content-Type"] = "application/json" + output = self.app.post( + "/api/0/test/options/update", headers=headers, data=data + ) + self.assertEqual(output.status_code, 200) + data = json.loads(output.get_data(as_text=True)) + self.assertEqual( + data, + { + "message": "Edited successfully settings of repo: test", + "status": "ok", + }, + ) + + # check after + headers = {"Authorization": "token aaabbbcccddd"} + output = self.app.get("/api/0/test/options", headers=headers) + self.assertEqual(output.status_code, 200) + after = json.loads(output.get_data(as_text=True)) + self.assertNotEqual(before, after) + before["settings"]["issue_tracker"] = False + self.assertEqual(after, before) + class PagureFlaskApiProjectCreateAPITokenTests(tests.Modeltests): """ Tests for the flask API of pagure for creating user project API token