test-api: Pass non-admin client to the test helpers.

This commit is contained in:
Yago González 2018-06-23 22:46:44 +02:00 committed by Tim Abbott
parent ecc5b75500
commit a3d42d9901
2 changed files with 26 additions and 20 deletions

View File

@ -22,6 +22,7 @@ from zerver.lib.api_test_helpers import test_the_api, test_invalid_api_key, \
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.test_settings' os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.test_settings'
django.setup() django.setup()
from zerver.lib.actions import do_create_user
from zerver.models import get_user, get_realm from zerver.models import get_user, get_realm
usage = """test-js-with-casper [options]""" usage = """test-js-with-casper [options]"""
@ -39,6 +40,9 @@ if not options.force:
sys.exit(1) sys.exit(1)
with test_server_running(force=options.force, external_host='zulipdev.com:9981'): 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 email = 'iago@zulip.com' # Iago is an admin
realm = get_realm("zulip") realm = get_realm("zulip")
api_key = get_user(email, realm).api_key api_key = get_user(email, realm).api_key
@ -47,25 +51,21 @@ with test_server_running(force=options.force, external_host='zulipdev.com:9981')
client = Client( client = Client(
email=email, email=email,
api_key=api_key, api_key=api_key,
site=site) site=site
)
print("Running API tests...") # Prepare the admin client
test_the_api(client) email = 'guest@zulip.com' # guest is not an admin
guest_user = do_create_user('guest@zulip.com', 'secret',
email = 'newbie@zulip.com' # newbie is not an admin get_realm('zulip'), 'Mr. Guest', 'guest')
realm = get_realm("zulip") api_key = guest_user.api_key
api_key = get_user(email, realm).api_key
site = 'http://zulip.zulipdev.com:9981'
nonadmin_client = Client( nonadmin_client = Client(
email=email, email=email,
api_key=api_key, api_key=api_key,
site=site site=site
) )
# Run tests that require non-admin client test_the_api(client, nonadmin_client)
test_update_message_edit_permission_error(client, nonadmin_client)
test_user_not_authorized_error(nonadmin_client)
test_authorization_errors_fatal(client, nonadmin_client)
# Test error payloads # Test error payloads
client = Client( client = Client(

View File

@ -564,8 +564,8 @@ def assertIn(key, result):
else: else:
assert key in result assert key in result
def test_messages(client): def test_messages(client, nonadmin_client):
# type: (Client) -> None # type: (Client, Client) -> None
render_message(client) render_message(client)
message_id = stream_message(client) message_id = stream_message(client)
@ -574,6 +574,8 @@ def test_messages(client):
test_nonexistent_stream_error(client) test_nonexistent_stream_error(client)
test_private_message_invalid_recipient(client) test_private_message_invalid_recipient(client)
test_update_message_edit_permission_error(client, nonadmin_client)
def test_users(client): def test_users(client):
# type: (Client) -> None # type: (Client) -> None
@ -583,8 +585,8 @@ def test_users(client):
get_profile(client) get_profile(client)
upload_file(client) upload_file(client)
def test_streams(client): def test_streams(client, nonadmin_client):
# type: (Client) -> None # type: (Client, Client) -> None
add_subscriptions(client) add_subscriptions(client)
test_add_subscriptions_already_subscribed(client) test_add_subscriptions_already_subscribed(client)
@ -595,6 +597,10 @@ def test_streams(client):
remove_subscriptions(client) remove_subscriptions(client)
get_stream_topics(client, 1) get_stream_topics(client, 1)
test_user_not_authorized_error(nonadmin_client)
test_authorization_errors_fatal(client, nonadmin_client)
def test_queues(client): def test_queues(client):
# type: (Client) -> None # type: (Client) -> None
# Note that the example for api/get-events-from-queue is not tested. # Note that the example for api/get-events-from-queue is not tested.
@ -610,12 +616,12 @@ def test_errors(client):
test_missing_request_argument(client) test_missing_request_argument(client)
test_invalid_stream_error(client) test_invalid_stream_error(client)
def test_the_api(client): def test_the_api(client, nonadmin_client):
# type: (Client) -> None # type: (Client, Client) -> None
get_user_agent(client) get_user_agent(client)
test_users(client) test_users(client)
test_streams(client) test_streams(client, nonadmin_client)
test_messages(client) test_messages(client, nonadmin_client)
test_queues(client) test_queues(client)
test_errors(client) test_errors(client)