do not send an auto reply to 'noreply*' addresses

This commit is contained in:
jedi 2024-01-12 20:58:37 +01:00
parent 1804939407
commit c79b3185e5
3 changed files with 38 additions and 9 deletions

View file

@ -182,11 +182,11 @@ def receive_email(envelope, log=None):
if new:
# auto reply if new issue
references = collect_references(active_issue_thread)
reply_email = Email.objects.create(
sender=recipient, recipient=sender, body="Thank you for your message.", subject="Message received",
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
reply = make_reply(reply_email, references)
if not sender.startswith('noreply'):
reply_email = Email.objects.create(
sender=recipient, recipient=sender, body="Thank you for your message.", subject="Message received",
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
reply = make_reply(reply_email, references)
else:
# change state if not new
if active_issue_thread.state != 'pending_new':
@ -223,7 +223,7 @@ class LMTPHandler:
"message": "email received"}
)
log.info(f"Sent message to frontend")
if new:
if new and reply:
await send_smtp(reply, log)
log.info("Sent auto reply")

View file

@ -491,3 +491,33 @@ dGVzdGltYWdl
file_content = EmailAttachment.objects.all()[0].file.read()
self.assertEqual(b'testimage', file_content)
def test_mail_noreply(self):
from aiosmtpd.smtp import Envelope
from asgiref.sync import async_to_sync
import aiosmtplib
aiosmtplib.send = make_mocked_coro()
handler = LMTPHandler()
server = mock.Mock()
session = mock.Mock()
envelope = Envelope()
envelope.mail_from = 'noreply@test'
envelope.rcpt_tos = ['test2@test']
envelope.content = b'Subject: test\nFrom: noreply@test\nTo: test2@test\nMessage-ID: <1@test>\n\ntest'
result = async_to_sync(handler.handle_DATA)(server, session, envelope)
self.assertEqual(result, '250 Message accepted for delivery')
self.assertEqual(len(Email.objects.all()), 1)
self.assertEqual(len(IssueThread.objects.all()), 1)
aiosmtplib.send.assert_not_called()
self.assertEqual('test', Email.objects.all()[0].subject)
self.assertEqual('noreply@test', Email.objects.all()[0].sender)
self.assertEqual('test2@test', Email.objects.all()[0].recipient)
self.assertEqual('test', Email.objects.all()[0].body)
self.assertEqual(IssueThread.objects.all()[0], Email.objects.all()[0].issue_thread)
self.assertEqual('<1@test>', Email.objects.all()[0].reference)
self.assertEqual(None, Email.objects.all()[0].in_reply_to)
self.assertEqual('test', IssueThread.objects.all()[0].name)
self.assertEqual('pending_new', IssueThread.objects.all()[0].state)
self.assertEqual(None, IssueThread.objects.all()[0].assigned_to)
states = StateChange.objects.filter(issue_thread=IssueThread.objects.all()[0])
self.assertEqual(1, len(states))
self.assertEqual('pending_new', states[0].state)

View file

@ -137,12 +137,11 @@ class IssueApiTest(TestCase):
self.assertEqual('test', response.json()[1]['timeline'][1]['subject'])
self.assertEqual('test', response.json()[1]['timeline'][1]['body'])
self.assertEqual(mail1.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
response.json()[1]['timeline'][1]['timestamp'])
response.json()[1]['timeline'][1]['timestamp'])
self.assertEqual('pending_new', response.json()[2]['timeline'][0]['state'])
self.assertEqual('test', response.json()[2]['timeline'][1]['comment'])
self.assertEqual(comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
response.json()[2]['timeline'][1]['timestamp'])
response.json()[2]['timeline'][1]['timestamp'])
def test_manual_creation(self):
response = self.client.post('/api/2/tickets/manual/',