# What this PR does - Rename `SlackClientWithErrorHandling` to just `SlackClient` - Add more error classes + improve the way errors are raised based on the Slack error code - Add API call retries on Slack server errors (e.g. when Slack returns `5xx` errors) - Refactor some methods working with Slack API + add tests ## Which issue(s) this PR fixes - https://github.com/grafana/oncall-private/issues/1837 - https://github.com/grafana/oncall-private/issues/1840 - https://github.com/grafana/oncall-private/issues/1842 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required)
68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
from datetime import timedelta
|
|
|
|
import pytest
|
|
from django.utils import timezone
|
|
|
|
from apps.slack.client import SlackClient
|
|
from apps.slack.scenarios.distribute_alerts import AlertShootingStep
|
|
|
|
|
|
@pytest.fixture()
|
|
def mock_slack_api_call(monkeypatch):
|
|
def _mock_api_call(*args, **kwargs):
|
|
return {
|
|
"status": 200,
|
|
"user": {
|
|
"profile": {"image_512": "TEST_SLACK_IMAGE_URL"},
|
|
"name": "TEST_SLACK_LOGIN",
|
|
"real_name": "TEST_SLACK_NAME",
|
|
},
|
|
"team": {"name": "TEST_SLACK_TEAM_NAME"},
|
|
}
|
|
|
|
monkeypatch.setattr(SlackClient, "api_call", _mock_api_call)
|
|
|
|
|
|
@pytest.fixture()
|
|
def make_resolved_ack_new_silenced_alert_groups(make_alert_group, make_alert_receive_channel, make_alert):
|
|
def _make_alert_groups_all_statuses(alert_receive_channel, channel_filter, alert_raw_request_data, **kwargs):
|
|
resolved_alert_group = make_alert_group(
|
|
alert_receive_channel,
|
|
channel_filter=channel_filter,
|
|
acknowledged_at=timezone.now() + timedelta(hours=1),
|
|
resolved_at=timezone.now() + timedelta(hours=2),
|
|
resolved=True,
|
|
acknowledged=True,
|
|
)
|
|
make_alert(alert_group=resolved_alert_group, raw_request_data=alert_raw_request_data)
|
|
|
|
ack_alert_group = make_alert_group(
|
|
alert_receive_channel,
|
|
channel_filter=channel_filter,
|
|
acknowledged_at=timezone.now() + timedelta(hours=1),
|
|
acknowledged=True,
|
|
)
|
|
make_alert(alert_group=ack_alert_group, raw_request_data=alert_raw_request_data)
|
|
|
|
new_alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)
|
|
make_alert(alert_group=new_alert_group, raw_request_data=alert_raw_request_data)
|
|
|
|
silenced_alert_group = make_alert_group(
|
|
alert_receive_channel,
|
|
channel_filter=channel_filter,
|
|
silenced=True,
|
|
silenced_at=timezone.now() + timedelta(hours=1),
|
|
)
|
|
make_alert(alert_group=silenced_alert_group, raw_request_data=alert_raw_request_data)
|
|
|
|
return resolved_alert_group, ack_alert_group, new_alert_group, silenced_alert_group
|
|
|
|
return _make_alert_groups_all_statuses
|
|
|
|
|
|
@pytest.fixture()
|
|
def mock_alert_shooting_step_post_alert_group_to_slack(monkeypatch):
|
|
def mock_post_alert_group_to_slack(*args, **kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(AlertShootingStep, "_post_alert_group_to_slack", mock_post_alert_group_to_slack)
|