-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log errors from Slack API calls (#209)
- Loading branch information
1 parent
305777f
commit deecd75
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
from spidermon.contrib.actions.slack import SlackMessageManager | ||
|
||
|
||
@pytest.fixture | ||
def logger_error(mocker): | ||
return mocker.patch("spidermon.contrib.actions.slack.logger.error") | ||
|
||
|
||
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_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] |