Exemplo n.º 1
0
def test_get_returns_none_when_not_found():
    # Given
    mock_slack_event = {}
    sut = SlackRequest(python_slackclient=None, slack_event=mock_slack_event)

    # When
    actual_type = sut.get("foo")

    # Then
    assert None is actual_type
Exemplo n.º 2
0
def test_write_calls_chat_postMessage_when_channel_is_found():
    # Given
    mock_content = "foo"
    mock_channel = "bar"
    mock_slack_event = {}

    mock_python_slackclient = tests.common.mocks.MockPythonSlackclient()
    sut = SlackRequest(python_slackclient=mock_python_slackclient, slack_event=mock_slack_event)

    # When
    sut.write(mock_content, mock_channel)
Exemplo n.º 3
0
def test_channel_calls_get_with_channel_parameter():
    # Given
    mock_get = MockGet()
    sut = SlackRequest(python_slackclient=None, slack_event=None)
    sut.get = mock_get.mock_get

    # When
    sut.channel

    # Then
    assert True is mock_get.has_mock_get_been_called
    assert "channel" == mock_get.key
Exemplo n.º 4
0
def test_subtype_calls_get_with_subtype_parameter():
    # Given
    mock_get = MockGet()
    sut = SlackRequest(python_slackclient=None, slack_event=None)
    sut.get = mock_get.mock_get

    # When
    sut.subtype

    # Then
    assert True is mock_get.has_mock_get_been_called
    assert "subtype" == mock_get.key
Exemplo n.º 5
0
def test_get_returns_value_when_stored():
    # Given
    key = "foo"
    expected_value = "bar"
    mock_slack_event = {key: expected_value}
    sut = SlackRequest(python_slackclient=None, slack_event=mock_slack_event)

    # When
    actual_value = sut.get(key)

    # Then
    assert expected_value == actual_value
Exemplo n.º 6
0
def test_write_which_raises_exception_will_reraise_exception():
    # Given
    mock_content = "foo"
    mock_channel = "bar"
    mock_slack_event = {}

    mock_python_slackclient = tests.common.mocks.MockPythonSlackclient(
        injectable_chat_postMessage_exception=Exception("mock exception")
    )
    sut = SlackRequest(python_slackclient=mock_python_slackclient, slack_event=mock_slack_event)

    # When
    sut.write(mock_content, mock_channel)
Exemplo n.º 7
0
def test_write_raises_exception_if_no_channel_found():
    # Given
    mock_content = "foo"
    mock_slack_event = {}

    mock_python_slackclient = tests.common.mocks.MockPythonSlackclient()
    sut = SlackRequest(python_slackclient=mock_python_slackclient, slack_event=mock_slack_event)

    # When
    try:
        sut.write(mock_content, None)
        pytest.fail("Exception should be thrown as channel cannot be determined")
    except Exception:
        pass  # expected
Exemplo n.º 8
0
def test_message_returns_none_if_not_found():
    # Given
    mock_slack_event = {}
    sut = SlackRequest(python_slackclient=None, slack_event=mock_slack_event)

    # When
    message = sut.message

    # Then
    assert None is message
Exemplo n.º 9
0
def test_message_returns_value_if_found():
    # Given
    mock_slack_event = {"text": "foo"}
    sut = SlackRequest(python_slackclient=None, slack_event=mock_slack_event)

    # When
    message = sut.message

    # Then
    assert "foo" == message
Exemplo n.º 10
0
def test_thread_ts_returns_empty_string_if_not_found():
    # Given
    mock_slack_event = {}
    sut = SlackRequest(python_slackclient=None, slack_event=mock_slack_event)

    # When
    thread_ts = sut.thread_ts

    # Then
    assert "" == thread_ts
Exemplo n.º 11
0
def test_thread_ts_returns_value_if_found():
    # Given
    mock_slack_event = {"thread_ts": "foo"}
    sut = SlackRequest(python_slackclient=None, slack_event=mock_slack_event)

    # When
    sut.thread_ts

    # Then
    assert "foo" == sut.thread_ts
Exemplo n.º 12
0
def test_slack_request_initializer_stores_python_slackclient_and_slack_event():
    # Given
    mock_python_slack_client = 42
    mock_slack_event = 1337

    # When
    sut = SlackRequest(python_slackclient=mock_python_slack_client, slack_event=mock_slack_event)

    # Then
    assert mock_python_slack_client == sut._python_slackclient
    assert mock_slack_event == sut.slack_event
Exemplo n.º 13
0
def roll_callback(request: SlackRequest):
    """This function is called every time a message is sent to a channel out Bot is in
    :param request: the SlackRequest we receive along with the event. See the README.md for full documentation
    """
    if request.message.lower().startswith("roll"):
        dice_num_and_value = request.message.lower().split("roll")[1]

        try:
            dice_num = int(dice_num_and_value.split("d")[0])
            dice_value = int(dice_num_and_value.split("d")[1])
        except ValueError:
            request.write("Invalid format detected. Please call this bot using roll [int]d[int]")
            return

        total = 0
        rolls = []

        if dice_num > 25:
            request.write("We will not process more than 25 dice rolls.")
            return

        for _ in range(dice_num):
            roll = random.randint(1, dice_value)
            total += roll
            rolls.append(roll)

        request.write(f"Rolled {dice_num_and_value} and got {str(total)} ({rolls})")
Exemplo n.º 14
0
def test_str_returns_json_representation_of_slack_event():
    class MockSlackEvent:
        def __init__(self, json):
            self.json = json

    # Given
    expected_str = "foo"
    mock_slack_event = MockSlackEvent(expected_str)
    sut = SlackRequest(python_slackclient=None, slack_event=mock_slack_event)

    # When
    actual_str = str(sut)

    # Then
    assert expected_str == actual_str
def test_route_request_to_callbacks_routes_correct_type_to_correct_callback():
    # Given
    class Monitor:
        was_called = False

        def monitor_if_called(self, request):
            Monitor.was_called = True

    mockslack_event = SlackEvent({"type": "message"})
    mock_slackrequest = SlackRequest(python_slackclient=None,
                                     slack_event=mockslack_event)
    sut = SimpleSlackBot(slack_bot_token="Mock slack bot token")
    monitor = Monitor()
    sut._registrations = {}
    sut._registrations["message"] = [monitor.monitor_if_called]

    # When
    sut.route_request_to_callbacks(mock_slackrequest)

    # Then
    assert Monitor.was_called is True