def test_do_not_log_text_when_fake_is_not_set(logger_info): text_not_to_be_logged = "text not to be logged" manager = SlackMessageManager(sender_token="anything", sender_name="@someone", fake=False) manager.send_message(to=[], text=text_not_to_be_logged) assert logger_info.call_count == 0
def test_log_text_when_fake_set(logger_info): text_to_be_logged = "text to be logged" manager = SlackMessageManager(sender_token="anything", sender_name="someone", fake=True) manager.send_message(to=["someone"], text=text_to_be_logged) assert logger_info.call_count == 1 assert text_to_be_logged in logger_info.call_args[0]
def test_get_invalid_permissions_icon_url(mock_webclient): class FakeResponse: data = {"error": "missing_scope", "needed": "users:read"} fake_error = SlackApiError("message", FakeResponse()) mock_webclient().users_list.side_effect = fake_error manager = SlackMessageManager(sender_token="Fake", sender_name="test_invalid_bot") url = manager._get_icon_url() assert url is None
def test_api_call_with_no_error_should_not_log_messages(mocker, logger_error): api_valid_message = {"ok": True, "text": "valid response"} mocker.patch( "spidermon.contrib.actions.slack.SlackClient.api_call", return_value=api_valid_message, ) manager = SlackMessageManager("SENDER_TOKEN", "SENDER_NAME") manager._api_call("some_method") assert logger_error.call_count == 0
def test_get_invalid_user_icon_url(mock_webclient): mock_webclient().users_list.return_value = { "members": [{ "name": "test_valid_user", "profile": { "image_48": "fake.jpg" } }] } manager = SlackMessageManager(sender_token="Fake", sender_name="test_invalid_user") url = manager._get_icon_url() assert url is None
def test_log_text_and_attachment_when_fake_set(logger_info): text_to_be_logged = "text to be logged" attach_to_be_logged = "attachment content" manager = SlackMessageManager(sender_token="anything", sender_name="@someone", fake=True) manager.send_message(to=[], text=text_to_be_logged, attachments=attach_to_be_logged) assert logger_info.call_count == 2 assert text_to_be_logged in logger_info.call_args_list[0][0] assert attach_to_be_logged in logger_info.call_args_list[1][0]
def test_get_invalid_unknown_slack_error_icon_url(mock_webclient): class FakeResponse: data = {"error": "unknown", "needed": "unknown"} fake_error = SlackApiError("mocked error", FakeResponse()) mock_webclient().users_list.side_effect = fake_error manager = SlackMessageManager(sender_token="Fake", sender_name="test_invalid_bot") with pytest.raises(SlackApiError) as excinfo: manager._get_icon_url() assert excinfo.value.response.data == { "error": "unknown", "needed": "unknown" }
def test_api_call_with_error_should_log_error_msg(mocker, logger_error): error_msg = "API call failure" # API call response with error (see https://api.slack.com/rtm#errors) api_error_message = { "ok": False, "reply_to": 1, "error": { "code": 2, "msg": error_msg }, } mocker.patch( "spidermon.contrib.actions.slack.SlackClient.api_call", return_value=api_error_message, ) manager = SlackMessageManager("SENDER_TOKEN", "SENDER_NAME") manager._api_call("some_method") assert logger_error.call_count == 1 assert error_msg in logger_error.call_args_list[0][0]