oncall-engine/engine/apps/telegram/tests/test_keyboard_renderer.py
Yulya Artyukhina 3c93375244
Update alert group state by backsync (#4089)
# What this PR does
Adds method to update alert group state by backsync
Related to https://github.com/grafana/oncall-private/issues/2542
Should be merged with
https://github.com/grafana/oncall-private/pull/2606

## 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] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-03-27 12:37:01 +00:00

192 lines
6.5 KiB
Python

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
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}",
),
]
@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 = [
[
InlineKeyboardButton(
text="Acknowledge",
callback_data=f"{alert_group.pk}:0:oncall-uuid{organization.uuid}",
)
],
[
InlineKeyboardButton(
text="Resolve",
callback_data=f"{alert_group.pk}:2:oncall-uuid{organization.uuid}",
)
],
generate_silence_buttons(alert_group, organization),
]
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)
alert_group.acknowledge_by_user_or_backsync(user)
renderer = TelegramKeyboardRenderer(alert_group=alert_group)
keyboard = renderer.render_actions_keyboard()
expected_keyboard = [
[
InlineKeyboardButton(
text="Unacknowledge",
callback_data=f"{alert_group.pk}:1:oncall-uuid{organization.uuid}",
)
],
[
InlineKeyboardButton(
text="Resolve",
callback_data=f"{alert_group.pk}:2:oncall-uuid{organization.uuid}",
)
],
generate_silence_buttons(alert_group, organization),
]
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)
alert_group.resolve_by_user_or_backsync(user)
renderer = TelegramKeyboardRenderer(alert_group=alert_group)
keyboard = renderer.render_actions_keyboard()
expected_keyboard = [
[
InlineKeyboardButton(
text="Unresolve",
callback_data=f"{alert_group.pk}:3:oncall-uuid{organization.uuid}",
)
],
]
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)
alert_group.silence_by_user_or_backsync(user, silence_delay=None)
renderer = TelegramKeyboardRenderer(alert_group=alert_group)
keyboard = renderer.render_actions_keyboard()
expected_keyboard = [
[
InlineKeyboardButton(
text="Acknowledge",
callback_data=f"{alert_group.pk}:0:oncall-uuid{organization.uuid}",
)
],
[
InlineKeyboardButton(
text="Resolve",
callback_data=f"{alert_group.pk}:2:oncall-uuid{organization.uuid}",
)
],
[
InlineKeyboardButton(
text="Unsilence",
callback_data=f"{alert_group.pk}:5:oncall-uuid{organization.uuid}",
)
],
]
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")
expected_keyboard = [[InlineKeyboardButton(text="Go to the alert group", url="http://test.com")]]
assert are_keyboards_equal(keyboard.inline_keyboard, expected_keyboard) is True