2020-09-15 00:24:01 +02:00
|
|
|
# Zulip's OpenAPI-based API documentation system is documented at
|
|
|
|
# https://zulip.readthedocs.io/en/latest/documentation/api.html
|
|
|
|
#
|
|
|
|
# This file contains the top-level logic for testing the cURL examples
|
|
|
|
# in Zulip's API documentation; the details are in
|
|
|
|
# zerver.openapi.curl_param_value_generators.
|
|
|
|
|
2019-08-07 10:55:41 +02:00
|
|
|
import glob
|
2020-06-11 00:54:34 +02:00
|
|
|
import html
|
2019-08-07 10:55:41 +02:00
|
|
|
import json
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import markdown
|
2019-08-07 10:55:41 +02:00
|
|
|
from zulip import Client
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2019-08-07 10:55:41 +02:00
|
|
|
from zerver.models import get_realm
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.openapi import markdown_extension
|
2021-04-02 12:28:50 +02:00
|
|
|
from zerver.openapi.curl_param_value_generators import assert_all_helper_functions_called
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2019-08-07 10:55:41 +02:00
|
|
|
|
2021-01-05 18:09:03 +01:00
|
|
|
def test_generated_curl_examples_for_success(client: Client, owner_client: Client) -> None:
|
2020-06-09 00:25:09 +02:00
|
|
|
authentication_line = f"{client.email}:{client.api_key}"
|
2020-08-11 01:47:49 +02:00
|
|
|
# A limited Markdown engine that just processes the code example syntax.
|
2019-08-07 10:55:41 +02:00
|
|
|
realm = get_realm("zulip")
|
2021-02-12 08:19:30 +01:00
|
|
|
md_engine = markdown.Markdown(
|
|
|
|
extensions=[markdown_extension.makeExtension(api_url=realm.uri + "/api")]
|
|
|
|
)
|
2019-08-07 10:55:41 +02:00
|
|
|
|
2020-09-12 07:06:07 +02:00
|
|
|
# We run our curl tests in alphabetical order (except that we
|
|
|
|
# delay the deactivate-user test to the very end), since we depend
|
2020-04-21 17:29:17 +02:00
|
|
|
# on "add" tests coming before "remove" tests in some cases. We
|
|
|
|
# should try to either avoid ordering dependencies or make them
|
|
|
|
# very explicit.
|
2020-09-14 17:23:17 +02:00
|
|
|
for file_name in sorted(glob.glob("templates/zerver/api/*.md")):
|
2020-10-24 09:33:54 +02:00
|
|
|
with open(file_name) as f:
|
|
|
|
for line in f:
|
|
|
|
# A typical example from the Markdown source looks like this:
|
|
|
|
# {generate_code_example(curl, ...}
|
|
|
|
if not line.startswith("{generate_code_example(curl"):
|
|
|
|
continue
|
|
|
|
# To do an end-to-end test on the documentation examples
|
|
|
|
# that will be actually shown to users, we use the
|
|
|
|
# Markdown rendering pipeline to compute the user-facing
|
|
|
|
# example, and then run that to test it.
|
|
|
|
curl_command_html = md_engine.convert(line.strip())
|
|
|
|
unescaped_html = html.unescape(curl_command_html)
|
2021-02-12 08:19:30 +01:00
|
|
|
curl_command_text = unescaped_html[len("<p><code>curl\n") : -len("</code></p>")]
|
2020-10-24 09:33:54 +02:00
|
|
|
curl_command_text = curl_command_text.replace(
|
2021-02-12 08:19:30 +01:00
|
|
|
"BOT_EMAIL_ADDRESS:BOT_API_KEY", authentication_line
|
|
|
|
)
|
2019-08-07 10:55:41 +02:00
|
|
|
|
2021-01-05 18:09:03 +01:00
|
|
|
# TODO: This needs_reactivation block is a hack.
|
|
|
|
# However, it's awkward to test the "deactivate
|
|
|
|
# myself" endpoint with how this system tries to use
|
|
|
|
# the same account for all tests without some special
|
|
|
|
# logic for that endpoint; and the hack is better than
|
|
|
|
# just not documenting the endpoint.
|
|
|
|
needs_reactivation = False
|
|
|
|
user_id = 0
|
|
|
|
if file_name == "templates/zerver/api/deactivate-own-user.md":
|
|
|
|
needs_reactivation = True
|
|
|
|
user_id = client.get_profile()["user_id"]
|
|
|
|
|
2020-10-24 09:33:54 +02:00
|
|
|
print("Testing {} ...".format(curl_command_text.split("\n")[0]))
|
2019-08-07 10:55:41 +02:00
|
|
|
|
2020-10-24 09:33:54 +02:00
|
|
|
# Turn the text into an arguments list.
|
2021-02-12 08:19:30 +01:00
|
|
|
generated_curl_command = [x for x in shlex.split(curl_command_text) if x != "\n"]
|
2019-08-07 10:55:41 +02:00
|
|
|
|
2020-10-24 09:33:54 +02:00
|
|
|
response_json = None
|
|
|
|
response = None
|
|
|
|
try:
|
|
|
|
# We split this across two lines so if curl fails and
|
|
|
|
# returns non-JSON output, we'll still print it.
|
2021-02-12 08:19:30 +01:00
|
|
|
response_json = subprocess.check_output(
|
|
|
|
generated_curl_command, universal_newlines=True
|
|
|
|
)
|
2020-10-24 09:33:54 +02:00
|
|
|
response = json.loads(response_json)
|
2021-02-12 08:19:30 +01:00
|
|
|
assert response["result"] == "success"
|
2021-01-05 18:09:03 +01:00
|
|
|
if needs_reactivation:
|
|
|
|
owner_client.reactivate_user_by_id(user_id)
|
2020-10-24 09:33:54 +02:00
|
|
|
except (AssertionError, Exception):
|
|
|
|
error_template = """
|
2019-08-07 10:55:41 +02:00
|
|
|
Error verifying the success of the API documentation curl example.
|
|
|
|
|
|
|
|
File: {file_name}
|
|
|
|
Line: {line}
|
2020-10-23 02:43:28 +02:00
|
|
|
Curl command:
|
2019-08-07 10:55:41 +02:00
|
|
|
{curl_command}
|
|
|
|
Response:
|
|
|
|
{response}
|
|
|
|
|
|
|
|
This test is designed to check each generate_code_example(curl) instance in the
|
|
|
|
API documentation for success. If this fails then it means that the curl example
|
|
|
|
that was generated was faulty and when tried, it resulted in an unsuccessful
|
|
|
|
response.
|
|
|
|
|
|
|
|
Common reasons for why this could occur:
|
|
|
|
1. One or more example values in zerver/openapi/zulip.yaml for this endpoint
|
|
|
|
do not line up with the values in the test database.
|
|
|
|
2. One or more mandatory parameters were included in the "exclude" list.
|
|
|
|
|
|
|
|
To learn more about the test itself, see zerver/openapi/test_curl_examples.py.
|
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
error_template.format(
|
|
|
|
file_name=file_name,
|
|
|
|
line=line,
|
|
|
|
curl_command=generated_curl_command,
|
|
|
|
response=response_json
|
|
|
|
if response is None
|
|
|
|
else json.dumps(response, indent=4),
|
|
|
|
)
|
|
|
|
)
|
2020-10-24 09:33:54 +02:00
|
|
|
raise
|
2019-10-21 12:43:00 +02:00
|
|
|
|
2021-04-02 12:28:50 +02:00
|
|
|
assert_all_helper_functions_called()
|