playgrounds: Avoid json encoding each field in POST /realm/playgrounds.

This prevents us from having to json encode every field in the POST
request to /realm/playgrounds, and keeps the client logic simpler
when adding a playground.
This commit is contained in:
Sumanth V Rao
2021-04-17 11:56:57 +05:30
committed by Tim Abbott
parent 3b974d9ef7
commit f9b79999ed
5 changed files with 16 additions and 26 deletions

View File

@@ -5,7 +5,6 @@
# based on Zulip's OpenAPI definitions, as well as test setup and
# fetching of appropriate parameter values to use when running the
# cURL examples as part of the tools/test-api test suite.
import json
from functools import wraps
from typing import Any, Callable, Dict, List, Optional, Set, Tuple
@@ -283,8 +282,8 @@ def upload_custom_emoji() -> Dict[str, object]:
def add_realm_playground() -> Dict[str, object]:
return {
"name": "Python2 playground",
"pygments_language": json.dumps("Python2"),
"url_prefix": json.dumps("https://python2.example.com"),
"pygments_language": "Python2",
"url_prefix": "https://python2.example.com",
}

View File

@@ -390,8 +390,8 @@ def add_realm_playground(client: Client) -> None:
# Add a realm playground for Python
request = {
"name": "Python playground",
"pygments_language": json.dumps("Python"),
"url_prefix": json.dumps("https://python.example.com"),
"pygments_language": "Python",
"url_prefix": "https://python.example.com",
}
result = client.call_endpoint(url="/realm/playgrounds", method="POST", request=request)
# {code_example|end}

View File

@@ -6692,7 +6692,8 @@ paths:
`POST {{ api_url }}/v1/realm/playgrounds`
**Changes**: New in Zulip 4.0 (feature level 49).
**Changes**: New in Zulip 4.0 (feature level 49). A parameter encoding bug was
fixed in Zulip 4.0 (feature level 57).
parameters:
- name: name
in: query
@@ -6702,7 +6703,7 @@ paths:
playground options exist for that programming language.
schema:
type: string
example: "Python playground"
example: Python playground
required: true
- name: pygments_language
in: query
@@ -6711,7 +6712,7 @@ paths:
programming language.
schema:
type: string
example: "Python"
example: Python
required: true
- name: url_prefix
in: query

View File

@@ -1,18 +1,9 @@
from typing import Dict
import orjson
from zerver.lib.actions import do_add_realm_playground
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmPlayground, get_realm
class RealmPlaygroundTests(ZulipTestCase):
def json_serialize(self, payload: Dict[str, str]) -> Dict[str, str]:
payload["url_prefix"] = orjson.dumps(payload["url_prefix"]).decode()
payload["pygments_language"] = orjson.dumps(payload["pygments_language"]).decode()
return payload
def test_create_one_playground_entry(self) -> None:
iago = self.example_user("iago")
@@ -22,7 +13,7 @@ class RealmPlaygroundTests(ZulipTestCase):
"url_prefix": "https://python.example.com",
}
# Now send a POST request to the API endpoint.
resp = self.api_post(iago, "/json/realm/playgrounds", self.json_serialize(payload))
resp = self.api_post(iago, "/json/realm/playgrounds", payload)
self.assert_json_success(resp)
# Check if the actual object exists
@@ -47,7 +38,7 @@ class RealmPlaygroundTests(ZulipTestCase):
},
]
for payload in data:
resp = self.api_post(iago, "/json/realm/playgrounds", self.json_serialize(payload))
resp = self.api_post(iago, "/json/realm/playgrounds", payload)
self.assert_json_success(resp)
realm = get_realm("zulip")
@@ -66,12 +57,12 @@ class RealmPlaygroundTests(ZulipTestCase):
"pygments_language": "Python",
"url_prefix": "https://invalid-url",
}
resp = self.api_post(iago, "/json/realm/playgrounds", self.json_serialize(payload))
resp = self.api_post(iago, "/json/realm/playgrounds", payload)
self.assert_json_error(resp, "url_prefix is not a URL")
payload["url_prefix"] = "https://python.example.com"
payload["pygments_language"] = "a$b$c"
resp = self.api_post(iago, "/json/realm/playgrounds", self.json_serialize(payload))
resp = self.api_post(iago, "/json/realm/playgrounds", payload)
self.assert_json_error(resp, "Invalid characters in pygments language")
def test_create_already_existing_playground(self) -> None:
@@ -82,11 +73,10 @@ class RealmPlaygroundTests(ZulipTestCase):
"pygments_language": "Python",
"url_prefix": "https://python.example.com",
}
serialized_payload = self.json_serialize(payload)
resp = self.api_post(iago, "/json/realm/playgrounds", serialized_payload)
resp = self.api_post(iago, "/json/realm/playgrounds", payload)
self.assert_json_success(resp)
resp = self.api_post(iago, "/json/realm/playgrounds", serialized_payload)
resp = self.api_post(iago, "/json/realm/playgrounds", payload)
self.assert_json_error(resp, "Realm playground with this Realm and Name already exists.")
def test_not_realm_admin(self) -> None:

View File

@@ -39,8 +39,8 @@ def add_realm_playground(
request: HttpRequest,
user_profile: UserProfile,
name: str = REQ(),
url_prefix: str = REQ(json_validator=check_url),
pygments_language: str = REQ(json_validator=check_pygments_language),
url_prefix: str = REQ(str_validator=check_url),
pygments_language: str = REQ(str_validator=check_pygments_language),
) -> HttpResponse:
try:
playground_id = do_add_realm_playground(