2022-06-03 08:09:47 -06:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from telegram import InlineKeyboardButton
|
|
|
|
|
|
|
|
|
|
from apps.alerts.models import AlertReceiveChannel
|
|
|
|
|
from apps.telegram.renderers.keyboard import TelegramKeyboardRenderer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def are_buttons_equal(button: InlineKeyboardButton, other: InlineKeyboardButton) -> bool:
|
|
|
|
|
return button.text == other.text and button.callback_data == other.callback_data and button.url == other.url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def are_keyboards_equal(keyboard: List[List[InlineKeyboardButton]], other: List[List[InlineKeyboardButton]]) -> bool:
|
|
|
|
|
if len(keyboard) != len(other):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
for i in range(len(keyboard)):
|
|
|
|
|
row = keyboard[i]
|
|
|
|
|
other_row = other[i]
|
|
|
|
|
|
|
|
|
|
if len(row) != len(other_row):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
for j in range(len(row)):
|
|
|
|
|
button = row[j]
|
|
|
|
|
other_button = other_row[j]
|
|
|
|
|
|
|
|
|
|
if not are_buttons_equal(button, other_button):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2023-04-27 10:52:35 -04:00
|
|
|
def generate_silence_buttons(alert_group, organization) -> List:
|
|
|
|
|
return [
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="🔕 forever",
|
|
|
|
|
callback_data=f"{alert_group.pk}:4:oncall-uuid{organization.uuid}",
|
|
|
|
|
),
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="... for 1h",
|
|
|
|
|
callback_data=f"{alert_group.pk}:4:3600:oncall-uuid{organization.uuid}",
|
|
|
|
|
),
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="... for 4h",
|
|
|
|
|
callback_data=f"{alert_group.pk}:4:14400:oncall-uuid{organization.uuid}",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_actions_keyboard_alerting(make_organization, make_alert_receive_channel, make_alert_group, make_alert):
|
|
|
|
|
organization = make_organization()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(
|
|
|
|
|
organization, integration=AlertReceiveChannel.INTEGRATION_GRAFANA, verbal_name="Test integration"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
|
make_alert(alert_group=alert_group, raw_request_data=alert_receive_channel.config.example_payload)
|
|
|
|
|
|
|
|
|
|
renderer = TelegramKeyboardRenderer(alert_group=alert_group)
|
|
|
|
|
keyboard = renderer.render_actions_keyboard()
|
|
|
|
|
|
|
|
|
|
expected_keyboard = [
|
|
|
|
|
[
|
2022-10-25 14:53:07 +08:00
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Acknowledge",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:0:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Resolve",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:2:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
2023-04-27 10:52:35 -04:00
|
|
|
generate_silence_buttons(alert_group, organization),
|
2022-06-03 08:09:47 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert are_keyboards_equal(keyboard.inline_keyboard, expected_keyboard) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_actions_keyboard_acknowledged(
|
|
|
|
|
make_organization_and_user, make_alert_receive_channel, make_alert_group, make_alert
|
|
|
|
|
):
|
|
|
|
|
organization, user = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(
|
|
|
|
|
organization, integration=AlertReceiveChannel.INTEGRATION_GRAFANA, verbal_name="Test integration"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
|
make_alert(alert_group=alert_group, raw_request_data=alert_receive_channel.config.example_payload)
|
2024-03-27 13:37:01 +01:00
|
|
|
alert_group.acknowledge_by_user_or_backsync(user)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
renderer = TelegramKeyboardRenderer(alert_group=alert_group)
|
|
|
|
|
keyboard = renderer.render_actions_keyboard()
|
|
|
|
|
|
|
|
|
|
expected_keyboard = [
|
2022-10-25 14:53:07 +08:00
|
|
|
[
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Unacknowledge",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:1:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Resolve",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:2:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
2023-04-27 10:52:35 -04:00
|
|
|
generate_silence_buttons(alert_group, organization),
|
2022-06-03 08:09:47 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert are_keyboards_equal(keyboard.inline_keyboard, expected_keyboard) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_actions_keyboard_resolved(
|
|
|
|
|
make_organization_and_user, make_alert_receive_channel, make_alert_group, make_alert
|
|
|
|
|
):
|
|
|
|
|
organization, user = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(
|
|
|
|
|
organization, integration=AlertReceiveChannel.INTEGRATION_GRAFANA, verbal_name="Test integration"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
|
make_alert(alert_group=alert_group, raw_request_data=alert_receive_channel.config.example_payload)
|
2024-03-27 13:37:01 +01:00
|
|
|
alert_group.resolve_by_user_or_backsync(user)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
renderer = TelegramKeyboardRenderer(alert_group=alert_group)
|
|
|
|
|
keyboard = renderer.render_actions_keyboard()
|
|
|
|
|
|
|
|
|
|
expected_keyboard = [
|
2022-10-25 14:53:07 +08:00
|
|
|
[
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Unresolve",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:3:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
2022-06-03 08:09:47 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert are_keyboards_equal(keyboard.inline_keyboard, expected_keyboard) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_actions_keyboard_silenced(
|
|
|
|
|
make_organization_and_user, make_alert_receive_channel, make_alert_group, make_alert
|
|
|
|
|
):
|
|
|
|
|
organization, user = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(
|
|
|
|
|
organization, integration=AlertReceiveChannel.INTEGRATION_GRAFANA, verbal_name="Test integration"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
|
make_alert(alert_group=alert_group, raw_request_data=alert_receive_channel.config.example_payload)
|
2024-03-27 13:37:01 +01:00
|
|
|
alert_group.silence_by_user_or_backsync(user, silence_delay=None)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
renderer = TelegramKeyboardRenderer(alert_group=alert_group)
|
|
|
|
|
keyboard = renderer.render_actions_keyboard()
|
|
|
|
|
|
|
|
|
|
expected_keyboard = [
|
2022-10-25 14:53:07 +08:00
|
|
|
[
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Acknowledge",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:0:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Resolve",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:2:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
InlineKeyboardButton(
|
|
|
|
|
text="Unsilence",
|
2022-12-07 23:54:29 +08:00
|
|
|
callback_data=f"{alert_group.pk}:5:oncall-uuid{organization.uuid}",
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
|
|
|
|
],
|
2022-06-03 08:09:47 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert are_keyboards_equal(keyboard.inline_keyboard, expected_keyboard) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_link_to_channel_keyboard():
|
|
|
|
|
keyboard = TelegramKeyboardRenderer.render_link_to_channel_keyboard(link="http://test.com")
|
2023-01-04 17:44:01 +00:00
|
|
|
expected_keyboard = [[InlineKeyboardButton(text="Go to the alert group", url="http://test.com")]]
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
assert are_keyboards_equal(keyboard.inline_keyboard, expected_keyboard) is True
|