Exemplo n.º 1
0
    def test_edited_message_disallow(self):
        query = BotQuery()
        query.is_edited = True
        invalid_command = InvalidCommand(query)
        self.assertTrue(invalid_command.skip_edited())

        invalid_command.handle_normal_query = Mock()
        invalid_command.execute()
        self.assertFalse(invalid_command.handle_normal_query.called)
Exemplo n.º 2
0
    def test_trivia_command_callback(self):
        query = BotQuery()
        query.has_callback_query = True
        query.callback_query = BotQuery()
        query.callback_query.callback_data = "payload here/trivia"

        result = CommandParser.parse_command(query)
        self.assertIsInstance(result, TriviaCommand)
        self.assertIs(query, result.query)
Exemplo n.º 3
0
    def test_edited_message_disallow(self):
        query = BotQuery()
        query.is_edited = True
        help_command = HelpCommand(query)
        self.assertTrue(help_command.skip_edited())

        help_command.handle_normal_query = Mock()
        help_command.execute()
        self.assertFalse(help_command.handle_normal_query.called)
Exemplo n.º 4
0
    def test_trivia_callback_query(self):
        query = BotQuery()
        query.has_callback_query = True

        trivia_command = TriviaCommand(query)
        trivia_command.handle_callback_query = Mock()

        trivia_command.execute()
        trivia_command.handle_callback_query.assert_called_once()
Exemplo n.º 5
0
 def test_private_chat(self):
     query = BotQuery()
     query.is_private = True
     query.user_first_name = "Lambda"
     help_command = HelpCommand(query)
     self.assertIs(help_command.query, query)
     
     help_command.handle_normal_query = Mock()
     help_command.execute()
     self.assertTrue(help_command.handle_normal_query.called)
Exemplo n.º 6
0
    def test_edited_message_allow(self):
        query = BotQuery()
        query.is_edited = True
        invalid_command = InvalidCommand(query)
        invalid_command.ignore_edited = False
        self.assertFalse(invalid_command.skip_edited())

        invalid_command.handle_normal_query = Mock()
        invalid_command.execute()
        invalid_command.handle_normal_query.assert_called_once_with(
            "Sorry! I don't understand. Try /help")
Exemplo n.º 7
0
    def test_trivia_callback_query_correct(self):
        query = BotQuery()
        query.has_callback_query = True
        query.callback_query = BotQuery()
        query.callback_query.callback_data = "CC/trivia"
        trivia_command = TriviaCommand(query)

        with patch("commands.trivia_command.requests.post") as mock_post:
            trivia_command.execute()
            response_text = mock_post.call_args[1]["json"]["text"]
            self.assertTrue(response_text.endswith("*C* is correct!"))
Exemplo n.º 8
0
    def test_edited_message_allow(self):
        query = BotQuery()
        query.is_edited = True
        start_command = StartCommand(query)
        start_command.ignore_edited = False

        self.assertFalse(start_command.skip_edited())
        start_command.handle_normal_query = Mock()
        start_command.execute()
        start_command.handle_normal_query.assert_called_once_with(
            "Greetings from the DAS bot!")
Exemplo n.º 9
0
    def test_private_chat(self):
        query = BotQuery()
        query.is_private = True
        query.user_first_name = "Lambda"
        start_command = StartCommand(query)
        self.assertIs(start_command.query, query)

        start_command.handle_normal_query = Mock()
        start_command.execute()
        start_command.handle_normal_query.assert_called_once_with(
            "Hello Lambda! Welcome to the DAS bot!")
Exemplo n.º 10
0
    def test_group_chat(self):
        query = BotQuery()
        query.is_group = True
        query.user_first_name = "Foobar"
        query.chat_title = "Anonymous"
        help_command = HelpCommand(query)
        self.assertIs(help_command.query, query)

        help_command.handle_normal_query = Mock()
        help_command.execute()
        self.assertTrue(help_command.handle_normal_query.called)
Exemplo n.º 11
0
    def test_group_chat(self):
        query = BotQuery()
        query.is_group = True
        query.user_first_name = "Foobar"
        query.chat_title = "Anonymous"
        start_command = StartCommand(query)
        self.assertIs(start_command.query, query)

        start_command.handle_normal_query = Mock()
        start_command.execute()
        start_command.handle_normal_query.assert_called_once_with(
            "Hello Anonymous! I am the DAS Bot, Foobar has summoned me here!")
Exemplo n.º 12
0
def handle_normal_query(data):
    msg = data["message"]

    if msg.get("via_bot") and msg["via_bot"]["username"] == BOT_USERNAME:
        handle_bot_response(msg)
        return

    try:
        query = BotQuery.parse_event(data)
    except Exception as err:
        logger.error(f"Unsupported query: {err}")
        return

    msg = query.message
    if msg.startswith("/start"):
        send_message(
            query.chat_id,
            f"Hello! Anibot at your service! You can search by typing @{BOT_USERNAME}... or type '/' to see all the available commands."
        )
    elif msg.startswith("/debug"):
        return
        # send_message(query.chat_id, vars(query))
    elif msg.startswith("/login"):
        handle_login_command(query)
    elif msg.startswith("/logout"):
        handle_logout_command(query)
    elif msg.startswith("/watch"):
        handle_watch_command(query)
    elif msg.startswith("/read"):
        handle_read_command(query)
Exemplo n.º 13
0
def handler(event, context):
    data = event["body"]

    logger.info(f"Received event with content: {data}; context: {context}")

    body = {"request": data}

    try:
        query = BotQuery.parse_event(data)
    except Exception as err:
        logger.error(f"Unsupported query: {err}")
        return

    # Inline query
    if query.is_inline_query:
        handle_inline_query(query)
    elif query.has_callback_query:
        handle_callback_query(query)
    elif query.message:  # Normal query
        handle_normal_query(data)
    else:
        logger.warning("Received unknown request, skipping...")

    # Remove
    return {"statusCode": 200, "body": json.dumps(body)}
Exemplo n.º 14
0
 def test_top_given_year(self):
     query = BotQuery()
     trends_command = TrendsCommand(query, ["/trends", "tOp", "2011"])
     with patch("commands.trends_command.TrendsCommand.execute_top_searches"
                ) as mock_execute_top_searches:
         trends_command.execute()
         mock_execute_top_searches.assert_called_once_with("2011")
Exemplo n.º 15
0
 def test_graph_bad_unknown_mode(self):
     query = BotQuery()
     trends_command = TrendsCommand(query, ["/trends", "mymode"])
     with patch("commands.trends_command.TrendsCommand.handle_normal_query"
                ) as mock_handle_normal_query:
         trends_command.execute()
         mock_handle_normal_query.assert_called_once_with(
             "Unable to fetch the trends: Unknown mode 'mymode'")
Exemplo n.º 16
0
 def test_graph_bad_no_mode(self):
     query = BotQuery()
     trends_command = TrendsCommand(query, ["/trends"])
     with patch("commands.trends_command.TrendsCommand.handle_normal_query"
                ) as mock_handle_normal_query:
         trends_command.execute()
         mock_handle_normal_query.assert_called_once_with(
             "Unable to fetch the trends: No mode provided. Use '/trends help' for more info"
         )
Exemplo n.º 17
0
 def test_trending_location_1(self):
     query = BotQuery()
     trends_command = TrendsCommand(query,
                                    ["/trends", "trending", "MEXICO"])
     with patch(
             "commands.trends_command.TrendsCommand.execute_trending_searches"
     ) as mock_execute_trending_searches:
         trends_command.execute()
         mock_execute_trending_searches.assert_called_once_with("mexico")
Exemplo n.º 18
0
 def test_trending_default(self):
     query = BotQuery()
     trends_command = TrendsCommand(query, ["/trends", "trending"])
     with patch(
             "commands.trends_command.TrendsCommand.execute_trending_searches"
     ) as mock_execute_trending_searches:
         trends_command.execute()
         mock_execute_trending_searches.assert_called_once_with(
             "united_states")
Exemplo n.º 19
0
 def test_graph_bad_graph_no_keywords(self):
     query = BotQuery()
     trends_command = TrendsCommand(query, ["/trends", "graph"])
     with patch("commands.trends_command.TrendsCommand.handle_normal_query"
                ) as mock_handle_normal_query:
         trends_command.execute()
         mock_handle_normal_query.assert_called_once_with(
             "Unable to fetch the trends: No keywords provided for graphing"
         )
Exemplo n.º 20
0
 def test_graph_good_default(self):
     query = BotQuery()
     trends_command = TrendsCommand(
         query, ["/trends", "graPH", "subject1", "subject2"])
     with patch("commands.trends_command.TrendsCommand.execute_graph"
                ) as mock_execute_graph:
         trends_command.execute()
         mock_execute_graph.assert_called_once_with(
             {"keywords": ["subject1", "subject2"]})
Exemplo n.º 21
0
 def test_get_help_text(self, mock_get_help_text):
     mock_get_help_text.return_value = "halp"
     query = BotQuery()
     trends_command = TrendsCommand(
         query, ["/trends", "help", "other", "useless", "arg"])
     with patch("commands.trends_command.TrendsCommand.handle_normal_query"
                ) as mock_handle_normal_query:
         trends_command.execute()
         mock_handle_normal_query.assert_called_once_with("halp")
Exemplo n.º 22
0
    def test_trivia_help_text(self):
        query = BotQuery()
        trivia_command = TriviaCommand(query, ["/trivia", "help"])

        with patch("trivia.Trivia.get_help_text", return_value="help text"):
            trivia_command.handle_normal_query = Mock()
            trivia_command.execute()
            trivia_command.handle_normal_query.assert_called_once_with(
                "help text")
Exemplo n.º 23
0
 def test_fail_not_ok(self):
     query = BotQuery()
     joke_command = JokeCommand(query)
     joke_command.handle_normal_query = Mock()
     with patch("commands.joke_command.requests.get") as mock_get:
         mock_get.return_value.ok = False
         joke_command.execute()
         joke_command.handle_normal_query.assert_called_once_with(
             "Oops! Can't fetch joke right now.")
Exemplo n.º 24
0
 def test_graph_bad_no_time_argument(self):
     query = BotQuery()
     trends_command = TrendsCommand(query,
                                    ["/trends", "graph", "github", "-t"])
     with patch("commands.trends_command.TrendsCommand.handle_normal_query"
                ) as mock_handle_normal_query:
         trends_command.execute()
         mock_handle_normal_query.assert_called_once_with(
             "Unable to fetch the trends: No argument provided for option '-t'"
         )
Exemplo n.º 25
0
    def test_trivia_fetch_question_fail_unhandled_error(self, _):
        query = BotQuery()
        trivia_command = TriviaCommand(query, ["/trivia"])

        with patch("trivia.Trivia.fetch_question",
                   side_effect=Exception("trivia error")):
            trivia_command.handle_normal_query = Mock()
            trivia_command.execute()
            trivia_command.handle_normal_query.assert_called_once_with(
                "Failed to fetch trivia question!")
Exemplo n.º 26
0
 def test_trending_location_2(self):
     query = BotQuery()
     trends_command = TrendsCommand(query,
                                    ["/trends", "trenDing", "South Korea"])
     with patch(
             "commands.trends_command.TrendsCommand.execute_trending_searches"
     ) as mock_execute_trending_searches:
         trends_command.execute()
         mock_execute_trending_searches.assert_called_once_with(
             "south_korea")
Exemplo n.º 27
0
    def test_successful_request(self):
        query = BotQuery()
        joke_command = JokeCommand(query)
        joke_command.handle_normal_query = Mock()

        with patch("commands.joke_command.requests.get") as mock_get:
            mock_get.return_value.ok = True
            mock_get.return_value.content = b"Dad joke here!"
            joke_command.execute()
            joke_command.handle_normal_query.assert_called_once_with(
                "Dad joke here!")
Exemplo n.º 28
0
def trigger(event_raw: dict, context):
    try:
        bot_query = BotQuery.parse_event(event_raw.get("body"))
        bot_command = CommandParser.parse_command(bot_query)
        bot_command.execute()
    except BotError as err:
        print(f"Caught checked exception:\n\t{err}")
    except Exception as err:
        print(f"Caught unchecked exception:\n\t{err}")
    finally:
        return {"statusCode": 200}
Exemplo n.º 29
0
 def test_fail_not_ok(self):
     query = BotQuery()
     fact_command = FactCommand(query)
     fact_command.handle_normal_query = Mock()
     with patch("commands.fact_command.requests.get") as mock_get:
         with patch("commands.fact_command.print") as mock_print:
             mock_get.return_value.ok = False
             fact_command.execute()
             fact_command.handle_normal_query.assert_called_once_with(
                 "Oops! Can't fetch fact right now.")
             self.assertFalse(mock_print.called)
Exemplo n.º 30
0
 def test_fail_exception(self):
     query = BotQuery()
     fact_command = FactCommand(query)
     fact_command.handle_normal_query = Mock()
     with patch("commands.fact_command.requests.get",
                side_effect=Exception("Fact server is down")) as mock_get:
         with patch("commands.fact_command.print") as mock_print:
             fact_command.execute()
             self.assertTrue(mock_print.called)
             fact_command.handle_normal_query.assert_called_once_with(
                 "Oops! Can't fetch fact right now.")