#5058 Make the API endpoint to update project's options accept JSON
Merged 3 years ago by pingou. Opened 3 years ago by pingou.

file modified
+16 -2
@@ -2865,6 +2865,19 @@ 

      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 @@ 

      _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(

@@ -4413,6 +4413,67 @@ 

          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

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 pingou@pingoured.fr

rebased onto 2b7b26e5b1c18bbb2de154e74d7f7156e2e31a38

3 years ago

probably related: pagure/api/project.py line 2868 does not have a sample request.

    Sample request body
    ^^^^^^^^^^^^^^^^^^^

     ::

        {
            'issue_tracker': false,
            'disable_non_fast-forward_merges': true
        }

rebased onto a76d141c8c999d485871fa924ccbcd452a17717a

3 years ago

probably related: pagure/api/project.py line 2868 does not have a sample request.

Adjusted :)

rebased onto 0504b8a4b3768e77f42d52f762565fecc39adb52

3 years ago

rebased onto 0b399a3

3 years ago

Pull-Request has been merged by pingou

3 years ago