Skip to content

Commit

Permalink
zulip: Add tests for zulip apis.
Browse files Browse the repository at this point in the history
  • Loading branch information
LoopThrough-i-j committed May 5, 2021
1 parent 4d482e0 commit aca98cc
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
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
90 changes: 90 additions & 0 deletions zulip/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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': ''}))

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:
responses.add(
responses.POST,
url="https://test.zulipapi.com/api/v1/register",
json={'queue_id': 10, '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': 123}]},
status=200
)

def terminate() -> None:
raise TerminationException()
client = zulip.Client(config_file="zulip/tests/test_zuliprc")
try:
client.call_on_each_event(
lambda x: terminate(),
['message'],
)
except TerminationException:
pass

try:
client.call_on_each_event(
lambda x: terminate(),
)
except TerminationException:
pass

try:
client.call_on_each_event(
lambda x: terminate(),
['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

0 comments on commit aca98cc

Please sign in to comment.