Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add environment to data refresh slack alert username #477

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ingestion_server/ingestion_server/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

import requests
from decouple import config


log = logging.getLogger(__name__)
Expand All @@ -25,10 +26,12 @@ def message(text: str, summary: str = None) -> None:
else:
summary = text

environment = config("ENVIRONMENT", default="local")

data = {
"blocks": [{"text": {"text": text, "type": "mrkdwn"}, "type": "section"}],
"text": summary,
"username": "Data Refresh Notification",
"username": f"Data Refresh Notification | {environment.upper()}",
"icon_emoji": "arrows_counterclockwise",
}
try:
Expand Down
59 changes: 59 additions & 0 deletions ingestion_server/test/unit_tests/test_slack.py
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())