Пример #1
0
    def test_listen_to(self):
        pattern = "test_regexp"

        def original_function(self, message):
            pass

        wrapped_function = listen_to(pattern, re.IGNORECASE)(original_function)

        assert isinstance(wrapped_function, MessageFunction)
        # Verify that both the regexp and its flags are correct
        assert wrapped_function.matcher == re.compile(pattern, re.IGNORECASE)
        assert wrapped_function.function == original_function
Пример #2
0
    def test_direct_only(self):
        wrapped = mock.create_autospec(example_listener)
        wrapped.__qualname__ = "wrapped"
        f = listen_to("", direct_only=True)(wrapped)

        # A mention is not a direct message, so shouldn't trigger
        f(
            create_message(mentions=["qmw86q7qsjriura9jos75i4why"],
                           channel_type="O"))
        wrapped.assert_not_called()

        f(create_message(mentions=[], channel_type="D"))
        wrapped.assert_called_once()
Пример #3
0
    def test_needs_mention(self):  # noqa
        wrapped = mock.create_autospec(example_listener)
        wrapped.__qualname__ = "wrapped"
        f = listen_to("", needs_mention=True)(wrapped)
        f.plugin = ExamplePlugin().initialize(Driver())

        # The default message mentions the specified user ID, so should be called
        f(create_message(mentions=["qmw86q7qsjriura9jos75i4why"]))
        wrapped.assert_called_once()
        wrapped.reset_mock()

        # No mention, so the function should still only have been called once in total
        f(create_message(mentions=[]))
        wrapped.assert_not_called()

        # But if this is a direct message, we do want to trigger
        f(create_message(mentions=[], channel_type="D"))
        wrapped.assert_called_once()
Пример #4
0
    def test_allowed_users(self):
        wrapped = mock.create_autospec(example_listener)
        wrapped.__qualname__ = "wrapped"
        # Create a driver with a mocked reply function
        driver = Driver()

        def fake_reply(message, text):
            assert "you do not have permission" in text.lower()

        driver.reply_to = mock.Mock(wraps=fake_reply)

        f = listen_to("", allowed_users=["Betty"])(wrapped)
        f.plugin = ExamplePlugin().initialize(driver)

        # This is fine, the names are not caps sensitive
        f(create_message(sender_name="betty"))
        wrapped.assert_called_once()
        wrapped.reset_mock()

        # This is not fine, and we expect the fake reply to be called.
        f(create_message(sender_name="not_betty"))
        wrapped.assert_not_called()
        driver.reply_to.assert_called_once()