def test_external_sync_disabling(db, mock_queue_client):
    purge_other_accounts()
    account = add_fake_account(db.session, email_address='*****@*****.**')
    other_account = add_fake_account(db.session,
                                     email_address='*****@*****.**')
    qp = QueuePopulator(zone='testzone')
    qp.queue_client = mock_queue_client
    s = patched_sync_service(mock_queue_client)

    qp.enqueue_new_accounts()
    s.poll()
    s.poll()
    assert len(s.syncing_accounts) == 2

    account.mark_deleted()
    db.session.commit()
    assert account.sync_should_run is False
    assert account._sync_status['sync_disabled_reason'] == 'account deleted'

    account.mark_invalid()
    db.session.commit()
    assert account.sync_should_run is False
    assert account.sync_state == 'invalid'
    assert account._sync_status['sync_disabled_reason'] == \
        'invalid credentials'

    qp.unassign_disabled_accounts()
    s.poll()
    assert s.syncing_accounts == {other_account.id}
def test_data_deduplication(s3_db, raw_message):
    from inbox.models import Namespace

    default_namespace = s3_db.session.query(Namespace).get(1)
    thread = add_fake_thread(s3_db.session, default_namespace.id)
    msg = new_message_from_synced(s3_db, default_namespace.account, raw_message)
    msg.thread = thread
    s3_db.session.add(msg)
    s3_db.session.commit()

    account = add_fake_account(s3_db.session)
    thread = add_fake_thread(s3_db.session, account.namespace.id)
    duplicate_msg = new_message_from_synced(s3_db, account, raw_message)
    duplicate_msg.thread = thread
    s3_db.session.add(duplicate_msg)
    s3_db.session.commit()

    assert len(msg.parts) == len(duplicate_msg.parts)

    for i in range(len(msg.parts)):
        msg_block = msg.parts[i].block
        duplicate_msg_block = duplicate_msg.parts[i].block

        assert msg_block.namespace_id == msg.namespace_id
        assert duplicate_msg_block.namespace_id == duplicate_msg.namespace_id

        assert msg_block.size == duplicate_msg_block.size
        assert msg_block.data_sha256 == duplicate_msg_block.data_sha256
        assert msg_block.data == duplicate_msg_block.data
def test_data_deduplication(db, default_namespace, raw_message):
    thread = add_fake_thread(db.session, default_namespace.id)
    msg = new_message_from_synced(db, default_namespace.account, raw_message)
    msg.thread = thread
    db.session.add(msg)
    db.session.commit()

    account = add_fake_account(db.session)
    thread = add_fake_thread(db.session, account.namespace.id)
    duplicate_msg = new_message_from_synced(db, account, raw_message)
    duplicate_msg.thread = thread
    db.session.add(duplicate_msg)
    db.session.commit()

    assert len(msg.parts) == len(duplicate_msg.parts)

    for i in range(len(msg.parts)):
        msg_block = msg.parts[i].block
        duplicate_msg_block = duplicate_msg.parts[i].block

        assert msg_block.namespace_id == msg.namespace_id
        assert duplicate_msg_block.namespace_id == duplicate_msg.namespace_id

        assert msg_block.size == duplicate_msg_block.size
        assert msg_block.data_sha256 == duplicate_msg_block.data_sha256
        assert msg_block.data == duplicate_msg_block.data
def test_namespace_deletion(db, default_account):
    from inbox.models import (Account, Thread, Message, Block,
                              Contact, Event, Transaction)
    from inbox.models.util import delete_namespace

    models = [Thread, Message]

    namespace = default_account.namespace
    namespace_id = namespace.id
    account_id = default_account.id

    account = db.session.query(Account).get(account_id)
    assert account

    thread = add_fake_thread(db.session, namespace_id)

    message = add_fake_message(db.session, namespace_id, thread)

    for m in models:
        c = db.session.query(m).filter(
            m.namespace_id == namespace_id).count()
        print "count for", m, ":", c
        assert c != 0

    fake_account = add_fake_account(db.session)
    fake_account_id = fake_account.id

    assert fake_account_id != account.id and \
        fake_account.namespace.id != namespace_id

    thread = add_fake_thread(db.session, fake_account.namespace.id)
    thread_id = thread.id

    message = add_fake_message(db.session, fake_account.namespace.id, thread)
    message_id = message.id

    # Delete namespace, verify data corresponding to this namespace /only/
    # is deleted
    delete_namespace(account_id, namespace_id)
    db.session.commit()

    account = db.session.query(Account).get(account_id)
    assert not account

    for m in models:
        assert db.session.query(m).filter(
            m.namespace_id == namespace_id).count() == 0

    fake_account = db.session.query(Account).get(fake_account_id)
    assert fake_account

    thread = db.session.query(Thread).get(thread_id)
    message = db.session.query(Message).get(message_id)
    assert thread and message
def test_namespace_deletion(db, default_account):
    from inbox.models import (Account, Thread, Message, Block, Contact, Event,
                              Transaction)
    from inbox.models.util import delete_namespace

    models = [Thread, Message]

    namespace = default_account.namespace
    namespace_id = namespace.id
    account_id = default_account.id

    account = db.session.query(Account).get(account_id)
    assert account

    thread = add_fake_thread(db.session, namespace_id)

    message = add_fake_message(db.session, namespace_id, thread)

    for m in models:
        c = db.session.query(m).filter(m.namespace_id == namespace_id).count()
        print "count for", m, ":", c
        assert c != 0

    fake_account = add_fake_account(db.session)
    fake_account_id = fake_account.id

    assert fake_account_id != account.id and \
        fake_account.namespace.id != namespace_id

    thread = add_fake_thread(db.session, fake_account.namespace.id)
    thread_id = thread.id

    message = add_fake_message(db.session, fake_account.namespace.id, thread)
    message_id = message.id

    # Delete namespace, verify data corresponding to this namespace /only/
    # is deleted
    delete_namespace(account_id, namespace_id)
    db.session.commit()

    account = db.session.query(Account).get(account_id)
    assert not account

    for m in models:
        assert db.session.query(m).filter(
            m.namespace_id == namespace_id).count() == 0

    fake_account = db.session.query(Account).get(fake_account_id)
    assert fake_account

    thread = db.session.query(Thread).get(thread_id)
    message = db.session.query(Message).get(message_id)
    assert thread and message