zulip/tools/test-api

80 lines
2.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
import argparse
2017-01-13 13:50:39 +01:00
import os
import sys
# check for the venv
from lib import sanity_check
sanity_check.check_venv(__file__)
2017-01-13 13:50:39 +01:00
import django
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 get_provisioning_status
2017-01-13 13:50:39 +01:00
from tools.lib.test_server import test_server_running
from zerver.lib.api_test_helpers import test_the_api, test_invalid_api_key
2017-01-13 13:50:39 +01:00
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.test_settings'
django.setup()
from zerver.lib.actions import do_create_user
from zerver.lib.users import get_api_key
2017-07-19 15:35:08 +02:00
from zerver.models import get_user, get_realm
2017-01-13 13:50:39 +01:00
2018-09-06 20:40:22 +02:00
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()
2017-01-13 13:50:39 +01:00
if not options.force:
ok, msg = get_provisioning_status()
if not ok:
print(msg)
print('If you really know what you are doing, use --force to run anyway.')
sys.exit(1)
with test_server_running(force=options.force, external_host='zulipdev.com:9981'):
print("Running API tests...")
# Prepare the admin client
email = 'iago@zulip.com' # Iago is an admin
2017-07-19 15:35:08 +02:00
realm = get_realm("zulip")
user = get_user(email, realm)
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 the 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', '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 error payloads
client = Client(
email=email,
api_key='abcedrsdfd',
site=site
)
test_invalid_api_key(client)
2017-01-26 01:42:09 +01:00
print("API tests passed!")