zulip/tools/test-api

99 lines
2.8 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
import argparse
2017-01-13 13:50:39 +01:00
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__)
2017-01-13 13:50:39 +01:00
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ZULIP_PATH)
os.chdir(ZULIP_PATH)
2017-01-13 13:50:39 +01:00
from zulip import Client
from tools.lib.test_script import add_provision_check_override_param, assert_provisioning_status_ok
2017-01-13 13:50:39 +01:00
from tools.lib.test_server import test_server_running
2018-09-06 20:40:22 +02:00
usage = """test-api [options]"""
parser = argparse.ArgumentParser(usage)
add_provision_check_override_param(parser)
options = parser.parse_args()
2017-01-13 13:50:39 +01:00
assert_provisioning_status_ok(options.skip_provision_check)
with test_server_running(
skip_provision_check=options.skip_provision_check, external_host="zulipdev.com:9981"
):
# Zerver imports should happen after `django.setup()` is run
# by the test_server_running decorator.
from zerver.lib.actions import do_create_user
from zerver.lib.test_helpers import reset_emails_in_zulip_realm
from zerver.lib.users import get_api_key
from zerver.models import get_realm, get_user
from zerver.openapi.javascript_examples import test_js_bindings
from zerver.openapi.python_examples import test_invalid_api_key, test_the_api
from zerver.openapi.test_curl_examples import test_generated_curl_examples_for_success
print("Running API tests...")
reset_emails_in_zulip_realm()
# Prepare the admin client
email = "iago@zulip.com" # Iago is an admin
realm = get_realm("zulip")
user = get_user(email, realm)
# Required to test can_create_users endpoints.
user.can_create_users = True
user.save(update_fields=["can_create_users"])
api_key = get_api_key(user)
site = "http://zulip.zulipdev.com:9981"
2017-01-13 13:50:39 +01:00
client = Client(
email=email,
api_key=api_key,
site=site,
)
2017-01-26 01:42:09 +01:00
# Prepare a generic bot client for curl testing
email = "default-bot@zulip.com"
realm = get_realm("zulip")
bot_user = get_user(email, realm)
api_key = get_api_key(bot_user)
bot_client = Client(
email=email,
api_key=api_key,
site=site,
)
# Prepare the non-admin client
email = "guest@zulip.com" # guest is not an admin
guest_user = do_create_user(
"guest@zulip.com", "secret", get_realm("zulip"), "Mr. Guest", acting_user=None
)
api_key = get_api_key(guest_user)
nonadmin_client = Client(
email=email,
api_key=api_key,
site=site,
)
test_the_api(client, nonadmin_client)
test_generated_curl_examples_for_success(client)
test_js_bindings(client)
# Test error payloads
client = Client(
email=email,
api_key="X" * 32,
site=site,
)
test_invalid_api_key(client)
2017-01-26 01:42:09 +01:00
print("API tests passed!")