mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-17 05:41:25 +02:00
Merge pull request #1136 from modoboa/fix/dns_notifications
Fixed notifications.
This commit is contained in:
@@ -11,6 +11,8 @@ import gevent
|
||||
from gevent import socket
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import mail
|
||||
from django.core.mail import EmailMessage
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.functional import cached_property
|
||||
@@ -131,41 +133,47 @@ class CheckMXRecords(BaseCommand):
|
||||
result = "" if not results[mx] else results[mx]
|
||||
dnsbl_result = models.DNSBLResult.objects.filter(
|
||||
domain=domain, provider=provider, mx=mx).first()
|
||||
trigger = False
|
||||
if dnsbl_result is None:
|
||||
to_create.append(
|
||||
models.DNSBLResult(
|
||||
domain=domain, provider=provider, mx=mx,
|
||||
status=result)
|
||||
)
|
||||
if result:
|
||||
trigger = True
|
||||
else:
|
||||
if not dnsbl_result.status and result:
|
||||
if domain not in alerts:
|
||||
alerts[domain] = []
|
||||
alerts[domain].append((provider, mx))
|
||||
dnsbl_result.status = result
|
||||
dnsbl_result.save()
|
||||
if not dnsbl_result.status and result:
|
||||
trigger = True
|
||||
if trigger:
|
||||
if domain not in alerts:
|
||||
alerts[domain] = []
|
||||
alerts[domain].append((provider, mx))
|
||||
models.DNSBLResult.objects.bulk_create(to_create)
|
||||
if not alerts:
|
||||
return
|
||||
emails = options["email"]
|
||||
emails = list(options["email"])
|
||||
if not options["skip_admin_emails"]:
|
||||
emails.extend(
|
||||
domain.admins.exclude(email="").values_list("email", flat=True)
|
||||
)
|
||||
if not len(emails):
|
||||
return
|
||||
content = render_to_string(
|
||||
"admin/notifications/domain_in_dnsbl.html", {
|
||||
"domain": domain, "alerts": alerts
|
||||
})
|
||||
subject = _("[modoboa] DNSBL issue(s) for domain {}").format(
|
||||
domain.name)
|
||||
for email in emails:
|
||||
status, msg = email_utils.sendmail_simple(
|
||||
self.sender, email,
|
||||
subject=subject, content=content)
|
||||
if not status:
|
||||
print(msg)
|
||||
with mail.get_connection() as connection:
|
||||
for domain, providers in list(alerts.items()):
|
||||
content = render_to_string(
|
||||
"admin/notifications/domain_in_dnsbl.html", {
|
||||
"domain": domain, "alerts": providers
|
||||
})
|
||||
subject = _("[modoboa] DNSBL issue(s) for domain {}").format(
|
||||
domain.name)
|
||||
msg = EmailMessage(
|
||||
subject, content.strip(), self.sender, emails,
|
||||
connection=connection
|
||||
)
|
||||
msg.send()
|
||||
|
||||
def check_valid_mx(self, domain, mx_list, **options):
|
||||
"""Check that domain's MX record exist.
|
||||
@@ -195,7 +203,7 @@ class CheckMXRecords(BaseCommand):
|
||||
)
|
||||
if not alerts:
|
||||
return
|
||||
emails = options["email"]
|
||||
emails = list(options["email"])
|
||||
if not options["skip_admin_emails"]:
|
||||
emails.extend(
|
||||
domain.admins.exclude(email="").values_list("email", flat=True)
|
||||
@@ -208,12 +216,8 @@ class CheckMXRecords(BaseCommand):
|
||||
})
|
||||
subject = _("[modoboa] MX issue(s) for domain {}").format(
|
||||
domain.name)
|
||||
for email in emails:
|
||||
status, msg = email_utils.sendmail_simple(
|
||||
self.sender, email,
|
||||
subject=subject, content=content)
|
||||
if not status:
|
||||
print(msg)
|
||||
msg = EmailMessage(subject, content.strip(), self.sender, emails)
|
||||
msg.send()
|
||||
|
||||
def check_domain(self, domain, timeout=3, ttl=7200, **options):
|
||||
"""Check specified domain."""
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from mock import patch
|
||||
|
||||
from django.core import mail
|
||||
from django.core import management
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test import override_settings
|
||||
@@ -65,6 +68,7 @@ class MXTestCase(ModoTestCase):
|
||||
"modo", "check_mx", "--domain", "toto.com")
|
||||
|
||||
|
||||
@override_settings(DNSBL_PROVIDERS=["zen.spamhaus.org"])
|
||||
class DNSBLTestCase(ModoTestCase):
|
||||
"""TestCase for DNSBL related features."""
|
||||
|
||||
@@ -80,7 +84,6 @@ class DNSBLTestCase(ModoTestCase):
|
||||
name="modoboa.com", enable_dns_checks=False)
|
||||
models.DNSBLResult.objects.all().delete()
|
||||
|
||||
@override_settings(DNSBL_PROVIDERS=["zen.spamhaus.org"])
|
||||
def test_management_command(self):
|
||||
"""Check that command works fine."""
|
||||
self.assertEqual(models.DNSBLResult.objects.count(), 0)
|
||||
@@ -95,7 +98,14 @@ class DNSBLTestCase(ModoTestCase):
|
||||
self.assertFalse(self.domain.uses_a_reserved_tld)
|
||||
self.assertTrue(self.domain2.uses_a_reserved_tld)
|
||||
|
||||
@override_settings(DNSBL_PROVIDERS=["zen.spamhaus.org"])
|
||||
@patch("gevent.socket.gethostbyname")
|
||||
def test_notifications(self, mock_gethostbyname):
|
||||
"""Check notifications."""
|
||||
mock_gethostbyname.return_value = "1.2.3.4"
|
||||
management.call_command(
|
||||
"modo", "check_mx", "--email", "user@example.test")
|
||||
self.assertEqual(len(mail.outbox), 2)
|
||||
|
||||
def test_management_command_no_dnsbl(self):
|
||||
"""Check that command works fine without dnsbl."""
|
||||
self.assertEqual(models.DNSBLResult.objects.count(), 0)
|
||||
|
||||
Reference in New Issue
Block a user