sendmail: grag recipient from environ (#88821)
gitea/docbow/pipeline/head There was a failure building this commit Details

This commit is contained in:
Emmanuel Cazenave 2024-03-28 14:32:19 +01:00
parent 42e5b05fb4
commit fee87f6d92
4 changed files with 23 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import email.header
import email.utils
import logging
import mailbox
import os
import re
import sys
import time
@ -52,7 +53,6 @@ In case of failure the following return value is returned:
message_id = None
def add_arguments(self, parser):
parser.add_argument('recipient', type=str)
parser.add_argument('--sender')
parser.add_argument('--file')
@ -74,7 +74,7 @@ In case of failure the following return value is returned:
except Exception:
logger.exception('mbox exception')
try:
self.handle_mail(mail, (options['recipient'],), **options)
self.handle_mail(mail, (os.environ['RECIPIENT'],), **options)
except Exception:
logger.exception('Unknown exception')
self.error('7.7.1 Internal error when handling the mail', exit_code=5)

View File

@ -4,6 +4,7 @@ import email.header
import email.utils
import logging
import mailbox
import os
import re
import sys
import time
@ -54,7 +55,6 @@ In case of failure the following return value is returned:
subject = ''
def add_arguments(self, parser):
parser.add_argument('recipient', type=str)
parser.add_argument('--sender')
parser.add_argument('--file')
@ -76,7 +76,7 @@ In case of failure the following return value is returned:
except Exception:
logger.exception('mbox exception')
try:
self.handle_mail(mail, (options['recipient'],), **options)
self.handle_mail(mail, (os.environ['RECIPIENT'],), **options)
except Exception:
logger.exception('Unknown exception')
self.error('5.6.0 Internal error when handling the mail', exit_code=5)

View File

@ -5,6 +5,7 @@ when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
import os
import sys
import tempfile
from contextlib import contextmanager
@ -110,7 +111,8 @@ class SendMailTestCase(TestCase):
with tempfile.NamedTemporaryFile() as f:
f.write(force_bytes(content))
f.flush()
management.call_command('sendmail', recipient_email, file=f.name, sender=expedition_email)
with mock.patch.dict(os.environ, {'RECIPIENT': recipient_email}, clear=True):
management.call_command('sendmail', file=f.name, sender=expedition_email)
def send_tabellio_doc(self, expedition_email, recipient_email, doc_type, subject):
content = '''\
@ -133,9 +135,11 @@ Coucou
@stderr_output('7.7.1 No sender\n7.7.1 No sender\n')
def test_fail_sender(self):
with self.assertRaises(SystemExit):
management.call_command('sendmail', 'yyy')
with mock.patch.dict(os.environ, {'RECIPIENT': 'yyy'}, clear=True):
management.call_command('sendmail')
with self.assertRaises(SystemExit):
management.call_command('sendmail', 'yyy', sender='')
with mock.patch.dict(os.environ, {'RECIPIENT': 'yyy'}, clear=True):
management.call_command('sendmail', sender='')
@stderr_output('7.7.1 Mail is missing a Message-ID\n')
def test_fail_on_missing_message_id(self):
@ -282,6 +286,7 @@ class SendMailAttachedFileTestCase(TestCase):
return force_bytes(message.as_string())
@stderr_output('')
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True)
def test_attached_file1(self):
with tempfile.NamedTemporaryFile() as f:
f.write(
@ -294,7 +299,7 @@ class SendMailAttachedFileTestCase(TestCase):
)
)
f.flush()
management.call_command('sendmail', RECIPIENT_EMAIL, file=f.name, sender=EXPEDITION_EMAIL)
management.call_command('sendmail', file=f.name, sender=EXPEDITION_EMAIL)
self.assertEqual(Document.objects.count(), 1)
document = Document.objects.get()
assert document.attached_files.count() == 1
@ -455,6 +460,7 @@ Coucou
f.write(force_bytes(content))
f.flush()
with mock.patch('docbow_project.pfwb.management.commands.sendmail.urlopen', mockurllib.urlopen):
management.call_command('sendmail', RECIPIENT_EMAIL, file=f.name, sender=EXPEDITION_EMAIL)
with mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True):
management.call_command('sendmail', file=f.name, sender=EXPEDITION_EMAIL)
assert Document.objects.count() == 1

View File

@ -5,11 +5,13 @@ when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
import os
import sys
import tempfile
from contextlib import contextmanager
from functools import wraps
from io import StringIO
from unittest import mock
from django.contrib.auth.models import User
from django.core import management
@ -120,6 +122,7 @@ class SendMailAttachedFileTestCase(TestCase):
self.to_list.members.add(self.to_user)
@stderr_output('')
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True)
def test_attached_file_with_forced_sender(self):
with MessageFile(
self.pjd_filetype,
@ -128,9 +131,7 @@ class SendMailAttachedFileTestCase(TestCase):
'coucou',
(('attached-file', 'content'),),
) as f:
management.call_command(
'sendmail', RECIPIENT_EMAIL, file=f.name, sender=self.expedition_user.username
)
management.call_command('sendmail', file=f.name, sender=self.expedition_user.username)
self.assertEqual(Document.objects.count(), 1)
document = Document.objects.get()
assert document.comment == 'coucou'
@ -142,6 +143,7 @@ class SendMailAttachedFileTestCase(TestCase):
assert document.to_user.get() == self.to_user
@stderr_output('')
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True)
def test_attached_file_with_forced_sender_and_private_header(self):
with MessageFile(
self.pjd_filetype,
@ -151,9 +153,7 @@ class SendMailAttachedFileTestCase(TestCase):
(('attached-file', 'content'),),
headers={'Private': '1'},
) as f:
management.call_command(
'sendmail', RECIPIENT_EMAIL, file=f.name, sender=self.expedition_user.username
)
management.call_command('sendmail', file=f.name, sender=self.expedition_user.username)
self.assertEqual(Document.objects.count(), 1)
document = Document.objects.get()
assert document.private is True
@ -166,6 +166,7 @@ class SendMailAttachedFileTestCase(TestCase):
assert document.to_user.get() == self.to_user
@stderr_output('')
@mock.patch.dict(os.environ, {'RECIPIENT': PRIVATE_RECIPIENT_EMAIL}, clear=True)
def test_attached_file_with_forced_sender_and_private_email(self):
with MessageFile(
self.pjd_filetype,
@ -174,9 +175,7 @@ class SendMailAttachedFileTestCase(TestCase):
'coucou',
(('attached-file', 'content'),),
) as f:
management.call_command(
'sendmail', PRIVATE_RECIPIENT_EMAIL, file=f.name, sender=self.expedition_user.username
)
management.call_command('sendmail', file=f.name, sender=self.expedition_user.username)
self.assertEqual(Document.objects.count(), 1)
document = Document.objects.get()
assert document.private is True