curl_examples: Add testing for template endpoints.

This commit adds support for testing of
those endpoints whose .md files would
be deleted in favour of their pages
to be generated automatically by the template.

curl examples for such endpoints must exist
in accordance to the pattern of template, which
can be used to run the tests for them.
This commit is contained in:
Suyash Vardhan Mathur
2021-06-04 17:10:46 +05:30
committed by Tim Abbott
parent 93cbe6f175
commit 44c25619d7

View File

@@ -22,6 +22,7 @@ from zerver.openapi.curl_param_value_generators import (
AUTHENTICATION_LINE,
assert_all_helper_functions_called,
)
from zerver.openapi.openapi import get_endpoint_from_operationid
def test_generated_curl_examples_for_success(client: Client) -> None:
@@ -49,7 +50,8 @@ def test_generated_curl_examples_for_success(client: Client) -> None:
file_name = os.path.join(settings.DEPLOY_ROOT, "templates/zerver/api/", article_name)
curl_commands_to_test = []
with open(file_name) as f:
if os.path.exists(file_name):
f = open(file_name, "r")
for line in f:
# Set AUTHENTICATION_LINE to default_authentication_line.
# Set this every iteration, because deactivate_own_user
@@ -59,6 +61,17 @@ def test_generated_curl_examples_for_success(client: Client) -> None:
# {generate_code_example(curl, ...}
if line.startswith("{generate_code_example(curl"):
curl_commands_to_test.append(line)
else:
# If the file doesn't exist, then it has been
# deleted and its page is generated by the
# template. Thus, the curl example would just
# a single one following the template's pattern.
endpoint_path, endpoint_method = get_endpoint_from_operationid(
endpoint.replace("-", "_")
)
endpoint_string = endpoint_path + ":" + endpoint_method
command = f"{{generate_code_example(curl)|{endpoint_string}|example}}"
curl_commands_to_test.append(command)
for line in curl_commands_to_test:
# To do an end-to-end test on the documentation examples