forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-api
executable file
·87 lines (71 loc) · 2.52 KB
/
test-api
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
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__)
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ZULIP_PATH)
os.chdir(ZULIP_PATH)
from zulip import Client
from tools.lib.test_script import assert_provisioning_status_ok
from tools.lib.test_server import test_server_running
usage = """test-api [options]"""
parser = argparse.ArgumentParser(usage)
parser.add_argument('--force', dest='force',
action="store_true",
default=False, help='Run tests despite possible provisioning problems.')
options = parser.parse_args()
assert_provisioning_status_ok(options.force)
with test_server_running(force=options.force, external_host='zulipdev.com:9981'):
# Zerver imports should happen after `django.setup()` is run
# by the test_server_running decorator.
from zerver.openapi.python_examples import test_the_api, test_invalid_api_key
from zerver.openapi.test_curl_examples import test_generated_curl_examples_for_success
from zerver.lib.actions import do_create_user
from zerver.lib.users import get_api_key
from zerver.models import get_user, get_realm
print("Running API tests...")
# Prepare the admin client
email = '[email protected]' # Iago is an admin
realm = get_realm("zulip")
user = get_user(email, realm)
api_key = get_api_key(user)
site = 'http://zulip.zulipdev.com:9981'
client = Client(
email=email,
api_key=api_key,
site=site
)
# Prepare a generic bot client for curl testing
email = '[email protected]'
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 = '[email protected]' # guest is not an admin
guest_user = do_create_user('[email protected]', 'secret',
get_realm('zulip'), 'Mr. Guest', 'guest')
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 error payloads
client = Client(
email=email,
api_key='X'*32,
site=site
)
test_invalid_api_key(client)
print("API tests passed!")