def test_run_validators(self): self.validated = 0 class Validators(WsgiValidators): custom_headers = ['X-Custom-Header'] def validate_ehlo(self2, ehlo): self.assertEqual('test', ehlo) self.validated += 1 def validate_sender(self2, sender): self.assertEqual('*****@*****.**', sender) self.validated += 2 def validate_recipient(self2, recipient): if recipient == '*****@*****.**': self.validated += 4 elif recipient == '*****@*****.**': self.validated += 8 else: raise AssertionError('bad recipient: ' + recipient) def validate_custom(self2, name, value): self.assertEqual('X-Custom-Header', name) self.assertEqual('custom test', value) self.validated += 16 w = WsgiEdge(None, validator_class=Validators) w._run_validators(self.environ) self.assertEqual(31, self.validated)
def test_ptr_lookup(self): environ = self.environ.copy() environ['REMOTE_ADDR'] = '1.2.3.4' resolver.query(IgnoreArg(), 'PTR').AndRaise(DNSException) resolver.query(IgnoreArg(), 'PTR').AndReturn(['example.com']) self.mox.ReplayAll() w = WsgiEdge(None) w._ptr_lookup(environ) self.assertNotIn('slimta.reverse_address', environ) w._ptr_lookup(environ) self.assertEqual('example.com', environ['slimta.reverse_address'])
def test_invalid_content_type(self): environ = self.environ.copy() environ['CONTENT_TYPE'] = 'text/plain' self.start_response.__call__('415 Unsupported Media Type', IsA(list)) self.mox.ReplayAll() w = WsgiEdge(self.queue) self.assertEqual([], w(environ, self.start_response))
def test_invalid_method(self): environ = self.environ.copy() environ['REQUEST_METHOD'] = 'PUT' self.start_response.__call__('405 Method Not Allowed', IsA(list)) self.mox.ReplayAll() w = WsgiEdge(self.queue) self.assertEqual([], w(environ, self.start_response))
def test_queueerror(self): self.queue.enqueue(IsA(Envelope)).AndReturn([(Envelope(), QueueError()) ]) self.start_response.__call__('500 Internal Server Error', IsA(list)) self.mox.ReplayAll() w = WsgiEdge(self.queue) self.assertEqual([], w(self.environ, self.start_response))
def test_unexpected_exception(self): environ = self.environ.copy() environ['wsgi.input'] = None self.start_response.__call__('500 Internal Server Error', IsA(list)) self.mox.ReplayAll() w = WsgiEdge(self.queue) assert_equal(["'NoneType' object has no attribute 'read'\n"], w(environ, self.start_response))
def test_invalid_path(self): environ = self.environ.copy() valid_paths = r'/good' environ['PATH_INFO'] = '/bad' self.start_response.__call__('404 Not Found', IsA(list)) self.mox.ReplayAll() w = WsgiEdge(self.queue, uri_pattern=valid_paths) self.assertEqual([], w(environ, self.start_response))
def test_run_validators(self): self.validated = 0 class Validators(WsgiValidators): custom_headers = ['X-Custom-Header'] def validate_ehlo(self2, ehlo): self.assertEqual('test', ehlo) self.validated += 1 def validate_sender(self2, sender): self.assertEqual('*****@*****.**', sender) self.validated += 2 def validate_recipient(self2, recipient): if recipient == '*****@*****.**': self.validated += 4 elif recipient == '*****@*****.**': self.validated += 8 else: raise AssertionError('bad recipient: '+recipient) def validate_custom(self2, name, value): self.assertEqual('X-Custom-Header', name) self.assertEqual('custom test', value) self.validated += 16 w = WsgiEdge(None, validator_class=Validators) w._run_validators(self.environ) self.assertEqual(31, self.validated)
def test_no_error(self): def verify_envelope(env): if not isinstance(env, Envelope): return False if '*****@*****.**' != env.sender: return False if '*****@*****.**' != env.recipients[0]: return False if '*****@*****.**' != env.recipients[1]: return False return True self.queue.enqueue(IsA(Envelope)).AndReturn([(Envelope(), 'testid')]) self.start_response.__call__('200 OK', IsA(list)) self.mox.ReplayAll() w = WsgiEdge(self.queue) assert_equal([], w(self.environ, self.start_response))
def _start_edge(self, name, options=None): if not options: options = getattr(self.cfg.edge, name) new_edges = [] queue_name = options.queue queue = self._start_queue(queue_name) if options.type == 'smtp': from slimta.edge.smtp import SmtpEdge from .helpers import build_smtpedge_validators from .helpers import fill_hostname_template kwargs = {} kwargs['context'] = self._get_server_ssl_context(options.tls) kwargs['tls_immediately'] = options.tls_immediately kwargs['validator_class'] = build_smtpedge_validators(options) kwargs['auth'] = [b'PLAIN', b'LOGIN'] kwargs['command_timeout'] = 20.0 kwargs['data_timeout'] = 30.0 kwargs['max_size'] = int(options.get('max_size', 10485760)) kwargs['hostname'] = fill_hostname_template(options.hostname) for listener in Listeners(options, 25): new_edge = SmtpEdge(listener, queue, **kwargs) if options.proxyprotocol: ProxyProtocol.mixin(new_edge) new_edge.start() self.edges.append(new_edge) elif options.type == 'http': from slimta.edge.wsgi import WsgiEdge from .helpers import build_wsgiedge_validators from .helpers import fill_hostname_template kwargs = {} kwargs['hostname'] = fill_hostname_template(options.hostname) kwargs['validator_class'] = build_wsgiedge_validators(options) kwargs['uri_pattern'] = options.uri kwargs['context'] = self._get_server_ssl_context(options.tls) listener = self._get_listener(options, 8025) for listener in Listeners(options, 8025): new_edge = WsgiEdge(queue, listener=listener, **kwargs) if options.proxyprotocol: ProxyProtocol.mixin(new_edge) new_edge.start() self.edges.append(new_edge) elif options.type == 'custom': new_edge = custom_factory(options, queue) self.edges.append(new_edge) else: msg = 'edge type does not exist: '+options.type raise ConfigValidationError(msg)
def test_ptr_lookup(self): environ = self.environ.copy() environ['REMOTE_ADDR'] = '1.2.3.4' self.mox.StubOutWithMock(dns_resolver, 'query') dns_resolver.query(IgnoreArg(), 'PTR').AndRaise(DNSException) dns_resolver.query(IgnoreArg(), 'PTR').AndReturn(['example.com']) self.mox.ReplayAll() w = WsgiEdge(None) w._ptr_lookup(environ) assert_not_in('slimta.reverse_address', environ) w._ptr_lookup(environ) assert_equal('example.com', environ['slimta.reverse_address'])