def test_mail_to_you_is_bouncing(): msg = MailRequest("fakepeer", None, None, open("tests/bounce.msg").read()) assert msg.is_bounce() bounce_rep = bounce.mail_to_you_is_bouncing(msg) assert bounce_rep assert_equal(bounce_rep['to'], msg.bounce.final_recipient)
def test_craft_response_attachment(): sample = MailResponse(To=list_name + "@librelist.com", From=user_full_address, Subject="Test message with attachments.", Body="The body as one attachment.") sample.attach(filename="tests/model/mailinglist_tests.py", content_type="text/plain", disposition="attachment") sample['message-id'] = '123545666' im = sample.to_message() assert_equal(len([x for x in im.walk()]), 3) inmsg = MailRequest("fakepeer", None, None, str(sample)) assert_equal(len(inmsg.all_parts()), 2) outmsg = craft_response(inmsg, list_name, list_name + "@librelist.com") om = outmsg.to_message() assert_equal(len([x for x in om.walk()]), len([x for x in im.walk()])) assert 'message-id' in outmsg
def test_mail_to_you_is_bouncing(): msg = MailRequest("fakepeer", None, None, open("tests/bounce.msg").read()) assert msg.is_bounce() bounce_rep = bounce.mail_to_you_is_bouncing(msg) assert bounce_rep assert_equal(bounce_rep["to"], msg.bounce.final_recipient)
def test_RoutingBase(): assert len(Router.ORDER) == 0 assert len(Router.REGISTERED) == 0 Router.load(['lamson_tests.simple_fsm_mod']) import simple_fsm_mod assert len(Router.ORDER) > 0 assert len(Router.REGISTERED) > 0 message = MailRequest('fakepeer', 'zedshaw@localhost', 'users-subscribe@localhost', "") Router.deliver(message) assert Router.in_state(simple_fsm_mod.CONFIRM, message) confirm = MailRequest('fakepeer', '"Zed Shaw" <zedshaw@localhost>', 'users-confirm-1@localhost', "") Router.deliver(confirm) assert Router.in_state(simple_fsm_mod.POSTING, message) Router.deliver(message) assert Router.in_state(simple_fsm_mod.NEXT, message) Router.deliver(message) assert Router.in_state(simple_fsm_mod.END, message) Router.deliver(message) assert Router.in_state(simple_fsm_mod.START, message) Router.clear_states() explosion = MailRequest('fakepeer', '<hacker@localhost>', 'start-explode@localhost', "") Router.LOG_EXCEPTIONS = True Router.deliver(explosion) assert Router.in_error(simple_fsm_mod.END, explosion) Router.clear_states() Router.LOG_EXCEPTIONS = False explosion = MailRequest('fakepeer', 'hacker@localhost', 'start-explode@localhost', "") assert_raises(RuntimeError, Router.deliver, explosion) Router.reload() assert 'lamson_tests.simple_fsm_mod' in Router.HANDLERS assert len(Router.ORDER) assert len(Router.REGISTERED) message = MailRequest( 'fakepeer', 'zedshaw@localhost', ['users-subscribe@localhost', 'users-confirm-1@localhost'], "Fake body.") Router.deliver(message) assert Router.in_state(simple_fsm_mod.POSTING, message), "Router state: %r" % Router.get_state( 'simple_fsm_mod', message)
def test_get_html_stubs(): """ Given some html, return a list of html stubs that represent alerts. """ alertsmsg = MailRequest( "fakepeer", sender, "*****@*****.**", open(home("tests/data/emails/alerts.msg")).read() ) stubs = alerts.get_html_stubs(alertsmsg.body()) assert len(stubs) == 26
def create_bounce(To, From): msg = MailRequest("fakepeer", From, To, open("tests/bounce.msg").read()) assert msg.is_bounce() msg.bounce.final_recipient = From msg.bounce.headers['Final-Recipient'] = From msg.bounce.original['from'] = From msg.bounce.original['to'] = To msg.bounce.original.To = set([To]) msg.bounce.original.From = From return msg
def test_get_remove_url(): alertsmsg = MailRequest( "fakepeer", sender, "*****@*****.**", open(home("tests/data/emails/alerts.msg")).read() ) url = alerts.get_remove_url(alertsmsg.body()) assert ( url == "/alerts/remove?s=AB2Xq4jsDy4ienBZYuYgWbzBWQ5i6LiD5L4y8JY&hl=en&gl=us&source=alertsmail&cd=4Ya67t6E3e4&cad=:s7:f1:v1:" ) msg = MailRequest( "fakepeer", sender, "*****@*****.**", open(home("tests/data/emails/confirmation.msg")).read() ) url = alerts.get_remove_url(msg.body()) assert url == "/alerts/remove?gl=us&hl=en&s=AB2Xq4jsDy4ienBZYuYgWbzBWQ5i6LiD5L4y8JY"
def test_Router_undeliverable_queue(): Router.clear_routes() Router.clear_states() Router.UNDELIVERABLE_QUEUE = Mock() msg = MailRequest('fakepeer', 'from@localhost', 'to@localhost', "Nothing") Router.deliver(msg) assert Router.UNDELIVERABLE_QUEUE.push.called
def test_post(): message = MailRequest("fakepeer", user, "*****@*****.**" % blog, "Fake body") message['Subject'] = 'Test subject' post.post(blog, user, "localhost", message) assert post.user_exists(user), "User dir not created." assert os.path.exists(post.blog_file_name(blog, user)), "File not made."
def test_craft_response_no_attachment(): sample = MailResponse(To=list_name + "@librelist.com", From=user_full_address, Subject="Test message with attachments.", Body="The body as one attachment.") im = sample.to_message() assert_equal(len([x for x in im.walk()]), 1) assert_equal(im.get_payload(), sample.Body) inmsg = MailRequest("fakepeer", None, None, str(sample)) assert_equal(len(inmsg.all_parts()), 0) assert_equal(inmsg.body(), sample.Body) outmsg = craft_response(inmsg, list_name, list_name + "@librelist.com") om = outmsg.to_message() assert_equal(om.get_payload(), sample.Body) assert_equal(len([x for x in om.walk()]), len([x for x in im.walk()]))
def test_white_list_cleanse(): msg = MailRequest('fakepeer', None, None, open('tests/lots_of_headers.msg').read()) resp = mailinglist.craft_response(msg, 'test.list', '*****@*****.**') archive.white_list_cleanse(resp) for key in resp.keys(): assert key in archive.ALLOWED_HEADERS assert '@' not in resp['from'] assert str(resp)
def test_post_message(): for i in range(0, 3): add_subscriber(user_full_address, list_name) sample = MailResponse(To=list_name + "@librelist.com", From=user_full_address, Subject="Test post message.", Body="I am telling you guys you are wrong.") sample['Message-Id'] = '12313123123123123' msg = MailRequest("fakepeer", sample['from'], sample['to'], str(sample)) post_message(relay(port=8825), msg, list_name, "librelist.com")
def test_remove_from_queue(): message = MailRequest("fakepeer", user, "*****@*****.**" % blog, "Fake body") message['Subject'] = 'Test subject' post_q = post.get_user_post_queue(post.get_user_dir(user)) post.post(blog, user, 'localhost', message) assert post_q.count(), "No messages in the post queue." count = post_q.count() post.remove_from_queue(blog, user) assert post_q.count() == count - 1, "It didn't get removed."
def test_get_raw_alert(): """ Given a chunk of html, turn it into a python dictionary we can use to populate a database record. """ alertsmsg = MailRequest( "fakepeer", sender, "*****@*****.**", open(home("tests/data/emails/alerts.msg")).read() ) stub = alerts.get_html_stubs(alertsmsg.body())[1] alert = alerts.get_raw_alert(stub) assert alert.has_key("blurb") assert alert["title"].startswith("Audio reveals swipes at Obama, other Illinois Democrats") assert alert["source"] == "Chicago Tribune", "Expected Chicago Tribune, source is actually %s" % alert["source"] assert alert["byline"] == "" alert[ "url" ] == "http://www.chicagotribune.com/news/local/blagojevich/ct-met-blagojevich-trial-0704-20100704,0,3385819.story" stub = alerts.get_html_stubs(alertsmsg.body())[0] alert = alerts.get_raw_alert(stub) assert alert["byline"] == "" assert alert["url"] == "http://www.foxnews.com/politics/2010/07/03/obama-announces-b-grants-clean-energy-jobs/"
def test_to_json(): msg = MailRequest('fakeperr', None, None, open("tests/bounce.msg").read()) resp = mailinglist.craft_response(msg, 'test.list', '*****@*****.**') # attach an the message back but fake it as an image it'll be garbage resp.attach(filename="tests/bounce.msg", content_type="image/png", disposition="attachment") resp.to_message() # prime the pump js = archive.to_json(resp.base) assert js rtjs = json.loads(js) assert rtjs assert rtjs['parts'][-1]['encoding']['format'] == 'base64'
def convert_queue(arg, dirname, names): if dirname.endswith("new"): print dirname, names jpath = dirname + "/../../json" if not os.path.exists(jpath): os.mkdir(jpath) for key in names: json_file = key + ".json" json_archive = os.path.join(jpath, json_file) fpath = os.path.join(dirname, key) msg = MailRequest('librelist.com', None, None, open(fpath).read()) f = open(json_archive, "w") f.write(archive.to_json(msg.base)) f.close()
raise Exception("IDLE not handled? : %s" % response) def done(connection): connection.send("DONE\r\n") connection.loop = False imaplib.IMAP4.idle = idle imaplib.IMAP4.done = done if __name__ == '__main__': import os from lamson.mail import MailRequest user = os.environ['EMAIL'] password = os.environ['PASSWORD'] print os.environ['SERVER'] conn = imaplib.IMAP4_SSL(os.environ['SERVER']) conn.login(user, password) conn.select() loop = True while loop: for uid, msg in conn.idle(): print uid, msg if msg == "EXISTS": conn.done() status, datas = conn.fetch(uid, '(RFC822)') m = MailRequest('localhost', None, None, datas[0][1]) print m.keys() print m.all_parts() print m.is_bounce()
def done(connection): connection.send("DONE\r\n".encode('utf8')) connection.loop = False imaplib.IMAP4.idle = idle imaplib.IMAP4.done = done if __name__ == '__main__': import os from lamson.mail import MailRequest user = os.environ['EMAIL'] password = os.environ['PASSWORD'] print(os.environ['SERVER']) conn = imaplib.IMAP4_SSL(os.environ['SERVER']) conn.login(user, password) conn.select() loop = True while loop: for uid, msg in conn.idle(): print(uid, msg) if msg == "EXISTS": conn.done() status, datas = conn.fetch(uid, '(RFC822)') m = MailRequest('localhost', None, None, datas[0][1]) print(m.keys()) print(m.all_parts()) print(m.is_bounce())