diff --git a/docbow_project/pfwb/management/commands/sendmail.py b/docbow_project/pfwb/management/commands/sendmail.py index 808656d..4a33bb0 100644 --- a/docbow_project/pfwb/management/commands/sendmail.py +++ b/docbow_project/pfwb/management/commands/sendmail.py @@ -4,6 +4,7 @@ import email.header import email.utils import logging import mailbox +import os import re import sys import time @@ -52,12 +53,10 @@ 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') 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: @@ -74,7 +73,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) @@ -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: diff --git a/docbow_project/pw/management/commands/sendmail.py b/docbow_project/pw/management/commands/sendmail.py index c5b7770..9a23729 100644 --- a/docbow_project/pw/management/commands/sendmail.py +++ b/docbow_project/pw/management/commands/sendmail.py @@ -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) diff --git a/tests/pfwb/test_pfwb.py b/tests/pfwb/test_pfwb.py index 1283d97..8157c83 100644 --- a/tests/pfwb/test_pfwb.py +++ b/tests/pfwb/test_pfwb.py @@ -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,10 @@ 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, '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 = '''\ @@ -133,9 +137,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', '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): @@ -282,6 +288,7 @@ class SendMailAttachedFileTestCase(TestCase): return force_bytes(message.as_string()) @stderr_output('') + @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( @@ -294,7 +301,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) self.assertEqual(Document.objects.count(), 1) document = Document.objects.get() assert document.attached_files.count() == 1 @@ -455,6 +462,9 @@ 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, 'SENDER': EXPEDITION_EMAIL}, clear=True + ): + management.call_command('sendmail', file=f.name) assert Document.objects.count() == 1 diff --git a/tests/pw/test_pw.py b/tests/pw/test_pw.py index d4a2184..4c84d98 100644 --- a/tests/pw/test_pw.py +++ b/tests/pw/test_pw.py @@ -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 @@ -189,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) @@ -196,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, )