From 7afb19e0d4038118ea9b39b9047ac9f5d2bed55b Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 13 Jan 2017 04:50:39 -0800 Subject: [PATCH] Add test driver for the API. --- tools/lib/api_tests.py | 185 +++++++++++++++++++++++++++++++++++++++++ tools/test-api | 44 ++++++++++ 2 files changed, 229 insertions(+) create mode 100644 tools/lib/api_tests.py create mode 100755 tools/test-api diff --git a/tools/lib/api_tests.py b/tools/lib/api_tests.py new file mode 100644 index 0000000000..c53a3ad1f3 --- /dev/null +++ b/tools/lib/api_tests.py @@ -0,0 +1,185 @@ +from __future__ import print_function + +if False: + from typing import Any + from api.zulip import Client + +def add_subscriptions(client): + # type: (Client) -> None + + request = [ + dict( + name='new stream', + description='New stream for testing', + ) + ] + result = client.add_subscriptions(request) + assert result['result'] == 'success' + +def create_user(client): + # type: (Client) -> None + + request = dict( + email='newbie@zulip.com', + full_name='New User', + short_name='Newbie', + password='temp', + ) + result = client.create_user(request) + assert result['result'] == 'success' + +def get_members(client): + # type: (Client) -> None + + result = client.get_members() + assert result['result'] == 'success' + members = [m for m in result['members'] if m['email'] == 'newbie@zulip.com'] + assert len(members) == 1 + iago = members[0] + + assert iago['is_admin'] == False + assert iago['full_name'] == 'New User' + +def get_profile(client): + # type: (Client) -> None + + result = client.get_profile() + assert result['is_admin'] == True + assert result['email'] == 'iago@zulip.com' + assert result['full_name'] == 'Iago' + +def get_stream_id(client): + # type: (Client) -> None + + stream_name = 'new stream' + result = client.get_stream_id(stream_name) + assert int(result['stream_id']) + +def get_streams(client): + # type: (Client) -> None + + result = client.get_streams() + assert result['result'] == 'success' + streams = [s for s in result['streams'] if s['name'] == 'new stream'] + assert streams[0]['description'] == 'New stream for testing' + +def get_subscribers(client): + # type: (Client) -> None + + result = client.get_subscribers(stream='new stream') + assert result['subscribers'] == ['iago@zulip.com'] + +def get_user_agent(client): + # type: (Client) -> None + + result = client.get_user_agent() + assert result.startswith('ZulipPython/') + +def list_subscriptions(client): + # type: (Client) -> None + + result = client.list_subscriptions() + assert result['result'] == 'success' + 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 + + result = client.remove_subscriptions(['new stream']) + assert result['result'] == 'success' + + # 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 + +def render_message(client): + # type: (Client) -> None + + request = dict(content='**foo**') + result = client.render_message(request) + assert result['result'] == 'success' + assert result['rendered'] == '

foo

' + +def send_message(client): + # type: (Client) -> int + + request = dict( + type='stream', + to='Denmark', + subject='Copenhagen', + content='hello', + ) + result = client.send_message(request) + assert result['result'] == 'success' + message_id = result['id'] + + # test it was actually sent + url = 'messages/' + str(message_id) + result = client.call_endpoint( + url=url, + method='GET' + ) + assert result['result'] == 'success' + assert result['raw_content'] == 'hello' + + return message_id + +def update_message(client, message_id): + # type: (Client, int) -> None + + assert int(message_id) + request = dict( + message_id=message_id, + content='new content', + ) + result = client.update_message(request) + assert result['result'] == 'success' + + # test it was actually updated + url = 'messages/' + str(message_id) + result = client.call_endpoint( + url=url, + method='GET' + ) + assert result['result'] == 'success' + assert result['raw_content'] == 'new content' + +# SETUP METHODS FOLLOW + +def test_messages(client): + # type: (Client) -> None + + render_message(client) + message_id = send_message(client) + update_message(client, message_id) + +def test_users(client): + # type: (Client) -> None + + create_user(client) + get_members(client) + get_profile(client) + +def test_streams(client): + # type: (Client) -> None + + add_subscriptions(client) + list_subscriptions(client) + get_stream_id(client) + get_streams(client) + get_subscribers(client) + +def test_the_api(client): + # type: (Client) -> None + + get_user_agent(client) + test_users(client) + test_streams(client) + test_messages(client) + + # print(dir(client)) + + diff --git a/tools/test-api b/tools/test-api new file mode 100755 index 0000000000..195e7329d9 --- /dev/null +++ b/tools/test-api @@ -0,0 +1,44 @@ +#!/usr/bin/env python +from __future__ import print_function + +import os +import sys + +try: + import django +except ImportError as e: + print("ImportError: {}".format(e)) + print("You need to run the Zulip tests inside a Zulip dev environment.") + print("If you are using Vagrant, you can `vagrant ssh` to enter the Vagrant guest.") + sys.exit(1) + +TOOLS_DIR = os.path.dirname(__file__) +ROOT_DIR = os.path.dirname(TOOLS_DIR) +sys.path.insert(0, ROOT_DIR) + +api_path = os.path.abspath(os.path.join(ROOT_DIR, 'api')) +if not os.path.exists(api_path): + raise Exception('programming error--files probably got moved') + +sys.path.insert(0, api_path) +from zulip import Client + +from tools.lib.test_server import test_server_running +from tools.lib.api_tests import test_the_api + +os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.test_settings' +django.setup() +from zerver.models import get_user_profile_by_email + + +with test_server_running(): + email = 'iago@zulip.com' # Iago is an admin + api_key = get_user_profile_by_email(email).api_key + site = 'http://127.0.0.1:9981' + + client = Client( + email=email, + api_key=api_key, + site=site) + + test_the_api(client)