Compare commits

..

1 Commits

Author SHA1 Message Date
Emmanuel Cazenave ee9f9c3396 sendmail: grag recipient from environ (#88821)
gitea/docbow/pipeline/head This commit looks good Details
Alse grab sender from environ (only in pfwb sendmail).
2024-03-28 14:49:48 +01:00
3 changed files with 15 additions and 12 deletions

View File

@ -53,11 +53,10 @@ In case of failure the following return value is returned:
message_id = None
def add_arguments(self, parser):
parser.add_argument('--sender')
parser.add_argument('--file')
def handle(self, *args, **options):
if not options.get('sender'):
if not os.environ.get('SENDER'):
self.error('7.7.1 No sender', exit_code=8)
self.setup_mailing_list_dict()
try:
@ -123,7 +122,7 @@ In case of failure the following return value is returned:
recipients = []
mailing_list_recipients = []
description = ''
from_email = email.utils.parseaddr(options['sender'])[1]
from_email = email.utils.parseaddr(os.environ['SENDER'])[1]
if not from_email:
self.error('7.7.1 No sender', exit_code=8)
if from_email == app_settings.PFWB_SENDMAIL_TABELLIO_EXPEDITION_EMAIL:

View File

@ -111,8 +111,10 @@ class SendMailTestCase(TestCase):
with tempfile.NamedTemporaryFile() as f:
f.write(force_bytes(content))
f.flush()
with mock.patch.dict(os.environ, {'RECIPIENT': recipient_email}, clear=True):
management.call_command('sendmail', file=f.name, sender=expedition_email)
with mock.patch.dict(
os.environ, {'RECIPIENT': recipient_email, 'SENDER': expedition_email}, clear=True
):
management.call_command('sendmail', file=f.name)
def send_tabellio_doc(self, expedition_email, recipient_email, doc_type, subject):
content = '''\
@ -138,8 +140,8 @@ Coucou
with mock.patch.dict(os.environ, {'RECIPIENT': 'yyy'}, clear=True):
management.call_command('sendmail')
with self.assertRaises(SystemExit):
with mock.patch.dict(os.environ, {'RECIPIENT': 'yyy'}, clear=True):
management.call_command('sendmail', sender='')
with mock.patch.dict(os.environ, {'RECIPIENT': 'yyy', 'SENDER': ''}, clear=True):
management.call_command('sendmail')
@stderr_output('7.7.1 Mail is missing a Message-ID\n')
def test_fail_on_missing_message_id(self):
@ -286,7 +288,7 @@ class SendMailAttachedFileTestCase(TestCase):
return force_bytes(message.as_string())
@stderr_output('')
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True)
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL, 'SENDER': EXPEDITION_EMAIL}, clear=True)
def test_attached_file1(self):
with tempfile.NamedTemporaryFile() as f:
f.write(
@ -299,7 +301,7 @@ class SendMailAttachedFileTestCase(TestCase):
)
)
f.flush()
management.call_command('sendmail', file=f.name, sender=EXPEDITION_EMAIL)
management.call_command('sendmail', file=f.name)
self.assertEqual(Document.objects.count(), 1)
document = Document.objects.get()
assert document.attached_files.count() == 1
@ -460,7 +462,9 @@ Coucou
f.write(force_bytes(content))
f.flush()
with mock.patch('docbow_project.pfwb.management.commands.sendmail.urlopen', mockurllib.urlopen):
with mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True):
management.call_command('sendmail', file=f.name, sender=EXPEDITION_EMAIL)
with mock.patch.dict(
os.environ, {'RECIPIENT': RECIPIENT_EMAIL, 'SENDER': EXPEDITION_EMAIL}, clear=True
):
management.call_command('sendmail', file=f.name)
assert Document.objects.count() == 1

View File

@ -188,6 +188,7 @@ class SendMailAttachedFileTestCase(TestCase):
assert document.to_user.get() == self.to_user
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True)
def test_email_wrong_encoding(db, settings):
settings.MEDIA_ROOT = MEDIA_ROOT
expedition_user = User.objects.create(username='expedition', id=1)
@ -195,7 +196,6 @@ def test_email_wrong_encoding(db, settings):
FileType.objects.create(name='QE-Question', id=2)
management.call_command(
'sendmail',
RECIPIENT_EMAIL,
file='tests/data/email-encoded-iso-8859-15-but-says-utf-8',
sender=expedition_user.username,
)