95 lines
2.7 KiB
Python
95 lines
2.7 KiB
Python
import pytest
|
|
|
|
from infrasynth.notifications.models import (
|
|
ChannelConfig,
|
|
NotificationDispatch,
|
|
NotificationTemplate,
|
|
)
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
class TestNotificationTemplate:
|
|
def test_create(self):
|
|
template = NotificationTemplate.objects.create(
|
|
slug="welcome",
|
|
name="Welcome",
|
|
channel="email",
|
|
subject_template="Hi {{ name }}",
|
|
body_template="Hello {{ name }}",
|
|
is_html=True,
|
|
)
|
|
assert template.slug == "welcome"
|
|
assert template.is_html is True
|
|
|
|
def test_unique_slug_per_namespace(self):
|
|
NotificationTemplate.objects.create(
|
|
slug="welcome",
|
|
name="A",
|
|
channel="email",
|
|
subject_template="",
|
|
body_template="",
|
|
namespace="",
|
|
)
|
|
NotificationTemplate.objects.create(
|
|
slug="welcome",
|
|
name="B",
|
|
channel="email",
|
|
subject_template="",
|
|
body_template="",
|
|
namespace="other",
|
|
)
|
|
assert NotificationTemplate.objects.count() == 2
|
|
with pytest.raises(Exception):
|
|
NotificationTemplate.objects.create(
|
|
slug="welcome",
|
|
name="C",
|
|
channel="email",
|
|
subject_template="",
|
|
body_template="",
|
|
namespace="",
|
|
)
|
|
|
|
|
|
class TestNotificationDispatch:
|
|
def test_default_status_pending(self, user):
|
|
dispatch = NotificationDispatch.objects.create(
|
|
recipient="a@b.com",
|
|
channel="email",
|
|
subject="S",
|
|
body="B",
|
|
)
|
|
assert dispatch.status == NotificationDispatch.Status.PENDING
|
|
assert dispatch.attempt == 0
|
|
assert dispatch.context_snapshot == {}
|
|
|
|
def test_status_choices(self, user):
|
|
dispatch = NotificationDispatch.objects.create(
|
|
recipient="a@b.com",
|
|
channel="sms",
|
|
subject="S",
|
|
body="B",
|
|
status=NotificationDispatch.Status.FAILED,
|
|
)
|
|
assert dispatch.status == "failed"
|
|
|
|
|
|
class TestChannelConfig:
|
|
def test_create(self):
|
|
config = ChannelConfig.objects.create(
|
|
slug="email",
|
|
channel_type="email",
|
|
display_name="Email",
|
|
config={"host": "smtp.example.com"},
|
|
)
|
|
assert config.config["host"] == "smtp.example.com"
|
|
assert config.is_active is True
|
|
assert config.priority == 0
|
|
|
|
def test_pk_is_slug(self):
|
|
config = ChannelConfig.objects.create(
|
|
slug="telegram",
|
|
channel_type="telegram",
|
|
display_name="Telegram",
|
|
)
|
|
assert config.pk == "telegram"
|