-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d482e0
commit aca98cc
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[api] | ||
[email protected] | ||
key=K1PZuAp18Cn9RFjTsf5O1HeRW6TVpyhF | ||
site=https://test.zulipapi.com |