def test_router_two_routes_two_predicates(): emails = [] router = Router() receipts = [] updates = [] for filename in glob('./tests/fixtures/emails/*'): with open(filename) as f: emails.append(parse_email(f.read())) @router.match(from_=".*[email protected].*", subject=r"^Update on your donation to (?P<receiver>.*)$") def process_update(msg, receiver=None): assert(receiver == 'Maung Win') updates.append(msg) @router.match(from_=".*[email protected].*", subject=r"^Receipt for your donation to (?P<receiver>.*)$") def process_receipt(msg, receiver=None): assert(receiver == 'Tu Tu') receipts.append(msg) assert(router.routes) print router.routes assert(len(router.routes) is 2) for email in emails: router.route(email) print(receipts) assert(len(receipts) is 1) receipt = receipts[0] assert(receipt['Subject'] == 'Receipt for your donation to Tu Tu') assert(len(updates) is 1) update = updates[0] assert(update['Subject'] == 'Update on your donation to Maung Win')
def get_mail(self, delete=True): """ fetches emails from the mailbox. keyword arguments: delete - when True, deletes the messages after fetching them. defaults to False """ conn = imaplib.IMAP4_SSL(self.host) conn.login(self.user, self.password) conn.select(self.mailbox) search_result, search_data = conn.uid("search", None, "ALL") uids = search_data[0].split() message_count = len(uids) fetch_email = lambda uid: conn.uid('fetch', uid, '(RFC822)')[1][0][1] emails = [parse_email(fetch_email(uid)) for uid in uids] if(message_count and delete): delete_mail(uids) conn.expunge() conn.close() conn.logout() return emails
def test_router_single_route_simple_match(): emails = [] router = Router() matched = [] for filename in glob('./tests/fixtures/emails/*'): with open(filename) as f: emails.append(parse_email(f.read())) @router.match(from_=".*[email protected].*") def process(msg, receiver=None): assert(msg) matched.append(msg) assert(router.routes) assert(len(router.routes) is 1) for email in emails: router.route(email) assert(len(matched) is 2) receipt = matched[0] assert(receipt['Subject'] == 'Receipt for your donation to Tu Tu') update = matched[1] assert(update['Subject'] == 'Update on your donation to Maung Win')