This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add environment to data refresh slack alert username (#477)
* Add environment to slack notification username * Add tests for slack module
- Loading branch information
1 parent
01a3330
commit 59f9092
Showing
2 changed files
with
63 additions
and
1 deletion.
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,59 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from ingestion_server import slack | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text, summary, expected_summary", | ||
[ | ||
# Short text with default summary | ||
("sample text", None, "sample text"), | ||
# Short text with explicit summary | ||
("sample text", "different summary", "different summary"), | ||
# Multi-line text with default summary | ||
("sample text\nLook a new line!", None, "Ingestion server message"), | ||
# Multi-line text with explicit summary | ||
("sample text\nLook a new line!", "different summary", "different summary"), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"webhook, should_alert", | ||
[ | ||
# Actual webhook supplied | ||
("http://fake", True), | ||
# No webhook supplied | ||
("", False), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"environment", | ||
[ | ||
# Default environment | ||
None, | ||
# Different, explicit environment | ||
"staging", | ||
], | ||
) | ||
def test_message( | ||
text, | ||
summary, | ||
webhook, | ||
should_alert, | ||
expected_summary, | ||
environment, | ||
monkeypatch, | ||
): | ||
monkeypatch.setenv("ENVIRONMENT", environment) | ||
monkeypatch.setenv(slack.SLACK_WEBHOOK, webhook) | ||
with mock.patch("requests.post") as mock_post: | ||
slack.message(text, summary) | ||
assert mock_post.called == should_alert | ||
if not should_alert: | ||
return | ||
data = mock_post.call_args.kwargs["json"] | ||
assert data["blocks"][0]["text"]["text"] == text | ||
assert data["text"] == expected_summary | ||
if environment: | ||
assert data["username"].endswith(environment.upper()) |