def mk_envelope(self, data, sender=None, recipients=None): env = Envelope(sender=sender) env.parse(data) if not recipients: recipients = ['root@localhost'] env.recipients = recipients return env
def test_deliver(self): result = AsyncResult() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test \x81\r\n') self.sock.sendall(b'EHLO there\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 8BITMIME\r\n') self.sock.sendall(b'MAIL FROM:<*****@*****.**>\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.sock.sendall(b'RCPT TO:<*****@*****.**>\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.sock.sendall(b'DATA\r\n') self.sock.recv(IsA(int)).AndReturn(b'354 Go ahead\r\n') self.sock.sendall( b'From: [email protected]\r\n\r\ntest test \x81\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), self.queue, socket_creator=self._socket_creator, ehlo_as='there') client._connect() client._ehlo() client._deliver(result, env) self.assertEqual({'*****@*****.**': Reply('250', 'Ok')}, result.get_nowait())
def test_attempt(self): self.mox.StubOutWithMock(subprocess, 'Popen') env = Envelope('*****@*****.**', ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') self._mock_popen('*****@*****.**', 0, '') self._mock_popen('*****@*****.**', 1337, 'transient') self._mock_popen('*****@*****.**', 1337, '5.0.0 permanent') subprocess.Popen(['relaytest', '-f', '*****@*****.**', '*****@*****.**'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).AndRaise(Timeout) self.mox.ReplayAll() m = PipeRelay(['relaytest', '-f', '{sender}', '{recipient}']) results = m.attempt(env, 0) self.assertEqual(4, len(results)) self.assertEqual(None, results['*****@*****.**']) self.assertIsInstance(results['*****@*****.**'], TransientRelayError) self.assertEqual('transient', str(results['*****@*****.**'])) self.assertEqual('450', results['*****@*****.**'].reply.code) self.assertIsInstance(results['*****@*****.**'], PermanentRelayError) self.assertEqual('5.0.0 permanent', str(results['*****@*****.**'])) self.assertEqual('550', results['*****@*****.**'].reply.code) self.assertIsInstance(results['*****@*****.**'], TransientRelayError) self.assertEqual('Delivery timed out', str(results['*****@*****.**'])) self.assertEqual('450', results['*****@*****.**'].reply.code)
def test_deliver_multircpt(self): result = self.mox.CreateMock(AsyncResult) env = Envelope( '*****@*****.**', ['*****@*****.**', '*****@*****.**', '*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') self.sock.sendall(b'LHLO test\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall( b'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n' ) self.sock.recv(IsA(int)).AndReturn( b'250 Ok\r\n250 Ok\r\n550 Nope\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall( b'From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n450 Yikes\r\n') result.set({ '*****@*****.**': None, '*****@*****.**': IsA(PermanentRelayError), '*****@*****.**': IsA(TransientRelayError) }) self.sock.sendall(b'RSET\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.mox.ReplayAll() client = LmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._ehlo() client._deliver(result, env)
def test_run_multiple(self): result1 = AsyncResult() result2 = AsyncResult() env1 = Envelope('*****@*****.**', ['*****@*****.**']) env1.parse(b'From: [email protected]\r\n\r\ntest test\r\n') env2 = Envelope('*****@*****.**', ['*****@*****.**']) env2.parse(b'From: [email protected]\r\n\r\ntest test\r\n') queue = BlockingDeque() queue.append((result1, env1)) queue.append((result2, env2)) self.sock.recv(IsA(int)).AndReturn(b'220 Welcome\r\n') self.sock.sendall(b'EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall(b'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall(b'From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.sock.sendall(b'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall(b'From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.sock.sendall(b'QUIT\r\n') self.sock.recv(IsA(int)).AndReturn(b'221 Goodbye\r\n') self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient('addr', queue, socket_creator=self._socket_creator, ehlo_as='test', idle_timeout=0.0) client._run() self.assertEqual({'*****@*****.**': Reply('250', 'Ok')}, result1.get_nowait()) self.assertEqual({'*****@*****.**': Reply('250', 'Ok')}, result2.get_nowait())
def test_deliver_conversion(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test \x81\r\n') self.sock.sendall(b'LHLO test\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall( b'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n' ) self.sock.recv( IsA(int)).AndReturn(b'250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall( b'From: [email protected]\r\nContent-Transfer-Encoding: base64\r\n\r\ndGVzdCB0ZXN0IIENCg==\n\r\n.\r\n' ) self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') result.set({'*****@*****.**': None}) self.mox.ReplayAll() client = LmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, ehlo_as='test', binary_encoder=encode_base64) client._connect() client._ehlo() client._deliver(result, env)
def test_run(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\r\n') queue = BlockingDeque() queue.append((result, env)) self.sock.recv(IsA(int)).AndReturn('220 Welcome\r\n') self.sock.sendall('EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn('250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall( 'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n' ) self.sock.recv( IsA(int)).AndReturn('250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall('From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') result.set(True) self.sock.sendall('QUIT\r\n') self.sock.recv(IsA(int)).AndReturn('221 Goodbye\r\n') self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient(None, queue, socket_creator=self._socket_creator, ehlo_as='test') client._run()
def test_parse_nonascii_headers(self): env = Envelope() env.parse(b'Subject: \xc3\xa9\xc3\xa9\n') try: self.assertEqual(b'\xc3\xa9\xc3\xa9', env.headers['subject'].encode()) except UnicodeDecodeError: self.assertEqual(b'\xc3\xa9\xc3\xa9', env.headers['subject'])
def test_add_message_id_header_existing(self): env = Envelope() env.parse(b'Message-Id: testing\r\n') amih = AddMessageIdHeader() self.assertEqual('testing', env.headers['Message-Id']) amih.apply(env) self.assertEqual('testing', env.headers['Message-Id'])
def message_to_envelope(message): generated_message = message.message() envelope = Envelope() envelope.parse(generated_message.as_bytes()) envelope.sender = message.from_email envelope.recipients.append(message.to[0]) return envelope
def test_add_message_id_header_existing(self): env = Envelope() env.parse('Message-Id: testing\r\n') amih = AddMessageIdHeader() self.assertEqual('testing', env.headers['Message-Id']) amih.apply(env) self.assertEqual('testing', env.headers['Message-Id'])
def test_recipientsplit_apply(self): env = Envelope('*****@*****.**', ['*****@*****.**', '*****@*****.**']) env.parse("""\ From: [email protected] To: [email protected] To: [email protected] test test\r """) policy = RecipientSplit() env1, env2 = policy.apply(env) assert_equal('*****@*****.**', env1.sender) assert_equal(['*****@*****.**'], env1.recipients) assert_equal('*****@*****.**', env1.headers['from']) assert_equal(['*****@*****.**', '*****@*****.**'], env1.headers.get_all('To')) assert_equal('test test\r\n', env1.message) assert_equal('*****@*****.**', env2.sender) assert_equal(['*****@*****.**'], env2.recipients) assert_equal('*****@*****.**', env2.headers['from']) assert_equal(['*****@*****.**', '*****@*****.**'], env2.headers.get_all('To')) assert_equal('test test\r\n', env2.message)
def test_add_date_header_existing(self): env = Envelope() env.parse('Date: testing\r\n') adh = AddDateHeader() self.assertEqual('testing', env.headers['Date']) adh.apply(env) self.assertEqual('testing', env.headers['Date'])
def as_envelope(self, must_raise=True): envelope = Envelope() headers_filters.process( self.headers, self, settings.TRANSACTIONAL['HEADERS_FILTERS_PARAMS']) headers = "" for key, value in self.headers.items(): if key in ('Subject', ): header = make_header(decode_header(value), header_name=key, maxlinelen=78) value = header.encode(linesep='\r\n') headers += "{}: {}\n".format(key, value) message = "" if self.message: message = self.message.content or "" if self.message is None and must_raise: raise Exception("Can't build this envelope because " "there is no RawMail attached to it.") envelope.parse(headers.encode('utf-8') + message.encode('utf-8')) envelope.sender = self.sender envelope.recipients.append(self.recipient) return envelope
def test_run_multiple(self): result1 = self.mox.CreateMock(AsyncResult) result2 = self.mox.CreateMock(AsyncResult) env1 = Envelope('*****@*****.**', ['*****@*****.**']) env1.parse('From: [email protected]\r\n\r\ntest test\r\n') env2 = Envelope('*****@*****.**', ['*****@*****.**']) env2.parse('From: [email protected]\r\n\r\ntest test\r\n') queue = BlockingDeque() queue.append((result1, env1)) queue.append((result2, env2)) self.sock.recv(IsA(int)).AndReturn('220 Welcome\r\n') self.sock.sendall('EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn('250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall('MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall('From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') result1.set([None]) self.sock.sendall('MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall('From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') result2.set([None]) self.sock.sendall('QUIT\r\n') self.sock.recv(IsA(int)).AndReturn('221 Goodbye\r\n') self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient(None, queue, socket_creator=self._socket_creator, ehlo_as='test', idle_timeout=0.0) client._run()
def test_recipientsplit_apply(self): env = Envelope('*****@*****.**', ['*****@*****.**', '*****@*****.**']) env.parse(b"""\ From: [email protected] To: [email protected] To: [email protected] test test\r """) policy = RecipientSplit() env1, env2 = policy.apply(env) self.assertEqual('*****@*****.**', env1.sender) self.assertEqual(['*****@*****.**'], env1.recipients) self.assertEqual('*****@*****.**', env1.headers['from']) self.assertEqual(['*****@*****.**', '*****@*****.**'], env1.headers.get_all('To')) self.assertEqual(b'test test\r\n', env1.message) self.assertEqual('*****@*****.**', env2.sender) self.assertEqual(['*****@*****.**'], env2.recipients) self.assertEqual('*****@*****.**', env2.headers['from']) self.assertEqual(['*****@*****.**', '*****@*****.**'], env2.headers.get_all('To')) self.assertEqual(b'test test\r\n', env2.message)
def test_deliver_baddata(self): result = AsyncResult() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') self.sock.sendall(b'EHLO there\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall( b'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n' ) self.sock.recv( IsA(int)).AndReturn(b'250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall( b'From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'450 Yikes\r\n') self.sock.sendall(b'RSET\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), self.queue, socket_creator=self._socket_creator, ehlo_as='there') client._connect() client._ehlo() client._deliver(result, env) with self.assertRaises(TransientRelayError): result.get_nowait()
def test_deliver_conversion(self): result = AsyncResult() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test \x81\r\n') self.sock.sendall(b'EHLO there\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall( b'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n' ) self.sock.recv( IsA(int)).AndReturn(b'250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') if pycompat.PY3: self.sock.sendall( b'From: [email protected]\r\nContent-Transfer-Encoding: base64\r\n\r\ndGVzdCB0ZXN0IIEK\r\n.\r\n' ) else: self.sock.sendall( b'From: [email protected]\r\nContent-Transfer-Encoding: base64\r\n\r\ndGVzdCB0ZXN0IIENCg==\r\n.\r\n' ) self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), self.queue, socket_creator=self._socket_creator, ehlo_as='there', binary_encoder=encode_base64) client._connect() client._ehlo() client._deliver(result, env) self.assertEqual({'*****@*****.**': Reply('250', 'Ok')}, result.get_nowait())
def test_run(self): result = AsyncResult() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') queue = BlockingDeque() queue.append((result, env)) self.sock.recv(IsA(int)).AndReturn(b'220 Welcome\r\n') self.sock.sendall(b'EHLO there\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall( b'MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n' ) self.sock.recv( IsA(int)).AndReturn(b'250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall( b'From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.sock.sendall(b'QUIT\r\n') self.sock.recv(IsA(int)).AndReturn(b'221 Goodbye\r\n') self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), queue, socket_creator=self._socket_creator, ehlo_as='there') client._run() self.assertEqual({'*****@*****.**': Reply('250', 'Ok')}, result.get_nowait())
def as_envelope(self): message = self.as_message() envelope = Envelope() envelope.parse(message.message().as_bytes()) envelope.sender = message.from_email envelope.recipients.append(self.recipient) return envelope
def test_add_date_header(self): env = Envelope() env.parse('') env.timestamp = 1234567890 adh = AddDateHeader() self.assertEqual(None, env.headers['Date']) adh.apply(env) self.assertTrue(env.headers['Date'])
def test_add_date_header(self): env = Envelope() env.parse(b'') env.timestamp = 1234567890 adh = AddDateHeader() self.assertEqual(None, env.headers['Date']) adh.apply(env) self.assertTrue(env.headers['Date'])
def test_add_date_header_existing(self): env = Envelope() epoch = 'Thu, 01 Jan 1970 00:00:00 -0000' env.parse(b'Date: ' + epoch.encode() + b'\r\n') adh = AddDateHeader() self.assertEqual(epoch, env.headers['Date']) adh.apply(env) self.assertEqual(epoch, env.headers['Date'])
def test_add_date_header_existing(self): env = Envelope() epoch = 'Thu, 01 Jan 1970 00:00:00 -0000' env.parse(b'Date: '+epoch.encode()+b'\r\n') adh = AddDateHeader() self.assertEqual(epoch, env.headers['Date']) adh.apply(env) self.assertEqual(epoch, env.headers['Date'])
def test_add_headers(self): self.data['*****@*****.**'] = {'add_headers': '{"X-Test-A": "one"}'} self.data['*****@*****.**'] = {'add_headers': '{"X-Test-B": "two"}'} env = Envelope('*****@*****.**', ['*****@*****.**', '*****@*****.**']) env.parse(b"""\n\n""") self.policy.apply(env) self.assertEquals('one', env.headers['x-test-a']) self.assertEquals('two', env.headers['x-test-b'])
def test_parse_onlyheaders(self): env = Envelope() env.parse("""\ From: [email protected] Subject: important things """) assert_equal('*****@*****.**', env.headers['from']) assert_equal('important things', env.headers['subject']) assert_equal('', env.message)
def _get_envelope(self, environ): sender = self._get_sender(environ) recipients = self._get_recipients(environ) env = Envelope(sender, recipients) content_length = int(environ.get('CONTENT_LENGTH', 0)) data = environ['wsgi.input'].read(content_length) env.parse(data) return env
def test_parse_onlyheaders(self): env = Envelope() env.parse("""\ From: [email protected] Subject: important things """) self.assertEqual('*****@*****.**', env.headers['from']) self.assertEqual('important things', env.headers['subject']) self.assertEqual('', env.message)
def test_add_message_id_header(self): env = Envelope() env.parse('') env.timestamp = 1234567890 amih = AddMessageIdHeader('example.com') self.assertEqual(None, env.headers['Message-Id']) amih.apply(env) pattern = r'^<[0-9a-fA-F]{32}\[email protected]>$' self.assertRegexpMatches(env.headers['Message-Id'], pattern)
def test_apply(self): env = Envelope() env.parse(b"""X-Spam-Status: NO\r\n\r\n""") self.mox.StubOutWithMock(self.sa, 'scan') self.sa.scan(env).AndReturn((True, ['one', 'two'])) self.mox.ReplayAll() self.sa.apply(env) self.assertEqual('YES', env.headers['X-Spam-Status']) self.assertEqual('one, two', env.headers['X-Spam-Symbols'])
def test_add_message_id_header(self): env = Envelope() env.parse(b'') env.timestamp = 1234567890 amih = AddMessageIdHeader('example.com') self.assertEqual(None, env.headers['Message-Id']) amih.apply(env) pattern = r'^<[0-9a-fA-F]{32}\[email protected]>$' self.assertRegexpMatches(env.headers['Message-Id'], pattern)
def test_parse_onlyheaders(self): env = Envelope() env.parse(b"""\ From: [email protected] Subject: important things """.replace(b'\n', b'\r\n')) self.assertEqual('*****@*****.**', env.headers['from']) self.assertEqual('important things', env.headers['subject']) self.assertEqual(b'', env.message)
def test_add_headers(self): self.data['*****@*****.**'] = { 'add_headers': '{"X-Test-A": "one"}' } self.data['*****@*****.**'] = {'add_headers': '{"X-Test-B": "two"}'} env = Envelope('*****@*****.**', ['*****@*****.**', '*****@*****.**']) env.parse(b"""\n\n""") self.address_policy.apply(env) self.assertEquals('one', env.headers['x-test-a']) self.assertEquals('two', env.headers['x-test-b'])
def test_parse_message_object(self): data = Message() data['From'] = '*****@*****.**' data['To'] = '*****@*****.**' data['To'] = '*****@*****.**' data.set_payload('test test\r\n') env = Envelope() env.parse(data) assert_equal('*****@*****.**', env.headers['from']) assert_equal(['*****@*****.**', '*****@*****.**'], env.headers.get_all('to')) assert_equal('test test\r\n', env.message)
def test_send_envelope_conversion_failure(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test \x81\r\n') self.sock.sendall('EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn('250-Hello\r\n250 PIPELINING\r\n') result.set_exception(IsA(PermanentRelayError)) self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._ehlo() client._send_envelope(result, env)
def test_parse_message_object(self): data = Message() data['From'] = '*****@*****.**' data['To'] = '*****@*****.**' data['To'] = '*****@*****.**' data.set_payload('test test\r\n') env = Envelope() env.parse(data) self.assertEqual('*****@*****.**', env.headers['from']) self.assertEqual(['*****@*****.**', '*****@*****.**'], env.headers.get_all('to')) self.assertEqual('test test\r\n', env.message)
def test_attempt_timeout(self): self.mox.StubOutWithMock(subprocess, 'Popen') env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') subprocess.Popen(['maildrop', '-f', '*****@*****.**'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).AndRaise(Timeout) self.mox.ReplayAll() m = MaildropRelay() with self.assertRaises(TransientRelayError): m.attempt(env, 0)
def test_parse(self): env = Envelope() env.parse("""\ From: [email protected] To: [email protected] To: [email protected] test test\r """) assert_equal('*****@*****.**', env.headers['from']) assert_equal(['*****@*****.**', '*****@*****.**'], env.headers.get_all('To')) assert_equal('test test\r\n', env.message)
def test_add_received_header(self): env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('') env.timestamp = 1234567890 env.client['name'] = 'mail.example.com' env.client['ip'] = '1.2.3.4' env.client['protocol'] = 'ESMTPS' env.receiver = 'test.com' arh = AddReceivedHeader() arh.apply(env) self.assertRegexpMatches(env.headers['Received'], r'from mail\.example\.com \(unknown \[1.2.3.4\]\) by test.com ' r'\(slimta [^\)]+\) with ESMTPS for <*****@*****.**>; ')
def test_parse(self): env = Envelope() env.parse(b"""\ From: [email protected] To: [email protected] To: [email protected] test test """.replace(b'\n', b'\r\n')) self.assertEqual('*****@*****.**', env.headers['from']) self.assertEqual(['*****@*****.**', '*****@*****.**'], env.headers.get_all('To')) self.assertEqual(b'test test\r\n', env.message)
def test_flatten(self): header_str = b"""\ From: [email protected] To: [email protected] To: [email protected] """.replace(b'\n', b'\r\n') body_str = b'test test\r\n' env = Envelope() env.parse(header_str + body_str) ret_headers, ret_body = env.flatten() self.assertEqual(header_str, ret_headers) self.assertEqual(body_str, ret_body)
def test_recipientdomainsplit_apply_allbadrcpts(self): env = Envelope('*****@*****.**', ['rcpt1', 'rcpt2@']) env.parse("""\r\n\r\ntest test\r\n""") policy = RecipientDomainSplit() env1, env2 = policy.apply(env) assert_equal('*****@*****.**', env1.sender) assert_equal(['rcpt1'], env1.recipients) assert_equal('test test\r\n', env1.message) assert_equal('*****@*****.**', env2.sender) assert_equal(['rcpt2@'], env2.recipients) assert_equal('test test\r\n', env2.message)
def test_send_message_data(self): env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\r\n') self.sock.sendall('From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.sock.sendall('From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn('550 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator) client._connect() client._send_message_data(env) with self.assertRaises(PermanentRelayError): client._send_message_data(env)
def test_send_message_data(self): env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') self.sock.sendall(b'From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.sock.sendall(b'From: [email protected]\r\n\r\ntest test\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'550 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator) client._connect() client._send_message_data(env) with self.assertRaises(PermanentRelayError): client._send_message_data(env)
def as_envelope(self): envelope = Envelope() headers = "" for key, value in self.headers.items(): headers += "{}: {}\n".format(key, value) if self.message is None: raise Exception("Can't build this envelope because " "there is no RawMail attached to it.") message = self.message.content or "" envelope.parse(headers.encode('utf-8') + message.encode('utf-8')) envelope.sender = self.sender envelope.recipients.append(self.recipient) return envelope
def test_recipientdomainsplit_apply_allbadrcpts(self): env = Envelope('*****@*****.**', ['rcpt1', 'rcpt2@']) env.parse(b"""\r\ntest test\r\n""") policy = RecipientDomainSplit() env1, env2 = policy.apply(env) self.assertEqual('*****@*****.**', env1.sender) self.assertEqual(['rcpt1'], env1.recipients) self.assertEqual(b'test test\r\n', env1.message) self.assertEqual('*****@*****.**', env2.sender) self.assertEqual(['rcpt2@'], env2.recipients) self.assertEqual(b'test test\r\n', env2.message)
def test_deliver_conversion_failure(self): result = self.mox.CreateMockAnything() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test \x81\r\n') self.sock.sendall(b'LHLO there\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall(b'RSET\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') result.set_exception(IsA(PermanentRelayError)) self.mox.ReplayAll() client = LmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, ehlo_as='there') client._connect() client._ehlo() client._deliver(result, env)
def test_run_banner_failure(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\r\n') queue = BlockingDeque() queue.append((result, env)) self.sock.recv(IsA(int)).AndReturn('520 Not Welcome\r\n') result.set_exception(IsA(PermanentRelayError)) self.sock.sendall('QUIT\r\n') self.sock.recv(IsA(int)).AndReturn('221 Goodbye\r\n') self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient(None, queue, socket_creator=self._socket_creator, ehlo_as='test') client._run()
def test_run_banner_failure(self): result = AsyncResult() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\r\n') queue = BlockingDeque() queue.append((result, env)) self.sock.recv(IsA(int)).AndReturn(b'520 Not Welcome\r\n') self.sock.sendall(b'QUIT\r\n') self.sock.recv(IsA(int)).AndReturn(b'221 Goodbye\r\n') self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient('addr', queue, socket_creator=self._socket_creator, ehlo_as='test') client._run() with self.assertRaises(PermanentRelayError): result.get_nowait()
def test_send_envelope_conversion(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test \x81\r\n') self.sock.sendall('EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn('250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall('MAIL FROM:<*****@*****.**>\r\nRCPT TO:<*****@*****.**>\r\nDATA\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n250 Ok\r\n354 Go ahead\r\n') self.sock.sendall('From: [email protected]\r\nContent-Transfer-Encoding: base64\r\n\r\ndGVzdCB0ZXN0IIENCg==\n\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator, ehlo_as='test', binary_encoder=encode_base64) client._connect() client._ehlo() client._send_envelope(result, env)
def test_deliver_conversion_failure(self): result = AsyncResult() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test \x81\r\n') self.sock.sendall(b'EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 PIPELINING\r\n') self.sock.sendall(b'RSET\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._ehlo() client._deliver(result, env) with self.assertRaises(PermanentRelayError): result.get_nowait()
def test_run_timeout(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\r\n') queue = BlockingDeque() queue.append((result, env)) self.sock.recv(IsA(int)).AndRaise(Timeout(0.0)) result.ready().AndReturn(False) result.set_exception(IsA(TransientRelayError)) self.sock.sendall('QUIT\r\n') self.sock.recv(IsA(int)).AndReturn('221 Goodbye\r\n') self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient(None, queue, socket_creator=self._socket_creator, ehlo_as='test') client._run()