python_examples: Add assert statements to verify response.

Creates a helper function `validate_response_result` to verify the
response of API request for each test.
This commit is contained in:
Vector73 2024-05-24 14:53:44 +05:30 committed by Tim Abbott
parent dde385a8e4
commit 1d3e4307fa
1 changed files with 121 additions and 98 deletions

View File

@ -72,6 +72,11 @@ def ensure_users(ids_list: List[int], user_names: List[str]) -> None:
assert ids_list == user_ids
def validate_response_result(response: Dict[str, Any], expected_result: str = "success") -> None:
assert "result" in response
assert response["result"] == expected_result
def get_subscribed_stream_ids(client: Client) -> List[int]:
streams = client.get_subscriptions()
stream_ids = [stream["stream_id"] for stream in streams["subscriptions"]]
@ -100,7 +105,7 @@ def add_subscriptions(client: Client) -> None:
],
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "200")
ensure_users([25], ["newbie"])
@ -115,7 +120,7 @@ def add_subscriptions(client: Client) -> None:
principals=[user_id],
)
# {code_example|end}
assert result["result"] == "success"
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "200")
assert "newbie@zulip.com" in result["subscribed"]
@ -127,7 +132,7 @@ def test_add_subscriptions_already_subscribed(client: Client) -> None:
],
principals=["newbie@zulip.com"],
)
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "200")
@ -151,7 +156,7 @@ def test_authorization_errors_fatal(client: Client, nonadmin_client: Client) ->
],
authorization_errors_fatal=False,
)
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "200")
result = nonadmin_client.add_subscriptions(
@ -160,7 +165,7 @@ def test_authorization_errors_fatal(client: Client, nonadmin_client: Client) ->
],
authorization_errors_fatal=True,
)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "400")
@ -170,6 +175,7 @@ def get_presence(client: Client) -> None:
# Get presence information of all the users in an organization.
result = client.get_realm_presence()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/presence", "get", "200")
@ -189,7 +195,7 @@ def add_default_stream(client: Client) -> None:
# Add a channel to the set of default channels for new users.
result = client.add_default_stream(stream_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/default_streams", "post", "200")
@ -207,7 +213,7 @@ def remove_default_stream(client: Client) -> None:
request=request,
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/default_streams", "delete", "200")
@ -217,7 +223,7 @@ def get_user_presence(client: Client) -> None:
# Get presence information for "iago@zulip.com"
result = client.get_user_presence("iago@zulip.com")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id_or_email}/presence", "get", "200")
@ -231,7 +237,7 @@ def get_user_status(client: Client) -> None:
method="GET",
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id}/status", "get", "200")
@ -245,8 +251,7 @@ def update_presence(client: Client) -> None:
}
result = client.update_presence(request)
assert result["result"] == "success"
validate_response_result(result)
@openapi_test_function("/users:post")
@ -260,12 +265,12 @@ def create_user(client: Client) -> None:
}
result = client.create_user(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users", "post", "200")
# Test "Email already used error"
result = client.create_user(request)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/users", "post", "400")
@ -283,7 +288,7 @@ def update_status(client: Client) -> None:
result = client.call_endpoint(url="/users/me/status", method="POST", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/status", "post", "200")
# Test "status_text is too long error"
@ -292,6 +297,7 @@ def update_status(client: Client) -> None:
"away": "false",
}
result = client.call_endpoint(url="/users/me/status", method="POST", request=request)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/users/me/status", "post", "400")
@ -301,7 +307,7 @@ def get_members(client: Client) -> None:
# Get all users in the realm
result = client.get_members()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users", "get", "200")
members = [m for m in result["members"] if m["email"] == "newbie@zulip.com"]
@ -314,7 +320,7 @@ def get_members(client: Client) -> None:
# You may pass the `client_gravatar` query parameter as follows:
result = client.get_members({"client_gravatar": False})
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users", "get", "200")
assert result["members"][0]["avatar_url"] is not None
@ -322,7 +328,7 @@ def get_members(client: Client) -> None:
# You may pass the `include_custom_profile_fields` query parameter as follows:
result = client.get_members({"include_custom_profile_fields": True})
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users", "get", "200")
for member in result["members"]:
if member["is_bot"]:
@ -342,6 +348,7 @@ def get_user_by_email(client: Client) -> None:
method="GET",
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{email}", "get", "200")
@ -351,7 +358,7 @@ def get_invitations(client: Client) -> None:
# Get all invitations
result = client.call_endpoint(url="/invites", method="GET")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/invites", "get", "200")
@ -369,7 +376,7 @@ def send_invitations(client: Client) -> None:
}
result = client.call_endpoint(url="/invites", method="POST", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/invites", "post", "200")
@ -386,7 +393,7 @@ def create_reusable_invitation_link(client: Client) -> None:
}
result = client.call_endpoint(url="/invites/multiuse", method="POST", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/invites/multiuse", "post", "200")
@ -406,7 +413,7 @@ def revoke_email_invitation(client: Client) -> None:
invite_id = 3
result = client.call_endpoint(url=f"/invites/{invite_id}", method="DELETE")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/invites/{invite_id}", "delete", "200")
@ -425,7 +432,7 @@ def revoke_reusable_invitation_link(client: Client) -> None:
invite_id = 2
result = client.call_endpoint(url=f"/invites/multiuse/{invite_id}", method="DELETE")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/invites/multiuse/{invite_id}", "delete", "200")
@ -438,12 +445,14 @@ def get_single_user(client: Client) -> None:
user_id = 8
result = client.get_user_by_id(user_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id}", "get", "200")
# {code_example|start}
# If you'd like data on custom profile fields, you can request them as follows:
result = client.get_user_by_id(user_id, include_custom_profile_fields=True)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id}", "get", "200")
@ -456,6 +465,7 @@ def deactivate_user(client: Client) -> None:
user_id = 8
result = client.deactivate_user_by_id(user_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id}", "delete", "200")
@ -466,6 +476,7 @@ def reactivate_user(client: Client) -> None:
user_id = 8
result = client.reactivate_user_by_id(user_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id}/reactivate", "post", "200")
@ -478,6 +489,7 @@ def update_user(client: Client) -> None:
user_id = 10
result = client.update_user_by_id(user_id, full_name="New Name")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id}", "patch", "200")
# {code_example|start}
@ -485,6 +497,7 @@ def update_user(client: Client) -> None:
user_id = 8
result = client.update_user_by_id(user_id, profile_data=[{"id": 9, "value": "some data"}])
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/{user_id}", "patch", "200")
@ -501,6 +514,7 @@ def get_subscription_status(client: Client) -> None:
method="GET",
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(
result, "/users/{user_id}/subscriptions/{stream_id}", "get", "200"
)
@ -515,7 +529,7 @@ def get_realm_linkifiers(client: Client) -> None:
method="GET",
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/linkifiers", "get", "200")
@ -533,6 +547,7 @@ def reorder_realm_linkifiers(client: Client) -> None:
result = client.call_endpoint(url="/realm/linkifiers", method="PATCH", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/linkifiers", "patch", "200")
@ -545,6 +560,7 @@ def get_realm_profile_fields(client: Client) -> None:
method="GET",
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/profile_fields", "get", "200")
@ -563,6 +579,7 @@ def reorder_realm_profile_fields(client: Client) -> None:
result = client.call_endpoint(url="/realm/profile_fields", method="PATCH", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/profile_fields", "patch", "200")
@ -574,7 +591,7 @@ def create_realm_profile_field(client: Client) -> None:
result = client.call_endpoint(url="/realm/profile_fields", method="POST", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/profile_fields", "post", "200")
@ -592,7 +609,7 @@ def add_realm_filter(client: Client) -> int:
}
result = client.call_endpoint("/realm/filters", method="POST", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/filters", "post", "200")
return result["id"]
@ -610,7 +627,7 @@ def update_realm_filter(client: Client, filter_id: int) -> None:
url=f"/realm/filters/{filter_id}", method="PATCH", request=request
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/filters/{filter_id}", "patch", "200")
@ -620,7 +637,7 @@ def remove_realm_filter(client: Client, filter_id: int) -> None:
# Remove the linkifier (realm_filter).
result = client.remove_realm_filter(filter_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/filters/{filter_id}", "delete", "200")
@ -635,7 +652,7 @@ def add_realm_playground(client: Client) -> None:
}
result = client.call_endpoint(url="/realm/playgrounds", method="POST", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/playgrounds", "post", "200")
@ -645,7 +662,7 @@ def remove_realm_playground(client: Client) -> None:
# Remove the playground with ID 1
result = client.call_endpoint(url="/realm/playgrounds/1", method="DELETE")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/playgrounds/{playground_id}", "delete", "200")
@ -656,7 +673,7 @@ def get_profile(client: Client) -> None:
# which is `client` in this case:
result = client.get_profile()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me", "get", "200")
@ -671,10 +688,11 @@ def deactivate_own_user(client: Client, owner_client: Client) -> None:
method="DELETE",
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me", "delete", "200")
# Reactivate the account to avoid polluting other tests.
owner_client.reactivate_user_by_id(user_id)
validate_against_openapi_schema(result, "/users/me", "delete", "200")
@openapi_test_function("/get_stream_id:get")
@ -684,7 +702,7 @@ def get_stream_id(client: Client) -> int:
name = "python-test"
result = client.get_stream_id(name)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/get_stream_id", "get", "200")
return result["stream_id"]
@ -705,10 +723,9 @@ def archive_stream(client: Client) -> None:
stream_id = client.get_stream_id("example to archive")["stream_id"]
result = client.delete_stream(stream_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/streams/{stream_id}", "delete", "200")
assert result["result"] == "success"
@openapi_test_function("/streams/{stream_id}/delete_topic:post")
def delete_topic(client: Client, stream_id: int, topic: str) -> None:
@ -721,10 +738,9 @@ def delete_topic(client: Client, stream_id: int, topic: str) -> None:
url=f"/streams/{stream_id}/delete_topic", method="POST", request=request
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/streams/{stream_id}/delete_topic", "post", "200")
assert result["result"] == "success"
@openapi_test_function("/streams:get")
def get_streams(client: Client) -> None:
@ -732,7 +748,7 @@ def get_streams(client: Client) -> None:
# Get all channels that the user has access to.
result = client.get_streams()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/streams", "get", "200")
streams = [s for s in result["streams"] if s["name"] == "python-test"]
assert streams[0]["description"] == "Channel for testing Python"
@ -742,7 +758,7 @@ def get_streams(client: Client) -> None:
# as keyword arguments, like so:
result = client.get_streams(include_public=False)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/streams", "get", "200")
assert len(result["streams"]) == 7
@ -759,9 +775,8 @@ def update_stream(client: Client, stream_id: int) -> None:
result = client.update_stream(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/streams/{stream_id}", "patch", "200")
assert result["result"] == "success"
@openapi_test_function("/user_groups:get")
@ -770,7 +785,7 @@ def get_user_groups(client: Client) -> int:
# Get all user groups of the realm
result = client.get_user_groups()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_groups", "get", "200")
[hamlet_user_group] = (u for u in result["user_groups"] if u["name"] == "hamletcharacters")
assert hamlet_user_group["description"] == "Characters of Hamlet"
@ -781,7 +796,7 @@ def get_user_groups(client: Client) -> int:
def test_user_not_authorized_error(nonadmin_client: Client) -> None:
result = nonadmin_client.get_streams(include_all_active=True)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/rest-error-handling", "post", "400")
@ -795,6 +810,7 @@ def get_subscribers(client: Client) -> None:
# channel's ID.
result = client.get_subscribers(stream="python-test")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/streams/{stream_id}/members", "get", "200")
assert result["subscribers"] == [11, 25]
@ -810,7 +826,7 @@ def get_subscriptions(client: Client) -> None:
# Get all channels that the user is subscribed to.
result = client.get_subscriptions()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions", "get", "200")
streams = [s for s in result["subscriptions"] if s["name"] == "python-test"]
@ -825,7 +841,7 @@ def remove_subscriptions(client: Client) -> None:
["python-test"],
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions", "delete", "200")
# Confirm user is no longer subscribed to "python-test".
@ -840,7 +856,7 @@ def remove_subscriptions(client: Client) -> None:
principals=["newbie@zulip.com"],
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions", "delete", "200")
@ -867,7 +883,7 @@ def toggle_mute_topic(client: Client) -> None:
}
result = client.mute_topic(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions/muted_topics", "patch", "200")
# {code_example|start}
@ -880,7 +896,7 @@ def toggle_mute_topic(client: Client) -> None:
result = client.mute_topic(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions/muted_topics", "patch", "200")
@ -901,7 +917,7 @@ def update_user_topic(client: Client) -> None:
request=request,
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_topics", "post", "200")
# {code_example|start}
@ -918,7 +934,7 @@ def update_user_topic(client: Client) -> None:
request=request,
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_topics", "post", "200")
@ -930,7 +946,7 @@ def add_user_mute(client: Client) -> None:
muted_user_id = 10
result = client.call_endpoint(url=f"/users/me/muted_users/{muted_user_id}", method="POST")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/muted_users/{muted_user_id}", "post", "200")
@ -942,7 +958,7 @@ def remove_user_mute(client: Client) -> None:
muted_user_id = 10
result = client.call_endpoint(url=f"/users/me/muted_users/{muted_user_id}", method="DELETE")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(
result, "/users/me/muted_users/{muted_user_id}", "delete", "200"
)
@ -954,7 +970,7 @@ def mark_all_as_read(client: Client) -> None:
# Mark all of the user's unread messages as read
result = client.mark_all_as_read()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/mark_all_as_read", "post", "200")
@ -965,7 +981,7 @@ def mark_stream_as_read(client: Client) -> None:
# Mark the unread messages in the channel with ID 1 as read.
result = client.mark_stream_as_read(stream_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/mark_stream_as_read", "post", "200")
@ -980,7 +996,7 @@ def mark_topic_as_read(client: Client) -> None:
# as read.
result = client.mark_topic_as_read(stream_id, topic_name)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/mark_stream_as_read", "post", "200")
@ -1009,7 +1025,7 @@ def update_subscription_settings(client: Client) -> None:
]
result = client.update_subscription_settings(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/subscriptions/properties", "POST", "200")
@ -1022,7 +1038,7 @@ def render_message(client: Client) -> None:
}
result = client.render_message(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/render", "post", "200")
@ -1042,7 +1058,7 @@ def get_messages(client: Client) -> None:
}
result = client.get_messages(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages", "get", "200")
assert len(result["messages"]) <= request["num_before"]
@ -1066,7 +1082,7 @@ def check_messages_match_narrow(client: Client) -> None:
result = client.call_endpoint(url="messages/matches_narrow", method="GET", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/matches_narrow", "get", "200")
@ -1078,7 +1094,7 @@ def get_raw_message(client: Client, message_id: int) -> None:
# Get the raw content of the message with ID "message_id"
result = client.get_raw_message(message_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/{message_id}", "get", "200")
@ -1089,6 +1105,7 @@ def get_attachments(client: Client) -> int:
result = client.get_attachments()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/attachments", "get", "200")
return result["attachments"][0]["id"]
@ -1104,6 +1121,7 @@ def remove_attachment(client: Client, attachment_id: int) -> None:
method="DELETE",
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/attachments/{attachment_id}", "delete", "200")
@ -1122,7 +1140,7 @@ def send_message(client: Client) -> int:
result = client.send_message(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages", "post", "200")
# test that the message was actually sent
@ -1142,7 +1160,7 @@ def send_message(client: Client) -> int:
result = client.send_message(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages", "post", "200")
# test that the message was actually sent
@ -1164,6 +1182,7 @@ def add_reaction(client: Client, message_id: int) -> None:
result = client.add_reaction(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/{message_id}/reactions", "post", "200")
@ -1179,6 +1198,7 @@ def remove_reaction(client: Client, message_id: int) -> None:
result = client.remove_reaction(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/{message_id}/reactions", "delete", "200")
@ -1188,6 +1208,7 @@ def get_read_receipts(client: Client, message_id: int) -> None:
# Get read receipts for a message
result = client.call_endpoint(f"/messages/{message_id}/read_receipts", method="GET")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/{message_id}/read_receipts", "get", "200")
@ -1199,7 +1220,7 @@ def test_nonexistent_stream_error(client: Client) -> None:
"content": "I come not, friends, to steal away your hearts.",
}
result = client.send_message(request)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/messages", "post", "400")
@ -1210,7 +1231,7 @@ def test_private_message_invalid_recipient(client: Client) -> None:
"content": "With mirth and laughter let old wrinkles come.",
}
result = client.send_message(request)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/messages", "post", "400")
@ -1228,7 +1249,7 @@ def update_message(client: Client, message_id: int) -> None:
}
result = client.update_message(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/{message_id}", "patch", "200")
# test it was actually updated
@ -1249,7 +1270,7 @@ def test_update_message_edit_permission_error(client: Client, nonadmin_client: C
"content": "New content",
}
result = nonadmin_client.update_message(request)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/messages/{message_id}", "patch", "400")
@ -1259,7 +1280,7 @@ def delete_message(client: Client, message_id: int) -> None:
# Delete the message with ID "message_id"
result = client.delete_message(message_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/{message_id}", "delete", "200")
@ -1273,7 +1294,7 @@ def test_delete_message_edit_permission_error(client: Client, nonadmin_client: C
result = client.send_message(request)
result = nonadmin_client.delete_message(result["id"])
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/messages/{message_id}", "delete", "400")
@ -1283,7 +1304,7 @@ def get_message_history(client: Client, message_id: int) -> None:
# Get the edit history for message with ID "message_id"
result = client.get_message_history(message_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/{message_id}/history", "get", "200")
@ -1292,7 +1313,7 @@ def get_realm_emoji(client: Client) -> None:
# {code_example|start}
result = client.get_realm_emoji()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/emoji", "GET", "200")
@ -1316,7 +1337,7 @@ def update_message_flags(client: Client) -> None:
}
result = client.update_message_flags(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/flags", "post", "200")
# {code_example|start}
@ -1328,7 +1349,7 @@ def update_message_flags(client: Client) -> None:
}
result = client.update_message_flags(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/messages/flags", "post", "200")
@ -1336,7 +1357,7 @@ def register_queue_all_events(client: Client) -> str:
# Register the queue and get all events
# Mainly for verifying schema of /register.
result = client.register()
validate_response_result(result)
validate_against_openapi_schema(result, "/register", "post", "200")
return result["queue_id"]
@ -1349,7 +1370,7 @@ def register_queue(client: Client) -> str:
event_types=["message", "realm_emoji"],
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/register", "post", "200")
return result["queue_id"]
@ -1361,11 +1382,12 @@ def deregister_queue(client: Client, queue_id: str) -> None:
# to be removed)
result = client.deregister(queue_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/events", "delete", "200")
# Test "BAD_EVENT_QUEUE_ID" error
result = client.deregister(queue_id)
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/events", "delete", "400")
@ -1377,6 +1399,7 @@ def get_queue(client: Client, queue_id: str) -> None:
# parameters, like so:
result = client.get_events(queue_id=queue_id, last_event_id=-1)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/events", "get", "200")
@ -1386,7 +1409,7 @@ def get_server_settings(client: Client) -> None:
# Fetch the settings for this server
result = client.get_server_settings()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/server_settings", "get", "200")
@ -1401,7 +1424,7 @@ def update_settings(client: Client) -> None:
}
result = client.call_endpoint("/settings", method="PATCH", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/settings", "patch", "200")
@ -1424,7 +1447,7 @@ def upload_file(client: Client) -> None:
}
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_uploads", "post", "200")
@ -1433,7 +1456,7 @@ def get_stream_topics(client: Client, stream_id: int) -> None:
# {code_example|start}
result = client.get_stream_topics(stream_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/{stream_id}/topics", "get", "200")
@ -1443,7 +1466,7 @@ def add_apns_token(client: Client) -> None:
request = {"token": "apple-tokenbb", "appid": "org.zulip.Zulip"}
result = client.call_endpoint(url="/users/me/apns_device_token", method="POST", request=request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/apns_device_token", "post", "200")
@ -1457,7 +1480,7 @@ def remove_apns_token(client: Client) -> None:
url="/users/me/apns_device_token", method="DELETE", request=request
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/apns_device_token", "delete", "200")
@ -1469,6 +1492,7 @@ def add_fcm_token(client: Client) -> None:
url="/users/me/android_gcm_reg_id", method="POST", request=request
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/android_gcm_reg_id", "post", "200")
@ -1482,6 +1506,7 @@ def remove_fcm_token(client: Client) -> None:
url="/users/me/android_gcm_reg_id", method="DELETE", request=request
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/android_gcm_reg_id", "delete", "200")
@ -1502,7 +1527,7 @@ def set_typing_status(client: Client) -> None:
result = client.set_typing_status(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/typing", "post", "200")
# {code_example|start}
@ -1518,7 +1543,7 @@ def set_typing_status(client: Client) -> None:
result = client.set_typing_status(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/typing", "post", "200")
# {code_example|start}
@ -1536,7 +1561,7 @@ def set_typing_status(client: Client) -> None:
result = client.set_typing_status(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/typing", "post", "200")
# {code_example|start}
@ -1554,7 +1579,7 @@ def set_typing_status(client: Client) -> None:
result = client.set_typing_status(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/typing", "post", "200")
@ -1572,7 +1597,7 @@ def upload_custom_emoji(client: Client) -> None:
files=[fp],
)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/emoji/{emoji_name}", "post", "200")
@ -1583,7 +1608,7 @@ def delete_custom_emoji(client: Client) -> None:
emoji_name = "my_custom_emoji"
result = client.call_endpoint(f"realm/emoji/{emoji_name}", method="DELETE")
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/realm/emoji/{emoji_name}", "delete", "200")
@ -1593,10 +1618,9 @@ def get_alert_words(client: Client) -> None:
# Get all of the user's configured alert words.
result = client.get_alert_words()
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/alert_words", "get", "200")
assert result["result"] == "success"
@openapi_test_function("/users/me/alert_words:post")
def add_alert_words(client: Client) -> None:
@ -1606,10 +1630,9 @@ def add_alert_words(client: Client) -> None:
result = client.add_alert_words(word)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/alert_words", "post", "200")
assert result["result"] == "success"
@openapi_test_function("/users/me/alert_words:delete")
def remove_alert_words(client: Client) -> None:
@ -1619,10 +1642,9 @@ def remove_alert_words(client: Client) -> None:
# Remove words (or phrases) from the user's set of configured alert words.
result = client.remove_alert_words(words)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/users/me/alert_words", "delete", "200")
assert result["result"] == "success"
@openapi_test_function("/user_groups/create:post")
def create_user_group(client: Client) -> None:
@ -1637,10 +1659,9 @@ def create_user_group(client: Client) -> None:
result = client.create_user_group(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_groups/create", "post", "200")
assert result["result"] == "success"
@openapi_test_function("/user_groups/{user_group_id}:patch")
def update_user_group(client: Client, user_group_id: int) -> None:
@ -1653,6 +1674,7 @@ def update_user_group(client: Client, user_group_id: int) -> None:
result = client.update_user_group(request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_groups/{user_group_id}", "patch", "200")
@ -1661,9 +1683,8 @@ def remove_user_group(client: Client, user_group_id: int) -> None:
# {code_example|start}
result = client.remove_user_group(user_group_id)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_groups/{user_group_id}", "delete", "200")
assert result["result"] == "success"
@openapi_test_function("/user_groups/{user_group_id}/members:post")
@ -1677,17 +1698,19 @@ def update_user_group_members(client: Client, user_group_id: int) -> None:
result = client.update_user_group_members(user_group_id, request)
# {code_example|end}
validate_response_result(result)
validate_against_openapi_schema(result, "/user_groups/{group_id}/members", "post", "200")
def test_invalid_api_key(client_with_invalid_key: Client) -> None:
result = client_with_invalid_key.get_subscriptions()
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/rest-error-handling", "post", "400")
def test_missing_request_argument(client: Client) -> None:
result = client.render_message({})
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/rest-error-handling", "post", "400")
@ -1711,7 +1734,7 @@ def test_realm_deactivated(client: Client) -> None:
def test_invalid_stream_error(client: Client) -> None:
result = client.get_stream_id("nonexistent")
validate_response_result(result, expected_result="error")
validate_against_openapi_schema(result, "/get_stream_id", "get", "400")