Commit graph

276 commits

Author SHA1 Message Date
Steve Howell f8cefc5352 minor: Fix imports in test_xkcd.py.
We no longer need the `future` imports and we can
use `StubBotTestCase` now.
2017-12-10 09:25:12 -06:00
Marco Burstein 700ce6a673 bots: Create Chess Bot.
Chess Bot is a bot that allows you to play chess against either another
user or the computer. Use `start with other user` or
`start as <color> with computer` to start a game.

In order to play against a computer, `chess.conf` must be set with the
key `stockfish_location` set to the location of the Stockfish program on
this computer.

Use `bot_handler.storage` to preserve game state across messages.

(@showell also did minor work here to have the test use verify_dialog()
and have the bot respond to empty messages)
2017-12-10 09:14:32 -06: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
Kai Chen 1cdb0bffe6 mypy: Add annotations for encrypt. 2017-12-10 04:59:27 -08:00
Kai Chen b382eacd18 mypy: Add annotations for define. 2017-12-10 04:59:27 -08:00
Kai Chen d33e9b9d92 mypy: Add annotations for converter. 2017-12-10 04:59:27 -08:00
neiljp (Neil Pilgrim) 31853cfa80 Add test_bot_responds_to_empty_message to StubBotTestCase. 2017-12-09 15:06:41 -08:00
Sivagiri Visakan 6f9d010ed3 bots: Refactor Youtube bot. 2017-12-09 03:56:33 -08:00
Ivche1337 f947ff44f8 wikipedia bot: Return up to three links from wikipedia for keyword.
This change includes updates to the docs and tests as well.
2017-12-08 16:40:16 -08:00
Ivche1337 2597de87ef wikipedia bot: Fix page-not-found error. 2017-12-08 16:40:16 -08:00
fredfishgames ab9128d939 mypy: Add annotations for weather. 2017-12-08 14:55:35 -08:00
fredfishgames f7f54d159f mypy: Add annotations for virtual_fs. 2017-12-08 14:55:35 -08:00
fredfishgames a7f9c6e743 Fixed giphy warning message in bot tests 2017-12-08 14:52:44 -08:00
fredfishgames a026c48278 Added more googlesearch tests 2017-12-08 14:52:44 -08:00
fredfishgames 841efcde80 Added too many argument test for googletranslate bot. 2017-12-08 14:52:44 -08:00
neiljp (Neil Pilgrim) d4284f88f9 XKCD: Adjust tests to use loops, inline responses, remove comments. 2017-12-08 11:21:02 -08:00
neiljp (Neil Pilgrim) d97401df86 XKCD: Migrate tests to use verify_reply. 2017-12-08 11:21:02 -08:00
neiljp (Neil Pilgrim) 6c7a03e37a XKCD: Split single test into multiple tests. 2017-12-08 11:21:02 -08:00
fredfishgames 9492495f4b Removed self: Any from googlesearch 2017-12-08 10:48:42 -08:00
Steve Howell cb849611c8 bot tests: Add sender_full_name to verify_dialog. 2017-12-08 08:51:57 -08:00
fredfishgames 4c4a60d90f Remove self: Any mypy annotations 2017-12-08 08:46:21 -08:00
Steve Howell 5f70e17259 bots: Simplify test_github_details.py. 2017-12-07 20:15:33 -08:00
Steve Howell f421e90a64 bots: Simplify test_encrpyt.py. 2017-12-07 20:15:33 -08:00
Steve Howell 7285affbc4 bots: Simplify test_define.py. 2017-12-07 20:15:33 -08:00
Steve Howell 2c42b0e42e minor: Clean up test_converter.py.
This removes some imports and uses StubBotTestCase
and verify_dialog.
2017-12-07 20:15:33 -08:00
Steve Howell 6f0d5239e8 test_googlesearch: Remove self: Any annotations.
We can avoid `Any` annotations for `self`, since it is more
noise than signal and since the type of self is already
implicit from how Python classes work.
2017-12-07 19:40:32 -08:00
Steve Howell 011095018b Simplify TestGoogleSearchBot.
We use verify_reply() for all the tests and de-duplicate
help_message by just having a single test validate both
the '' and 'help' inputs.
2017-12-07 19:40:32 -08:00
Steve Howell c01fbe02e1 Remove unneeded imports in test_googlesearch.py. 2017-12-07 19:40:32 -08:00
fredfishgames 9d5fbc2b5b mypy: Add annotations for help. 2017-12-07 19:12:39 -08:00
fredfishgames 40c09b4b16 mypy: Add annotations for googlesearch. 2017-12-07 19:12:39 -08:00
fredfishgames b7dbe7af41 mypy: Add annotations for github_detail. 2017-12-07 19:12:39 -08:00
fredfishgames 1b16b54780 interactive bots: Create googletranslate bot. 2017-12-07 06:18:01 -08:00
Steve Howell 7a963916f2 bot tests: Use StubBotTestCase in test_giphy.py.
For three of the tests we use the simpler verify_reply()
API.  For the 403 test, we don't need to rely on setUp
any more to simulate everything for us, and we do more
surgical patching.
2017-12-06 20:29:36 -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
Steve Howell be9570c9c4 minor: Remove unused imports in test_giphy.py. 2017-12-06 20:29:36 -08:00
Jerry Zhang 77e9be0783 bots/giphy: Use Python 3 type mypy annotations. 2017-12-07 00:22:57 -03:30
Jerry Zhang 95b8ea4751 bots/followup: Use Python 3 type mypy annotations. 2017-12-07 00:22:57 -03:30
Robert Hönig 1102057e35 zulip_bots: Make typing import work in Python 3.5.
See https://github.com/python/mypy/issues/1838.
2017-12-06 12:49:46 +01:00
Skunk 29e22c2c0a bots: Create Link Shortener Bot.
Create Link Shortener Bot using the goo.gl Link Shortening API.

Link Shortener Bot can be mentioned in a conversation, and it will
respond with shortened, goo.gl links for every URL in the message.

For example,

 > @link_shortener_bot @johnsmith Check out this file:
 > https://github.com/zulip/python-zulip-api/blob/master/zulip_bots/

and Link Shortener Bot would respond

 > https://github.com/zulip/python-zulip-api/blob/master/zulip_bots/:
 > https://goo.gl/Mt5z3c

In order to use Link Shortener Bot, an API key for goo.gl must be set
in `link_shortener.conf` in the `link_shortener` folder.
2017-12-05 06:08:30 -08:00
Rohitt Vashishtha 8cd310493a mypy: Annotate helloworld bot. 2017-12-04 07:27:20 -08:00
fredfishgames b157b817ae interactive bots: Improve googlesearch bot. 2017-12-03 14:03:47 -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 fab9d46f93 Simplify test_wikipedia.py.
We now use StubBotTestCase.verify_reply().
2017-12-01 13:42:59 -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 c02e141f7f bot tests: Use StubBotTestCase for helloworld bot. 2017-12-01 07:00:20 -08:00
Steve Howell 4ede1a5564 bot tests: Use StubBotTestCase for help bot. 2017-12-01 07:00:20 -08:00
Steve Howell 6087cf95e2 bot tests: Use StubBotTestCase in tictactoe.
Note that we now only have one conversation, since the case
of sending to streams has the same mechanics as sending PMs.

We'll eventually want a separate test to drive out differences
in the actual mechanics of the reply.
2017-12-01 07:00:20 -08:00
Steve Howell 77dbe92ad8 minor: Remove some dead code in BotTestCaseBase. 2017-12-01 07:00:20 -08:00
Steve Howell c0ceb9857f Rewrite incrementor bot tests.
Using StubBotHandler makes it fairly easy to verify the key
feature of the incrementor bot, which is that it updates
messages sequentially.
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) 39601d190c bot lib tests: Add test_send_reply. 2017-11-29 15:56:30 -08:00
Steve Howell efeac92d42 Add tools/test-lib.
This starts to add direct test coverage on
zulip_bots/zulip_bots/lib.py.

It is not yet integrated into tools/test-main.
2017-11-29 13:45:51 -08:00
neiljp (Neil Pilgrim) 3e135b8e1e bot testing: Enable testing of multiple responses from single messages. 2017-11-29 10:05:46 -08:00
Abhijeet Kaur 91c4ff668d bots: Clean up terminal.py for running bots directly.
Make terminal.py exit gracefully with a message.
Modify output bot reply for better understanding in multi-line
output.
2017-11-29 10:05:06 -08:00
Eeshan Garg c3ab265d3e packaging: Upgrade to release 0.3.9. 2017-11-29 01:23:12 -03:30
Robert Hönig 2cb43256e0 zulip_bots: Get 100% test coverage for giphy bot. 2017-11-28 15:42:22 -08:00
Robert Hönig 93edededdd zulip_bots: Use real Response object for mock_http_conversation(). 2017-11-28 15:42:22 -08:00
Robert Hönig 41d8ffc20e zulip_bots: Fix yoda bot response parsing. 2017-11-28 15:42:22 -08:00
Robert Hönig d6a4c02ba5 Rename bot storage endpoint from state to storage. 2017-11-28 20:41:59 +01:00
Steve Howell 80e4ef9f72 Create terminal.py to run bots in the terminal.
This program replaces zulip_bot_output.py, which had
gotten a little out of date.

It should be able to simulate a terminal conversation for
all of our bots, including those that use "advanced" features:

    third party config files: tested with giphy
    message updates: tested with incrementor
    storage: tested with virtual_fs and others
2017-11-28 10:52:13 -08:00
Steve Howell 536ba1843a Add a command line option to run.py for bot config files.
Before this change, we were looking for config files in
default locations in source control, which is not a good
place to look for them.  Now `run.py` and friends have a
command line argument where users can specify the config
files.

Note that the change to server.py is only a partial fix
to make it so that bots that don't use third party config
files won't crash.  That program needs an overhaul, anyway.
2017-11-28 10:52:13 -08:00
Steve Howell 57c90ddca0 refactor: Extract display_config_file_errors().
This will be useful for handling errors with third party
config files.
2017-11-28 10:52:13 -08:00
Steve Howell 8a15452525 bots: Require bots_details in ExternalBotHandler.
This parameter was defaulting to a dictionary, which is
a classic Python pitfall.
2017-11-28 10:52:13 -08:00
Steve Howell ef30261858 giphy: Make config_info an instance variable.
There was no reason to use a `global` here.
2017-11-28 10:52:13 -08:00
Eeshan Garg 50b5ec0f49 packages: Release 0.3.8 for all PyPI packages. 2017-11-27 23:34:38 -03:30
Robert Hönig a75f1abc60 zulip_bots: Add -c shortcut for --config-file for zulip-run-bots. 2017-11-27 16:45:37 +01:00
Robert Hönig ea6ddb2312 zulip_bots: Only cache storage.get(). Don't cache put(). 2017-11-24 10:44:34 -08:00
Steve Howell a32446f557 Report client.get_profile() errors nicely (e.g. API key).
This patch is particularly useful in the scenario that your
API key is wrong or out of date, but it's targeted more
generally at any error that `client.get_profile()` reports.
2017-11-22 11:15:00 -08:00
Steve Howell 043d963a99 Fail fast for bad connections with the API and bots.
The API has aggressive retry logic for connecting to a
server, which may make sense for situation where you have
connection blips or server restarts.

When you're first connecting to the API, however, connection
failures are almost certainly a sign of misconfiguration, so
now we fail fast.

The bot lib takes advantage of this API change by catching the
ZulipError exception and exiting gracefully.
2017-11-21 14:21:04 -08:00
Steve Howell a19278da65 Handle configparser errors more gracefully. 2017-11-21 11:54:59 -08:00
Steve Howell e216a29277 minor: Extract client_name local var. 2017-11-21 11:26:12 -08:00
Steve Howell 73ebd719b2 Handle missing config files gracefully.
Before this commit, you would get a traceback if you supplied
a non-existent filename for your config file.  Now we exit
gracefully with a useful error message.
2017-11-21 10:32:45 -08:00
Steve Howell f6440e5b65 Require --config-file and simplify command line usage.
We now require users to specify where their config file is
located, and we no longer default to ~/.zuliprc.

As part of this, we needed to make the "usage" more accurate
in the command line, which I mostly achieved by cutting out
unnecessary stuff.
2017-11-21 09:11:41 -08:00
derAnfaenger 940e1260b2 docs: Update outdated links. 2017-11-20 15:09:50 +01:00
derAnfaenger 2a74ad11c5 zulip_bots: Make StateHandler optional.
This has the convenient side-effect of making
the bot_handler.storage attribute read-only.
2017-11-18 08:58:47 -08:00
derAnfaenger f41030b515 API: rename get_state & update_state to get_storage & update_storage. 2017-11-18 08:58:47 -08:00
derAnfaenger 2736223073 bots: Make StateHandler store state on Zulip server.
This makes the StateHandler functional. To reduce the
number of server roundtrips when fetching/updating the
state, the entire state is fetched ocne at bot
initialization and cached. All changes are stored in the
cache and only saved externally after handle_message()
has been executed.

Fixes #141.
2017-11-18 08:58:47 -08:00
derAnfaenger 979bbb1c14 bot tests: Consistently use mock_bot_handler instance of MockClass. 2017-11-18 08:58:47 -08:00
derAnfaenger daad7f24e4 bots: Set JSON as default serializer for StateHandler.
With this change, StateHandler.put() does only accept JSON-able
objects by default. The incrementor test tried to store the return
value of send_reply(), a non-JSON-able MockObject, in the state.
Therefore, this commits also sets functional default test return
values for send_message() and send_reply().
Finally, it fixes the tictactoe bot which relied on directly
modifying the state_ attribute.
2017-11-18 08:58:47 -08:00
Eeshan Garg bd7db69bac bots/giphy: Document the bot. 2017-11-16 09:09:04 -08:00
derAnfaenger a8b858d78f zulip_bots: Update links to docs. 2017-11-16 13:54:02 +01:00
neiljp (Neil Pilgrim) 59b86b5b68 mypy: Add reported issue to 'type: ignore' line in generate_manifest.py. 2017-11-15 13:43:08 -08:00
neiljp (Neil Pilgrim) 03f5e9110b mypy: Add comment on ConfigParser.readfp 'type: ignore'.
readfp remains valid but is deprecated, so is not in the python3 stubs.
2017-11-15 13:43:08 -08:00
derAnfaenger 761e3e5855 zulip_bots: Update README.md.
This stubs the zulip_bots README.md
and moves architectural information
to architecture.md.
2017-11-14 09:51:09 -08:00
derAnfaenger 5f3b3436f6 zulip_bots: Remove redundant custom tests in encrypt. 2017-11-14 09:39:00 -08:00
derAnfaenger 9c37f92a01 zulip_bots: Add common tests.
unittest includes by default all module-level classes that inherit
from TestCase and implement at least one method starting with 'test'.
Since it doesn't provide a convenient way for excluding TestSuites,
we need to manually filter out the unwanted testing of our test base
class itself.
2017-11-14 09:39:00 -08:00
Josh Mandel 94b7c2eaef zulip_bots: Make xkcd bot error more verbose. 2017-11-13 17:00:32 +01:00
derAnfaenger 55332d8cbc zulip_bots: Fix and simplify extract_query_without_mention.
This fixes quirks related to the regex not covering all potential
weird usernames and adds tests for stripping the @-mentions.
2017-11-10 16:09:20 -08:00
derAnfaenger e05ce661c5 zulip_bots: Enforce default config file schema.
This requires the bot's config section to be named
after the bot. All config entries must be contained
in this section.
2017-11-06 14:20:39 -08:00
derAnfaenger 1a096b317b zulip_bots: Standardize weather bot's config file. 2017-11-06 14:20:39 -08:00
derAnfaenger 56ac3f1b73 zulip_bots: Add config file for github_detail bot. 2017-11-06 14:20:39 -08:00
derAnfaenger 8d81d37351 zulip_bots: Move unmaintained bots to unmaintained dir. 2017-11-02 15:15:52 +01:00