2018-01-26 22:11:42 +01:00
|
|
|
from typing import Dict, Any, Optional, Iterable
|
2018-02-25 01:01:45 +01:00
|
|
|
from io import StringIO
|
|
|
|
|
2018-04-27 22:09:57 +02:00
|
|
|
import json
|
2018-04-27 23:48:47 +02:00
|
|
|
import os
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-04-27 22:09:57 +02:00
|
|
|
from zerver.lib import mdiff
|
2018-05-31 19:41:17 +02:00
|
|
|
from zerver.lib.openapi import validate_against_openapi_schema
|
2018-04-27 22:09:57 +02:00
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
if False:
|
2017-10-05 08:05:41 +02:00
|
|
|
from zulip import Client
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-01-26 22:11:42 +01:00
|
|
|
ZULIP_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
FIXTURE_PATH = os.path.join(ZULIP_DIR, 'templates', 'zerver', 'api', 'fixtures.json')
|
|
|
|
|
|
|
|
def load_api_fixtures():
|
|
|
|
# type: () -> Dict[str, Any]
|
|
|
|
with open(FIXTURE_PATH, 'r') as fp:
|
2018-04-27 23:48:47 +02:00
|
|
|
json_dict = json.loads(fp.read())
|
2018-01-26 22:11:42 +01:00
|
|
|
return json_dict
|
|
|
|
|
|
|
|
FIXTURES = load_api_fixtures()
|
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
def add_subscriptions(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-02-06 04:17:10 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Subscribe to the stream "new stream"
|
|
|
|
result = client.add_subscriptions(
|
|
|
|
streams=[
|
|
|
|
{
|
|
|
|
'name': 'new stream',
|
|
|
|
'description': 'New stream for testing'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-07 19:47:49 +02:00
|
|
|
validate_against_openapi_schema(result, '/users/me/subscriptions', 'post',
|
|
|
|
'200_without_principals')
|
2018-02-06 04:17:10 +01:00
|
|
|
|
|
|
|
# {code_example|start}
|
|
|
|
# To subscribe another user to a stream, you may pass in
|
|
|
|
# the `principals` argument, like so:
|
|
|
|
result = client.add_subscriptions(
|
|
|
|
streams=[
|
|
|
|
{'name': 'new stream', 'description': 'New stream for testing'}
|
|
|
|
],
|
|
|
|
principals=['newbie@zulip.com']
|
|
|
|
)
|
|
|
|
# {code_example|end}
|
2017-01-13 13:50:39 +01:00
|
|
|
assert result['result'] == 'success'
|
2018-02-06 04:17:10 +01:00
|
|
|
assert 'newbie@zulip.com' in result['subscribed']
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-02-16 22:41:29 +01:00
|
|
|
def test_add_subscriptions_already_subscribed(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
result = client.add_subscriptions(
|
|
|
|
streams=[
|
|
|
|
{'name': 'new stream', 'description': 'New stream for testing'}
|
|
|
|
],
|
|
|
|
principals=['newbie@zulip.com']
|
|
|
|
)
|
|
|
|
|
2018-06-07 19:47:49 +02:00
|
|
|
validate_against_openapi_schema(result, '/users/me/subscriptions', 'post',
|
|
|
|
'200_already_subscribed')
|
2018-02-16 22:41:29 +01:00
|
|
|
|
2018-02-16 23:22:45 +01:00
|
|
|
def test_authorization_errors_fatal(client, nonadmin_client):
|
|
|
|
# type: (Client, Client) -> None
|
|
|
|
client.add_subscriptions(
|
|
|
|
streams=[
|
|
|
|
{'name': 'private_stream'}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
stream_id = client.get_stream_id('private_stream')['stream_id']
|
|
|
|
client.call_endpoint(
|
|
|
|
'streams/{}'.format(stream_id),
|
|
|
|
method='PATCH',
|
|
|
|
request={'is_private': True}
|
|
|
|
)
|
|
|
|
|
|
|
|
result = nonadmin_client.add_subscriptions(
|
|
|
|
streams=[
|
|
|
|
{'name': 'private_stream'}
|
|
|
|
],
|
|
|
|
authorization_errors_fatal=False,
|
|
|
|
)
|
|
|
|
|
2018-06-07 19:47:49 +02:00
|
|
|
validate_against_openapi_schema(result, '/users/me/subscriptions', 'post',
|
|
|
|
'400_unauthorized_errors_fatal_false')
|
2018-02-16 23:22:45 +01:00
|
|
|
|
|
|
|
result = nonadmin_client.add_subscriptions(
|
|
|
|
streams=[
|
|
|
|
{'name': 'private_stream'}
|
|
|
|
],
|
|
|
|
authorization_errors_fatal=True,
|
|
|
|
)
|
|
|
|
|
2018-06-07 19:47:49 +02:00
|
|
|
validate_against_openapi_schema(result, '/users/me/subscriptions', 'post',
|
|
|
|
'400_unauthorized_errors_fatal_true')
|
2018-02-16 23:22:45 +01:00
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
def create_user(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-01-31 05:36:57 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Create a user
|
|
|
|
request = {
|
|
|
|
'email': 'newbie@zulip.com',
|
|
|
|
'password': 'temp',
|
|
|
|
'full_name': 'New User',
|
|
|
|
'short_name': 'newbie'
|
|
|
|
}
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.create_user(request)
|
2018-01-31 05:36:57 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-01 22:47:36 +02:00
|
|
|
fixture = FIXTURES['successful-response-empty']
|
2018-02-16 23:38:10 +01:00
|
|
|
test_against_fixture(result, fixture)
|
|
|
|
|
|
|
|
# Test "Email already used error"
|
|
|
|
result = client.create_user(request)
|
|
|
|
|
2018-06-01 22:47:36 +02:00
|
|
|
fixture = FIXTURES['email_already_used_error']
|
2018-01-31 05:36:57 +01:00
|
|
|
test_against_fixture(result, fixture)
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
def get_members(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-02-07 04:25:23 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Get all users in the realm
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.get_members()
|
2018-02-07 04:25:23 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 19:43:40 +02:00
|
|
|
validate_against_openapi_schema(result, '/users', 'get', '200')
|
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
members = [m for m in result['members'] if m['email'] == 'newbie@zulip.com']
|
|
|
|
assert len(members) == 1
|
2018-02-07 04:25:23 +01:00
|
|
|
newbie = members[0]
|
|
|
|
assert not newbie['is_admin']
|
|
|
|
assert newbie['full_name'] == 'New User'
|
|
|
|
|
|
|
|
# {code_example|start}
|
|
|
|
# You may pass the `client_gravatar` query parameter as follows:
|
2018-07-26 21:04:19 +02:00
|
|
|
result = client.get_members({'client_gravatar': True})
|
2018-02-07 04:25:23 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 19:43:40 +02:00
|
|
|
validate_against_openapi_schema(result, '/users', 'get', '200')
|
2018-02-07 04:25:23 +01:00
|
|
|
assert result['members'][0]['avatar_url'] is None
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
def get_profile(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-01-31 06:06:56 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Get the profile of the user/bot that requests this endpoint,
|
|
|
|
# which is `client` in this case:
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.get_profile()
|
2018-01-31 06:06:56 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
|
|
|
fixture = FIXTURES['get-profile']
|
|
|
|
check_if_equal = ['email', 'full_name', 'msg', 'result', 'short_name']
|
|
|
|
check_if_exists = ['client_id', 'is_admin', 'is_bot', 'max_message_id',
|
|
|
|
'pointer', 'user_id']
|
|
|
|
test_against_fixture(result, fixture, check_if_equal=check_if_equal,
|
|
|
|
check_if_exists=check_if_exists)
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
def get_stream_id(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-01-28 00:06:45 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Get the ID of a given stream
|
2017-01-13 13:50:39 +01:00
|
|
|
stream_name = 'new stream'
|
|
|
|
result = client.get_stream_id(stream_name)
|
2018-01-28 00:06:45 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 19:20:55 +02:00
|
|
|
validate_against_openapi_schema(result, '/get_stream_id', 'get', '200')
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
def get_streams(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-01-31 05:04:43 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Get all streams that the user has access to
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.get_streams()
|
2018-01-31 05:04:43 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 18:53:13 +02:00
|
|
|
validate_against_openapi_schema(result, '/streams', 'get', '200')
|
2017-01-13 13:50:39 +01:00
|
|
|
streams = [s for s in result['streams'] if s['name'] == 'new stream']
|
|
|
|
assert streams[0]['description'] == 'New stream for testing'
|
|
|
|
|
2018-02-03 00:04:48 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# You may pass in one or more of the query parameters mentioned above
|
|
|
|
# as keyword arguments, like so:
|
|
|
|
result = client.get_streams(include_public=False)
|
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 18:53:13 +02:00
|
|
|
validate_against_openapi_schema(result, '/streams', 'get', '200')
|
2018-02-03 00:04:48 +01:00
|
|
|
assert len(result['streams']) == 4
|
|
|
|
|
2018-02-16 21:58:03 +01:00
|
|
|
def test_user_not_authorized_error(nonadmin_client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
result = nonadmin_client.get_streams(include_all_active=True)
|
|
|
|
|
|
|
|
fixture = FIXTURES['user-not-authorized-error']
|
|
|
|
test_against_fixture(result, fixture)
|
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
def get_subscribers(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
|
|
|
result = client.get_subscribers(stream='new stream')
|
2018-02-06 04:17:10 +01:00
|
|
|
assert result['subscribers'] == ['iago@zulip.com', 'newbie@zulip.com']
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
def get_user_agent(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
|
|
|
result = client.get_user_agent()
|
|
|
|
assert result.startswith('ZulipPython/')
|
|
|
|
|
|
|
|
def list_subscriptions(client):
|
|
|
|
# type: (Client) -> None
|
2018-01-28 00:39:16 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Get all streams that the user is subscribed to
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.list_subscriptions()
|
2018-01-28 00:39:16 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
|
|
|
fixture = FIXTURES['get-subscribed-streams']
|
|
|
|
test_against_fixture(result, fixture, check_if_equal=['msg', 'result'],
|
|
|
|
check_if_exists=['subscriptions'])
|
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
streams = [s for s in result['subscriptions'] if s['name'] == 'new stream']
|
|
|
|
assert streams[0]['description'] == 'New stream for testing'
|
|
|
|
|
|
|
|
def remove_subscriptions(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-02-06 04:47:40 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Unsubscribe from the stream "new stream"
|
|
|
|
result = client.remove_subscriptions(
|
|
|
|
['new stream']
|
|
|
|
)
|
|
|
|
# {code_example|end}
|
|
|
|
|
|
|
|
fixture = FIXTURES['remove-subscriptions']
|
|
|
|
test_against_fixture(result, fixture)
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
# test it was actually removed
|
|
|
|
result = client.list_subscriptions()
|
|
|
|
assert result['result'] == 'success'
|
|
|
|
streams = [s for s in result['subscriptions'] if s['name'] == 'new stream']
|
|
|
|
assert len(streams) == 0
|
|
|
|
|
2018-02-08 04:25:11 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Unsubscribe another user from the stream "new stream"
|
|
|
|
result = client.remove_subscriptions(
|
|
|
|
['new stream'],
|
|
|
|
principals=['newbie@zulip.com']
|
|
|
|
)
|
|
|
|
# {code_example|end}
|
|
|
|
|
|
|
|
test_against_fixture(result, fixture)
|
2018-02-06 04:47:40 +01:00
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
def render_message(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
2018-01-26 22:11:42 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Render a message
|
|
|
|
request = {
|
|
|
|
'content': '**foo**'
|
|
|
|
}
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.render_message(request)
|
2018-01-26 22:11:42 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 19:29:12 +02:00
|
|
|
validate_against_openapi_schema(result, '/messages/render', 'post', '200')
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-06-18 17:44:48 +02:00
|
|
|
def send_message(client):
|
2017-01-13 13:50:39 +01:00
|
|
|
# type: (Client) -> int
|
|
|
|
|
2018-01-27 22:26:16 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Send a stream message
|
|
|
|
request = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": "Denmark",
|
|
|
|
"subject": "Castle",
|
|
|
|
"content": "Something is rotten in the state of Denmark."
|
|
|
|
}
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.send_message(request)
|
2018-01-27 22:26:16 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 18:15:08 +02:00
|
|
|
validate_against_openapi_schema(result, '/messages', 'post', '200')
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-06-18 17:44:48 +02:00
|
|
|
# test that the message was actually sent
|
2018-01-27 22:26:16 +01:00
|
|
|
message_id = result['id']
|
2017-01-13 13:50:39 +01:00
|
|
|
url = 'messages/' + str(message_id)
|
|
|
|
result = client.call_endpoint(
|
|
|
|
url=url,
|
|
|
|
method='GET'
|
|
|
|
)
|
|
|
|
assert result['result'] == 'success'
|
2018-01-27 22:26:16 +01:00
|
|
|
assert result['raw_content'] == request['content']
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-01-27 22:56:36 +01:00
|
|
|
# {code_example|start}
|
|
|
|
# Send a private message
|
|
|
|
request = {
|
|
|
|
"type": "private",
|
|
|
|
"to": "iago@zulip.com",
|
|
|
|
"content": "I come not, friends, to steal away your hearts."
|
|
|
|
}
|
|
|
|
result = client.send_message(request)
|
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 18:15:08 +02:00
|
|
|
validate_against_openapi_schema(result, '/messages', 'post', '200')
|
2018-01-27 22:56:36 +01:00
|
|
|
|
2018-06-18 17:44:48 +02:00
|
|
|
# test that the message was actually sent
|
2018-01-27 22:56:36 +01:00
|
|
|
message_id = result['id']
|
|
|
|
url = 'messages/' + str(message_id)
|
|
|
|
result = client.call_endpoint(
|
|
|
|
url=url,
|
|
|
|
method='GET'
|
|
|
|
)
|
|
|
|
assert result['result'] == 'success'
|
|
|
|
assert result['raw_content'] == request['content']
|
|
|
|
|
2018-06-18 17:44:48 +02:00
|
|
|
return message_id
|
|
|
|
|
|
|
|
def test_nonexistent_stream_error(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
request = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": "nonexistent_stream",
|
|
|
|
"subject": "Castle",
|
|
|
|
"content": "Something is rotten in the state of Denmark."
|
|
|
|
}
|
|
|
|
result = client.send_message(request)
|
|
|
|
|
2018-06-18 18:15:08 +02:00
|
|
|
validate_against_openapi_schema(result, '/messages', 'post',
|
|
|
|
'400_non_existing_stream')
|
2018-06-18 17:44:48 +02:00
|
|
|
|
2018-02-16 20:35:05 +01:00
|
|
|
def test_private_message_invalid_recipient(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
request = {
|
|
|
|
"type": "private",
|
|
|
|
"to": "eeshan@zulip.com",
|
|
|
|
"content": "I come not, friends, to steal away your hearts."
|
|
|
|
}
|
|
|
|
result = client.send_message(request)
|
|
|
|
|
2018-06-18 18:15:08 +02:00
|
|
|
validate_against_openapi_schema(result, '/messages', 'post',
|
|
|
|
'400_non_existing_user')
|
2018-02-16 20:35:05 +01:00
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
def update_message(client, message_id):
|
|
|
|
# type: (Client, int) -> None
|
|
|
|
|
|
|
|
assert int(message_id)
|
2018-01-27 23:18:19 +01:00
|
|
|
|
|
|
|
# {code_example|start}
|
|
|
|
# Edit a message
|
|
|
|
# (make sure that message_id below is set to the ID of the
|
|
|
|
# message you wish to update)
|
|
|
|
request = {
|
|
|
|
"message_id": message_id,
|
|
|
|
"content": "New content"
|
|
|
|
}
|
2017-01-13 13:50:39 +01:00
|
|
|
result = client.update_message(request)
|
2018-01-27 23:18:19 +01:00
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 16:32:30 +02:00
|
|
|
validate_against_openapi_schema(result, '/messages/{message_id}', 'patch',
|
|
|
|
'200')
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
# test it was actually updated
|
|
|
|
url = 'messages/' + str(message_id)
|
|
|
|
result = client.call_endpoint(
|
|
|
|
url=url,
|
|
|
|
method='GET'
|
|
|
|
)
|
|
|
|
assert result['result'] == 'success'
|
2018-01-27 23:18:19 +01:00
|
|
|
assert result['raw_content'] == request['content']
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-02-16 21:26:43 +01:00
|
|
|
def test_update_message_edit_permission_error(client, nonadmin_client):
|
|
|
|
# type: (Client, Client) -> None
|
|
|
|
request = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": "Denmark",
|
|
|
|
"subject": "Castle",
|
|
|
|
"content": "Something is rotten in the state of Denmark."
|
|
|
|
}
|
|
|
|
result = client.send_message(request)
|
|
|
|
|
|
|
|
request = {
|
|
|
|
"message_id": result["id"],
|
|
|
|
"content": "New content"
|
|
|
|
}
|
|
|
|
result = nonadmin_client.update_message(request)
|
|
|
|
|
|
|
|
fixture = FIXTURES['update-message-edit-permission-error']
|
|
|
|
test_against_fixture(result, fixture)
|
|
|
|
|
2018-02-07 05:00:10 +01:00
|
|
|
def register_queue(client):
|
|
|
|
# type: (Client) -> str
|
|
|
|
|
|
|
|
# {code_example|start}
|
|
|
|
# Register the queue
|
|
|
|
result = client.register(
|
2018-04-27 06:15:50 +02:00
|
|
|
event_types=['messages', 'realm_emoji']
|
2018-02-07 05:00:10 +01:00
|
|
|
)
|
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-20 23:03:01 +02:00
|
|
|
validate_against_openapi_schema(result, '/register', 'post', '200')
|
2018-02-07 05:00:10 +01:00
|
|
|
return result['queue_id']
|
|
|
|
|
2018-02-08 03:54:20 +01:00
|
|
|
def deregister_queue(client, queue_id):
|
|
|
|
# type: (Client, str) -> None
|
|
|
|
|
|
|
|
# {code_example|start}
|
|
|
|
# Delete a queue (queue_id is the ID of the queue
|
|
|
|
# to be removed)
|
|
|
|
result = client.deregister(queue_id)
|
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-21 01:28:33 +02:00
|
|
|
validate_against_openapi_schema(result, '/events', 'delete', '200')
|
2018-02-08 03:54:20 +01:00
|
|
|
|
2018-02-16 23:49:35 +01:00
|
|
|
# Test "BAD_EVENT_QUEUE_ID" error
|
|
|
|
result = client.deregister(queue_id)
|
2018-06-21 01:28:33 +02:00
|
|
|
validate_against_openapi_schema(result, '/events', 'delete', '400')
|
2018-02-16 23:49:35 +01:00
|
|
|
|
2018-02-25 01:01:45 +01:00
|
|
|
def upload_file(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
fp = StringIO("zulip")
|
|
|
|
fp.name = "zulip.txt"
|
|
|
|
|
|
|
|
# {code_example|start}
|
|
|
|
# Upload a file
|
|
|
|
# (Make sure that 'fp' is a file object)
|
|
|
|
result = client.call_endpoint(
|
|
|
|
'user_uploads',
|
|
|
|
method='POST',
|
|
|
|
files=[fp]
|
|
|
|
)
|
|
|
|
# {code_example|end}
|
|
|
|
|
|
|
|
fixture = FIXTURES['upload-file']
|
|
|
|
test_against_fixture(result, fixture, check_if_equal=['msg', 'result'],
|
|
|
|
check_if_exists=['uri'])
|
|
|
|
|
2018-06-01 18:58:07 +02:00
|
|
|
def get_stream_topics(client, stream_id):
|
|
|
|
# type: (Client, int) -> None
|
|
|
|
|
|
|
|
# {code_example|start}
|
|
|
|
result = client.get_stream_topics(stream_id)
|
|
|
|
# {code_example|end}
|
|
|
|
|
2018-06-18 16:32:30 +02:00
|
|
|
validate_against_openapi_schema(result, '/users/me/{stream_id}/topics',
|
|
|
|
'get', '200')
|
2018-06-01 18:58:07 +02:00
|
|
|
|
2018-02-06 21:04:07 +01:00
|
|
|
def test_invalid_api_key(client_with_invalid_key):
|
|
|
|
# type: (Client) -> None
|
|
|
|
result = client_with_invalid_key.list_subscriptions()
|
|
|
|
fixture = FIXTURES['invalid-api-key']
|
|
|
|
test_against_fixture(result, fixture)
|
|
|
|
|
2018-02-16 21:03:50 +01:00
|
|
|
def test_missing_request_argument(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
result = client.render_message({})
|
|
|
|
|
|
|
|
fixture = FIXTURES['missing-request-argument-error']
|
|
|
|
test_against_fixture(result, fixture)
|
2018-02-06 21:04:07 +01:00
|
|
|
|
2018-02-16 22:23:51 +01:00
|
|
|
def test_invalid_stream_error(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
result = client.get_stream_id('nonexistent')
|
|
|
|
|
2018-06-18 19:20:55 +02:00
|
|
|
validate_against_openapi_schema(result, '/get_stream_id', 'get', '400')
|
2018-02-16 22:23:51 +01:00
|
|
|
|
2018-01-26 22:11:42 +01:00
|
|
|
TEST_FUNCTIONS = {
|
2018-06-18 19:29:12 +02:00
|
|
|
'/messages/render:post': render_message,
|
2018-06-18 18:15:08 +02:00
|
|
|
'/messages:post': send_message,
|
2018-05-15 19:28:42 +02:00
|
|
|
'/messages/{message_id}:patch': update_message,
|
2018-06-18 19:20:55 +02:00
|
|
|
'/get_stream_id:get': get_stream_id,
|
2018-01-28 00:39:16 +01:00
|
|
|
'get-subscribed-streams': list_subscriptions,
|
2018-06-18 18:53:13 +02:00
|
|
|
'/streams:get': get_streams,
|
2018-01-31 05:36:57 +01:00
|
|
|
'create-user': create_user,
|
2018-01-31 06:06:56 +01:00
|
|
|
'get-profile': get_profile,
|
2018-02-06 04:17:10 +01:00
|
|
|
'add-subscriptions': add_subscriptions,
|
2018-02-06 04:47:40 +01:00
|
|
|
'remove-subscriptions': remove_subscriptions,
|
2018-06-18 19:43:40 +02:00
|
|
|
'/users:get': get_members,
|
2018-06-20 23:03:01 +02:00
|
|
|
'/register:post': register_queue,
|
2018-06-21 01:28:33 +02:00
|
|
|
'/events:delete': deregister_queue,
|
2018-02-25 01:01:45 +01:00
|
|
|
'upload-file': upload_file,
|
2018-06-01 18:58:07 +02:00
|
|
|
'/users/me/{stream_id}/topics:get': get_stream_topics
|
2018-01-26 22:11:42 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
# SETUP METHODS FOLLOW
|
|
|
|
|
2018-01-26 22:11:42 +01:00
|
|
|
def test_against_fixture(result, fixture, check_if_equal=[], check_if_exists=[]):
|
|
|
|
# type: (Dict[str, Any], Dict[str, Any], Optional[Iterable[str]], Optional[Iterable[str]]) -> None
|
2018-04-27 22:09:57 +02:00
|
|
|
assertLength(result, fixture)
|
2018-01-27 22:26:16 +01:00
|
|
|
|
2018-01-26 22:11:42 +01:00
|
|
|
if not check_if_equal and not check_if_exists:
|
|
|
|
for key, value in fixture.items():
|
2018-04-27 22:09:57 +02:00
|
|
|
assertEqual(key, result, fixture)
|
2018-01-26 22:11:42 +01:00
|
|
|
|
|
|
|
if check_if_equal:
|
|
|
|
for key in check_if_equal:
|
2018-04-27 22:09:57 +02:00
|
|
|
assertEqual(key, result, fixture)
|
2018-01-26 22:11:42 +01:00
|
|
|
|
|
|
|
if check_if_exists:
|
|
|
|
for key in check_if_exists:
|
2018-04-27 22:09:57 +02:00
|
|
|
assertIn(key, result)
|
|
|
|
|
|
|
|
def assertEqual(key, result, fixture):
|
|
|
|
# type: (str, Dict[str, Any], Dict[str, Any]) -> None
|
|
|
|
if result[key] != fixture[key]:
|
|
|
|
first = "{key} = {value}".format(key=key, value=result[key])
|
|
|
|
second = "{key} = {value}".format(key=key, value=fixture[key])
|
|
|
|
raise AssertionError("Actual and expected outputs do not match; showing diff:\n" +
|
|
|
|
mdiff.diff_strings(first, second))
|
|
|
|
else:
|
|
|
|
assert result[key] == fixture[key]
|
|
|
|
|
|
|
|
def assertLength(result, fixture):
|
|
|
|
# type: (Dict[str, Any], Dict[str, Any]) -> None
|
|
|
|
if len(result) != len(fixture):
|
|
|
|
result_string = json.dumps(result, indent=4, sort_keys=True)
|
|
|
|
fixture_string = json.dumps(fixture, indent=4, sort_keys=True)
|
|
|
|
raise AssertionError("The lengths of the actual and expected outputs do not match; showing diff:\n" +
|
|
|
|
mdiff.diff_strings(result_string, fixture_string))
|
|
|
|
else:
|
|
|
|
assert len(result) == len(fixture)
|
|
|
|
|
|
|
|
def assertIn(key, result):
|
|
|
|
# type: (str, Dict[str, Any]) -> None
|
|
|
|
if key not in result.keys():
|
|
|
|
raise AssertionError(
|
|
|
|
"The actual output does not contain the the key `{key}`.".format(key=key)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert key in result
|
2018-01-26 22:11:42 +01:00
|
|
|
|
2018-06-23 22:46:44 +02:00
|
|
|
def test_messages(client, nonadmin_client):
|
|
|
|
# type: (Client, Client) -> None
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
render_message(client)
|
2018-06-18 17:44:48 +02:00
|
|
|
message_id = send_message(client)
|
2017-01-13 13:50:39 +01:00
|
|
|
update_message(client, message_id)
|
|
|
|
|
2018-02-16 20:25:59 +01:00
|
|
|
test_nonexistent_stream_error(client)
|
2018-02-16 20:35:05 +01:00
|
|
|
test_private_message_invalid_recipient(client)
|
2018-06-23 22:46:44 +02:00
|
|
|
test_update_message_edit_permission_error(client, nonadmin_client)
|
|
|
|
|
2018-02-16 20:25:59 +01:00
|
|
|
|
2017-01-13 13:50:39 +01:00
|
|
|
def test_users(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
|
|
|
|
create_user(client)
|
|
|
|
get_members(client)
|
|
|
|
get_profile(client)
|
2018-02-25 01:01:45 +01:00
|
|
|
upload_file(client)
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-06-23 22:46:44 +02:00
|
|
|
def test_streams(client, nonadmin_client):
|
|
|
|
# type: (Client, Client) -> None
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
add_subscriptions(client)
|
2018-02-16 22:41:29 +01:00
|
|
|
test_add_subscriptions_already_subscribed(client)
|
2017-01-13 13:50:39 +01:00
|
|
|
list_subscriptions(client)
|
|
|
|
get_stream_id(client)
|
|
|
|
get_streams(client)
|
|
|
|
get_subscribers(client)
|
2018-02-06 04:47:40 +01:00
|
|
|
remove_subscriptions(client)
|
2018-06-01 18:58:07 +02:00
|
|
|
get_stream_topics(client, 1)
|
2017-01-13 13:50:39 +01:00
|
|
|
|
2018-06-23 22:46:44 +02:00
|
|
|
test_user_not_authorized_error(nonadmin_client)
|
|
|
|
test_authorization_errors_fatal(client, nonadmin_client)
|
|
|
|
|
|
|
|
|
2018-02-07 05:00:10 +01:00
|
|
|
def test_queues(client):
|
|
|
|
# type: (Client) -> None
|
2018-02-16 02:53:45 +01:00
|
|
|
# Note that the example for api/get-events-from-queue is not tested.
|
|
|
|
# Since, methods such as client.get_events() or client.call_on_each_message
|
|
|
|
# are blocking calls and since the event queue backend is already
|
|
|
|
# thoroughly tested in zerver/tests/test_event_queue.py, it is not worth
|
|
|
|
# the effort to come up with asynchronous logic for testing those here.
|
2018-02-08 03:54:20 +01:00
|
|
|
queue_id = register_queue(client)
|
|
|
|
deregister_queue(client, queue_id)
|
2018-02-07 05:00:10 +01:00
|
|
|
|
2018-02-16 21:03:50 +01:00
|
|
|
def test_errors(client):
|
|
|
|
# type: (Client) -> None
|
|
|
|
test_missing_request_argument(client)
|
2018-02-16 22:23:51 +01:00
|
|
|
test_invalid_stream_error(client)
|
2018-02-16 21:03:50 +01:00
|
|
|
|
2018-06-23 22:46:44 +02:00
|
|
|
def test_the_api(client, nonadmin_client):
|
|
|
|
# type: (Client, Client) -> None
|
2017-01-13 13:50:39 +01:00
|
|
|
|
|
|
|
get_user_agent(client)
|
|
|
|
test_users(client)
|
2018-06-23 22:46:44 +02:00
|
|
|
test_streams(client, nonadmin_client)
|
|
|
|
test_messages(client, nonadmin_client)
|
2018-02-07 05:00:10 +01:00
|
|
|
test_queues(client)
|
2018-02-16 21:03:50 +01:00
|
|
|
test_errors(client)
|