From 8e9ccdf376b78b20e2782c22268c728b185d203f Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Mon, 21 Oct 2019 16:13:00 +0530 Subject: [PATCH] tests: Remove get-raw-message from curl test exclude_list. --- tools/test-api | 2 + zerver/lib/bugdown/api_code_examples.py | 6 +++ zerver/openapi/curl_param_value_generators.py | 49 +++++++++++++++++++ zerver/openapi/test_curl_examples.py | 6 ++- zproject/settings.py | 2 + zproject/test_settings.py | 3 ++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 zerver/openapi/curl_param_value_generators.py diff --git a/tools/test-api b/tools/test-api index 5ba66898f8..ac2a6a316d 100755 --- a/tools/test-api +++ b/tools/test-api @@ -3,6 +3,8 @@ import argparse import os import sys +os.environ["RUNNING_OPENAPI_CURL_TEST"] = "1" + # check for the venv from lib import sanity_check sanity_check.check_venv(__file__) diff --git a/zerver/lib/bugdown/api_code_examples.py b/zerver/lib/bugdown/api_code_examples.py index 9dfabb8cae..45a998db22 100644 --- a/zerver/lib/bugdown/api_code_examples.py +++ b/zerver/lib/bugdown/api_code_examples.py @@ -2,6 +2,8 @@ import re import json import inspect +from django.conf import settings + from markdown.extensions import Extension from markdown.preprocessors import Preprocessor from typing import Any, Dict, Optional, List, Tuple @@ -159,6 +161,10 @@ def generate_curl_example(endpoint: str, method: str, openapi_entry = openapi_spec.spec()['paths'][endpoint][method.lower()] openapi_params = openapi_entry.get("parameters", []) + if settings.RUNNING_OPENAPI_CURL_TEST: # nocoverage + from zerver.openapi.curl_param_value_generators import patch_openapi_params + openapi_params = patch_openapi_params(endpoint + ":" + method.lower(), openapi_params) + format_dict = {} for param in openapi_params: if param["in"] != "path": diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py new file mode 100644 index 0000000000..30180f35f3 --- /dev/null +++ b/zerver/openapi/curl_param_value_generators.py @@ -0,0 +1,49 @@ +from typing import Dict, Any, Callable, Set, List + +from functools import wraps + +from zerver.lib.test_classes import ZulipTestCase + +GENERATOR_FUNCTIONS = dict() # type: Dict[str, Callable[..., Dict[Any, Any]]] +REGISTERED_GENERATOR_FUNCTIONS = set() # type: Set[str] +CALLED_GENERATOR_FUNCTIONS = set() # type: Set[str] + +helpers = ZulipTestCase() + +def openapi_param_value_generator(endpoints: List[str]) -> Callable[[Callable[..., Any]], + Callable[..., Any]]: + """This decorator is used to register openapi param value genarator functions + with endpoints. Example usage: + + @openapi_param_value_generator(["/messages/render:post"]) + def ... + """ + def wrapper(generator_func: Callable[..., Dict[Any, Any]]) -> Callable[..., Dict[Any, Any]]: + @wraps(generator_func) + def _record_calls_wrapper(*args: Any, **kwargs: Any) -> Dict[Any, Any]: + CALLED_GENERATOR_FUNCTIONS.add(generator_func.__name__) + return generator_func(*args, **kwargs) + + REGISTERED_GENERATOR_FUNCTIONS.add(generator_func.__name__) + for endpoint in endpoints: + GENERATOR_FUNCTIONS[endpoint] = _record_calls_wrapper + + return _record_calls_wrapper + return wrapper + +def patch_openapi_params(openapi_entry: str, openapi_params: List[Dict[str, Any]]) -> List[Dict[str, Any]]: + if openapi_entry not in GENERATOR_FUNCTIONS: + return openapi_params + func = GENERATOR_FUNCTIONS[openapi_entry] + realm_param_values = func() # type: Dict[str, Any] + for param in openapi_params: + param_name = param["name"] + if param_name in realm_param_values: + param["example"] = realm_param_values[param_name] + return openapi_params + +@openapi_param_value_generator(["/messages/{message_id}:get"]) +def iago_message_id() -> Dict[str, int]: + return { + "message_id": helpers.send_stream_message(helpers.example_email("iago"), "Denmark") + } diff --git a/zerver/openapi/test_curl_examples.py b/zerver/openapi/test_curl_examples.py index cfd25fc0e3..f63c7d3211 100644 --- a/zerver/openapi/test_curl_examples.py +++ b/zerver/openapi/test_curl_examples.py @@ -8,12 +8,12 @@ import os from zulip import Client from zerver.lib.bugdown import api_code_examples from zerver.models import get_realm +from zerver.openapi.curl_param_value_generators import REGISTERED_GENERATOR_FUNCTIONS, CALLED_GENERATOR_FUNCTIONS 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', @@ -111,3 +111,7 @@ To learn more about the test itself, see zerver/openapi/test_curl_examples.py. curl_command=generated_curl_command, response=json.dumps(response, indent=4))) raise + + if REGISTERED_GENERATOR_FUNCTIONS != CALLED_GENERATOR_FUNCTIONS: + raise Exception("Some registered generator functions were not called:\n" + " " + str(REGISTERED_GENERATOR_FUNCTIONS - CALLED_GENERATOR_FUNCTIONS)) diff --git a/zproject/settings.py b/zproject/settings.py index 3d5fde7fe4..59b7e52b0c 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -100,6 +100,8 @@ TEST_SUITE = False TUTORIAL_ENABLED = True # This is overridden in test_settings.py for the test suites CASPER_TESTS = False +# This is overridden in test_settings.py for the test suites +RUNNING_OPENAPI_CURL_TEST = False # Google Compute Engine has an /etc/boto.cfg that is "nicely # configured" to work with GCE's storage service. However, their diff --git a/zproject/test_settings.py b/zproject/test_settings.py index f176937823..5a11a4cfc3 100644 --- a/zproject/test_settings.py +++ b/zproject/test_settings.py @@ -51,6 +51,9 @@ if "CASPER_TESTS" in os.environ: # Disable search pills prototype for production use SEARCH_PILLS_ENABLED = False +if "RUNNING_OPENAPI_CURL_TEST" in os.environ: + RUNNING_OPENAPI_CURL_TEST = True + # Decrease the get_updates timeout to 1 second. # This allows CasperJS to proceed quickly to the next test step. POLL_TIMEOUT = 1000