From c33ac65ac933980cfb3a459bddaf2d852dcfb263 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Tue, 19 Sep 2017 13:43:08 -0700 Subject: [PATCH] define bot: add check & test to avoid sending words with non-letters. --- zulip_bots/zulip_bots/bots/define/define.py | 7 +++++++ zulip_bots/zulip_bots/bots/define/test_define.py | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/zulip_bots/zulip_bots/bots/define/define.py b/zulip_bots/zulip_bots/bots/define/define.py index 69d9722..6f0ea83 100644 --- a/zulip_bots/zulip_bots/bots/define/define.py +++ b/zulip_bots/zulip_bots/bots/define/define.py @@ -3,6 +3,7 @@ import logging import json import requests import html2text +import string class DefineHandler(object): ''' @@ -14,6 +15,7 @@ class DefineHandler(object): REQUEST_ERROR_MESSAGE = 'Could not load definition.' EMPTY_WORD_REQUEST_ERROR_MESSAGE = 'Please enter a word to define.' PHRASE_ERROR_MESSAGE = 'Definitions for phrases are not available.' + SYMBOLS_PRESENT_ERROR_MESSAGE = 'Definitions of words with symbols are not possible.' def usage(self): return ''' @@ -36,6 +38,11 @@ class DefineHandler(object): to_define = split_content[0].strip() to_define_lower = to_define.lower() + # Check for presence of non-letters + non_letters = set(to_define_lower) - set(string.ascii_lowercase) + if len(non_letters): + return self.SYMBOLS_PRESENT_ERROR_MESSAGE + # No word was entered. if not to_define_lower: return self.EMPTY_WORD_REQUEST_ERROR_MESSAGE diff --git a/zulip_bots/zulip_bots/bots/define/test_define.py b/zulip_bots/zulip_bots/bots/define/test_define.py index 71b5f6e..614e00c 100755 --- a/zulip_bots/zulip_bots/bots/define/test_define.py +++ b/zulip_bots/zulip_bots/bots/define/test_define.py @@ -58,6 +58,14 @@ class TestDefineBot(BotTestCase): expected_method='send_reply' ) + # Symbols are considered invalid for words + bot_response = "Definitions of words with symbols are not possible." + self.assert_bot_response( + message = {'content': '#'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + # Empty messages are returned with a prompt to reply. No request is sent to the Internet. bot_response = "Please enter a word to define." self.assert_bot_response(