Files
notifications-api/tests/app/service/test_sender.py

103 lines
3.7 KiB
Python
Raw Permalink Normal View History

import pytest
from flask import current_app
2024-10-31 11:32:27 -07:00
from sqlalchemy import func, select
2024-10-31 11:32:27 -07:00
from app import db
from app.dao.services_dao import dao_add_user_to_service
from app.enums import NotificationType, TemplateType
from app.models import Notification
from app.service.sender import send_notification_to_service_users
from tests.app.db import create_service, create_template, create_user
@pytest.mark.parametrize(
"notification_type", [NotificationType.EMAIL, NotificationType.SMS]
)
def test_send_notification_to_service_users_persists_notifications_correctly(
2023-08-29 14:54:30 -07:00
notify_service, notification_type, sample_service, mocker
):
2023-08-29 14:54:30 -07:00
mocker.patch("app.service.sender.send_notification_to_queue")
template = create_template(sample_service, template_type=notification_type)
2023-08-29 14:54:30 -07:00
send_notification_to_service_users(
service_id=sample_service.id, template_id=template.id
)
2024-11-18 09:26:04 -08:00
notification = db.session.execute(select(Notification)).scalars().one()
2024-10-31 11:32:27 -07:00
stmt = select(func.count()).select_from(Notification)
count = db.session.execute(stmt).scalar() or 0
assert count == 1
assert notification.to == "1"
2023-08-29 14:54:30 -07:00
assert str(notification.service_id) == current_app.config["NOTIFY_SERVICE_ID"]
assert notification.template.id == template.id
assert notification.template.template_type == notification_type
assert notification.notification_type == notification_type
2023-08-29 14:54:30 -07:00
assert (
notification.reply_to_text
== notify_service.get_default_reply_to_email_address()
)
def test_send_notification_to_service_users_sends_to_queue(
2023-08-29 14:54:30 -07:00
notify_service, sample_service, mocker
):
2023-08-29 14:54:30 -07:00
send_mock = mocker.patch("app.service.sender.send_notification_to_queue")
template = create_template(sample_service, template_type=NotificationType.EMAIL)
2023-08-29 14:54:30 -07:00
send_notification_to_service_users(
service_id=sample_service.id, template_id=template.id
)
assert send_mock.called
assert send_mock.call_count == 1
def test_send_notification_to_service_users_includes_user_fields_in_personalisation(
2023-08-29 14:54:30 -07:00
notify_service, sample_service, mocker
):
2023-08-29 14:54:30 -07:00
persist_mock = mocker.patch("app.service.sender.persist_notification")
mocker.patch("app.service.sender.send_notification_to_queue")
2024-11-27 09:02:34 -08:00
mocker.patch("app.service.sender.redis_store")
user = sample_service.users[0]
template = create_template(sample_service, template_type=TemplateType.EMAIL)
send_notification_to_service_users(
service_id=sample_service.id,
template_id=template.id,
2023-08-29 14:54:30 -07:00
include_user_fields=["name", "email_address", "state"],
)
2017-05-15 15:02:16 +01:00
persist_call = persist_mock.call_args_list[0][1]
assert len(persist_mock.call_args_list) == 1
2023-08-29 14:54:30 -07:00
assert persist_call["personalisation"] == {
"name": user.name,
"email_address": user.email_address,
"state": user.state,
}
def test_send_notification_to_service_users_sends_to_active_users_only(
2023-08-29 14:54:30 -07:00
notify_service, mocker
):
2023-08-29 14:54:30 -07:00
mocker.patch("app.service.sender.send_notification_to_queue")
2024-11-27 10:46:49 -08:00
mocker.patch("app.service.sender.redis_store", autospec=True)
2023-08-29 14:54:30 -07:00
first_active_user = create_user(email="foo@bar.com", state="active")
second_active_user = create_user(email="foo1@bar.com", state="active")
pending_user = create_user(email="foo2@bar.com", state="pending")
service = create_service(user=first_active_user)
dao_add_user_to_service(service, second_active_user)
2024-11-27 10:19:09 -08:00
dao_add_user_to_service(service, pending_user)
2024-11-27 10:19:09 -08:00
template = create_template(service, template_type=TemplateType.EMAIL)
send_notification_to_service_users(service_id=service.id, template_id=template.id)
2024-10-31 11:32:27 -07:00
stmt = select(func.count()).select_from(Notification)
count = db.session.execute(stmt).scalar() or 0
assert count == 2