166 lines
6.7 KiB
Python
166 lines
6.7 KiB
Python
import json
|
|
|
|
from django.test import TestCase
|
|
|
|
from vbv_lernwelt.core.models import User
|
|
from vbv_lernwelt.core.tests.factories import UserFactory
|
|
from vbv_lernwelt.notify.email.email_services import EmailTemplate
|
|
from vbv_lernwelt.notify.models import Notification, NotificationType
|
|
from vbv_lernwelt.notify.services import NotificationService
|
|
|
|
|
|
class TestNotificationService(TestCase):
|
|
def _on_send_email(self, *_, **__) -> bool:
|
|
self._emails_sent += 1
|
|
return True
|
|
|
|
def _has_sent_emails(self) -> bool:
|
|
return self._emails_sent != 0
|
|
|
|
def setUp(self) -> None:
|
|
self._emails_sent = 0
|
|
self.notification_service = NotificationService
|
|
self.notification_service._send_email = self._on_send_email
|
|
|
|
self.admin = UserFactory(username="admin", email="admin")
|
|
self.sender_username = "Bob"
|
|
UserFactory(username=self.sender_username, email="bob@gmail.com")
|
|
self.sender = User.objects.get(username=self.sender_username)
|
|
|
|
self.recipient_username = "Alice"
|
|
UserFactory(username=self.recipient_username, email="alice@gmail.com")
|
|
self.recipient = User.objects.get(username=self.recipient_username)
|
|
self.recipient.additional_json_data["email_notification_types"] = json.dumps(
|
|
["USER_INTERACTION", "INFORMATION"]
|
|
)
|
|
self.recipient.save()
|
|
|
|
self.client.login(username=self.recipient, password="pw")
|
|
|
|
def test_send_information_notification(self):
|
|
verb = "Wartungsarbeiten: 13.12 10:00 - 13:00 Uhr"
|
|
target_url = "https://www.vbv.ch"
|
|
self.notification_service.send_information_notification(
|
|
recipient=self.recipient,
|
|
verb=verb,
|
|
target_url=target_url,
|
|
email_template=EmailTemplate.ATTENDANCE_COURSE_REMINDER,
|
|
)
|
|
self.assertEqual(1, Notification.objects.count())
|
|
notification: Notification = Notification.objects.first()
|
|
self.assertEqual(self.admin, notification.actor)
|
|
self.assertEqual(verb, notification.verb)
|
|
self.assertEqual(target_url, notification.target_url)
|
|
self.assertEqual(
|
|
str(NotificationType.INFORMATION), notification.notification_type
|
|
)
|
|
self.assertTrue(notification.emailed)
|
|
|
|
def test_send_progress_notification(self):
|
|
verb = "Super Fortschritt! Melde dich jetzt an."
|
|
target_url = "https://www.vbv.ch"
|
|
course = "Versicherungsvermittler/in"
|
|
self.notification_service.send_progress_notification(
|
|
recipient=self.recipient,
|
|
verb=verb,
|
|
target_url=target_url,
|
|
course=course,
|
|
email_template=EmailTemplate.ATTENDANCE_COURSE_REMINDER,
|
|
)
|
|
self.assertEqual(1, Notification.objects.count())
|
|
notification: Notification = Notification.objects.first()
|
|
self.assertEqual(self.admin, notification.actor)
|
|
self.assertEqual(verb, notification.verb)
|
|
self.assertEqual(target_url, notification.target_url)
|
|
self.assertEqual(course, notification.course)
|
|
self.assertEqual(str(NotificationType.PROGRESS), notification.notification_type)
|
|
self.assertFalse(notification.emailed)
|
|
|
|
def test_send_user_interaction_notification(self):
|
|
verb = "Anne hat deinen Auftrag bewertet"
|
|
target_url = "https://www.vbv.ch"
|
|
course = "Versicherungsvermittler/in"
|
|
self.notification_service.send_user_interaction_notification(
|
|
sender=self.sender,
|
|
recipient=self.recipient,
|
|
verb=verb,
|
|
target_url=target_url,
|
|
course=course,
|
|
email_template=EmailTemplate.ATTENDANCE_COURSE_REMINDER,
|
|
)
|
|
self.assertEqual(1, Notification.objects.count())
|
|
notification: Notification = Notification.objects.first()
|
|
self.assertEqual(self.sender, notification.actor)
|
|
self.assertEqual(verb, notification.verb)
|
|
self.assertEqual(target_url, notification.target_url)
|
|
self.assertEqual(course, notification.course)
|
|
self.assertEqual(
|
|
str(NotificationType.USER_INTERACTION), notification.notification_type
|
|
)
|
|
self.assertTrue(notification.emailed)
|
|
|
|
def test_does_not_send_duplicate_notification(self):
|
|
verb = "Anne hat deinen Auftrag bewertet"
|
|
target_url = "https://www.vbv.ch"
|
|
course = "Versicherungsvermittler/in"
|
|
|
|
for i in range(2):
|
|
self.notification_service.send_user_interaction_notification(
|
|
sender=self.sender,
|
|
recipient=self.recipient,
|
|
verb=verb,
|
|
target_url=target_url,
|
|
course=course,
|
|
email_template=EmailTemplate.ATTENDANCE_COURSE_REMINDER,
|
|
template_data={
|
|
"blah": 123,
|
|
"foo": "ich habe hunger",
|
|
},
|
|
)
|
|
|
|
self.assertEqual(1, Notification.objects.count())
|
|
notification: Notification = Notification.objects.first()
|
|
self.assertEqual(self.sender, notification.actor)
|
|
self.assertEqual(verb, notification.verb)
|
|
self.assertEqual(target_url, notification.target_url)
|
|
self.assertEqual(course, notification.course)
|
|
self.assertEqual(
|
|
str(NotificationType.USER_INTERACTION), notification.notification_type
|
|
)
|
|
self.assertTrue(notification.emailed)
|
|
|
|
def test_only_sends_email_if_enabled(self):
|
|
# Assert no mail is sent if corresponding email notification type is not enabled
|
|
self.recipient.additional_json_data["email_notification_types"] = json.dumps(
|
|
["INFORMATION"]
|
|
)
|
|
self.recipient.save()
|
|
self.notification_service.send_user_interaction_notification(
|
|
sender=self.sender,
|
|
recipient=self.recipient,
|
|
verb="should not be sent",
|
|
target_url="",
|
|
course="",
|
|
email_template=EmailTemplate.CASEWORK_EVALUATED,
|
|
template_data={},
|
|
)
|
|
self.assertEqual(1, Notification.objects.count())
|
|
self.assertFalse(self._has_sent_emails())
|
|
|
|
# Assert mail is sent if corresponding email notification type is enabled
|
|
self.recipient.additional_json_data["email_notification_types"] = json.dumps(
|
|
["USER_INTERACTION"]
|
|
)
|
|
self.recipient.save()
|
|
self.notification_service.send_user_interaction_notification(
|
|
sender=self.sender,
|
|
recipient=self.recipient,
|
|
verb="should be sent",
|
|
target_url="",
|
|
course="",
|
|
email_template=EmailTemplate.CASEWORK_EVALUATED,
|
|
template_data={},
|
|
)
|
|
self.assertEqual(2, Notification.objects.count())
|
|
self.assertTrue(self._has_sent_emails())
|