import glob import json import shlex import subprocess import markdown import os from zulip import Client from zerver.lib.bugdown import api_code_examples from zerver.models import get_realm exclude_list = [ # The endpoint in these docs expect one or more param values that reflects the DB state. # We currently get the example values from openapi specs and they don't refelect the # state of the DB. This results in the curl request to fail. 'get-raw-message.md', 'update-message.md', 'delete-message.md', 'get-message-history.md', 'update-message-flags.md', 'mark-as-read-bulk.md', 'update-subscription-properties.md', 'remove-subscriptions.md', 'get-stream-topics.md', 'delete-stream.md', 'get-presence.md', 'create-user-group.md', 'update-user-group.md', 'delete-user-group.md', 'add-linkifiers.md', 'get-events-from-queue.md', 'delete-queue.md', # Endpoint can be called only by administrators. 'create-user.md', 'remove-linkifiers.md', # Endpoint does not accept bot requests 'get-user-groups.md', ] def test_generated_curl_examples_for_success(client: Client) -> None: authentication_line = "{}:{}".format(client.email, client.api_key) # A limited markdown engine that just processes the code example syntax. realm = get_realm("zulip") md_engine = markdown.Markdown(extensions=[api_code_examples.makeExtension( api_url=realm.uri + "/api")]) for file_name in glob.glob("templates/zerver/api/*.md"): if os.path.basename(file_name) in exclude_list: print("Skipping", file_name) continue documentation_lines = open(file_name, "r").readlines() for line in documentation_lines: # 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()) curl_command_text = curl_command_html[len("
curl\n"):-len("