Merge lp:~macobo/mailman/macobo-rcpt-stage-reject into lp:mailman

Proposed by Karl-Aksel Puulmann
Status: Needs review
Proposed branch: lp:~macobo/mailman/macobo-rcpt-stage-reject
Merge into: lp:mailman
Diff against target: 82 lines (+48/-0)
2 files modified
src/mailman/runners/lmtp.py (+18/-0)
src/mailman/runners/tests/test_lmtp.py (+30/-0)
To merge this branch: bzr merge lp:~macobo/mailman/macobo-rcpt-stage-reject
Reviewer Review Type Date Requested Status
Mailman Coders Pending
Review via email: mp+157516@code.launchpad.net

Description of the change

It probably needs a little more work before merging.

Open questions:
1) Is the test naming okay?
2) Should the tests be done in a different way? I didn't find any other tests that used the mock library.
3) Should the mocking be done within the setUp stage?
4) If that's okay, should get_mocked_lmtp_channel be moved to tests/helpers.py?

To post a comment you must log in.
7213. By Karl-Aksel Puulmann

Remove white-space added by mistake

Unmerged revisions

7213. By Karl-Aksel Puulmann

Remove white-space added by mistake

7212. By Karl-Aksel Puulmann

Moved get_mocked_lmtp_channel to top of file

7211. By Karl-Aksel Puulmann

Fixed a LMTP bug, causing it not to validate email list addresses until DATA stage. (LP: 982555)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/mailman/runners/lmtp.py'
--- src/mailman/runners/lmtp.py 2013-01-20 21:23:09 +0000
+++ src/mailman/runners/lmtp.py 2013-04-06 14:55:22 +0000
@@ -147,6 +147,24 @@
147 """HELO is not a valid LMTP command."""147 """HELO is not a valid LMTP command."""
148 self.push(ERR_502)148 self.push(ERR_502)
149149
150 def smtp_RCPT(self, arg):
151 if not self._SMTPChannel__mailfrom:
152 self.push(b'503 Error: need MAIL command')
153 return
154 address = self._SMTPChannel__getaddr('TO:', arg) if arg else None
155 if not address:
156 self.push(b'501 Syntax: RCPT TO: <address>')
157 return
158 # Refresh the list of list names every time we process a message
159 # since the set of mailing lists could have changed.
160 listnames = set(getUtility(IListManager).names)
161 listname, subaddress, domain = split_recipient(address)
162 listname = listname.lower() + '@' + domain
163 if listname not in listnames:
164 self.push(ERR_550)
165 return
166 self._SMTPChannel__rcpttos.append(address)
167 self.push(b'250 Ok')
150168
151169
152170
153class LMTPRunner(Runner, smtpd.SMTPServer):171class LMTPRunner(Runner, smtpd.SMTPServer):
154172
=== modified file 'src/mailman/runners/tests/test_lmtp.py'
--- src/mailman/runners/tests/test_lmtp.py 2013-03-06 21:59:15 +0000
+++ src/mailman/runners/tests/test_lmtp.py 2013-04-06 14:55:22 +0000
@@ -29,8 +29,10 @@
29import smtplib29import smtplib
30import unittest30import unittest
3131
32from mock import Mock
32from datetime import datetime33from datetime import datetime
3334
35from mailman.runners.lmtp import Channel
34from mailman.config import config36from mailman.config import config
35from mailman.app.lifecycle import create_list37from mailman.app.lifecycle import create_list
36from mailman.database.transaction import transaction38from mailman.database.transaction import transaction
@@ -38,6 +40,14 @@
38from mailman.testing.layers import LMTPLayer40from mailman.testing.layers import LMTPLayer
3941
4042
43def get_mocked_lmtp_channel():
44 connection = Mock()
45 connection.getpeername = Mock(return_value='FakePeer')
46 channel = Channel(None, connection, None)
47 channel.push = Mock()
48 return channel
49
50
4151
4252
43class TestLMTP(unittest.TestCase):53class TestLMTP(unittest.TestCase):
44 """Test various aspects of the LMTP server."""54 """Test various aspects of the LMTP server."""
@@ -144,3 +154,23 @@
144 self.assertEqual(len(messages), 1)154 self.assertEqual(len(messages), 1)
145 self.assertEqual(messages[0].msgdata['listname'],155 self.assertEqual(messages[0].msgdata['listname'],
146 'my-list@example.com')156 'my-list@example.com')
157
158 def test_lp982555_RCPT_before_mail(self):
159 channel = get_mocked_lmtp_channel()
160 channel.smtp_RCPT('TO: <thislist@doesnotexist.com>')
161 channel.push.assert_called_with('503 Error: need MAIL command')
162
163 def test_lp982555_RCPT_existing_list(self):
164 with transaction():
165 create_list('my-list@example.com')
166 channel = get_mocked_lmtp_channel()
167 channel.smtp_MAIL('FROM: anne@example.com')
168 channel.smtp_RCPT('TO: <my-list@example.com>')
169 channel.push.assert_called_with('250 Ok')
170
171 def test_lp982555_RCPT_nonexisting_list(self):
172 channel = get_mocked_lmtp_channel()
173 channel.smtp_MAIL('FROM: anne@example.com')
174 channel.smtp_RCPT('TO: <thislist@doesnotexist.com>')
175 channel.push.assert_called_with(
176 '550 Requested action not taken: mailbox unavailable')