diff --git a/apps/notifications/apps.py b/apps/notifications/apps.py index 264776a92..9ea88c26a 100644 --- a/apps/notifications/apps.py +++ b/apps/notifications/apps.py @@ -4,7 +4,3 @@ class NotificationsConfig(AppConfig): name = "apps.notifications" verbose_name = "Notifications" - - def ready(self): - # noinspection PyUnresolvedReferences - from apps.notifications import signals diff --git a/apps/notifications/signals.py b/apps/notifications/signals.py deleted file mode 100644 index b85183664..000000000 --- a/apps/notifications/signals.py +++ /dev/null @@ -1,16 +0,0 @@ -from django.dispatch import receiver -from django_comments.signals import comment_was_posted - -from apps.landmatrix.models.activity import HistoricalActivity -from .distribution import send_notifications_for_comment_on_activity - - -@receiver(comment_was_posted, dispatch_uid="comment_on_activity_posted") -def handle_comment_on_activity_posted(comment, request, **kwargs): - """ - If we get a new comment, check that it was on an activity, and if so - send any notifications required. - """ - activity = comment.content_object - if isinstance(activity, HistoricalActivity): - send_notifications_for_comment_on_activity(comment, request, activity) diff --git a/apps/notifications/tests/__init__.py b/apps/notifications/tests/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/apps/notifications/tests/test_admin.py b/apps/notifications/tests/test_admin.py deleted file mode 100644 index 515a04e45..000000000 --- a/apps/notifications/tests/test_admin.py +++ /dev/null @@ -1,39 +0,0 @@ -from django.contrib.admin.sites import AdminSite -from django.core import mail -from django.test import RequestFactory, TestCase - -from apps.notifications.admin import NotificationEmailAdmin -from apps.notifications.models import NotificationEmail - - -class NotificationEmailAdminTestCase(TestCase): - def setUp(self): - self.admin = NotificationEmailAdmin( - model=NotificationEmail, admin_site=AdminSite() - ) - self.request = RequestFactory() - - def test_has_add_permission(self): - self.assertFalse(self.admin.has_add_permission(self.request)) - - def test_has_delete_permission(self): - self.assertFalse(self.admin.has_delete_permission(self.request)) - - def test_resend_failed_emails(self): - NotificationEmail.objects.create( - to="reporter@example.com", - subject="NEW", - sent_status=NotificationEmail.STATUS_NEW, - ) - NotificationEmail.objects.create( - to="reporter@example.com", - subject="ERROR", - sent_status=NotificationEmail.STATUS_ERROR, - ) - queryset = NotificationEmail.objects.all() - self.admin.resend_failed_emails(self.request, queryset) - self.assertEqual( - {NotificationEmail.STATUS_SENT}, - set(queryset.values_list("sent_status", flat=True)), - ) - self.assertGreater(len(mail.outbox), 0) diff --git a/apps/notifications/tests/test_distribution.py b/apps/notifications/tests/test_distribution.py deleted file mode 100644 index 568090dc6..000000000 --- a/apps/notifications/tests/test_distribution.py +++ /dev/null @@ -1,74 +0,0 @@ -from django.core import mail -from django.test import RequestFactory, TestCase - -from apps.landmatrix.models import HistoricalActivity -from apps.landmatrix.tests.mixins import ActivitiesFixtureMixin -from apps.notifications.distribution import * -from apps.public_comments.models import ThreadedComment - - -class DistributionTestCase(ActivitiesFixtureMixin, TestCase): - - act_fixtures = [{"id": 10, "activity_identifier": 1}] - - def setUp(self): - super().setUp() - user = get_user_model().objects.get(username="reporter") - self.activity = HistoricalActivity.objects.get(id=10) - self.comment = ThreadedComment.objects.create( - user=user, comment="comment", content_object=self.activity, site_id=1 - ) - self.parent_comment = ThreadedComment.objects.create( - user_email="reporter-2@example.com", - comment="parent comment", - content_object=self.activity, - site_id=1, - ) - self.request = RequestFactory() - - def test_get_recipients_for_comment_on_activity(self): - recipients = get_recipients_for_comment_on_activity(self.comment, self.activity) - expected = { - "administrator-myanmar@example.com", - "editor-asia@example.com", - "administrator-asia@example.com", - "editor-myanmar@example.com", - } - self.assertEqual(expected, recipients) - - def test_get_recipients_for_comment_on_activity_with_parent(self): - user = get_user_model().objects.get(username="reporter-2") - self.parent_comment.user = user - self.parent_comment.save() - self.comment.parent = self.parent_comment - self.comment.save() - recipients = get_recipients_for_comment_on_activity(self.comment, self.activity) - expected = { - "editor-myanmar@example.com", - "editor-asia@example.com", - "reporter2@example.com", - "administrator-asia@example.com", - "administrator-myanmar@example.com", - } - self.assertEqual(expected, recipients) - - def test_get_recipients_for_comment_on_activity_with_parent_email(self): - self.comment.parent = self.parent_comment - self.comment.save() - recipients = get_recipients_for_comment_on_activity(self.comment, self.activity) - expected = { - "administrator-myanmar@example.com", - "editor-asia@example.com", - "administrator-asia@example.com", - "reporter-2@example.com", - "editor-myanmar@example.com", - } - self.assertEqual(expected, recipients) - - def test_send_notifications_for_comment_on_activity(self): - send_notifications_for_comment_on_activity( - self.comment, self.request, self.activity - ) - queryset = NotificationEmail.objects.all() - self.assertGreater(queryset.count(), 0) - self.assertGreater(len(mail.outbox), 0) diff --git a/apps/notifications/tests/test_models.py b/apps/notifications/tests/test_models.py deleted file mode 100644 index 4dde77739..000000000 --- a/apps/notifications/tests/test_models.py +++ /dev/null @@ -1,88 +0,0 @@ -from django.core import mail -from django.test import TestCase -from django.utils import timezone - -from apps.notifications.exceptions import AlreadySentError, NotificationError -from apps.notifications.models import NotificationEmail, NotificationEmailManager - - -class NotificationEmailManagerTestCase(TestCase): - def test(self): - manager = NotificationEmailManager() - manager.send(to="report@example.com", subject="NEW") - self.assertGreater(NotificationEmail.objects.count(), 0) - self.assertGreater(len(mail.outbox), 0) - - -class NotificationEmailTestCase(TestCase): - def setUp(self): - self.context = { - "comment": { - "name": "name", - "title": "title", - "comment": "comment", - "content_object": {"activity_identifier": 1}, - } - } - self.email_new = NotificationEmail.objects.create( - to="to@example.com", - cc="cc@example.com", - bcc="bcc@example.com", - reply_to="reply_to@example.com", - subject="NEW", - sent_status=NotificationEmail.STATUS_NEW, - template_name="comment_posted", - context=self.context, - ) - self.email_sent = NotificationEmail.objects.create( - to="reporter@example.com", - subject="SENT", - sent_status=NotificationEmail.STATUS_NEW, - template_name="comment_posted", - context=self.context, - ) - self.email_sent.sent_status = NotificationEmail.STATUS_SENT - self.email_sent.sent_on = timezone.now() - self.email_sent.save() - - def test_render_template(self): - self.email_new.render_template("comment_posted", self.context) - self.assertGreater(len(self.email_new.body_text), 0) - self.assertGreater(len(self.email_new.body_html), 0) - - def test_render_template_without_new(self): - with self.assertRaises(NotificationError): - self.email_sent.render_template("comment_posted", self.context) - - def test_is_sent_with_sent(self): - self.assertEqual(True, self.email_sent.is_sent) - - def test_is_sent_without_sent(self): - self.assertEqual(False, self.email_new.is_sent) - - def test_is_new_with_new(self): - self.assertEqual(True, self.email_new.is_new) - - def test_is_new_without_new(self): - self.assertEqual(False, self.email_sent.is_new) - - def test_send_with_html(self): - self.email_new.body_text = "body_text" - self.email_new.body_html = "body_html" - self.email_new.send() - self.assertEqual(NotificationEmail.STATUS_SENT, self.email_new.sent_status) - self.assertIsNotNone(self.email_new.sent_on) - self.assertEqual("", self.email_new.sent_exception) - self.assertGreater(len(mail.outbox), 0) - - def test_send_without_html(self): - self.email_new.body_text = "body_text" - self.email_new.send() - self.assertEqual(NotificationEmail.STATUS_SENT, self.email_new.sent_status) - self.assertIsNotNone(self.email_new.sent_on) - self.assertEqual("", self.email_new.sent_exception) - self.assertGreater(len(mail.outbox), 0) - - def test_send_with_sent(self): - with self.assertRaises(AlreadySentError): - self.email_sent.send() diff --git a/apps/notifications/tests/test_signals.py b/apps/notifications/tests/test_signals.py deleted file mode 100644 index c4e40aa71..000000000 --- a/apps/notifications/tests/test_signals.py +++ /dev/null @@ -1,35 +0,0 @@ -from django.core import mail -from django.test import TestCase, override_settings -from django.urls import reverse -from django.utils.datastructures import MultiValueDict - -from apps.landmatrix.models import HistoricalActivity -from apps.landmatrix.tests.mixins import ActivitiesFixtureMixin -from apps.notifications.models import NotificationEmail -from apps.public_comments.forms import PublicCommentForm - - -class SignalsTestCase(ActivitiesFixtureMixin, TestCase): - - act_fixtures = [{"id": 10, "activity_identifier": 1}] - - @override_settings(DEBUG=True) - def test_handle_comment_on_activity_posted(self): - data = MultiValueDict( - { - "name": ["name"], - "email": ["reporter@example.com"], - "url": ["https://example.com"], - "comment": ["comment"], - "g-recaptcha-response": ["g-recaptcha-response"], - } - ) - activity = HistoricalActivity.objects.get(id=10) - form = PublicCommentForm(activity) - data.update(form.generate_security_data()) - self.client.login(username="reporter", password="test") - response = self.client.post(reverse("comments-post-comment"), data) - self.client.logout() - self.assertEqual(302, response.status_code) - self.assertGreater(NotificationEmail.objects.count(), 0) - self.assertGreater(len(mail.outbox), 0)