Commit graph

73 commits

Author SHA1 Message Date
Anders Kaseorg e8bb65b188 tests: Replace deprecated assertRaisesRegexp with assertRaisesRegex.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-08-24 17:20:53 -07:00
PIG208 6f3f9bf7e4 black: Reformat without skipping string normalization. 2021-06-02 18:45:57 -07:00
PIG208 fba21bb00d black: Reformat skipping string normalization. 2021-06-02 18:45:57 -07:00
PIG208 5580c68ae5 isort: Reformat using isort. 2021-06-02 18:45:57 -07:00
PIG208 d949f2024f zulip_bots: Rename SimpleMessageServer to MockMessageServer.
Given that the purpose of this message server is solely for testing,
we rename it for clarity.
2021-05-27 23:52:07 -07:00
LoopThrough-i-j a994c58439 zulip-bots: Add BotHandler Protocol.
- The `BotHandler` Protocol is a mypy Protocol
  s.t. all BotHandlers can use it as a default type.
- Fix ExternalBotHandler and StubBotHandler to
  follow `BotHandler` Protocol

Fixes part of #639
2021-03-03 13:31:37 -08:00
Lunatic Luna 3b1ef57694 bots: Support adding reactions to message for a bot.
It adds a react() function that allows a bot to react to a message in lib.py.
It adds an example of the use of react() function and its test.

The changes are in the following files:
  - lib.py
  - helloworld.py
  - tests/test_lib.py
  - test_lib.py
2020-05-08 10:46:56 -04:00
Anders Kaseorg 6f40bcf745 cleanup: Fix whitespace around parameter equals.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-18 20:31:14 -07:00
Anders Kaseorg 5428c5f296 typing: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and two fixes for use-before-define
issues:

-    def set_zulip_client(self, zulipToJabberClient: ZulipToJabberBot) -> None:
+    def set_zulip_client(self, zulipToJabberClient: 'ZulipToJabberBot') -> None:

-def init_from_options(options: Any, client: Optional[str] = None) -> Client:
+def init_from_options(options: Any, client: Optional[str] = None) -> 'Client':

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-18 20:31:14 -07:00
novokrest 7fc1ff5e0e bots: Add bot for uploading files to Zulip server.
Add file_uploader bot as an example of using
ExternalBotHandler's methods for uploading files
to Zulip server.
2018-07-01 07:57:45 -04:00
neiljp (Neil Pilgrim) 6cdb83ce72 bot tests: Adjust TestCase inheritance to avoid need to skip/filter.
Previously the test-bots script filtered out base-class tests from
BotTestCase. With this change, BotTestCase continues to inherit from
unittest.TestCase, but the default test_* methods previously in this
class are now in a new DefaultTests class, which does not. Instead, each
bot needs to inherit from BotTestCase and DefaultTests *explicitly*.

This avoids the need to filter out the base-class tests, which
simplifies the test-bots script, and may ease any migration to eg.
pytest.

The DefaultTests class does require some non-implemented methods which
BotTestCase provides.
2018-06-09 12:49:49 -04:00
Steve Howell fcd39204a9 bots: Pass through widget_content if passed in. 2018-06-04 05:59:10 -04:00
neiljp (Neil Pilgrim) 56c59d915a bots: Support determining bot runtime identity.
This improves the ability of a bot to specify how to mention it,
which varies at run-time depending upon the identity used to run it;
this is commonly used in many bot help commands.
2018-05-31 06:10:50 -04:00
dkvasov 0040182ec4 zulip_bots: Obey pep8, python3 type annotations. 2018-05-17 17:49:16 +03:00
Dhruv Thakker 9ed47266aa test_lib : Add helper for connection test 2018-03-08 08:27:32 -05:00
Robert Hönig 192e9e101d bots: Add magic method validate_config().
This method allows bots to validate their config info
in a standardized way. Adding the method to a bot is
optional, but recommended for bots with config options
that can be invalid, like api keys. The giphy bot serves
as an example.
The primary reason behind this is to allow the zulip
backend to validate config data for embedded bots. The
backend does not have a permanent bot class instance, so
validate_config() must be a static method.
2018-02-13 11:02:17 -08:00
fredfishgames ec5be8fc7e zulip_bots: Consolidate game-playing bots. 2018-01-22 11:30:31 -05:00
Steve Howell 917bd82019 bot tests: Extract get_reply_dict() test helper. 2018-01-10 16:54:05 -05:00
Robert Hönig b46a079804 zulip_bots: Move from mock to unittest.mock in most files. 2018-01-06 16:39:52 +01:00
neiljp (Neil Pilgrim) 1fd4dfc86e bot testing: Amend StubBotHandler.get_config_info to return {}.
This ensures the return type matches the annotated type, which
matches the return type of ExternalBotHandler.
2018-01-02 14:05:33 -05:00
Robert Hönig ff65666ac8 Add quit() function to ExternalBotHandler.
bot_handler.quit() should be used whenever a bot
wishes to terminate. This allows a flexible reaction
suited to the bot's environment: For external bots,
sys.exit() will be called, whereas for embedded bots,
different code can be executed.
2017-12-31 07:12:19 -05:00
Robert Hönig b306324bfa zulip_bots: Make BotTestCase the only helper library.
Renames StubBotTestCase to BotTestCase and removes
legacy code for supporting both names.
2017-12-14 05:56:55 -05:00
fredfishgames 657c6d7b9c interactive bots: Create DialogFlow bot. 2017-12-11 13:15:43 -06:00
Steve Howell 7bc79a8911 Extract StubBotTestCase.make_request_message().
This de-duplicates some code and also makes it a bit
easier in the future to override how we create messages.
2017-12-11 09:39:48 -06:00
Steve Howell 5686d603ef minor: Clean up test_lib imports. 2017-12-11 09:34:11 -06:00
Steve Howell 971b48cbfc Decouple mock_request_exception from StubBotTestCase.
Tests that need mock_request_exception can just import it
directly now.
2017-12-11 09:26:36 -06:00
Steve Howell 1dbb73a1c8 Extract test_file_utils.py.
This isolates all the code that depends on our current
directory structure, and it de-clutters test_lib.py a bit.
2017-12-11 09:23:24 -06:00
Steve Howell 5034220829 bots: Extract request_test_lib.py.
The new module mocks out calls to the requests library, which
is used by many of our bots that use third party services.

Mocking of requests.py is mostly orthogonal to our other
testing concerns.
2017-12-11 09:05:06 -06:00
Steve Howell fb8ee29bc1 zulip_bots/test_lib.py: Remove unneeded imports/shebang. 2017-12-11 08:26:50 -06:00
Steve Howell b4705c2343 bot tests: Remove BotTestCase.
All of the functionality of BotTestCase was either directly
moved to StubBotTestCase or replaced by similar functions,
and all bots have been ported to StubBotTestCase.
2017-12-11 08:26:50 -06:00
sColin16 1221c7107f test_lib.py: Add mock_request_exception. 2017-12-11 08:05:09 -06:00
Steve Howell a7f6c0f142 Add display_recipient to StubBotTestCase.verify_dialog. 2017-12-11 07:57:36 -06:00
neiljp (Neil Pilgrim) 84d92337be bots: Simplify test_googletranslate.py.
Also add sender_full_name parameter in StubBotTestCase.verify_reply().
2017-12-10 16:26:03 -08:00
Steve Howell 2fa677a3e0 bot tests: Extract StubBotTestCase._get_handlers().
This is mostly a pure refactoring, but it also ensures
that `initialize` is called in a consistent way by most
of our test helpers.  (This didn't cause problems before,
since some bots don't require initialization.)
2017-12-10 06:35:51 -08:00
Steve Howell 01f8366bea Improve test_bot_responds_to_empty_message().
This fixes the TODO of inlining get_response(), and it
makes a slightly tighter assertion about the response.
2017-12-10 06:20:19 -08:00
Nikhil Mishra 81b207795f followup bot: Fix help response and configure streams.
From @showell:

We had a PR here with lots going on, and the commits weren't
very well organized, and then there were some tricky merge
conflicts from another PR.  So I just squashed them all into
one commit.

What this does:
    * allow you to configure your followup stream
    * provide help in followup stream
    * add more testing to followup stream
    * add get_response() helper for tests

Fixes #173
Fixes #174
2017-12-10 05:23:35 -08:00
neiljp (Neil Pilgrim) 31853cfa80 Add test_bot_responds_to_empty_message to StubBotTestCase. 2017-12-09 15:06:41 -08:00
Steve Howell cb849611c8 bot tests: Add sender_full_name to verify_dialog. 2017-12-08 08:51:57 -08:00
Steve Howell 87662da139 bot tests: Add StubBotTestCase.mock_config_info().
We now auto-initialize bots in verify_reply() and have
a mock_config_info() helper that can override the new
StubBotHandler.get_config_info().
2017-12-06 20:29:36 -08:00
Skunk c2c79cf989 tests: Refactor mock_http_conversation with deduplicating.
Move `get_response` inside of `mock_http_conversation`, as it is not
used anywhere else. Also create `assert_called_with_fields`.
`assert_called_with_fields` calls the `assert_called_with` method of a
mock object by using an HTTP request and a list of fields to look for.
2017-12-03 10:26:42 -08:00
Skunk bd0462f7b5 tests: Extend mock_http_conversation to support POST requests. 2017-12-03 10:26:42 -08:00
Steve Howell 16e50e991b bot tests: Add StubBotTestCase.verify_reply(). 2017-12-01 13:42:59 -08:00
Steve Howell db08586df6 Promote mock_http_conversations to its superclass. 2017-12-01 13:42:59 -08:00
Steve Howell 3f79136dd7 bot tests: Introduce transcript in StubBotHandler.
This sets us up to validate more aspects of the conversation,
and it also introduces the more rigorously checked
`unique_response` helper.

(This also fixes a minor copy/paste error from a prior commit
that was harmless.)
2017-12-01 13:42:59 -08:00
Steve Howell 77dbe92ad8 minor: Remove some dead code in BotTestCaseBase. 2017-12-01 07:00:20 -08:00
Steve Howell fc251460fa bot tests: Eliminate BotTestCaseBase.
We now just put `test_bot_usage` in StubBotTestCase and have
BotTestCase inherit from that.
2017-12-01 07:00:20 -08:00
Steve Howell 205f7c16c7 Add StubBotTestCase and StubBotHandler.
We will start to use these for some of our bot test code.
2017-12-01 07:00:20 -08:00
Steve Howell fe662ed145 bot tests: Split up mock_http_conversation().
This method had two pretty easy-to-separate concerns:

    * find the fixture data using our directory conventions
    * use the fixture data to simulate a real HTTP request

Part of the goal here is to make the extracted functions a
bit easier to use in other TestCase-based classes without
needing to subclass from BotTestCaseBase, which is kind of
complex with its setUp/tearDown.
2017-12-01 07:00:20 -08:00
Steve Howell 0a477b0736 Extract get_bot_message_handler(). 2017-12-01 07:00:20 -08:00
neiljp (Neil Pilgrim) 3e135b8e1e bot testing: Enable testing of multiple responses from single messages. 2017-11-29 10:05:46 -08:00