tests: add initial testing of bounce processing

This commit is contained in:
Frédéric Péters 2014-12-26 19:35:15 +01:00
parent 53e328673f
commit 6d2df356dc
2 changed files with 49 additions and 13 deletions

View File

@ -0,0 +1,29 @@
import email
import StringIO
from wcs.ctl.process_bounce import CmdProcessBounce
def test_garbage():
msg = StringIO.StringIO('garbage')
addrs = CmdProcessBounce.get_bounce_addrs(msg)
assert addrs is None
def test_normal_email():
msg = email.message_from_string('test')
msg['From'] = 'bar@example.net'
msg['To'] = 'foo@example.net'
msg = StringIO.StringIO(msg.as_string())
addrs = CmdProcessBounce.get_bounce_addrs(msg)
assert addrs is None
def test_bounce_email():
msg = email.message_from_string('test')
msg['From'] = 'bar@example.net'
msg['To'] = 'foo@example.net'
# this is how exim adds failed recipients in its outgoing bounce emails
msg['x-failed-recipients'] = 'baz@example.net'
msg = StringIO.StringIO(msg.as_string())
addrs = CmdProcessBounce.get_bounce_addrs(msg)
assert addrs == ['baz@example.net']

View File

@ -42,19 +42,10 @@ class CmdProcessBounce(Command):
return
try:
parser = email.Parser.Parser()
bouncers_dir = os.path.join(os.path.dirname(__file__), 'Bouncers')
sys.path.append(bouncers_dir)
msg = parser.parse(sys.stdin)
for modname in BouncerAPI.BOUNCE_PIPELINE:
__import__(modname)
addrs = sys.modules[modname].process(msg)
if addrs is BouncerAPI.Stop:
return # Stop means to ignore message
if addrs:
break
else:
return # didn't find any match
addrs = self.get_bounce_addrs(sys.stdin)
if addrs is None:
# not a bounce
return
try:
to = msg['To']
@ -89,4 +80,20 @@ class CmdProcessBounce(Command):
pub.notify_of_exception(sys.exc_info(), context='[BOUNCE]')
sys.exit(1)
@classmethod
def get_bounce_addrs(cls, message_fd):
parser = email.Parser.Parser()
bouncers_dir = os.path.join(os.path.dirname(__file__), 'Bouncers')
sys.path.append(bouncers_dir)
msg = parser.parse(message_fd)
for modname in BouncerAPI.BOUNCE_PIPELINE:
__import__(modname)
addrs = sys.modules[modname].process(msg)
if addrs is BouncerAPI.Stop:
return None # Stop means to ignore message
if addrs:
return addrs
else:
return None # didn't find any match
CmdProcessBounce.register()