def test_valid_prefix(self): recipient = '*****@*****.**' dsn = DSN_REPORT.replace('||TO||', recipient) dsn = dsn.replace('||RETURNPATH||', 'foo@bar') message = email.message_from_string(dsn) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] BackMuncherQueuePolicy().apply(envelope)
def test_invalide_returnpath(self): recipient = '*****@*****.**' arf = ARF_REPORT.replace('||TO||', '*****@*****.**') arf = arf.replace('||RETURNPATH||', recipient) message = email.message_from_string(arf) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = ARFHandler(envelope, message) self.assertIsNone(handler.apply())
def test_invalid_recipient(self): recipient = '*****@*****.**' dsn = DSN_REPORT.replace('||TO||', recipient) dsn = dsn.replace('||RETURNPATH||', recipient) message = email.message_from_string(dsn) envelope = Envelope() envelope.parse_msg(message) with self.assertRaises(QueueError) as exc: BackMuncherQueuePolicy().apply(envelope) self.assertEqual(str(exc.exception), 'Missing recipient')
def test_invalide_recipient(self): recipient = '*****@*****.**' dsn = DSN_REPORT.replace('||TO||', recipient) dsn = dsn.replace('||RETURNPATH||', 'foo@bar') message = email.message_from_string(dsn) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = DSNHandler(envelope, message) self.assertIsNone(handler.apply())
def test_invalide_returnpath(self): recipient = '*****@*****.**' unsubscribe = UNSUBSCRIBE_EMAIL.replace('||TO||', recipient) unsubscribe = unsubscribe.replace('||RETURNPATH||', recipient) message = email.message_from_string(unsubscribe) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = UnsubscribeHandler(envelope, message) self.assertIsNone(handler.apply())
def test_invalid_return_path(self): recipient = 'foo@bar' dsn = DSN_REPORT.replace('||TO||', recipient) dsn = dsn.replace('||RETURNPATH||', recipient) message = email.message_from_string(dsn) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] with self.assertRaises(QueueError) as exc: BackMuncherQueuePolicy().apply(envelope) self.assertEqual(str(exc.exception), 'Domain not valid')
def test_parse_message_object(self): msg = Message() msg['From'] = '*****@*****.**' msg['To'] = '*****@*****.**' msg['To'] = '*****@*****.**' msg.set_payload(b'test test\r\n') env = Envelope() env.parse_msg(msg) self.assertEqual('*****@*****.**', env.headers['from']) self.assertEqual(['*****@*****.**', '*****@*****.**'], env.headers.get_all('to')) self.assertEqual(b'test test\r\n', env.message)
def test_parse(self): recipient = 'unsubscribe-{}@test.munch.example.com'.format( get_mail_identifier()) unsubscribe = UNSUBSCRIBE_EMAIL.replace('||TO||', recipient) unsubscribe = unsubscribe.replace('||RETURNPATH||', recipient) message = email.message_from_string(unsubscribe) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = UnsubscribeHandler(envelope, message) main_headers = dict(handler.message) self.assertEqual(main_headers['To'], recipient)
def test_valid_arf(self): user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) recipient = 'return-{}@test.munch.example.com'.format( mail.identifier) arf = ARF_REPORT.replace('||TO||', '*****@*****.**') arf = arf.replace('||RETURNPATH||', recipient) message = email.message_from_string(arf) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = ARFHandler(envelope, message) self.assertIsNotNone(handler.apply())
def test_valid_unsubscribe(self): user = UserFactory() message = MessageFactory(author=user) mail = MailFactory(message=message) recipient = 'unsubscribe-{}@test.munch.example.com'.format( mail.identifier) unsubscribe = UNSUBSCRIBE_EMAIL.replace('||TO||', recipient) unsubscribe = unsubscribe.replace('||RETURNPATH||', recipient) message = email.message_from_string(unsubscribe) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = UnsubscribeHandler(envelope, message) self.assertIsNotNone(handler.apply())
def test_parse_message_object(self): msg = Message() msg['From'] = '*****@*****.**' msg['To'] = '*****@*****.**' msg['To'] = '*****@*****.**' msg.set_payload(b'test test\r\n') env = Envelope() env.parse_msg(msg) self.assertEqual('*****@*****.**', env.headers['from']) self.assertEqual(['*****@*****.**', '*****@*****.**'], env.headers.get_all('to')) # email.generators.Generator.flatten behaviour differs from py2 to py3 if six.PY2: self.assertEqual(b'test test\r\n', env.message) elif six.PY3: self.assertEqual(b'test test\n', env.message)
def test_parse(self): recipient = 'return-{}@test.munch.example.com'.format( get_mail_identifier()) arf = ARF_REPORT.replace('||TO||', recipient) arf = arf.replace('||RETURNPATH||', recipient) message = email.message_from_string(arf) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = ARFHandler(envelope, message) main_headers = email.message_from_string(handler.message.as_string()) orig_headers = handler.get_original() report_headers = handler.get_report() self.assertEqual(orig_headers['Return-Path'], recipient) self.assertEqual(report_headers['Feedback-Type'], 'abuse') self.assertEqual(main_headers['Date'], 'Thu, 8 Mar 2005 17:40:36 EDT')
def test_parse(self): recipient = 'return-{}@test.munch.example.com'.format( get_mail_identifier()) dsn = DSN_REPORT.replace('||TO||', recipient) dsn = dsn.replace('||RETURNPATH||', recipient) message = email.message_from_string(dsn) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = DSNHandler(envelope, message) main_headers = email.message_from_string(handler.message.as_string()) report_headers = handler.get_report() self.assertEqual(main_headers['To'], recipient) self.assertEqual( report_headers['Arrival-Date'], 'Mon, 1 Sep 2014 17:36:45 +0000 (UTC)') self.assertEqual(report_headers['Diagnostic-Code'], 'smtp; 250 OK') self.assertEqual(report_headers['Status'], '2.0.0') self.assertEqual(report_headers['Action'], 'relayed')
def test_unknown_identifier(self): recipient = 'return-{}@test.munch.example.com'.format( get_base_mail_identifier()) dsn = DSN_REPORT.replace('||TO||', recipient) dsn = dsn.replace('||RETURNPATH||', 'foo@bar') message = email.message_from_string(dsn) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = DSNHandler(envelope, message) self.assertIsNone(handler.apply()) recipient = 'return-{}@test.munch.example.com'.format( get_mail_identifier()) dsn = DSN_REPORT.replace('||TO||', recipient) dsn = dsn.replace('||RETURNPATH||', 'foo@bar') message = email.message_from_string(dsn) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = DSNHandler(envelope, message) self.assertIsNone(handler.apply())
def test_unknown_identifier(self): recipient = 'return-{}@test.munch.example.com'.format( get_base_mail_identifier()) unsubscribe = UNSUBSCRIBE_EMAIL.replace('||TO||', recipient) unsubscribe = unsubscribe.replace('||RETURNPATH||', recipient) message = email.message_from_string(unsubscribe) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = UnsubscribeHandler(envelope, message) self.assertIsNone(handler.apply()) recipient = 'return-{}@test.munch.example.com'.format( get_mail_identifier()) unsubscribe = UNSUBSCRIBE_EMAIL.replace('||TO||', recipient) unsubscribe = unsubscribe.replace('||RETURNPATH||', recipient) message = email.message_from_string(unsubscribe) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = UnsubscribeHandler(envelope, message) self.assertIsNone(handler.apply())
def test_unknown_identifier(self): recipient = 'return-{}@test.munch.example.com'.format( get_base_mail_identifier()) arf = ARF_REPORT.replace('||TO||', '*****@*****.**') arf = arf.replace('||RETURNPATH||', recipient) message = email.message_from_string(arf) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = ARFHandler(envelope, message) self.assertIsNone(handler.apply()) recipient = 'return-{}@test.munch.example.com'.format( get_mail_identifier()) arf = ARF_REPORT.replace('||TO||', '*****@*****.**') arf = arf.replace('||RETURNPATH||', recipient) message = email.message_from_string(arf) envelope = Envelope() envelope.parse_msg(message) envelope.recipients = [recipient] handler = ARFHandler(envelope, message) self.assertIsNone(handler.apply())