oncall-engine/engine/apps/base/tests/test_user_notification_policy.py
Joey Orlando abedea72bf
don't force create default user notification policies (#4608)
# What this PR does

Related to https://github.com/grafana/oncall/issues/4410

The changes in this PR are a prerequisite to
https://github.com/grafana/terraform-provider-grafana/pull/1653. See the
conversation
[here](https://raintank-corp.slack.com/archives/C04JCU51NF8/p1719806995902499?thread_ts=1719520920.744319&cid=C04JCU51NF8)
for more context on why we decided to move away from always creating
default personal notification rules for users.

## 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-07-05 15:08:17 -04:00

103 lines
3.3 KiB
Python

import pytest
from django.utils.timezone import timedelta
from apps.base.models import UserNotificationPolicy
from apps.base.models.user_notification_policy import (
NotificationChannelAPIOptions,
NotificationChannelOptions,
NotificationChannelPublicAPIOptions,
validate_channel_choice,
)
from apps.base.tests.messaging_backend import TestOnlyBackend
@pytest.mark.parametrize(
"notification_type,kwargs, expected_verbal",
[
(
UserNotificationPolicy.Step.WAIT,
{
"wait_delay": timedelta(minutes=5),
},
"5 min",
),
(UserNotificationPolicy.Step.NOTIFY, {"notify_by": UserNotificationPolicy.NotificationChannel.SLACK}, "Slack"),
(UserNotificationPolicy.Step.WAIT, {}, "0 min"),
(None, {}, "Not set"),
],
)
@pytest.mark.django_db
def test_short_verbal(
make_organization,
make_user_for_organization,
make_user_notification_policy,
notification_type,
kwargs,
expected_verbal,
):
organization = make_organization()
user = make_user_for_organization(organization)
policy = make_user_notification_policy(user, notification_type, **kwargs)
assert policy.short_verbal == expected_verbal
@pytest.mark.django_db
def test_short_verbals_for_user(
make_organization,
make_user_for_organization,
make_user_notification_policy,
):
organization = make_organization()
user = make_user_for_organization(organization)
make_user_notification_policy(
user, UserNotificationPolicy.Step.NOTIFY, notify_by=UserNotificationPolicy.NotificationChannel.SLACK
)
make_user_notification_policy(user, UserNotificationPolicy.Step.WAIT, wait_delay=timedelta(minutes=5))
make_user_notification_policy(
user,
UserNotificationPolicy.Step.NOTIFY,
notify_by=UserNotificationPolicy.NotificationChannel.SMS,
important=True,
)
expected = (("Slack", "5 min"), ("SMS",))
assert UserNotificationPolicy.get_short_verbals_for_user(user) == expected
@pytest.mark.django_db
def test_extra_messaging_backends_details():
assert TestOnlyBackend.backend_id in UserNotificationPolicy.NotificationChannel.names
assert TestOnlyBackend.backend_id not in NotificationChannelOptions.AVAILABLE_FOR_USE
channel_choice = getattr(UserNotificationPolicy.NotificationChannel, TestOnlyBackend.backend_id)
assert NotificationChannelAPIOptions.LABELS[channel_choice] == "Test Only Backend"
assert NotificationChannelAPIOptions.SHORT_LABELS[channel_choice] == TestOnlyBackend.short_label
assert NotificationChannelPublicAPIOptions.LABELS[channel_choice] == "notify_by_{}".format(
TestOnlyBackend.backend_id.lower()
)
assert validate_channel_choice(channel_choice) is None
@pytest.mark.django_db
def test_can_delete_last_notification_policy(
make_organization,
make_user_for_organization,
make_user_notification_policy,
):
organization = make_organization()
user = make_user_for_organization(organization)
first_policy = make_user_notification_policy(
user, UserNotificationPolicy.Step.NOTIFY, notify_by=UserNotificationPolicy.NotificationChannel.SLACK
)
second_policy = make_user_notification_policy(
user, UserNotificationPolicy.Step.WAIT, wait_delay=timedelta(minutes=5)
)
first_policy.delete()
second_policy.delete()