define bot: add check & test to avoid sending words with non-letters.

This commit is contained in:
neiljp (Neil Pilgrim) 2017-09-19 13:43:08 -07:00 committed by Tim Abbott
parent f862cf2222
commit c33ac65ac9
2 changed files with 15 additions and 0 deletions

View file

@ -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

View file

@ -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(