This is an automated email from the ASF dual-hosted git repository. brondsem pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/allura.git
commit c0c57763149aa904ddc80a8b322700c695403a58 Author: Guillermo Cruz <[email protected]> AuthorDate: Tue Feb 4 14:46:27 2025 -0700 [#8574] updated test to make use of the BetterDebuggingServer class --- Allura/allura/tests/test_mail_util.py | 34 ++++++++++++++++++++-------- Allura/docs/getting_started/installation.rst | 2 +- AlluraTest/alluratest/smtp_debug.py | 8 ++++++- docker-compose.yml | 2 +- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Allura/allura/tests/test_mail_util.py b/Allura/allura/tests/test_mail_util.py index c549e5a01..19001b8d5 100644 --- a/Allura/allura/tests/test_mail_util.py +++ b/Allura/allura/tests/test_mail_util.py @@ -22,9 +22,9 @@ import pytest from ming.odm import ThreadLocalODMSession from tg import config as tg_config - +from smtplib import SMTP as SMTPClient from alluratest.controller import setup_basic_test, setup_global_objects -from allura.command.smtp_server import MailServer +from alluratest.smtp_debug import BetterDebuggingServer from allura.lib.utils import ConfigProxy from allura.app import Application from allura.lib.mail_util import ( @@ -38,7 +38,8 @@ ) from allura.lib.exceptions import AddressException from allura.tests import decorators as td - +from paste.deploy.converters import asint +from aiosmtpd.handlers import Debugging config = ConfigProxy( common_suffix='forgemail.domain', @@ -334,9 +335,24 @@ def setup_method(self, method): @mock.patch('allura.command.base.log', autospec=True) def test(self, log): - listen_port = ('0.0.0.0', 8825) - mailserver = MailServer(listen_port, None) - mailserver.process_message('127.0.0.1', '[email protected]', ['[email protected]'], - 'this is the email body with headers and everything Ο'.encode()) - assert [] == log.exception.call_args_list - assert log.info.call_args[0][0].startswith('Msg passed along as task '), log.info.call_args + mailserver = BetterDebuggingServer() + controller, handler = mailserver.start_server() + hostname = tg_config.get('forgemail.host', '0.0.0.0') + port = asint(tg_config.get('forgemail.port', 8825)) + with SMTPClient(hostname, port, timeout=0.1) as client: + resp = client.docmd("HELP", "HELO") + code, msg = client.ehlo("example.com") + assert code == 250 + client.sendmail('[email protected]', ['[email protected]'],""" + From: Anne Person <[email protected]> + To: Bart Person <[email protected]> + Subject: A test + Hi Bart, this is Anne. + """) + assert isinstance(handler, Debugging) + text = handler.stream.getvalue() + assert 'From: Anne Person <[email protected]>' in text + assert ' To: Bart Person <[email protected]>' in text + assert 'Subject: A test' in text + + diff --git a/Allura/docs/getting_started/installation.rst b/Allura/docs/getting_started/installation.rst index 972b58055..bb3771d56 100644 --- a/Allura/docs/getting_started/installation.rst +++ b/Allura/docs/getting_started/installation.rst @@ -341,7 +341,7 @@ your :file:`development.ini` (8826 by default). .. code-block:: bash - python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 -d + python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 This will create a new debugging server that discards messages and prints them to stdout. diff --git a/AlluraTest/alluratest/smtp_debug.py b/AlluraTest/alluratest/smtp_debug.py index 20db4fc32..b6cec8324 100644 --- a/AlluraTest/alluratest/smtp_debug.py +++ b/AlluraTest/alluratest/smtp_debug.py @@ -23,18 +23,24 @@ class BetterDebuggingServer: - def process_message(self, server, session, envelope): + def start_server(self): stream = StringIO() handler = Debugging(stream) # throws away the email hostname = tg.config.get('forgemail.host', '0.0.0.0') port = asint(tg.config.get('forgemail.port', 8825)) controller = Controller(handler, hostname=hostname, port=port) controller.start() + return controller, handler + async def handle_DATA(self, server, session, envelope): try: rcpttos = envelope.rcpt_tos + message = envelope.content.decode("utf-8") + message = "\n".join(message.split("\r\n")) print('TO: ' + ', '.join(rcpttos)) + print('FROM: ' + envelope.mail_from) + print(message) except Exception as error: return f'500 Could not process your message. Error {error}' return '250 OK' \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b0c0c28c5..a6c663959 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -64,7 +64,7 @@ services: environment: *env volumes_from: - web - command: python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 -d + command: python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 expose: - "8826"
