示例#1
0
def test_account(db, api_client, generic_account, gmail_account):
    # Because we're using the generic_account namespace
    api_client = new_api_client(db, generic_account.namespace)

    resp_data = api_client.get_data('/account')

    assert resp_data['id'] == generic_account.namespace.public_id
    assert resp_data['object'] == 'account'
    assert resp_data['account_id'] == generic_account.namespace.public_id
    assert resp_data['email_address'] == generic_account.email_address
    assert resp_data['name'] == generic_account.name
    assert resp_data['organization_unit'] == 'folder'
    assert 'sync_state' in resp_data
    assert 'server_settings' not in resp_data

    # Because we're using the gmail account namespace
    api_client = new_api_client(db, gmail_account.namespace)

    resp_data = api_client.get_data('/account')

    assert resp_data['id'] == gmail_account.namespace.public_id
    assert resp_data['provider'] == 'gmail'
    assert resp_data['organization_unit'] == 'label'
    assert 'sync_state' in resp_data
    assert 'server_settings' not in resp_data
示例#2
0
def test_account_expanded(db, api_client, generic_account, gmail_account):
    # Generic accounts expose a `server_settings` attribute
    # Custom IMAP
    api_client = new_api_client(db, generic_account.namespace)
    resp_data = api_client.get_data('/account/?view=expanded')
    assert resp_data['provider'] == 'custom'
    assert 'server_settings' in resp_data
    assert set(resp_data['server_settings']) == set({
        'imap_host': 'imap.custom.com',
        'smtp_host': 'smtp.custom.com',
        'imap_port': 993,
        'smtp_port': 587,
        'ssl_required': True})

    # Yahoo
    yahoo_account = add_fake_yahoo_account(db.session)
    api_client = new_api_client(db, yahoo_account.namespace)
    resp_data = api_client.get_data('/account/?view=expanded')
    assert resp_data['provider'] == 'yahoo'
    assert 'server_settings' in resp_data
    assert set(resp_data['server_settings']) == set({
        'imap_host': 'imap.mail.yahoo.com',
        'smtp_host': 'smtp.mail.yahoo.com',
        'imap_port': 993,
        'smtp_port': 587,
        'ssl_required': True})

    # Gmail accounts don't expose a `server_settings` attribute
    api_client = new_api_client(db, gmail_account.namespace)
    resp_data = api_client.get_data('/account/?view=expanded')
    assert resp_data['provider'] == 'gmail'
    assert 'server_settings' not in resp_data
def test_category_delete(db, gmail_account):
    """ Ensure that all associated MessageCategories are deleted
        when a Category is deleted """

    api_client = new_api_client(db, gmail_account.namespace)
    po_data = api_client.post_data('/labels/',
                                   {"display_name": "Test_Label"})
    assert po_data.status_code == 200

    category_public_id = json.loads(po_data.data)['id']
    category = db.session.query(Category).filter(
        Category.public_id == category_public_id).one()
    category_id = category.id

    for i in xrange(10):
        generic_thread = add_fake_thread(db.session,
                                         gmail_account.namespace.id)
        gen_message = add_fake_message(db.session,
                                       gmail_account.namespace.id,
                                       generic_thread)
        data = {"label_ids": [category_public_id]}
        resp = api_client.put_data('/messages/{}'.
                                   format(gen_message.public_id), data)
        assert resp.status_code == 200

    associated_mcs = db.session.query(MessageCategory). \
            filter(MessageCategory.category_id == category_id).all()
    assert len(associated_mcs) == 10

    db.session.delete(category)
    db.session.commit()

    assert db.session.query(MessageCategory). \
            filter(MessageCategory.category_id == category_id).all() == []
def test_message_delete(db, gmail_account):
    """ Ensure that all associated MessageCategories are deleted
        when a Message is deleted """

    api_client = new_api_client(db, gmail_account.namespace)

    generic_thread = add_fake_thread(db.session, gmail_account.namespace.id)
    gen_message = add_fake_message(db.session,
                                   gmail_account.namespace.id,
                                   generic_thread)

    category_ids = []
    for i in xrange(10):
        po_data = api_client.post_data('/labels/',
                                       {"display_name": str(i)})
        assert po_data.status_code == 200

        category_ids.append(json.loads(po_data.data)['id'])

    data = {"label_ids": category_ids}
    resp = api_client.put_data('/messages/{}'.
                               format(gen_message.public_id), data)
    assert resp.status_code == 200

    associated_mcs = db.session.query(MessageCategory). \
            filter(MessageCategory.message_id == gen_message.id).all()
    assert len(associated_mcs) == 10

    db.session.delete(gen_message)
    db.session.commit()

    assert db.session.query(MessageCategory). \
            filter(MessageCategory.message_id == gen_message.id).all() == []
示例#5
0
def test_folders_labels_delete(db, api_client, generic_account, gmail_account):
    api_client = new_api_client(db, generic_account.namespace)
    # Generic IMAP threads, messages have a 'folders' field
    generic_thread = add_fake_thread(db.session, generic_account.namespace.id)
    generic_message = add_fake_message(db.session,
                                       generic_account.namespace.id,
                                       generic_thread)
    resp = api_client.post_data('/folders/',
                                {"display_name": "Test_Folder"})
    assert resp.status_code == 200
    generic_folder = json.loads(resp.data)
    data = {"folder_id": generic_folder['id']}
    # Add message to folder
    api_client.put_data('/messages/{}'.format(generic_message.public_id), data)

    # try deleting folder that contains a message
    delete_data = api_client.delete('/folders/{}'.format(generic_folder['id']))
    assert delete_data.status_code == 403

    resp = api_client.post_data('/folders/',
                                {"display_name": "Test_Folder2"})
    empty_folder = json.loads(resp.data)

    # try deleting folder that contains a message
    delete_data = api_client.delete('/folders/{}'.format(empty_folder['id']))
    assert delete_data.status_code == 200

    # Because we're using the generic_account namespace
    api_client = new_api_client(db, gmail_account.namespace)

    # Gmail threads, messages have a 'labels' field
    gmail_thread = add_fake_thread(db.session, gmail_account.namespace.id)
    gmail_message = add_fake_message(db.session,
                                     gmail_account.namespace.id, gmail_thread)

    resp = api_client.post_data('/labels/',
                                {"display_name": "Test_Labels"})
    assert resp.status_code == 200
    gmail_label = json.loads(resp.data)
    data = {"folder_id": gmail_label['id']}
    # Add label to message
    api_client.put_data('/messages/{}'.format(gmail_message.public_id), data)

    # try deleting label
    delete_data = api_client.delete('/labels/{}'.format(gmail_label['id']))
    assert delete_data.status_code == 200
示例#6
0
def test_smtp_ssl_verification_bad_cert(db, bad_cert_smtp_server,
                                        example_draft, local_smtp_account,
                                        api_client, patched_smtp):

    api_client = new_api_client(db, local_smtp_account.namespace)
    gevent.sleep(0.2)  # let SMTP daemon start up
    r = api_client.post_data('/send', example_draft)
    assert r.status_code == 200
示例#7
0
def test_folders_labels(db, api_client, generic_account, gmail_account):
    # Generic IMAP threads, messages have a 'folders' field
    generic_thread = add_fake_thread(db.session, generic_account.namespace.id)
    generic_message = add_fake_message(db.session,
                                       generic_account.namespace.id,
                                       generic_thread)

    # Because we're using the generic_account namespace
    api_client = new_api_client(db, generic_account.namespace)

    resp_data = api_client.get_data(
        '/threads/{}'.format(generic_thread.public_id))

    assert resp_data['id'] == generic_thread.public_id
    assert resp_data['object'] == 'thread'
    assert 'folders' in resp_data and 'labels' not in resp_data

    resp_data = api_client.get_data(
        '/messages/{}'.format(generic_message.public_id))

    assert resp_data['id'] == generic_message.public_id
    assert resp_data['object'] == 'message'
    assert 'folder' in resp_data and 'labels' not in resp_data

    # Because we're using the generic_account namespace
    api_client = new_api_client(db, gmail_account.namespace)

    # Gmail threads, messages have a 'labels' field
    gmail_thread = add_fake_thread(db.session, gmail_account.namespace.id)
    gmail_message = add_fake_message(db.session,
                                     gmail_account.namespace.id, gmail_thread)

    resp_data = api_client.get_data(
        '/threads/{}'.format(gmail_thread.public_id))

    assert resp_data['id'] == gmail_thread.public_id
    assert resp_data['object'] == 'thread'
    assert 'labels' in resp_data and 'folders' not in resp_data

    resp_data = api_client.get_data(
        '/messages/{}'.format(gmail_message.public_id))

    assert resp_data['id'] == gmail_message.public_id
    assert resp_data['object'] == 'message'
    assert 'labels' in resp_data and 'folders' not in resp_data
示例#8
0
def test_gmail_pagination(db, default_account,
                          patch_crispin_client,
                          patch_handler_from_provider,
                          folder):
    for i in range(10):
        thread = add_fake_thread(db.session, default_account.namespace.id)
        message = add_fake_message(db.session, default_account.namespace.id,
                                   thread=thread,
                                   from_addr=[{'name': '', 'email':
                                               '{}@test.com'.format(str(i))}],
                                   subject='hi',
                                   g_msgid=i,
                                   received_date=datetime.
                                   datetime(2000 + i, 1, 1, 1, 0, 0))

        add_fake_imapuid(db.session, default_account.id, message,
                         folder, i)

    first_ten_messages_db = db.session.query(Message)\
                            .filter(Message.namespace_id ==
                                    default_account.namespace.id). \
                            order_by(desc(Message.received_date)). \
                            limit(10).all()

    api_client = new_api_client(db, default_account.namespace)

    first_ten_messages_api = api_client.get_data('/messages/search?q=hi'
                                                      '&limit=10')
    assert len(first_ten_messages_api) == len(first_ten_messages_db)

    for db_message, api_message in zip(first_ten_messages_db,
                                        first_ten_messages_api):
        assert db_message.public_id == api_message['id']

    imap_uids = db.session.query(ImapUid).join(Message) \
                    .filter(
                        ImapUid.message_id == Message.id,
                        Message.g_msgid != None).all()
    uids = [uid.msg_uid for uid in imap_uids]

    first_ten_threads_db = db.session.query(Thread) \
                            .join(Message) \
                            .join(ImapUid) \
                            .filter(ImapUid.account_id == default_account.id,
                                    ImapUid.msg_uid.in_(uids),
                                    Thread.id == Message.thread_id)\
                            .order_by(desc(Message.received_date)) \
                            .limit(10).all()

    first_ten_threads_api = api_client.get_data('/threads/search?q=hi'
                                                      '&limit=10')

    assert len(first_ten_threads_api) == len(first_ten_threads_db)

    for db_thread, api_thread in zip(first_ten_threads_db,
                                        first_ten_threads_api):
        assert db_thread.public_id == api_thread['id']
示例#9
0
def test_smtp_ssl_verification_bad_cert(db, bad_cert_smtp_server,
                                        example_draft, local_smtp_account,
                                        api_legacy_client):

    error_msg = 'SMTP server SSL certificate verify failed'

    api_client = new_api_client(db, local_smtp_account.namespace)
    gevent.sleep(0.2)  # let SMTP daemon start up
    r = api_client.post_data('/send', example_draft)
    assert r.status_code == 503
    assert json.loads(r.data)['message'] == error_msg
示例#10
0
def test_correct_thread_count(db, default_account,
                              patch_crispin_client,
                              patch_handler_from_provider,
                              sorted_gmail_messages):

    api_client = new_api_client(db, default_account.namespace)

    first_two_threads = api_client.get_data('/threads/search?q=hi'
                                              '&limit=2')

    assert len(first_two_threads) == 2
示例#11
0
def test_account_repr_for_new_account(db):
    account = add_fake_yahoo_account(db.session)

    # Sync for the account has not started yet.
    assert account.sync_state is None

    # However the API-returned account object has `sync_state=running`
    # so API clients can do the right thing.
    api_client = new_api_client(db, account.namespace)
    resp_data = api_client.get_data('/account')
    assert resp_data['id'] == account.namespace.public_id
    assert resp_data['sync_state'] == 'running'

    # Verify other sync_states are not masked.
    account.sync_state = 'invalid'
    db.session.commit()

    api_client = new_api_client(db, account.namespace)
    resp_data = api_client.get_data('/account')
    assert resp_data['id'] == account.namespace.public_id
    assert resp_data['sync_state'] == 'invalid'
示例#12
0
def test_folders_labels(db, api_client, generic_account, gmail_account):
    # Generic IMAP threads, messages have a 'folders' field
    generic_thread = add_fake_thread(db.session, generic_account.namespace.id)
    generic_message = add_fake_message(db.session, generic_account.namespace.id, generic_thread)

    # Because we're using the generic_account namespace
    api_client = new_api_client(db, generic_account.namespace)

    resp_data = api_client.get_data("/threads/{}".format(generic_thread.public_id))

    assert resp_data["id"] == generic_thread.public_id
    assert resp_data["object"] == "thread"
    assert "folders" in resp_data and "labels" not in resp_data

    resp_data = api_client.get_data("/messages/{}".format(generic_message.public_id))

    assert resp_data["id"] == generic_message.public_id
    assert resp_data["object"] == "message"
    assert "folder" in resp_data and "labels" not in resp_data

    # Because we're using the generic_account namespace
    api_client = new_api_client(db, gmail_account.namespace)

    # Gmail threads, messages have a 'labels' field
    gmail_thread = add_fake_thread(db.session, gmail_account.namespace.id)
    gmail_message = add_fake_message(db.session, gmail_account.namespace.id, gmail_thread)

    resp_data = api_client.get_data("/threads/{}".format(gmail_thread.public_id))

    assert resp_data["id"] == gmail_thread.public_id
    assert resp_data["object"] == "thread"
    assert "labels" in resp_data and "folders" not in resp_data

    resp_data = api_client.get_data("/messages/{}".format(gmail_message.public_id))

    assert resp_data["id"] == gmail_message.public_id
    assert resp_data["object"] == "message"
    assert "labels" in resp_data and "folders" not in resp_data
示例#13
0
def test_label_post(db, gmail_account):
    api_client = new_api_client(db, gmail_account.namespace)
    po_data = api_client.post_data('/labels/',
                                   {"display_name": "Test_Label"})
    assert po_data.status_code == 200

    category_id = json.loads(po_data.data)['id']
    category = db.session.query(Category).filter(
        Category.public_id == category_id).one()
    assert category.display_name == 'Test_Label'
    assert category.name == ''
    assert category.type == 'label'
    assert category.deleted_at == EPOCH
    assert category.is_deleted is False
示例#14
0
def label_client(db, gmail_account):
    api_client = new_api_client(db, gmail_account.namespace)

    # Whereas calling generic_account always makes a new IMAP account,
    # calling gmail_account checks first to see if there's an existing
    # Gmail account and uses it if so. This can cause namespace
    # conflicts if a label is "created" more than once. Since
    # labels can't be deleted and then re-created, this fixture only
    # makes a new label if there are no existing labels.
    g_data = api_client.get_raw('/labels/')
    if not json.loads(g_data.data):
        api_client.post_data('/labels/',
                             {"display_name": "Test_Label"})
    return api_client
示例#15
0
def test_account(db, api_client, generic_account, gmail_account):

    # Because we're using the generic_account namespace
    api_client = new_api_client(db, generic_account.namespace)

    resp_data = api_client.get_data("/account")

    assert resp_data["id"] == generic_account.namespace.public_id
    assert resp_data["object"] == "account"
    assert resp_data["account_id"] == generic_account.namespace.public_id
    assert resp_data["email_address"] == generic_account.email_address
    assert resp_data["name"] == generic_account.name
    assert resp_data["organization_unit"] == "folder"
    assert "sync_state" in resp_data

    # Because we're using the gmail account namespace
    api_client = new_api_client(db, gmail_account.namespace)

    resp_data = api_client.get_data("/account")

    assert resp_data["id"] == gmail_account.namespace.public_id
    assert resp_data["provider"] == "gmail"
    assert resp_data["organization_unit"] == "label"
    assert "sync_state" in resp_data
示例#16
0
def test_resource_views(resource_name, db, api_client, generic_account,
                        message, thread, event, label, contact, folder):
    """Exercises various tests for views, mostly related to
    filtering. Note: this only tests views, it assumes the
    resources are working as expected."""
    # Folders don't work with GMail accounts, need generic IMAP
    if resource_name == 'folders':
        api_client = new_api_client(db, generic_account.namespace)
    elements = api_client.get_data('/{}'.format(resource_name))
    count = api_client.get_data('/{}?view=count'.format(resource_name))

    assert count["count"] == len(elements)

    ids = api_client.get_data('/{}?view=ids'.format(resource_name))

    for i, elem in enumerate(elements):
        assert isinstance(ids[i], basestring), \
            "&views=ids should return string"
        assert elem["id"] == ids[i], "view=ids should preserve order"
def test_folder_post(db, generic_account):
    api_client = new_api_client(db, generic_account.namespace)
    po_data = api_client.post_data('/folders/',
                                   {"display_name": "Test_Folder"})
    assert po_data.status_code == 200
示例#18
0
def folder_client(db, generic_account):
    api_client = new_api_client(db, generic_account.namespace)

    api_client.post_data('/folders/',
                         {"display_name": "Test_Folder"})
    return api_client
def test_label_post(db, gmail_account):
    api_client = new_api_client(db, gmail_account.namespace)
    po_data = api_client.post_data('/labels/',
                                   {"display_name": "Test_Label"})
    assert po_data.status_code == 200
示例#20
0
def imap_api_client(db, generic_account):
    return new_api_client(db, generic_account.namespace)