Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zulip: Add tests for API functions. #682

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pytest
-e git+https://github.com/zulip/zulint@14e3974001bf8442a6a3486125865660f1f2eb68#egg=zulint==1.0.0
mypy==0.812
gitlint>=0.13.0
responses
95 changes: 95 additions & 0 deletions zulip/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import json
import unittest
import responses
import zulip
import urllib

from typing import Any, Tuple, Dict
from unittest import TestCase

class TerminationException(Exception):
pass

class TestAPI(TestCase):

@responses.activate
def test_add_reaction(self) -> None:
def request_callback(request: Any) -> Tuple[int, Dict[str, str], str]:
params = {}
for param in request.body.split("&"):
key, value = param.split("=")
params[key] = urllib.parse.unquote(value)
assert "emoji_name" in params or "emoji_code" in params
return (200, {}, json.dumps({'result': 'success', 'msg': ''}))
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this function -- why do we need this parsing logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function needs some work as well as I understand. This test is primarily for testing whether the payload provided is properly parsed, the URL is correct, and all the It accepts and sends a proper response, as a part of the normal API workflow.

This test makes sure all intermediate functions for an API call like do_api_query and call_endpoint, work correctly.


responses.add_callback(
method=responses.POST,
url="https://test.zulipapi.com/api/v1/messages/10/reactions",
callback=request_callback
)

client = zulip.Client(config_file="zulip/tests/test_zuliprc")
# request with emoji name
request = {
"message_id": 10,
"emoji_name": "octopus",
}
result = client.add_reaction(request)
self.assertEqual(result, {'result': 'success', 'msg': ''})
# request with emoji code
request = {
"message_id": 10,
"emoji_code": "1f419",
}
result = client.add_reaction(request)
self.assertEqual(result, {'result': 'success', 'msg': ''})

@responses.activate
def test_call_on_each_event(self) -> None:
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test could benefit from a comment or docstring explaining what we're trying to test here.


responses.add(
responses.POST,
url="https://test.zulipapi.com/api/v1/register",
json={'queue_id': '123456789', 'last_event_id': -1, 'msg': '', 'result': 'success'},
status=200
)
responses.add(
responses.GET,
url="https://test.zulipapi.com/api/v1/events",
json={'result': 'success', 'msg': '', 'events': [{'id': 0}, {'id': 1}, {'id': 2}]},
status=200
)

def event_callback(x: Dict[str, Any]) -> None:
print(x)
if x['id'] == 2:
raise TerminationException()

client = zulip.Client(config_file="zulip/tests/test_zuliprc")

try:
client.call_on_each_event(
event_callback,
)
except TerminationException:
pass

try:
client.call_on_each_event(
event_callback,
['message'],
)
except TerminationException:
pass

try:
client.call_on_each_event(
event_callback,
['message'],
[['some', 'narrow']]
)
except TerminationException:
pass

if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions zulip/tests/test_zuliprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[api]
[email protected]
key=K1PZuAp18Cn9RFjTsf5O1HeRW6TVpyhF
site=https://test.zulipapi.com