2023-11-03 11:22:38 +01:00
|
|
|
from unittest.mock import MagicMock, patch
|
2022-12-07 23:54:29 +08:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from apps.telegram.renderers.keyboard import Action
|
|
|
|
|
from apps.telegram.updates.update_handlers.button_press import ButtonPressHandler
|
|
|
|
|
|
|
|
|
|
|
2023-11-03 11:22:38 +01:00
|
|
|
@patch(
|
|
|
|
|
"apps.telegram.updates.update_handlers.button_press.ButtonPressHandler._get_alert_group_from_message",
|
|
|
|
|
return_value=None,
|
|
|
|
|
)
|
2022-12-07 23:54:29 +08:00
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_get_action_context(
|
2023-11-03 11:22:38 +01:00
|
|
|
mocked_get_alert_group_from_message,
|
2022-12-07 23:54:29 +08:00
|
|
|
make_organization_and_user_with_slack_identities,
|
|
|
|
|
make_alert_receive_channel,
|
|
|
|
|
make_alert_group,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Test to ensure that both legacy action_name and action_code format is supported.
|
|
|
|
|
"""
|
|
|
|
|
organization, _, _, _ = make_organization_and_user_with_slack_identities()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(
|
|
|
|
|
organization,
|
|
|
|
|
)
|
|
|
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
|
|
|
|
|
|
handler = ButtonPressHandler(MagicMock())
|
|
|
|
|
|
|
|
|
|
ack_data_with_action_name = f"{alert_group.id}:acknowledge:oncall-uuid{organization.uuid}"
|
|
|
|
|
ack_data_with_action_code = f"{alert_group.id}:0:oncall-uuid{organization.uuid}"
|
|
|
|
|
|
|
|
|
|
unack_data_with_action_name = f"{alert_group.id}:unacknowledge:oncall-uuid{organization.uuid}"
|
|
|
|
|
unack_data_with_action_code = f"{alert_group.id}:1:oncall-uuid{organization.uuid}"
|
|
|
|
|
|
|
|
|
|
resolve_data_with_action_name = f"{alert_group.id}:resolve:oncall-uuid{organization.uuid}"
|
|
|
|
|
resolve_data_with_action_code = f"{alert_group.id}:2:oncall-uuid{organization.uuid}"
|
|
|
|
|
|
|
|
|
|
unresolve_data_with_action_name = f"{alert_group.id}:unresolve:oncall-uuid{organization.uuid}"
|
|
|
|
|
unresolve_data_with_action_code = f"{alert_group.id}:3:oncall-uuid{organization.uuid}"
|
|
|
|
|
|
|
|
|
|
silence_data_with_action_name = f"{alert_group.id}:silence:3600:oncall-uuid{organization.uuid}"
|
|
|
|
|
silence_data_with_action_code = f"{alert_group.id}:4:3600:oncall-uuid{organization.uuid}"
|
|
|
|
|
|
|
|
|
|
unsilence_data_with_action_name = f"{alert_group.id}:unsilence:oncall-uuid{organization.uuid}"
|
|
|
|
|
unsilence_data_with_action_code = f"{alert_group.id}:5:oncall-uuid{organization.uuid}"
|
|
|
|
|
|
|
|
|
|
ACTION_TO_DATA_STR = {
|
|
|
|
|
Action.ACKNOWLEDGE: [ack_data_with_action_name, ack_data_with_action_code],
|
|
|
|
|
Action.UNACKNOWLEDGE: [unack_data_with_action_name, unack_data_with_action_code],
|
|
|
|
|
Action.RESOLVE: [resolve_data_with_action_name, resolve_data_with_action_code],
|
|
|
|
|
Action.UNRESOLVE: [unresolve_data_with_action_name, unresolve_data_with_action_code],
|
|
|
|
|
Action.SILENCE: [silence_data_with_action_name, silence_data_with_action_code],
|
|
|
|
|
Action.UNSILENCE: [unsilence_data_with_action_name, unsilence_data_with_action_code],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for action, data_strings in ACTION_TO_DATA_STR.items():
|
|
|
|
|
for data_str in data_strings:
|
|
|
|
|
action_context = handler._get_action_context(data_str)
|
|
|
|
|
assert action_context.action.value == action.value
|
2024-02-01 14:55:35 -03:00
|
|
|
|
|
|
|
|
# not existing alert group
|
|
|
|
|
data_strings = [
|
|
|
|
|
f"1234:acknowledge:oncall-uuid{organization.uuid}",
|
|
|
|
|
f"1234:5:oncall-uuid{organization.uuid}",
|
|
|
|
|
]
|
|
|
|
|
for data_str in data_strings:
|
|
|
|
|
action_context = handler._get_action_context(data_str)
|
|
|
|
|
assert action_context is None
|