From aca98cccbdf05af6fe2bad618e2efe2edbd7f162 Mon Sep 17 00:00:00 2001 From: LoopThrough-i-j Date: Wed, 5 May 2021 22:36:42 +0530 Subject: [PATCH] zulip: Add tests for zulip apis. --- requirements.txt | 1 + zulip/tests/test_api.py | 90 ++++++++++++++++++++++++++++++++++++++++ zulip/tests/test_zuliprc | 4 ++ 3 files changed, 95 insertions(+) create mode 100644 zulip/tests/test_api.py create mode 100644 zulip/tests/test_zuliprc diff --git a/requirements.txt b/requirements.txt index 7d0095a725..a0aa31ce43 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/zulip/tests/test_api.py b/zulip/tests/test_api.py new file mode 100644 index 0000000000..e2ccbd0cfe --- /dev/null +++ b/zulip/tests/test_api.py @@ -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() diff --git a/zulip/tests/test_zuliprc b/zulip/tests/test_zuliprc new file mode 100644 index 0000000000..bffe680f4b --- /dev/null +++ b/zulip/tests/test_zuliprc @@ -0,0 +1,4 @@ +[api] +email=test@zulipapi.com +key=K1PZuAp18Cn9RFjTsf5O1HeRW6TVpyhF +site=https://test.zulipapi.com