sendmail: grag recipient from environ (#88821)
gitea/docbow/pipeline/head This commit looks good
Details
gitea/docbow/pipeline/head This commit looks good
Details
Alse grab sender from environ (only in pfwb sendmail).
This commit is contained in:
parent
42e5b05fb4
commit
ee9f9c3396
|
@ -4,6 +4,7 @@ import email.header
|
||||||
import email.utils
|
import email.utils
|
||||||
import logging
|
import logging
|
||||||
import mailbox
|
import mailbox
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
@ -52,12 +53,10 @@ In case of failure the following return value is returned:
|
||||||
message_id = None
|
message_id = None
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument('recipient', type=str)
|
|
||||||
parser.add_argument('--sender')
|
|
||||||
parser.add_argument('--file')
|
parser.add_argument('--file')
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
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.error('7.7.1 No sender', exit_code=8)
|
||||||
self.setup_mailing_list_dict()
|
self.setup_mailing_list_dict()
|
||||||
try:
|
try:
|
||||||
|
@ -74,7 +73,7 @@ In case of failure the following return value is returned:
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('mbox exception')
|
logger.exception('mbox exception')
|
||||||
try:
|
try:
|
||||||
self.handle_mail(mail, (options['recipient'],), **options)
|
self.handle_mail(mail, (os.environ['RECIPIENT'],), **options)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Unknown exception')
|
logger.exception('Unknown exception')
|
||||||
self.error('7.7.1 Internal error when handling the mail', exit_code=5)
|
self.error('7.7.1 Internal error when handling the mail', exit_code=5)
|
||||||
|
@ -123,7 +122,7 @@ In case of failure the following return value is returned:
|
||||||
recipients = []
|
recipients = []
|
||||||
mailing_list_recipients = []
|
mailing_list_recipients = []
|
||||||
description = ''
|
description = ''
|
||||||
from_email = email.utils.parseaddr(options['sender'])[1]
|
from_email = email.utils.parseaddr(os.environ['SENDER'])[1]
|
||||||
if not from_email:
|
if not from_email:
|
||||||
self.error('7.7.1 No sender', exit_code=8)
|
self.error('7.7.1 No sender', exit_code=8)
|
||||||
if from_email == app_settings.PFWB_SENDMAIL_TABELLIO_EXPEDITION_EMAIL:
|
if from_email == app_settings.PFWB_SENDMAIL_TABELLIO_EXPEDITION_EMAIL:
|
||||||
|
|
|
@ -4,6 +4,7 @@ import email.header
|
||||||
import email.utils
|
import email.utils
|
||||||
import logging
|
import logging
|
||||||
import mailbox
|
import mailbox
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
@ -54,7 +55,6 @@ In case of failure the following return value is returned:
|
||||||
subject = ''
|
subject = ''
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument('recipient', type=str)
|
|
||||||
parser.add_argument('--sender')
|
parser.add_argument('--sender')
|
||||||
parser.add_argument('--file')
|
parser.add_argument('--file')
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ In case of failure the following return value is returned:
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('mbox exception')
|
logger.exception('mbox exception')
|
||||||
try:
|
try:
|
||||||
self.handle_mail(mail, (options['recipient'],), **options)
|
self.handle_mail(mail, (os.environ['RECIPIENT'],), **options)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Unknown exception')
|
logger.exception('Unknown exception')
|
||||||
self.error('5.6.0 Internal error when handling the mail', exit_code=5)
|
self.error('5.6.0 Internal error when handling the mail', exit_code=5)
|
||||||
|
|
|
@ -5,6 +5,7 @@ when you run "manage.py test".
|
||||||
Replace this with more appropriate tests for your application.
|
Replace this with more appropriate tests for your application.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
@ -110,7 +111,10 @@ class SendMailTestCase(TestCase):
|
||||||
with tempfile.NamedTemporaryFile() as f:
|
with tempfile.NamedTemporaryFile() as f:
|
||||||
f.write(force_bytes(content))
|
f.write(force_bytes(content))
|
||||||
f.flush()
|
f.flush()
|
||||||
management.call_command('sendmail', recipient_email, 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):
|
def send_tabellio_doc(self, expedition_email, recipient_email, doc_type, subject):
|
||||||
content = '''\
|
content = '''\
|
||||||
|
@ -133,9 +137,11 @@ Coucou
|
||||||
@stderr_output('7.7.1 No sender\n7.7.1 No sender\n')
|
@stderr_output('7.7.1 No sender\n7.7.1 No sender\n')
|
||||||
def test_fail_sender(self):
|
def test_fail_sender(self):
|
||||||
with self.assertRaises(SystemExit):
|
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):
|
with self.assertRaises(SystemExit):
|
||||||
management.call_command('sendmail', 'yyy', 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')
|
@stderr_output('7.7.1 Mail is missing a Message-ID\n')
|
||||||
def test_fail_on_missing_message_id(self):
|
def test_fail_on_missing_message_id(self):
|
||||||
|
@ -282,6 +288,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
return force_bytes(message.as_string())
|
return force_bytes(message.as_string())
|
||||||
|
|
||||||
@stderr_output('')
|
@stderr_output('')
|
||||||
|
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL, 'SENDER': EXPEDITION_EMAIL}, clear=True)
|
||||||
def test_attached_file1(self):
|
def test_attached_file1(self):
|
||||||
with tempfile.NamedTemporaryFile() as f:
|
with tempfile.NamedTemporaryFile() as f:
|
||||||
f.write(
|
f.write(
|
||||||
|
@ -294,7 +301,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
f.flush()
|
f.flush()
|
||||||
management.call_command('sendmail', RECIPIENT_EMAIL, file=f.name, sender=EXPEDITION_EMAIL)
|
management.call_command('sendmail', file=f.name)
|
||||||
self.assertEqual(Document.objects.count(), 1)
|
self.assertEqual(Document.objects.count(), 1)
|
||||||
document = Document.objects.get()
|
document = Document.objects.get()
|
||||||
assert document.attached_files.count() == 1
|
assert document.attached_files.count() == 1
|
||||||
|
@ -455,6 +462,9 @@ Coucou
|
||||||
f.write(force_bytes(content))
|
f.write(force_bytes(content))
|
||||||
f.flush()
|
f.flush()
|
||||||
with mock.patch('docbow_project.pfwb.management.commands.sendmail.urlopen', mockurllib.urlopen):
|
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, 'SENDER': EXPEDITION_EMAIL}, clear=True
|
||||||
|
):
|
||||||
|
management.call_command('sendmail', file=f.name)
|
||||||
|
|
||||||
assert Document.objects.count() == 1
|
assert Document.objects.count() == 1
|
||||||
|
|
|
@ -5,11 +5,13 @@ when you run "manage.py test".
|
||||||
Replace this with more appropriate tests for your application.
|
Replace this with more appropriate tests for your application.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core import management
|
from django.core import management
|
||||||
|
@ -120,6 +122,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
self.to_list.members.add(self.to_user)
|
self.to_list.members.add(self.to_user)
|
||||||
|
|
||||||
@stderr_output('')
|
@stderr_output('')
|
||||||
|
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True)
|
||||||
def test_attached_file_with_forced_sender(self):
|
def test_attached_file_with_forced_sender(self):
|
||||||
with MessageFile(
|
with MessageFile(
|
||||||
self.pjd_filetype,
|
self.pjd_filetype,
|
||||||
|
@ -128,9 +131,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
'coucou',
|
'coucou',
|
||||||
(('attached-file', 'content'),),
|
(('attached-file', 'content'),),
|
||||||
) as f:
|
) as f:
|
||||||
management.call_command(
|
management.call_command('sendmail', file=f.name, sender=self.expedition_user.username)
|
||||||
'sendmail', RECIPIENT_EMAIL, file=f.name, sender=self.expedition_user.username
|
|
||||||
)
|
|
||||||
self.assertEqual(Document.objects.count(), 1)
|
self.assertEqual(Document.objects.count(), 1)
|
||||||
document = Document.objects.get()
|
document = Document.objects.get()
|
||||||
assert document.comment == 'coucou'
|
assert document.comment == 'coucou'
|
||||||
|
@ -142,6 +143,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
assert document.to_user.get() == self.to_user
|
assert document.to_user.get() == self.to_user
|
||||||
|
|
||||||
@stderr_output('')
|
@stderr_output('')
|
||||||
|
@mock.patch.dict(os.environ, {'RECIPIENT': RECIPIENT_EMAIL}, clear=True)
|
||||||
def test_attached_file_with_forced_sender_and_private_header(self):
|
def test_attached_file_with_forced_sender_and_private_header(self):
|
||||||
with MessageFile(
|
with MessageFile(
|
||||||
self.pjd_filetype,
|
self.pjd_filetype,
|
||||||
|
@ -151,9 +153,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
(('attached-file', 'content'),),
|
(('attached-file', 'content'),),
|
||||||
headers={'Private': '1'},
|
headers={'Private': '1'},
|
||||||
) as f:
|
) as f:
|
||||||
management.call_command(
|
management.call_command('sendmail', file=f.name, sender=self.expedition_user.username)
|
||||||
'sendmail', RECIPIENT_EMAIL, file=f.name, sender=self.expedition_user.username
|
|
||||||
)
|
|
||||||
self.assertEqual(Document.objects.count(), 1)
|
self.assertEqual(Document.objects.count(), 1)
|
||||||
document = Document.objects.get()
|
document = Document.objects.get()
|
||||||
assert document.private is True
|
assert document.private is True
|
||||||
|
@ -166,6 +166,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
assert document.to_user.get() == self.to_user
|
assert document.to_user.get() == self.to_user
|
||||||
|
|
||||||
@stderr_output('')
|
@stderr_output('')
|
||||||
|
@mock.patch.dict(os.environ, {'RECIPIENT': PRIVATE_RECIPIENT_EMAIL}, clear=True)
|
||||||
def test_attached_file_with_forced_sender_and_private_email(self):
|
def test_attached_file_with_forced_sender_and_private_email(self):
|
||||||
with MessageFile(
|
with MessageFile(
|
||||||
self.pjd_filetype,
|
self.pjd_filetype,
|
||||||
|
@ -174,9 +175,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
'coucou',
|
'coucou',
|
||||||
(('attached-file', 'content'),),
|
(('attached-file', 'content'),),
|
||||||
) as f:
|
) as f:
|
||||||
management.call_command(
|
management.call_command('sendmail', file=f.name, sender=self.expedition_user.username)
|
||||||
'sendmail', PRIVATE_RECIPIENT_EMAIL, file=f.name, sender=self.expedition_user.username
|
|
||||||
)
|
|
||||||
self.assertEqual(Document.objects.count(), 1)
|
self.assertEqual(Document.objects.count(), 1)
|
||||||
document = Document.objects.get()
|
document = Document.objects.get()
|
||||||
assert document.private is True
|
assert document.private is True
|
||||||
|
@ -189,6 +188,7 @@ class SendMailAttachedFileTestCase(TestCase):
|
||||||
assert document.to_user.get() == self.to_user
|
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):
|
def test_email_wrong_encoding(db, settings):
|
||||||
settings.MEDIA_ROOT = MEDIA_ROOT
|
settings.MEDIA_ROOT = MEDIA_ROOT
|
||||||
expedition_user = User.objects.create(username='expedition', id=1)
|
expedition_user = User.objects.create(username='expedition', id=1)
|
||||||
|
@ -196,7 +196,6 @@ def test_email_wrong_encoding(db, settings):
|
||||||
FileType.objects.create(name='QE-Question', id=2)
|
FileType.objects.create(name='QE-Question', id=2)
|
||||||
management.call_command(
|
management.call_command(
|
||||||
'sendmail',
|
'sendmail',
|
||||||
RECIPIENT_EMAIL,
|
|
||||||
file='tests/data/email-encoded-iso-8859-15-but-says-utf-8',
|
file='tests/data/email-encoded-iso-8859-15-but-says-utf-8',
|
||||||
sender=expedition_user.username,
|
sender=expedition_user.username,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue