diff --git a/openwisp_notifications/handlers.py b/openwisp_notifications/handlers.py index eefac409..c14efdf3 100644 --- a/openwisp_notifications/handlers.py +++ b/openwisp_notifications/handlers.py @@ -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 @@ -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: diff --git a/openwisp_notifications/tests/test_notifications.py b/openwisp_notifications/tests/test_notifications.py index da076552..eb3dcd7e 100644 --- a/openwisp_notifications/tests/test_notifications.py +++ b/openwisp_notifications/tests/test_notifications.py @@ -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 @@ -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):