-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #968 from CodeForPhilly/lebovits/issu848-report-ba…
…ckend-errors-to-slack Lebovits/issu848 report backend errors to slack
- Loading branch information
Showing
3 changed files
with
175 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
from slack_sdk import WebClient | ||
|
||
|
||
def send_error_to_slack(error_message: str) -> None: | ||
"""Send error message to Slack.""" | ||
token: str | None = os.getenv("CAGP_SLACK_API_TOKEN") # token can be None | ||
if token: | ||
client = WebClient(token=token) | ||
client.chat_postMessage( | ||
channel="clean-and-green-philly-back-end", # Replace with actual Slack channel ID | ||
text=error_message, | ||
username="Backend Error Reporter", | ||
) | ||
else: | ||
raise ValueError("Slack API token not found in environment variables.") |
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,54 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
import sys | ||
import os | ||
|
||
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") | ||
|
||
from classes.slack_error_reporter import ( | ||
send_error_to_slack, | ||
) # Ensure correct file import | ||
|
||
|
||
class TestSlackNotifier(unittest.TestCase): | ||
@patch( | ||
"classes.slack_error_reporter.WebClient.chat_postMessage" | ||
) # Correct patching | ||
@patch( | ||
"classes.slack_error_reporter.os.getenv", return_value="mock_slack_token" | ||
) # Correct patching | ||
def test_send_error_to_slack(self, mock_getenv, mock_slack_post): | ||
"""Test that Slack error reporting is triggered correctly.""" | ||
|
||
error_message = "Test error message" | ||
|
||
# Call the Slack notification function | ||
send_error_to_slack(error_message) | ||
|
||
# Verify the Slack API call was made with the correct parameters | ||
mock_slack_post.assert_called_once_with( | ||
channel="clean-and-green-philly-back-end", # Use actual channel ID | ||
text=error_message, | ||
username="Backend Error Reporter", | ||
) | ||
|
||
@patch( | ||
"classes.slack_error_reporter.WebClient.chat_postMessage" | ||
) # Correct patching | ||
@patch( | ||
"classes.slack_error_reporter.os.getenv", return_value=None | ||
) # Simulate missing Slack token | ||
def test_no_error_no_slack_message(self, mock_getenv, mock_slack_post): | ||
"""Test that Slack notification is not triggered if there's no error.""" | ||
|
||
# Call the Slack notification function (with no valid token) | ||
with self.assertRaises(ValueError): | ||
send_error_to_slack("Test error message") | ||
|
||
# Ensure Slack's chat_postMessage was not called due to missing token | ||
mock_slack_post.assert_not_called() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |