Exemple #1
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_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
Exemple #3
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
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
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() == []
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_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() == []
Exemple #9
0
def test_invalid_basic_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {'Authorization': 'Basic {}'
                              .format(b64encode(BAD_TOKEN + ':'))}

    response = api_client.get_raw('/account')
    assert response.status_code == 401
Exemple #10
0
def test_invalid_bearer_token_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {
        'Authorization': 'Bearer {}'.format(BAD_TOKEN)}

    response = api_client.get_raw('/account')
    assert response.status_code == 401
Exemple #11
0
def test_basic_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)

    response = api_client.get_raw('/account')
    assert response.status_code == 200

    resp_data = json.loads(response.data)
    assert resp_data['id'] == generic_account.namespace.public_id
Exemple #12
0
def test_invalid_basic_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {
        'Authorization': 'Basic {}'.format(b64encode(BAD_TOKEN + ':'))
    }

    response = api_client.get_raw('/account')
    assert response.status_code == 401
Exemple #13
0
def test_basic_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)

    response = api_client.get_raw('/account')
    assert response.status_code == 200

    resp_data = json.loads(response.data)
    assert resp_data['id'] == generic_account.namespace.public_id
Exemple #14
0
def test_no_auth(db, generic_account):  # noqa
    # Because we're using the generic_account namespace

    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {}

    response = api_client.get_raw('/account')
    assert response.status_code == 401
Exemple #15
0
def test_no_auth(db, generic_account):  # noqa
    # Because we're using the generic_account namespace

    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {}

    response = api_client.get_raw('/account')
    assert response.status_code == 401
Exemple #16
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)
    while len(asyncore.socket_map) < 1:
        gevent.sleep(0)  # let SMTP daemon start up
    r = api_client.post_data('/send', example_draft)
    assert r.status_code == 200
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)
    while len(asyncore.socket_map) < 1:
        gevent.sleep(0)  # let SMTP daemon start up
    r = api_client.post_data('/send', example_draft)
    assert r.status_code == 200
Exemple #18
0
def test_bearer_token_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {
        'Authorization': 'Bearer {}'
        .format(generic_account.namespace.public_id)}

    response = api_client.get_raw('/account')
    assert response.status_code == 200

    resp_data = json.loads(response.data)
    assert resp_data['id'] == generic_account.namespace.public_id
Exemple #19
0
def test_bearer_token_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {
        'Authorization':
        'Bearer {}'.format(generic_account.namespace.public_id)
    }

    response = api_client.get_raw('/account')
    assert response.status_code == 200

    resp_data = json.loads(response.data)
    assert resp_data['id'] == generic_account.namespace.public_id
Exemple #20
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'
Exemple #21
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'
Exemple #22
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
Exemple #23
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
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
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
Exemple #26
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"
Exemple #27
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_message_labels(db, gmail_account):
    # Because we're using the gmail_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
Exemple #29
0
def test_message_labels(db, gmail_account):
    # Because we're using the gmail_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
Exemple #30
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 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
Exemple #32
0
def test_invalid_bearer_token_auth(db, generic_account):  # noqa
    api_client = new_api_client(db, generic_account.namespace)
    api_client.auth_header = {'Authorization': 'Bearer {}'.format(BAD_TOKEN)}

    response = api_client.get_raw('/account')
    assert response.status_code == 401