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_connect_failure(self): def socket_creator(address): raise socket_error(None, None) self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=socket_creator) with self.assertRaises(TransientRelayError): client._connect()
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_rset(self): self.sock.sendall('RSET\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) client._connect() client._rset()
def test_connect_failure(self): def socket_creator(address): raise socket_error(None, None) self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=socket_creator) with self.assertRaises(TransientRelayError): client._connect()
def test_rcptto(self): self.sock.sendall('RCPT TO:<recipient>\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) client._connect() client._rcptto('recipient')
def test_rset(self): 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) client._connect() client._rset()
def test_starttls(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.sendall("STARTTLS\r\n") sock.recv(IsA(int)).AndReturn("220 Go ahead\r\n") sock.tls_wrapper(sock, self.tls_args).AndReturn(sock) sock.sendall("STARTTLS\r\n") sock.recv(IsA(int)).AndReturn("420 Stop\r\n") self.mox.ReplayAll() client = SmtpRelayClient( None, self.queue, socket_creator=socket_creator, tls=self.tls_args, tls_wrapper=sock.tls_wrapper, tls_required=True, ) client._connect() client._starttls() with self.assertRaises(TransientRelayError): client._starttls()
def test_handshake_starttls(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.recv(IsA(int)).AndReturn("220 Welcome\r\n") sock.sendall("EHLO test\r\n") sock.recv(IsA(int)).AndReturn("250-Hello\r\n250 STARTTLS\r\n") sock.sendall("STARTTLS\r\n") sock.recv(IsA(int)).AndReturn("220 Go ahead\r\n") sock.tls_wrapper(sock, self.tls_args).AndReturn(sock) sock.sendall("EHLO test\r\n") sock.recv(IsA(int)).AndReturn("250 Hello\r\n") self.mox.ReplayAll() client = SmtpRelayClient( None, self.queue, socket_creator=socket_creator, tls=self.tls_args, tls_wrapper=sock.tls_wrapper, ehlo_as="test", ) client._connect() client._handshake()
def test_handshake_authenticate_callable(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.recv(IsA(int)).AndReturn(b'220 Welcome\r\n') sock.sendall(b'EHLO test\r\n') sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 AUTH PLAIN\r\n') sock.sendall(b'AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AcGFzc3dk\r\n') sock.recv(IsA(int)).AndReturn(b'235 Ok\r\n') self.mox.ReplayAll() def yield_creds(): yield '*****@*****.**' yield 'passwd' client = SmtpRelayClient('addr', self.queue, socket_creator=socket_creator, credentials=yield_creds, ehlo_as='test') client._connect() client._handshake()
def test_rcptto(self): self.sock.sendall(b'RCPT TO:<recipient>\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) client._connect() client._rcptto('recipient')
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_disconnect(self): 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', self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._disconnect()
def test_disconnect_failure(self): self.sock.sendall('QUIT\r\n') self.sock.recv(IsA(int)).AndRaise(socket_error(None, None)) self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._disconnect()
def test_disconnect(self): 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, self.queue, socket_creator=self._socket_creator, ehlo_as="test") client._connect() client._disconnect()
def test_disconnect_failure(self): self.sock.sendall(b'QUIT\r\n') self.sock.recv(IsA(int)).AndRaise(socket_error(None, None)) self.sock.close() self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._disconnect()
def test_disconnect(self): 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, self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._disconnect()
def test_banner(self): self.sock.recv(IsA(int)).AndReturn('220 Welcome\r\n') self.sock.recv(IsA(int)).AndReturn('420 Not Welcome\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator) client._connect() client._banner() with self.assertRaises(TransientRelayError): client._banner()
def test_banner(self): self.sock.recv(IsA(int)).AndReturn(b'220 Welcome\r\n') self.sock.recv(IsA(int)).AndReturn(b'420 Not Welcome\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator) client._connect() client._banner() with self.assertRaises(TransientRelayError): client._banner()
def test_handshake_tls_immediately(self): self.context.wrap_socket(self.sock, server_hostname='addr').AndReturn(self.sock) 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\n') self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), self.queue, socket_creator=self._socket_creator, context=self.context, tls_immediately=True, ehlo_as='there') client._connect() client._handshake()
def test_handshake_tls_immediately(self): tls_wrapper = self.mox.CreateMockAnything() tls_wrapper(self.sock, self.tls_args).AndReturn(self.sock) 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\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, tls=self.tls_args, tls_wrapper=tls_wrapper, tls_immediately=True, ehlo_as='there') client._connect() client._handshake()
def test_handshake_authenticate(self): 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 AUTH PLAIN\r\n') self.sock.sendall(b'AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AcGFzc3dk\r\n') self.sock.recv(IsA(int)).AndReturn(b'235 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), self.queue, socket_creator=self._socket_creator, credentials=('*****@*****.**', 'passwd'), ehlo_as='there') client._connect() client._handshake()
def test_ehlo(self): self.sock.sendall(b'EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 TEST\r\n') self.sock.sendall(b'EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn(b'420 Goodbye\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._ehlo() with self.assertRaises(TransientRelayError): client._ehlo()
def test_mailfrom(self): self.sock.sendall('MAIL FROM:<sender>\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.sock.sendall('MAIL FROM:<sender>\r\n') self.sock.recv(IsA(int)).AndReturn('550 Not Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator) client._connect() client._mailfrom('sender') with self.assertRaises(PermanentRelayError): client._mailfrom('sender')
def test_handshake_authenticate_badcreds(self): 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 AUTH PLAIN\r\n') self.sock.sendall(b'AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AcGFzc3dk\r\n') self.sock.recv(IsA(int)).AndReturn(b'535 Nope!\r\n') self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), self.queue, socket_creator=self._socket_creator, credentials=('*****@*****.**', 'passwd'), ehlo_as='there', auth_mechanism=b'PLAIN') client._connect() with self.assertRaises(PermanentRelayError): client._handshake()
def test_ehlo(self): self.sock.sendall('EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn('250-Hello\r\n250 TEST\r\n') self.sock.sendall('EHLO test\r\n') self.sock.recv(IsA(int)).AndReturn('420 Goodbye\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._ehlo() with self.assertRaises(TransientRelayError): client._ehlo()
def test_rcptto(self): self.sock.sendall('RCPT TO:<recipient>\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.sock.sendall('RCPT TO:<recipient>\r\n') self.sock.recv(IsA(int)).AndReturn('550 Not Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator) client._connect() client._rcptto('recipient') with self.assertRaises(PermanentRelayError): client._rcptto('recipient')
def test_mailfrom(self): self.sock.sendall(b'MAIL FROM:<sender>\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.sock.sendall(b'MAIL FROM:<sender>\r\n') self.sock.recv(IsA(int)).AndReturn(b'550 Not Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator) client._connect() client._mailfrom('sender') with self.assertRaises(PermanentRelayError): client._mailfrom('sender')
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_starttls(self): self.sock.sendall(b'STARTTLS\r\n') self.sock.recv(IsA(int)).AndReturn(b'220 Go ahead\r\n') self.context.wrap_socket(self.sock, server_hostname='addr').AndReturn(self.sock) self.sock.sendall(b'STARTTLS\r\n') self.sock.recv(IsA(int)).AndReturn(b'420 Stop\r\n') self.mox.ReplayAll() client = SmtpRelayClient(('addr', 0), self.queue, socket_creator=self._socket_creator, context=self.context, tls_required=True) client._connect() client._starttls() with self.assertRaises(TransientRelayError): client._starttls()
def test_handshake_tls_immediately(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.tls_wrapper(sock, self.tls_args).AndReturn(sock) sock.recv(IsA(int)).AndReturn(b'220 Welcome\r\n') sock.sendall(b'EHLO test\r\n') sock.recv(IsA(int)).AndReturn(b'250 Hello\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=socket_creator, tls=self.tls_args, tls_wrapper=sock.tls_wrapper, tls_immediately=True, ehlo_as='test') client._connect() client._handshake()
def test_starttls(self): tls_wrapper = self.mox.CreateMockAnything() self.sock.sendall(b'STARTTLS\r\n') self.sock.recv(IsA(int)).AndReturn(b'220 Go ahead\r\n') tls_wrapper(self.sock, self.tls_args).AndReturn(self.sock) self.sock.sendall(b'STARTTLS\r\n') self.sock.recv(IsA(int)).AndReturn(b'420 Stop\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, tls=self.tls_args, tls_wrapper=tls_wrapper, tls_required=True) client._connect() client._starttls() with self.assertRaises(TransientRelayError): client._starttls()
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_handshake_tls_immediately(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.tls_wrapper(sock, self.tls_args).AndReturn(sock) sock.recv(IsA(int)).AndReturn('220 Welcome\r\n') sock.sendall('EHLO test\r\n') sock.recv(IsA(int)).AndReturn('250 Hello\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=socket_creator, tls=self.tls_args, tls_wrapper=sock.tls_wrapper, tls_immediately=True, ehlo_as='test') client._connect() client._handshake()
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 test_handshake_authenticate(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.recv(IsA(int)).AndReturn(b'220 Welcome\r\n') sock.sendall(b'EHLO test\r\n') sock.recv(IsA(int)).AndReturn(b'250-Hello\r\n250 AUTH PLAIN\r\n') sock.sendall(b'AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AcGFzc3dk\r\n') sock.recv(IsA(int)).AndReturn(b'235 Ok\r\n') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=socket_creator, credentials=('*****@*****.**', 'passwd'), ehlo_as='test') client._connect() client._handshake()
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_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_handshake_authenticate_badcreds(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.recv(IsA(int)).AndReturn('220 Welcome\r\n') sock.sendall('EHLO test\r\n') sock.recv(IsA(int)).AndReturn('250-Hello\r\n250 AUTH PLAIN\r\n') sock.sendall('AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AcGFzc3dk\r\n') sock.recv(IsA(int)).AndReturn('535 Nope!\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=socket_creator, credentials=('*****@*****.**', 'passwd'), ehlo_as='test') client._connect() with self.assertRaises(PermanentRelayError): client._handshake()
def test_deliver_badrcpts(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\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\n550 Not ok\r\n354 Go ahead\r\n') self.sock.sendall('.\r\nRSET\r\n') self.sock.recv(IsA(int)).AndReturn('550 Yikes\r\n250 Ok\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._deliver(result, env)
def test_starttls(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 def socket_creator(address): return sock sock.sendall('STARTTLS\r\n') sock.recv(IsA(int)).AndReturn('220 Go ahead\r\n') sock.tls_wrapper(sock, self.tls_args).AndReturn(sock) sock.sendall('STARTTLS\r\n') sock.recv(IsA(int)).AndReturn('420 Stop\r\n') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=socket_creator, tls=self.tls_args, tls_wrapper=sock.tls_wrapper, tls_required=True) client._connect() client._starttls() with self.assertRaises(TransientRelayError): client._starttls()
def test_send_envelope(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\r\n') self.sock.sendall('MAIL FROM:<*****@*****.**>\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.sock.sendall('RCPT TO:<*****@*****.**>\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.sock.sendall('DATA\r\n') self.sock.recv(IsA(int)).AndReturn('354 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') self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator) client._connect() client._send_envelope(result, env)
def test_send_envelope_badrcpts(self): result = self.mox.CreateMock(AsyncResult) env = Envelope("*****@*****.**", ["*****@*****.**"]) env.parse("From: [email protected]\r\n\r\ntest test\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\n550 Not ok\r\n354 Go ahead\r\n") self.sock.sendall(".\r\nRSET\r\n") self.sock.recv(IsA(int)).AndReturn("550 Yikes\r\n250 Ok\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_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 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') self.mox.ReplayAll() client = SmtpRelayClient('addr', self.queue, socket_creator=self._socket_creator, ehlo_as='test', binary_encoder=encode_base64) client._connect() client._ehlo() client._deliver(result, env) self.assertEqual({'*****@*****.**': Reply('250', 'Ok')}, result.get_nowait())
def test_deliver_rset_exception(self): result = self.mox.CreateMock(AsyncResult) env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse('From: [email protected]\r\n\r\ntest test\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\n450 No!\r\n') result.set_exception(IsA(TransientRelayError)) self.sock.sendall('RSET\r\n') self.sock.recv(IsA(int)).AndRaise(ConnectionLost) self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._ehlo() with self.assertRaises(ConnectionLost): client._deliver(result, env)
def test_deliver_badrcpts(self): result = AsyncResult() env = Envelope('*****@*****.**', ['*****@*****.**']) env.parse(b'From: [email protected]\r\n\r\ntest test\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\n550 Not ok\r\n354 Go ahead\r\n') self.sock.sendall(b'.\r\nRSET\r\n') self.sock.recv(IsA(int)).AndReturn(b'550 Yikes\r\n250 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_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 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') 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(PermanentRelayError): result.get_nowait()
def test_deliver(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 8BITMIME\r\n') self.sock.sendall('MAIL FROM:<*****@*****.**>\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.sock.sendall('RCPT TO:<*****@*****.**>\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.sock.sendall('DATA\r\n') self.sock.recv(IsA(int)).AndReturn('354 Go ahead\r\n') self.sock.sendall('From: [email protected]\r\n\r\ntest test \x81\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') result.set([None]) self.mox.ReplayAll() client = SmtpRelayClient(None, self.queue, socket_creator=self._socket_creator, ehlo_as='test') client._connect() client._ehlo() client._deliver(result, env)