mirror of https://github.com/zulip/zulip.git
openapi: Refactor hacky openAPI curl test.
Fixes #17795 In PR #17014, we added support for deactivate-own-user. And while doing so, we first deactivated the client and then reactivated it. But this implementation is a bit hacky. So, to fix this, we're now deactivating a test_user so that we don't have to reactivate it. We did so by changing the value of authentication_line. As we want to keep endpoint code out of the “test_curl_examples”, we changed the value of authentication_line in `curl_param_value_generators.py`. To work this out, we create a new global variable named AUTHENTICATION_LINE in “curl_param_value_generators.py” and change its value in function “deactivate_own_user” and to use this change in “test_curl_examples,” we import AUTHENTICATION_LINE. AUTHENTICATION_LINE is of list data type because we want a pointer to original mutable object so that changes made during run time show across the module. Another way to do this is to change the way we import variable, but that will be inconsistent to the way we had in all other files. To remove confusion between AUTHENTICATION_LINE and authentication_line we renamed authentication_line to default_authentication_line.
This commit is contained in:
parent
37b4d07934
commit
21be0ef445
|
@ -106,7 +106,7 @@ with test_server_running(
|
||||||
)
|
)
|
||||||
|
|
||||||
test_the_api(client, nonadmin_client, owner_client)
|
test_the_api(client, nonadmin_client, owner_client)
|
||||||
test_generated_curl_examples_for_success(client, owner_client)
|
test_generated_curl_examples_for_success(client)
|
||||||
test_js_bindings(client)
|
test_js_bindings(client)
|
||||||
|
|
||||||
# Test error payloads
|
# Test error payloads
|
||||||
|
|
|
@ -20,11 +20,15 @@ from zerver.lib.actions import (
|
||||||
from zerver.lib.events import do_events_register
|
from zerver.lib.events import do_events_register
|
||||||
from zerver.lib.initial_password import initial_password
|
from zerver.lib.initial_password import initial_password
|
||||||
from zerver.lib.test_classes import ZulipTestCase
|
from zerver.lib.test_classes import ZulipTestCase
|
||||||
from zerver.models import Client, Message, UserGroup, UserPresence, get_realm
|
from zerver.lib.users import get_api_key
|
||||||
|
from zerver.models import Client, Message, UserGroup, UserPresence, get_realm, get_user
|
||||||
|
|
||||||
GENERATOR_FUNCTIONS: Dict[str, Callable[[], Dict[str, object]]] = {}
|
GENERATOR_FUNCTIONS: Dict[str, Callable[[], Dict[str, object]]] = {}
|
||||||
REGISTERED_GENERATOR_FUNCTIONS: Set[str] = set()
|
REGISTERED_GENERATOR_FUNCTIONS: Set[str] = set()
|
||||||
CALLED_GENERATOR_FUNCTIONS: Set[str] = set()
|
CALLED_GENERATOR_FUNCTIONS: Set[str] = set()
|
||||||
|
# This is a List rather than just a string in order to make it easier
|
||||||
|
# to write to it from another module.
|
||||||
|
AUTHENTICATION_LINE: List[str] = [""]
|
||||||
|
|
||||||
helpers = ZulipTestCase()
|
helpers = ZulipTestCase()
|
||||||
|
|
||||||
|
@ -310,3 +314,22 @@ def deactivate_user() -> Dict[str, object]:
|
||||||
acting_user=None,
|
acting_user=None,
|
||||||
)
|
)
|
||||||
return {"user_id": user_profile.id}
|
return {"user_id": user_profile.id}
|
||||||
|
|
||||||
|
|
||||||
|
@openapi_param_value_generator(["/users/me:delete"])
|
||||||
|
def deactivate_own_user() -> Dict[str, object]:
|
||||||
|
test_user_email = "delete-test@zulip.com"
|
||||||
|
deactivate_test_user = do_create_user(
|
||||||
|
test_user_email,
|
||||||
|
"secret",
|
||||||
|
get_realm("zulip"),
|
||||||
|
"Mr. Delete",
|
||||||
|
role=200,
|
||||||
|
acting_user=None,
|
||||||
|
)
|
||||||
|
realm = get_realm("zulip")
|
||||||
|
test_user = get_user(test_user_email, realm)
|
||||||
|
test_user_api_key = get_api_key(test_user)
|
||||||
|
# change authentication line to allow test_client to delete itself.
|
||||||
|
AUTHENTICATION_LINE[0] = f"{deactivate_test_user.email}:{test_user_api_key}"
|
||||||
|
return {}
|
||||||
|
|
|
@ -16,11 +16,14 @@ from zulip import Client
|
||||||
|
|
||||||
from zerver.models import get_realm
|
from zerver.models import get_realm
|
||||||
from zerver.openapi import markdown_extension
|
from zerver.openapi import markdown_extension
|
||||||
from zerver.openapi.curl_param_value_generators import assert_all_helper_functions_called
|
from zerver.openapi.curl_param_value_generators import (
|
||||||
|
AUTHENTICATION_LINE,
|
||||||
|
assert_all_helper_functions_called,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_generated_curl_examples_for_success(client: Client, owner_client: Client) -> None:
|
def test_generated_curl_examples_for_success(client: Client) -> None:
|
||||||
authentication_line = f"{client.email}:{client.api_key}"
|
default_authentication_line = f"{client.email}:{client.api_key}"
|
||||||
# A limited Markdown engine that just processes the code example syntax.
|
# A limited Markdown engine that just processes the code example syntax.
|
||||||
realm = get_realm("zulip")
|
realm = get_realm("zulip")
|
||||||
md_engine = markdown.Markdown(
|
md_engine = markdown.Markdown(
|
||||||
|
@ -35,6 +38,10 @@ def test_generated_curl_examples_for_success(client: Client, owner_client: Clien
|
||||||
for file_name in sorted(glob.glob("templates/zerver/api/*.md")):
|
for file_name in sorted(glob.glob("templates/zerver/api/*.md")):
|
||||||
with open(file_name) as f:
|
with open(file_name) as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
|
# Set AUTHENTICATION_LINE to default_authentication_line.
|
||||||
|
# Set this every iteration, because deactivate_own_user
|
||||||
|
# will override this for its test.
|
||||||
|
AUTHENTICATION_LINE[0] = default_authentication_line
|
||||||
# A typical example from the Markdown source looks like this:
|
# A typical example from the Markdown source looks like this:
|
||||||
# {generate_code_example(curl, ...}
|
# {generate_code_example(curl, ...}
|
||||||
if not line.startswith("{generate_code_example(curl"):
|
if not line.startswith("{generate_code_example(curl"):
|
||||||
|
@ -47,21 +54,9 @@ def test_generated_curl_examples_for_success(client: Client, owner_client: Clien
|
||||||
unescaped_html = html.unescape(curl_command_html)
|
unescaped_html = html.unescape(curl_command_html)
|
||||||
curl_command_text = unescaped_html[len("<p><code>curl\n") : -len("</code></p>")]
|
curl_command_text = unescaped_html[len("<p><code>curl\n") : -len("</code></p>")]
|
||||||
curl_command_text = curl_command_text.replace(
|
curl_command_text = curl_command_text.replace(
|
||||||
"BOT_EMAIL_ADDRESS:BOT_API_KEY", authentication_line
|
"BOT_EMAIL_ADDRESS:BOT_API_KEY", AUTHENTICATION_LINE[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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"]
|
|
||||||
|
|
||||||
print("Testing {} ...".format(curl_command_text.split("\n")[0]))
|
print("Testing {} ...".format(curl_command_text.split("\n")[0]))
|
||||||
|
|
||||||
# Turn the text into an arguments list.
|
# Turn the text into an arguments list.
|
||||||
|
@ -77,8 +72,6 @@ def test_generated_curl_examples_for_success(client: Client, owner_client: Clien
|
||||||
)
|
)
|
||||||
response = json.loads(response_json)
|
response = json.loads(response_json)
|
||||||
assert response["result"] == "success"
|
assert response["result"] == "success"
|
||||||
if needs_reactivation:
|
|
||||||
owner_client.reactivate_user_by_id(user_id)
|
|
||||||
except (AssertionError, Exception):
|
except (AssertionError, Exception):
|
||||||
error_template = """
|
error_template = """
|
||||||
Error verifying the success of the API documentation curl example.
|
Error verifying the success of the API documentation curl example.
|
||||||
|
|
Loading…
Reference in New Issue