def test_send_draft(db, api_client, example_draft, default_account): """ Tests the save_draft function, which saves the draft to the remote. """ from inbox.actions import _send r = api_client.post_data('/drafts', example_draft) assert r.status_code == 200 public_id = json.loads(r.data)['id'] r = api_client.get_data('/drafts') matching_saved_drafts = [draft for draft in r if draft['id'] == public_id] assert len(matching_saved_drafts) == 1 draft = matching_saved_drafts[0] sent = _send(default_account.id, draft.id, db.session) assert not sent.is_draft and sent.state == 'sent' with crispin_client(default_account.id, default_account.provider) as c: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format( example_draft['subject'])] c.conn.select_folder(default_account.drafts_folder.name, readonly=False) draft_uids = c.conn.search(criteria) assert not draft_uids, 'Message still in Drafts folder' c.conn.select_folder(default_account.sent_folder.name, readonly=False) sent_uids = c.conn.search(criteria) assert sent_uids, 'Message missing from Sent folder' c.conn.delete_messages(sent_uids) c.conn.expunge()
def test_reply_syncback(db, config, message): from inbox.server.sendmail.base import reply, recipients from inbox.server.models.tables.imap import ImapAccount account = db.session.query(ImapAccount).get(ACCOUNT_ID) to, subject, body = message attachments = None cc = '*****@*****.**' bcc = None reply(NAMESPACE_ID, account, THREAD_ID, recipients(to, cc, bcc), subject, body, attachments) with crispin_client(account.id, account.provider) as c: # Ensure the sent email message is present in the test account, # in both the Inbox and Sent folders: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(THREAD_TOPIC)] c.select_folder(account.inbox_folder.name, lambda x, y: None) inbox_uids = c.conn.search(criteria) assert inbox_uids > 1, 'Reply missing from Inbox' # TODO[k]: Don't delete original #c.delete_messages(inbox_uids) c.select_folder(account.sent_folder.name, lambda x, y: None) sent_uids = c.conn.search(criteria) assert sent_uids, 'Message missing from Sent'
def test_copy_delete_syncback(db, config): from inbox.server.actions.gmail import copy, delete, uidvalidity_cb from inbox.server.models.tables.base import Namespace from inbox.server.models.tables.imap import ImapAccount, ImapThread copy(ACCOUNT_ID, THREAD_ID, 'inbox', 'testlabel') g_thrid = db.session.query(ImapThread.g_thrid).filter_by( id=THREAD_ID, namespace_id=NAMESPACE_ID).one()[0] account = db.session.query(ImapAccount).join(Namespace) \ .filter_by(id=ACCOUNT_ID).one() client = crispin_client(account.id, account.provider) with client.pool.get() as c: client.select_folder(account.inbox_folder_name, uidvalidity_cb, c) inbox_uids = client.find_messages(g_thrid, c) assert inbox_uids, "thread missing from inbox" client.select_folder(account.archive_folder_name, uidvalidity_cb, c) archive_uids = client.find_messages(g_thrid, c) assert archive_uids, "thread missing from archive" client.select_folder('testlabel', uidvalidity_cb, c) testlabel_uids = client.find_messages(g_thrid, c) assert testlabel_uids, "thread missing from testlabel" # and put things back the way they were :) delete(ACCOUNT_ID, THREAD_ID, 'testlabel') with client.pool.get() as c: client.select_folder(account.inbox_folder_name, uidvalidity_cb, c) inbox_uids = client.find_messages(g_thrid, c) assert inbox_uids, "thread missing from inbox" client.select_folder(account.archive_folder_name, uidvalidity_cb, c) archive_uids = client.find_messages(g_thrid, c) assert archive_uids, "thread missing from archive" client.select_folder('testlabel', uidvalidity_cb, c) testlabel_uids = client.find_messages(g_thrid, c) assert not testlabel_uids, "thread still present in testlabel"
def test_remote_save_draft(db, config, message): """ Tests the save_draft function, which saves the draft to the remote. """ from inbox.actions.gmail import remote_save_draft from inbox.sendmail.base import _parse_recipients from inbox.sendmail.message import create_email, Recipients from inbox.models import Account account = db.session.query(Account).get(ACCOUNT_ID) to, subject, body = message to_addr = _parse_recipients(to) recipients = Recipients(to_addr, [], []) email = create_email(account.sender_name, account.email_address, None, recipients, subject, body, None) date = datetime.utcnow() remote_save_draft(account, account.drafts_folder.name, email.to_string(), db.session, date) with crispin_client(account.id, account.provider) as c: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(subject)] c.conn.select_folder(account.drafts_folder.name, readonly=False) draft_uids = c.conn.search(criteria) assert draft_uids, 'Message missing from Drafts folder' flags = c.conn.get_flags(draft_uids) for uid in draft_uids: f = flags.get(uid) assert f and '\\Draft' in f, "Message missing '\\Draft' flag" c.conn.delete_messages(draft_uids) c.conn.expunge()
def test_save_draft_syncback(db, config, message): from inbox.server.actions.gmail import remote_save_draft from inbox.server.sendmail.base import all_recipients from inbox.server.sendmail.message import create_email, SenderInfo from inbox.server.models.tables.imap import ImapAccount account = db.session.query(ImapAccount).get(ACCOUNT_ID) sender_info = SenderInfo(name=account.full_name, email=account.email_address) to, subject, body = message email = create_email(sender_info, all_recipients(to), subject, body, None) flags = [u'\\Draft'] date = datetime.utcnow() remote_save_draft(ACCOUNT_ID, account.drafts_folder.name, email.to_string(), flags=flags, date=date) with crispin_client(account.id, account.provider) as c: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(subject)] c.conn.select_folder(account.drafts_folder.name, readonly=False) inbox_uids = c.conn.search(criteria) assert inbox_uids, 'Message missing from Drafts folder' c.conn.delete_messages(inbox_uids) c.conn.expunge()
def test_send_draft(db, api_client, example_draft, default_account): """ Tests the save_draft function, which saves the draft to the remote. """ from inbox.actions import _send r = api_client.post_data('/drafts', example_draft) assert r.status_code == 200 public_id = json.loads(r.data)['id'] r = api_client.get_data('/drafts') matching_saved_drafts = [draft for draft in r if draft['id'] == public_id] assert len(matching_saved_drafts) == 1 draft = matching_saved_drafts[0] sent = _send(default_account.id, draft.id, db.session) assert not sent.is_draft and sent.state == 'sent' with crispin_client(default_account.id, default_account.provider) as c: criteria = [ 'NOT DELETED', 'SUBJECT "{0}"'.format(example_draft['subject']) ] c.conn.select_folder(default_account.drafts_folder.name, readonly=False) draft_uids = c.conn.search(criteria) assert not draft_uids, 'Message still in Drafts folder' c.conn.select_folder(default_account.sent_folder.name, readonly=False) sent_uids = c.conn.search(criteria) assert sent_uids, 'Message missing from Sent folder' c.conn.delete_messages(sent_uids) c.conn.expunge()
def test_archive_move_syncback(db, config): from inbox.server.actions.gmail import archive, move, uidvalidity_cb from inbox.server.models.tables.imap import ImapAccount, ImapThread archive(ACCOUNT_ID, THREAD_ID) g_thrid = db.session.query(ImapThread.g_thrid).filter_by( id=THREAD_ID, namespace_id=NAMESPACE_ID).one()[0] account = db.session.query(ImapAccount).get(ACCOUNT_ID) client = crispin_client(account.id, account.provider) with client.pool.get() as c: client.select_folder(account.inbox_folder_name, uidvalidity_cb, c) inbox_uids = client.find_messages(g_thrid, c) assert not inbox_uids, "thread still present in inbox" client.select_folder(account.archive_folder_name, uidvalidity_cb, c) archive_uids = client.find_messages(g_thrid, c) assert archive_uids, "thread missing from archive" # and put things back the way they were :) move(ACCOUNT_ID, THREAD_ID, 'archive', 'inbox') with client.pool.get() as c: client.select_folder(account.inbox_folder_name, uidvalidity_cb, c) inbox_uids = client.find_messages(g_thrid, c) assert inbox_uids, "thread missing from inbox" client.select_folder(account.archive_folder_name, uidvalidity_cb, c) archive_uids = client.find_messages(g_thrid, c) assert archive_uids, "thread missing from archive"
def test_remote_save_draft(db, config, message): """ Tests the save_draft function, which saves the draft to the remote. """ from inbox.actions.backends.gmail import remote_save_draft from inbox.sendmail.base import _parse_recipients from inbox.sendmail.message import create_email, Recipients from inbox.models import Account account = db.session.query(Account).get(ACCOUNT_ID) to, subject, body = message to_addr = _parse_recipients(to) recipients = Recipients(to_addr, [], []) email = create_email(account.sender_name, account.email_address, None, recipients, subject, body, None) date = datetime.utcnow() remote_save_draft(account, account.drafts_folder.name, email.to_string(), db.session, date) with crispin_client(account.id, account.provider) as c: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(subject)] c.conn.select_folder(account.drafts_folder.name, readonly=False) draft_uids = c.conn.search(criteria) assert draft_uids, 'Message missing from Drafts folder' flags = c.conn.get_flags(draft_uids) for uid in draft_uids: f = flags.get(uid) assert f and '\\Draft' in f, "Message missing '\\Draft' flag" c.conn.delete_messages(draft_uids) c.conn.expunge()
def test_archive_move_syncback(db, config): from inbox.actions.gmail import (set_remote_archived, remote_move, uidvalidity_cb) from inbox.models.backends.imap import ImapAccount, ImapThread g_thrid = db.session.query(ImapThread.g_thrid).filter_by( id=THREAD_ID, namespace_id=NAMESPACE_ID).one()[0] account = db.session.query(ImapAccount).get(ACCOUNT_ID) set_remote_archived(account, THREAD_ID, True, db.session) assert account.inbox_folder_id and account.all_folder_id, \ "`inbox_folder_id` and `all_folder_id` cannot be NULL" with crispin_client(account.id, account.provider) as client: client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert not inbox_uids, "thread still present in inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive" # and put things back the way they were :) remote_move(account, THREAD_ID, account.all_folder.name, account.inbox_folder.name, db.session) client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert inbox_uids, "thread missing from inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive"
def test_send_draft(db, api_client, example_draft, default_account): r = api_client.post_data("/drafts", example_draft) assert r.status_code == 200 public_id = json.loads(r.data)["id"] version = json.loads(r.data)["version"] r = api_client.post_data("/send", {"draft_id": public_id, "version": version}) assert r.status_code == 200 draft = api_client.get_data("/drafts/{}".format(public_id)) assert draft is not None assert draft["object"] != "draft" with crispin_client(default_account.id, default_account.provider) as c: criteria = ["NOT DELETED", 'SUBJECT "{0}"'.format(example_draft["subject"])] c.conn.select_folder(default_account.drafts_folder.name, readonly=False) draft_uids = c.conn.search(criteria) assert not draft_uids, "Message still in Drafts folder" c.conn.select_folder(default_account.sent_folder.name, readonly=False) sent_uids = c.conn.search(criteria) assert sent_uids, "Message missing from Sent folder" c.conn.delete_messages(sent_uids) c.conn.expunge()
def test_copy_delete_syncback(db, config): from inbox.actions.gmail import (remote_copy, remote_delete, uidvalidity_cb) from inbox.models.backends.imap import ImapAccount, ImapThread g_thrid = db.session.query(ImapThread.g_thrid). \ filter_by(id=THREAD_ID, namespace_id=NAMESPACE_ID).one()[0] account = db.session.query(ImapAccount).get(ACCOUNT_ID) remote_copy(account, THREAD_ID, account.inbox_folder.name, 'testlabel', db.session) with crispin_client(account.id, account.provider) as client: client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert inbox_uids, "thread missing from inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive" client.select_folder('testlabel', uidvalidity_cb) testlabel_uids = client.find_messages(g_thrid) assert testlabel_uids, "thread missing from testlabel" # and put things back the way they were :) remote_delete(account, THREAD_ID, 'testlabel', db.session) client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert inbox_uids, "thread missing from inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive" client.select_folder('testlabel', uidvalidity_cb) testlabel_uids = client.find_messages(g_thrid) assert not testlabel_uids, "thread still present in testlabel"
def test_copy_delete_syncback(db, config): from inbox.server.actions.gmail import (remote_copy, remote_delete, uidvalidity_cb) from inbox.server.models.tables.imap import ImapAccount, ImapThread g_thrid = db.session.query(ImapThread.g_thrid). \ filter_by(id=THREAD_ID, namespace_id=NAMESPACE_ID).one()[0] account = db.session.query(ImapAccount).get(ACCOUNT_ID) remote_copy(ACCOUNT_ID, THREAD_ID, account.inbox_folder.name, 'testlabel') with crispin_client(account.id, account.provider) as client: client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert inbox_uids, "thread missing from inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive" client.select_folder('testlabel', uidvalidity_cb) testlabel_uids = client.find_messages(g_thrid) assert testlabel_uids, "thread missing from testlabel" # and put things back the way they were :) remote_delete(ACCOUNT_ID, THREAD_ID, 'testlabel') client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert inbox_uids, "thread missing from inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive" client.select_folder('testlabel', uidvalidity_cb) testlabel_uids = client.find_messages(g_thrid) assert not testlabel_uids, "thread still present in testlabel"
def test_archive_move_syncback(db, config): from inbox.server.actions.gmail import (remote_archive, remote_move, uidvalidity_cb) from inbox.server.models.tables.imap import ImapAccount, ImapThread remote_archive(ACCOUNT_ID, THREAD_ID) g_thrid = db.session.query(ImapThread.g_thrid).filter_by( id=THREAD_ID, namespace_id=NAMESPACE_ID).one()[0] account = db.session.query(ImapAccount).get(ACCOUNT_ID) assert account.inbox_folder_id and account.all_folder_id, \ "`inbox_folder_id` and `all_folder_id` cannot be NULL" with crispin_client(account.id, account.provider) as client: client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert not inbox_uids, "thread still present in inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive" # and put things back the way they were :) remote_move(ACCOUNT_ID, THREAD_ID, account.all_folder.name, account.inbox_folder.name) client.select_folder(account.inbox_folder.name, uidvalidity_cb) inbox_uids = client.find_messages(g_thrid) assert inbox_uids, "thread missing from inbox" client.select_folder(account.all_folder.name, uidvalidity_cb) archive_uids = client.find_messages(g_thrid) assert archive_uids, "thread missing from archive"
def test_send_draft(db, api_client, example_draft, default_account): r = api_client.post_data('/drafts', example_draft) assert r.status_code == 200 public_id = json.loads(r.data)['id'] version = json.loads(r.data)['version'] r = api_client.post_data('/send', {'draft_id': public_id, 'version': version}) assert r.status_code == 200 draft = api_client.get_data('/drafts/{}'.format(public_id)) assert draft is not None assert draft['object'] != 'draft' with crispin_client(default_account.id, default_account.provider) as c: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format( example_draft['subject'])] c.conn.select_folder(default_account.drafts_folder.name, readonly=False) draft_uids = c.conn.search(criteria) assert not draft_uids, 'Message still in Drafts folder' c.conn.select_folder(default_account.sent_folder.name, readonly=False) sent_uids = c.conn.search(criteria) assert sent_uids, 'Message missing from Sent folder' c.conn.delete_messages(sent_uids) c.conn.expunge()
def cleanup(account, subject): with crispin_client(account.id, account.provider) as c: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(subject)] c.conn.select_folder(account.drafts_folder.name, readonly=False) inbox_uids = c.conn.search(criteria) if inbox_uids: c.conn.delete_messages(inbox_uids) c.conn.expunge()
def test_remote_delete_draft(db, config, message): """ Tests the delete_draft function, which deletes the draft from the remote. """ from inbox.actions.backends.gmail import remote_delete_draft, remote_save_draft from inbox.models import Account from inbox.sendmail.base import _parse_recipients from inbox.sendmail.message import Recipients, create_email account = db.session.query(Account).get(ACCOUNT_ID) to, subject, body = message to_addr = _parse_recipients(to) recipients = Recipients(to_addr, [], []) email = create_email( account.sender_name, account.email_address, None, recipients, subject, body, None, ) date = datetime.utcnow() # Save on remote remote_save_draft(account, account.drafts_folder.name, email.to_string(), db.session, date) inbox_uid = email.headers["X-INBOX-ID"] with crispin_client(account.id, account.provider) as c: criteria = [ "DRAFT", "NOT DELETED", "HEADER X-INBOX-ID {0}".format(inbox_uid) ] c.conn.select_folder(account.drafts_folder.name, readonly=False) uids = c.conn.search(criteria) assert uids, "Message missing from Drafts folder" # Delete on remote remote_delete_draft(account, account.drafts_folder.name, inbox_uid, db.session) c.conn.select_folder(account.drafts_folder.name, readonly=False) uids = c.conn.search(criteria) assert not uids, "Message still in Drafts folder"
def _delete_emails(db, account_id, subject): from inbox.server.models.tables.imap import ImapAccount account = db.session.query(ImapAccount).get(account_id) client = crispin_client(account.id, account.provider) with client.pool.get() as c: # Ensure the sent email message is present in the test account, # in both the Inbox and Sent folders: criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(subject)] c.select_folder(account.inbox_folder.name, None) inbox_uids = c.search(criteria) c.delete_messages(inbox_uids) c.select_folder(account.sent_folder.name, None) sent_uids = c.search(criteria) c.delete_messages(sent_uids)
def test_remote_unread_syncback(db, config): from inbox.actions.backends.gmail import set_remote_unread, uidvalidity_cb from inbox.models.backends.imap import ImapAccount, ImapThread account = db.session.query(ImapAccount).get(ACCOUNT_ID) g_thrid, = db.session.query(ImapThread.g_thrid).filter_by(id=THREAD_ID).one() set_remote_unread(account, THREAD_ID, True, db.session) with crispin_client(account.id, account.provider) as client: client.select_folder(account.all_folder.name, uidvalidity_cb) uids = client.find_messages(g_thrid) assert not any("\\Seen" in flags for flags, _ in client.flags(uids).values()) set_remote_unread(account, THREAD_ID, False, db.session) assert all("\\Seen" in flags for flags, _ in client.flags(uids).values()) set_remote_unread(account, THREAD_ID, True, db.session) assert not any("\\Seen" in flags for flags, _ in client.flags(uids).values())
def test_remote_delete_draft(db, config, message): """ Tests the delete_draft function, which deletes the draft from the remote. """ from inbox.actions.backends.gmail import (remote_save_draft, remote_delete_draft) from inbox.sendmail.base import _parse_recipients from inbox.sendmail.message import create_email, Recipients from inbox.models import Account account = db.session.query(Account).get(ACCOUNT_ID) to, subject, body = message to_addr = _parse_recipients(to) recipients = Recipients(to_addr, [], []) email = create_email(account.sender_name, account.email_address, None, recipients, subject, body, None) date = datetime.utcnow() # Save on remote remote_save_draft(account, account.drafts_folder.name, email.to_string(), db.session, date) inbox_uid = email.headers['X-INBOX-ID'] with crispin_client(account.id, account.provider) as c: criteria = ['DRAFT', 'NOT DELETED', 'HEADER X-INBOX-ID {0}'.format(inbox_uid)] c.conn.select_folder(account.drafts_folder.name, readonly=False) uids = c.conn.search(criteria) assert uids, 'Message missing from Drafts folder' # Delete on remote remote_delete_draft(account, account.drafts_folder.name, inbox_uid, db.session) c.conn.select_folder(account.drafts_folder.name, readonly=False) uids = c.conn.search(criteria) assert not uids, 'Message still in Drafts folder'
def test_remote_unread_syncback(db, config): from inbox.server.actions.gmail import set_remote_unread, uidvalidity_cb from inbox.server.models.tables.imap import ImapAccount, ImapThread g_thrid, = db.session.query(ImapThread.g_thrid).filter_by( id=THREAD_ID, namespace_id=NAMESPACE_ID).one() account = db.session.query(ImapAccount).get(ACCOUNT_ID) set_remote_unread(ACCOUNT_ID, THREAD_ID, True) with crispin_client(account.id, account.provider) as client: client.select_folder(account.all_folder.name, uidvalidity_cb) uids = client.find_messages(g_thrid) assert not any('\\Seen' in flags for flags, _ in client.flags(uids).values()) set_remote_unread(ACCOUNT_ID, THREAD_ID, False) assert all('\\Seen' in flags for flags, _ in client.flags(uids).values()) set_remote_unread(ACCOUNT_ID, THREAD_ID, True) assert not any('\\Seen' in flags for flags, _ in client.flags(uids).values())
def test_send_draft(db, api_client, example_draft, default_account): r = api_client.post_data('/drafts', example_draft) assert r.status_code == 200 public_id = json.loads(r.data)['id'] version = json.loads(r.data)['version'] r = api_client.post_data('/send', { 'draft_id': public_id, 'version': version }) assert r.status_code == 200 draft = api_client.get_data('/drafts/{}'.format(public_id)) assert draft is not None assert draft['object'] != 'draft' with crispin_client(default_account.id, default_account.provider) as c: criteria = [ 'NOT DELETED', 'SUBJECT "{0}"'.format(example_draft['subject']) ] c.conn.select_folder(default_account.drafts_folder.name, readonly=False) draft_uids = c.conn.search(criteria) assert not draft_uids, 'Message still in Drafts folder' c.conn.select_folder(default_account.sent_folder.name, readonly=False) sent_uids = c.conn.search(criteria) assert sent_uids, 'Message missing from Sent folder' c.conn.delete_messages(sent_uids) c.conn.expunge()