Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

api-docs: Fix having multiple curl auth line notes in examples.

Lauryn Menardc9210eb58740
diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.py
index 2a4f48104e4..ef5653ac33f 100644
--- a/zerver/openapi/markdown_extension.py
+++ b/zerver/openapi/markdown_extension.py
@@ -265,6 +265,19 @@ cURL example."""
return example_value
+INSECURE_OPERATIONS = [
+ "/dev_fetch_api_key:post",
+ "/fetch_api_key:post",
+ "/jwt/fetch_api_key:post",
+ "/dev_list_users:get",
+]
+
+
+def add_curl_auth_credentials_note(endpoint: str, method: str) -> bool:
+ operation = endpoint + ":" + method.lower()
+ return operation not in INSECURE_OPERATIONS
+
+
def generate_curl_example(
endpoint: str,
method: str,
@@ -276,17 +289,8 @@ def generate_curl_example(
operation_entry = openapi_spec.openapi()["paths"][endpoint][method.lower()]
global_security = openapi_spec.openapi()["security"]
- insecure_operations = [
- "/dev_fetch_api_key:post",
- "/fetch_api_key:post",
- "/jwt/fetch_api_key:post",
- "/dev_list_users:get",
- ]
lines = []
- if operation in insecure_operations:
- lines.append("```curl")
- else:
- lines.append("{!curl-auth-credentials.md!}\n\n```curl")
+ lines.append("```curl")
parameters = get_openapi_parameters(endpoint, method)
operation_request_body = operation_entry.get("requestBody", None)
@@ -318,7 +322,7 @@ def generate_curl_example(
"Unhandled global securityScheme. Please update the code to handle this scheme."
)
elif operation_security == []:
- if operation in insecure_operations:
+ if operation in INSECURE_OPERATIONS:
authentication_required = False
else:
raise AssertionError(
@@ -375,6 +379,8 @@ def render_curl_example(
kwargs: dict[str, Any] = {}
kwargs["api_url"] = api_url
rendered_example = []
+ if add_curl_auth_credentials_note(endpoint, method):
+ rendered_example += ["{!curl-auth-credentials.md!}\n\n"]
for element in get_curl_include_exclude(endpoint, method):
kwargs["include"] = None
kwargs["exclude"] = None
diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.py
index 08a00205fa4..1de4bf3af9a 100644
--- a/zerver/tests/test_openapi.py
+++ b/zerver/tests/test_openapi.py
@@ -753,7 +753,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
def test_generate_and_render_curl_example(self) -> None:
generated_curl_example = self.curl_example("/get_stream_id", "GET")
expected_curl_example = [
- "{!curl-auth-credentials.md!}\n\n```curl",
+ "```curl",
"curl -sSX GET -G http://localhost:9991/api/v1/get_stream_id \\",
" -u EMAIL_ADDRESS:API_KEY \\",
" --data-urlencode stream=Denmark",
@@ -782,7 +782,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
spec_mock.return_value = self.spec_mock_without_examples
generated_curl_example = self.curl_example("/mark_stream_as_read", "POST")
expected_curl_example = [
- "{!curl-auth-credentials.md!}\n\n```curl",
+ "```curl",
"curl -sSX POST http://localhost:9991/api/v1/mark_stream_as_read \\",
" -u EMAIL_ADDRESS:API_KEY \\",
" --data-urlencode stream_id=1 \\",
@@ -809,7 +809,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
],
)
expected_curl_example = [
- "{!curl-auth-credentials.md!}\n\n```curl",
+ "```curl",
"curl -sSX GET -G http://localhost:9991/api/v1/messages \\",
" -u EMAIL_ADDRESS:API_KEY \\",
" --data-urlencode anchor=43 \\",
@@ -828,7 +828,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
spec_mock.return_value = self.spec_mock_using_object
generated_curl_example = self.curl_example("/endpoint", "GET")
expected_curl_example = [
- "{!curl-auth-credentials.md!}\n\n```curl",
+ "```curl",
"curl -sSX GET -G http://localhost:9991/api/v1/endpoint \\",
" -u EMAIL_ADDRESS:API_KEY \\",
' --data-urlencode \'param1={"key": "value"}\'',
@@ -857,7 +857,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
spec_mock.return_value = self.spec_mock_using_param_in_path
generated_curl_example = self.curl_example("/endpoint/{param1}", "GET")
expected_curl_example = [
- "{!curl-auth-credentials.md!}\n\n```curl",
+ "```curl",
"curl -sSX GET -G http://localhost:9991/api/v1/endpoint/35 \\",
" -u EMAIL_ADDRESS:API_KEY \\",
' --data-urlencode \'param2={"key": "value"}\'',
@@ -870,7 +870,8 @@ class TestCurlExampleGeneration(ZulipTestCase):
"/get_stream_id:GET", api_url="https://zulip.example.com/api"
)
expected_curl_example = [
- "{!curl-auth-credentials.md!}\n\n```curl",
+ "{!curl-auth-credentials.md!}\n\n",
+ "```curl",
"curl -sSX GET -G https://zulip.example.com/api/v1/get_stream_id \\",
" -u EMAIL_ADDRESS:API_KEY \\",
" --data-urlencode stream=Denmark",
@@ -892,7 +893,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
],
)
expected_curl_example = [
- "{!curl-auth-credentials.md!}\n\n```curl",
+ "```curl",
"curl -sSX GET -G http://localhost:9991/api/v1/messages \\",
" -u EMAIL_ADDRESS:API_KEY \\",
" --data-urlencode anchor=43 \\",