py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-04-28 02:21:12 +02:00
|
|
|
import argparse
|
2017-01-13 13:50:39 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-10-21 12:43:00 +02:00
|
|
|
os.environ["RUNNING_OPENAPI_CURL_TEST"] = "1"
|
|
|
|
|
2017-08-28 01:03:45 +02: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
|
|
|
|
2021-07-03 08:22:44 +02:00
|
|
|
# check for the venv
|
|
|
|
from tools.lib import sanity_check
|
|
|
|
|
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
from zulip import Client
|
|
|
|
|
2021-03-03 05:00:15 +01:00
|
|
|
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]"""
|
2018-04-28 02:21:12 +02:00
|
|
|
parser = argparse.ArgumentParser(usage)
|
2021-03-03 05:00:15 +01:00
|
|
|
add_provision_check_override_param(parser)
|
2018-04-28 02:21:12 +02:00
|
|
|
options = parser.parse_args()
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2021-03-02 20:59:19 +01:00
|
|
|
assert_provisioning_status_ok(options.skip_provision_check)
|
2018-04-28 02:21:12 +02:00
|
|
|
|
2021-03-02 20:59:19 +01:00
|
|
|
with test_server_running(
|
|
|
|
skip_provision_check=options.skip_provision_check, external_host="zulipdev.com:9981"
|
|
|
|
):
|
2022-02-08 00:13:33 +01:00
|
|
|
# zerver imports should happen after `django.setup()` is run
|
2019-08-16 21:13:53 +02:00
|
|
|
# by the test_server_running decorator.
|
2022-04-14 23:53:15 +02:00
|
|
|
from zerver.actions.create_user import do_create_user, do_reactivate_user
|
2024-09-16 19:19:43 +02:00
|
|
|
from zerver.actions.realm_settings import (
|
|
|
|
do_change_realm_permission_group_setting,
|
|
|
|
do_deactivate_realm,
|
|
|
|
do_reactivate_realm,
|
|
|
|
)
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import change_user_is_active
|
2023-03-01 07:34:25 +01:00
|
|
|
from zerver.lib.test_helpers import reset_email_visibility_to_everyone_in_zulip_realm
|
2019-08-16 21:13:53 +02:00
|
|
|
from zerver.lib.users import get_api_key
|
2024-09-16 19:19:43 +02:00
|
|
|
from zerver.models.groups import NamedUserGroup, SystemGroups
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models.realms import get_realm
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import get_user
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.openapi.javascript_examples import test_js_bindings
|
2021-03-31 12:00:56 +02:00
|
|
|
from zerver.openapi.python_examples import (
|
|
|
|
test_invalid_api_key,
|
2021-03-31 13:14:08 +02:00
|
|
|
test_realm_deactivated,
|
2021-03-31 12:00:56 +02:00
|
|
|
test_the_api,
|
|
|
|
test_user_account_deactivated,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.openapi.test_curl_examples import test_generated_curl_examples_for_success
|
2019-08-16 21:13:53 +02:00
|
|
|
|
2018-06-23 22:46:44 +02:00
|
|
|
print("Running API tests...")
|
|
|
|
|
2023-03-01 07:34:25 +01:00
|
|
|
reset_email_visibility_to_everyone_in_zulip_realm()
|
2020-03-12 13:51:54 +01:00
|
|
|
|
2018-06-23 22:46:44 +02:00
|
|
|
# Prepare the admin client
|
2021-02-12 08:20:45 +01:00
|
|
|
email = "iago@zulip.com" # Iago is an admin
|
|
|
|
realm = get_realm("zulip")
|
2024-09-03 21:41:18 +02:00
|
|
|
iago = get_user(email, realm)
|
|
|
|
user = iago
|
2024-09-16 19:19:43 +02:00
|
|
|
|
|
|
|
# Iago needs permission to manage all user groups.
|
|
|
|
admins_group = NamedUserGroup.objects.get(
|
|
|
|
name=SystemGroups.ADMINISTRATORS, realm=realm, is_system_group=True
|
|
|
|
)
|
|
|
|
do_change_realm_permission_group_setting(
|
|
|
|
realm, "can_manage_all_groups", admins_group, acting_user=None
|
|
|
|
)
|
|
|
|
|
2024-09-03 21:41:18 +02:00
|
|
|
# Required to test can_create_users and can_change_user_emails endpoints.
|
2020-12-14 22:02:22 +01:00
|
|
|
user.can_create_users = True
|
2024-09-03 21:41:18 +02:00
|
|
|
user.can_change_user_emails = True
|
|
|
|
user.save(update_fields=["can_create_users", "can_change_user_emails"])
|
2020-12-14 22:02:22 +01:00
|
|
|
|
2018-08-01 10:53:40 +02:00
|
|
|
api_key = get_api_key(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
site = "http://zulip.zulipdev.com:9981"
|
2017-01-13 13:50:39 +01:00
|
|
|
client = Client(
|
|
|
|
email=email,
|
|
|
|
api_key=api_key,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
site=site,
|
2018-06-23 22:46:44 +02:00
|
|
|
)
|
2017-01-26 01:42:09 +01:00
|
|
|
|
2021-01-05 18:09:03 +01:00
|
|
|
# Prepare the owner client
|
|
|
|
email = "desdemona@zulip.com" # desdemona is an owner
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
user = get_user(email, realm)
|
2024-09-03 21:41:18 +02:00
|
|
|
|
2021-01-05 18:09:03 +01:00
|
|
|
api_key = get_api_key(user)
|
|
|
|
site = "http://zulip.zulipdev.com:9981"
|
|
|
|
owner_client = Client(
|
|
|
|
email=email,
|
|
|
|
api_key=api_key,
|
|
|
|
site=site,
|
|
|
|
)
|
|
|
|
|
2019-08-07 10:55:41 +02:00
|
|
|
# Prepare the non-admin client
|
2021-02-12 08:20:45 +01:00
|
|
|
email = "guest@zulip.com" # guest is not an admin
|
2020-07-16 14:10:43 +02:00
|
|
|
guest_user = do_create_user(
|
2021-02-06 14:27:06 +01:00
|
|
|
"guest@zulip.com", "secret", get_realm("zulip"), "Mr. Guest", acting_user=None
|
2020-07-16 14:10:43 +02:00
|
|
|
)
|
2018-08-01 10:53:40 +02:00
|
|
|
api_key = get_api_key(guest_user)
|
2018-02-16 21:26:43 +01:00
|
|
|
nonadmin_client = Client(
|
|
|
|
email=email,
|
|
|
|
api_key=api_key,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
site=site,
|
2018-02-16 21:26:43 +01:00
|
|
|
)
|
|
|
|
|
2021-01-05 18:09:03 +01:00
|
|
|
test_the_api(client, nonadmin_client, owner_client)
|
2021-04-02 12:28:50 +02:00
|
|
|
test_generated_curl_examples_for_success(client)
|
2020-05-17 12:04:53 +02:00
|
|
|
test_js_bindings(client)
|
2018-02-16 21:26:43 +01:00
|
|
|
|
2018-02-06 21:04:07 +01:00
|
|
|
# Test error payloads
|
|
|
|
client = Client(
|
|
|
|
email=email,
|
2021-02-12 08:20:45 +01:00
|
|
|
api_key="X" * 32,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
site=site,
|
2018-02-06 21:04:07 +01:00
|
|
|
)
|
|
|
|
test_invalid_api_key(client)
|
|
|
|
|
2021-03-31 12:00:56 +02:00
|
|
|
# Test account deactivated error
|
|
|
|
# we deactivate user manually because do_deactivate_user removes user session
|
|
|
|
change_user_is_active(guest_user, False)
|
|
|
|
client = Client(
|
|
|
|
email=email,
|
|
|
|
api_key=api_key,
|
|
|
|
site=site,
|
|
|
|
)
|
|
|
|
test_user_account_deactivated(client)
|
|
|
|
# reactivate user to avoid any side-effects in other tests.
|
|
|
|
do_reactivate_user(guest_user, acting_user=None)
|
|
|
|
|
2021-03-31 13:14:08 +02:00
|
|
|
# Test realm deactivated error
|
2023-09-25 22:59:44 +02:00
|
|
|
do_deactivate_realm(
|
|
|
|
guest_user.realm, acting_user=None, deactivation_reason="owner_request", email_owners=False
|
|
|
|
)
|
2021-03-31 13:14:08 +02:00
|
|
|
|
|
|
|
client = Client(
|
|
|
|
email=email,
|
|
|
|
api_key=api_key,
|
|
|
|
site=site,
|
|
|
|
)
|
|
|
|
test_realm_deactivated(client)
|
|
|
|
do_reactivate_realm(guest_user.realm)
|
|
|
|
|
2018-02-06 21:04:07 +01:00
|
|
|
|
2017-01-26 01:42:09 +01:00
|
|
|
print("API tests passed!")
|