Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add environment to data refresh slack alert username (#477)

Madison Swain-Bowden59f9092bde55
diff --git a/ingestion_server/ingestion_server/slack.py b/ingestion_server/ingestion_server/slack.py
index d03470f72..8789e24a7 100644
--- a/ingestion_server/ingestion_server/slack.py
+++ b/ingestion_server/ingestion_server/slack.py
@@ -2,6 +2,7 @@ import logging
import os
import requests
+from decouple import config
log = logging.getLogger(__name__)
@@ -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:
diff --git a/ingestion_server/test/unit_tests/test_slack.py b/ingestion_server/test/unit_tests/test_slack.py
new file mode 100644
index 000000000..15b280f83
--- /dev/null
+++ b/ingestion_server/test/unit_tests/test_slack.py
@@ -0,0 +1,59 @@
+from unittest import mock
+
+import pytest
+
+from ingestion_server import slack
+
+
+ "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"),
+ ],
+)
+ "webhook, should_alert",
+ [
+ # Actual webhook supplied
+ ("http://fake", True),
+ # No webhook supplied
+ ("", False),
+ ],
+)
+ "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())