Example #1
0
def test_event_filter(mocker):
    tester = mocker.Mock()

    @event_filter(lambda e: e['call_me'])
    def my_func(event):
        tester(event)

    assert my_func.__name__ == 'my_func'  # otherwise we're not transferring the function properties correctly

    my_func(Event({'call_me': False}))
    assert tester.called is False

    my_func(Event({'call_me': True}))
    assert tester.call_args == mocker.call({'call_me': True})
Example #2
0
def test_BotModule(mocker):
    tester1 = mocker.Mock()
    tester2 = mocker.Mock()
    tester3 = mocker.Mock()
    tester4 = mocker.Mock()

    class MyBotModule(BotModule):
        some_var = 'make sure we don\'t try to call this'

        @event_filter(lambda e: True)  # pass through all events
        def process_events_1(self, event):
            tester1(event)

        @process_all_events
        def process_events_2(self, event):
            tester2(event)

        def not_a_processor_3(self, event):
            tester3(event)

        def not_a_processor_4(self, event):
            tester4(event)

    my_bot_module = MyBotModule()
    event = {'some': 'message'}
    my_bot_module(Event(event))

    assert tester1.call_args == mocker.call(event)
    assert tester2.call_args == mocker.call(event)
    assert tester3.called == False
    assert tester4.called == False
Example #3
0
def interactive(request):
    payload = json.loads(request.POST['payload'])
    message_logger = logging.getLogger('messages')
    message_logger.info(payload)

    bot(Event(payload))

    return HttpResponse('')
Example #4
0
def test_event_filter_factory(mocker):
    tester = mocker.Mock()

    @event_filter_factory
    def is_color(color):
        def filter_func(event):
            return event['color'] == color

        return filter_func

    assert is_color.__name__ == 'is_color'  # otherwise we're not transferring the function properties correctly

    @is_color('green')
    def my_func(event):
        tester(event)

    assert my_func.__name__ == 'my_func'

    my_func(Event({'color': 'red'}))
    assert tester.called is False

    my_func(Event({'color': 'green'}))
    assert tester.call_args == mocker.call({'color': 'green'})
Example #5
0
def test_greeting_wrong_type(mocker):
    slack = mocker.Mock()
    greeter = GreetingBotModule(slack)
    event = Event({
        "user": "******",
        "type": "message",
        "subtype": "wrong_type",
        "ts": "1557281569.001300",
        "text": "<@U42HCBFEF> has joined the channel",
        "channel": "CHCM2MFHU",
        "event_ts": "1557281569.001300",
        "channel_type": "channel"
    })
    greeter(event)
    assert not slack.chat.post_message.called
Example #6
0
def test_greeting(mocker):
    slack = mocker.Mock()
    greeter = GreetingBotModule(slack)
    GreetingBotModule.GREETING_MESSAGE = 'welcome'
    event = Event({
        "user": "******",
        "type": "message",
        "subtype": "channel_join",
        "ts": "1557281569.001300",
        "text": "<@U42HCBFEF> has joined the channel",
        "channel": "CHCM2MFHU",
        "event_ts": "1557281569.001300",
        "channel_type": "channel"
    })
    greeter(event)
    assert slack.chat_postMessage.call_args == mocker.call(channel='U42HCBFEF',
                                                           text='welcome')
Example #7
0
def hook(request):
    blob = json.loads(request.body)
    message_logger = logging.getLogger('messages')
    message_logger.info(request.body)

    if 'challenge' in blob:
        return HttpResponse(json.loads(request.body)['challenge'])
    else:
        print(blob)
        event = blob['event']
        is_bot = False
        if 'subtype' in event and event['subtype'] == 'bot_message':
            is_bot = True
        if not is_bot:
            # channel = 'CHCM2MFHU'
            bot(Event(event))
            # text = event['text']
            # slack.chat.post_message('#penny-playground', text)
        return HttpResponse('')
Example #8
0
def test_Bot(mocker):
    processor = mocker.Mock()
    bot = Bot(event_processors=[processor])
    bot(Event({'some': 'message'}))
    assert processor.call_args == mocker.call({'some': 'message'})