Skip to content

Commit

Permalink
[chore] Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanus3133 committed May 19, 2024
1 parent c7bb6cf commit fcf0fc4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 4 additions & 6 deletions openwisp_notifications/handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging

from allauth.account.models import EmailAddress
from celery.exceptions import OperationalError
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -183,12 +182,11 @@ def send_email_notification(sender, instance, created, **kwargs):
# therefore send email anyway.
email_preference = True

email_address = EmailAddress.objects.filter(
user=instance.recipient, email=instance.recipient.email
).first()
is_email_verified = email_address.verified if email_address else False
email_verified = instance.recipient.emailaddress_set.filter(
verified=True, email=instance.recipient.email
).exists()

if not (email_preference and instance.recipient.email and is_email_verified):
if not (email_preference and instance.recipient.email and email_verified):
return

try:
Expand Down
6 changes: 6 additions & 0 deletions openwisp_notifications/tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import timedelta
from unittest.mock import patch

from allauth.account.models import EmailAddress
from celery.exceptions import OperationalError
from django.apps.registry import apps
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -889,6 +890,11 @@ def test_email_notif_without_notif_setting(self):
)
self.assertEqual(len(mail.outbox), 0)

def test_email_notification_with_unverified_status(self):
EmailAddress.objects.filter(user=self.admin).update(verified=False)
self._create_notification()
self.assertEqual(len(mail.outbox), 0)


class TestTransactionNotifications(TestOrganizationMixin, TransactionTestCase):
def setUp(self):
Expand Down

0 comments on commit fcf0fc4

Please sign in to comment.