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_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_copy_delete_syncback(db, config):
    from inbox.actions.backends.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_archive_move_syncback(db, config):
    from inbox.actions.backends.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, False, db.session)
    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"
Example #5
0
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_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_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.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())