From 66ca0c725f050c0ff4c51b57abe1894458df8ace Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Tue, 6 Feb 2018 23:55:23 -0330 Subject: [PATCH] api/get-all-users: Make fixtures/examples testable. api/get-all-users now uses the Markdown extension in bugdown/api_code_examples to generate code examples and fixtures from tests. --- templates/zerver/api/fixtures.json | 36 ++++++++++++++ templates/zerver/api/get-all-users.md | 69 +-------------------------- zerver/lib/api_test_helpers.py | 31 ++++++++++-- 3 files changed, 65 insertions(+), 71 deletions(-) diff --git a/templates/zerver/api/fixtures.json b/templates/zerver/api/fixtures.json index e704c9bc6e..d550bfcd28 100644 --- a/templates/zerver/api/fixtures.json +++ b/templates/zerver/api/fixtures.json @@ -192,5 +192,41 @@ "invalid-api-key": { "msg":"Invalid API key", "result":"error" + }, + "get-all-users": { + "members":[ + { + "avatar_url":"https:\/\/secure.gravatar.com\/avatar\/818c212b9f8830dfef491b3f7da99a14?d=identicon&version=1", + "bot_type":null, + "email":"AARON@zulip.com", + "full_name":"aaron", + "is_active":true, + "is_admin":false, + "is_bot":false, + "user_id":1 + }, + { + "avatar_url":"https:\/\/secure.gravatar.com\/avatar\/77c3871a68c8d70356156029fd0a4999?d=identicon&version=1", + "bot_type":null, + "email":"cordelia@zulip.com", + "full_name":"Cordelia Lear", + "is_active":true, + "is_admin":false, + "is_bot":false, + "user_id":3 + }, + { + "avatar_url":"https:\/\/secure.gravatar.com\/avatar\/0cbf08f3a355995fa2ec542246e35123?d=identicon&version=1", + "bot_type":null, + "email":"newbie@zulip.com", + "full_name":"New User", + "is_active":true, + "is_admin":false, + "is_bot":false, + "user_id":24 + } + ], + "msg":"", + "result":"success" } } diff --git a/templates/zerver/api/get-all-users.md b/templates/zerver/api/get-all-users.md index 69aeb5281d..45cce97108 100644 --- a/templates/zerver/api/get-all-users.md +++ b/templates/zerver/api/get-all-users.md @@ -30,24 +30,7 @@ curl {{ api_url }}/v1/users?client_gravatar=true \
-```python -#!/usr/bin/env python - -import zulip - -# Download ~/zuliprc-dev from your dev server -client = zulip.Client(config_file="~/zuliprc-dev") - -# Get all users in the realm -print(client.get_members()) - -# You may pass the `client_gravatar` query parameter as follows: -print(client.call_endpoint( - url='users?client_gravatar=true', - method='GET', -)) - -``` +{generate_code_example|get-all-users|example}
@@ -105,52 +88,4 @@ zulip(config).then((client) => { A typical successful JSON response may look like: -``` -{ - 'msg':'', - 'members':[ - { - 'email':'ZOE@zulip.com', - 'is_bot':False, - 'avatar_url':'https://secure.gravatar.com/avatar/0f030c97ab51312c7bbffd3966198ced?d=identicon&version=1', - 'full_name':'Zoe', - 'is_active':True, - 'is_admin':False, - 'bot_type':None, - 'user_id':23 - }, - { - 'email':'AARON@zulip.com', - 'is_bot':False, - 'avatar_url':'https://secure.gravatar.com/avatar/818c212b9f8830dfef491b3f7da99a14?d=identicon&version=1', - 'full_name':'aaron', - 'is_active':True, - 'is_admin':False, - 'bot_type':None, - 'user_id':22 - }, - { - 'bot_owner':'iago@zulip.com', - 'email':'sample-bot@localhost', - 'is_bot':True, - 'avatar_url':'https://secure.gravatar.com/avatar/0ea4ba8ec99b1fe07f62785a7c584cd3?d=identicon&version=1', - 'full_name':'sample', - 'is_active':True, - 'is_admin':False, - 'bot_type':1, - 'user_id':45 - }, - { - 'email':'iago@zulip.com', - 'is_bot':False, - 'avatar_url':'https://secure.gravatar.com/avatar/af4f06322c177ef4e1e9b2c424986b54?d=identicon&version=1', - 'full_name':'Iago', - 'is_active':True, - 'is_admin':True, - 'bot_type':None, - 'user_id':26 - } - ], - 'result':'success' -} -``` +{generate_code_example|get-all-users|fixture} diff --git a/zerver/lib/api_test_helpers.py b/zerver/lib/api_test_helpers.py index 19a8ac8d2b..5d05cd2e62 100644 --- a/zerver/lib/api_test_helpers.py +++ b/zerver/lib/api_test_helpers.py @@ -67,14 +67,36 @@ def create_user(client): def get_members(client): # type: (Client) -> None + # {code_example|start} + # Get all users in the realm result = client.get_members() - assert result['result'] == 'success' + # {code_example|end} + + fixture = FIXTURES['get-all-users'] + test_against_fixture(result, fixture, check_if_equal=['msg', 'result'], + check_if_exists=['members']) members = [m for m in result['members'] if m['email'] == 'newbie@zulip.com'] assert len(members) == 1 - iago = members[0] + newbie = members[0] + assert not newbie['is_admin'] + assert newbie['full_name'] == 'New User' - assert not iago['is_admin'] - assert iago['full_name'] == 'New User' + member_fixture = fixture['members'][0] + member_result = result['members'][0] + test_against_fixture(member_result, member_fixture, + check_if_exists=member_fixture.keys()) + + # {code_example|start} + # You may pass the `client_gravatar` query parameter as follows: + result = client.call_endpoint( + url='users?client_gravatar=true', + method='GET', + ) + # {code_example|end} + + test_against_fixture(result, fixture, check_if_equal=['msg', 'result'], + check_if_exists=['members']) + assert result['members'][0]['avatar_url'] is None def get_profile(client): # type: (Client) -> None @@ -298,6 +320,7 @@ TEST_FUNCTIONS = { 'get-profile': get_profile, 'add-subscriptions': add_subscriptions, 'remove-subscriptions': remove_subscriptions, + 'get-all-users': get_members, } # SETUP METHODS FOLLOW