def test_namespace_deletion(latest_db):
    from inbox.models import *
    from inbox.models.util import delete_namespace

    namespace = latest_db.session.query(Namespace).first()
    namespace_id = namespace.id

    account_id = namespace.account.id
    models = [Thread, Message, Block, Contact, Event, Transaction]

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

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

    # Delete namespace
    delete_namespace(account_id, namespace_id)
    latest_db.session.commit()

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

    for m in models:
        assert latest_db.session.query(m).filter(
            m.namespace_id == namespace_id).count() == 0
示例#2
0
def test_namespace_deletion(db, default_account):
    from inbox.models import Account, Thread, Message
    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_generic_imap_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

    assert len(
        db.session.query(Namespace).filter(
            Namespace.id == namespace_id).all()) > 0

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

    assert len(
        db.session.query(Namespace).filter(
            Namespace.id == namespace_id).all()) == 0

    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
示例#3
0
def test_multiple_fake_accounts(empty_db):
    # Add three fake accounts, check that removing one doesn't affect
    # the two others.
    from inbox.models import Block, Contact, Event, Message, Secret, Thread, Transaction
    from inbox.models.util import delete_namespace

    db = empty_db
    accounts = []
    accounts.append(add_completely_fake_account(db, "*****@*****.**"))
    accounts.append(add_completely_fake_account(db, "*****@*****.**"))

    # Count secrets and authcredentials now. We can't do it after adding
    # the third account because our object model is a bit cumbersome.
    secret_count = db.session.query(Secret).count()
    assert secret_count != 0

    accounts.append(add_completely_fake_account(db, "*****@*****.**"))

    stats = {}
    models = [Thread, Message, Event, Transaction, Contact, Block]

    for account in accounts:
        stats[account.email_address] = {}
        for model in models:
            clsname = model.__name__
            stats[account.email_address][clsname] = (
                db.session.query(model)
                .filter(model.namespace_id == account.namespace.id)
                .count()
            )

    # now delete the third account.
    last_namespace_id = accounts[2].namespace.id
    accounts[2].mark_for_deletion()

    delete_namespace(last_namespace_id)

    for account in accounts[:2]:
        for model in models:
            clsname = model.__name__
            assert (
                stats[account.email_address][clsname]
                == db.session.query(model)
                .filter(model.namespace_id == account.namespace.id)
                .count()
            )

    # check that no model from the last account is present.
    for model in models:
        clsname = model.__name__
        assert (
            db.session.query(model)
            .filter(model.namespace_id == last_namespace_id)
            .count()
            == 0
        )

    # check that we didn't delete a secret that wasn't ours.
    assert db.session.query(Secret).count() == secret_count
示例#4
0
def test_namespace_deletion(db, default_account):
    from inbox.models import Account, Thread, Message
    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_generic_imap_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

    assert len(db.session.query(Namespace).filter(Namespace.id == namespace_id).all()) > 0

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

    assert len(db.session.query(Namespace).filter(Namespace.id == namespace_id).all()) == 0

    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
示例#5
0
def test_multiple_fake_accounts(empty_db):
    # Add three fake accounts, check that removing one doesn't affect
    # the two others.
    from inbox.models import (Account, Thread, Message, Block, Part,
                              Secret, Contact, Event, Transaction)
    from inbox.models.backends.imap import ImapUid
    from inbox.models.backends.gmail import GmailAuthCredentials
    from inbox.models.util import delete_namespace

    db = empty_db
    accounts = []
    accounts.append(add_completely_fake_account(db, '*****@*****.**'))
    accounts.append(add_completely_fake_account(db, '*****@*****.**'))

    # Count secrets and authcredentials now. We can't do it after adding
    # the third account because our object model is a bit cumbersome.
    secret_count = db.session.query(Secret).count()
    authcredentials_count = db.session.query(GmailAuthCredentials).count()
    assert secret_count != 0
    assert authcredentials_count != 0

    accounts.append(add_completely_fake_account(db, '*****@*****.**'))

    stats = {}
    models = [Thread, Message, Event, Transaction, Contact, Block]

    for account in accounts:
        stats[account.email_address] = {}
        for model in models:
            clsname = model.__name__
            stats[account.email_address][clsname] = db.session.query(model).filter(
                model.namespace_id == account.namespace.id).count()

    # now delete the third account.
    last_account_id = accounts[2].id
    last_namespace_id = accounts[2].namespace.id

    delete_namespace(last_account_id, last_namespace_id)
    for account in accounts[:2]:
        for model in models:
            clsname = model.__name__
            assert stats[account.email_address][clsname] == db.session.query(model).filter(
                model.namespace_id == account.namespace.id).count()

    # check that no model from the last account is present.
    for model in models:
        clsname = model.__name__
        assert db.session.query(model).filter(
            model.namespace_id == last_namespace_id).count() == 0

    # check that we didn't delete a secret that wasn't ours.
    assert db.session.query(Secret).count() == secret_count
    assert db.session.query(GmailAuthCredentials).count() == authcredentials_count
示例#6
0
def test_fake_accounts(empty_db):
    from inbox.models import (Account, Thread, Message, Block, Secret, Contact,
                              Event, Transaction)
    from inbox.models.backends.imap import ImapUid
    from inbox.models.backends.gmail import GmailAuthCredentials
    from inbox.models.util import delete_namespace

    models = [Thread, Message, Event, Transaction, Contact, Block]

    db = empty_db
    account = add_completely_fake_account(db)

    for m in models:
        c = db.session.query(m).filter(
            m.namespace_id == account.namespace.id).count()
        assert c != 0

    assert db.session.query(ImapUid).count() != 0
    assert db.session.query(Secret).count() != 0
    assert db.session.query(GmailAuthCredentials).count() != 0
    assert db.session.query(Account).filter(
        Account.id == account.id).count() == 1

    # Try the dry-run mode:
    account.mark_for_deletion()
    delete_namespace(account.namespace.id, dry_run=True)

    for m in models:
        c = db.session.query(m).filter(
            m.namespace_id == account.namespace.id).count()
        assert c != 0

    assert db.session.query(Account).filter(
        Account.id == account.id).count() != 0

    assert db.session.query(Secret).count() != 0
    assert db.session.query(GmailAuthCredentials).count() != 0
    assert db.session.query(ImapUid).count() != 0

    # Now delete the account for reals.
    delete_namespace(account.namespace.id)

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

    assert db.session.query(Account).filter(
        Account.id == account.id).count() == 0

    assert db.session.query(Secret).count() == 0
    assert db.session.query(GmailAuthCredentials).count() == 0
    assert db.session.query(ImapUid).count() == 0
示例#7
0
def test_fake_accounts(empty_db):
    from inbox.models import (Account, Thread, Message, Block,
                              Secret, Contact, Event, Transaction)
    from inbox.models.backends.imap import ImapUid
    from inbox.models.backends.gmail import GmailAuthCredentials
    from inbox.models.util import delete_namespace

    models = [Thread, Message, Event, Transaction, Contact, Block]

    db = empty_db
    account = add_completely_fake_account(db)

    for m in models:
        c = db.session.query(m).filter(
            m.namespace_id == account.namespace.id).count()
        assert c != 0

    assert db.session.query(ImapUid).count() != 0
    assert db.session.query(Secret).count() != 0
    assert db.session.query(GmailAuthCredentials).count() != 0
    assert db.session.query(Account).filter(
        Account.id == account.id).count() == 1

    # Try the dry-run mode:
    account.mark_for_deletion()
    delete_namespace(account.namespace.id, dry_run=True)

    for m in models:
        c = db.session.query(m).filter(
            m.namespace_id == account.namespace.id).count()
        assert c != 0

    assert db.session.query(Account).filter(
        Account.id == account.id).count() != 0

    assert db.session.query(Secret).count() != 0
    assert db.session.query(GmailAuthCredentials).count() != 0
    assert db.session.query(ImapUid).count() != 0

    # Now delete the account for reals.
    delete_namespace(account.namespace.id)

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

    assert db.session.query(Account).filter(
        Account.id == account.id).count() == 0

    assert db.session.query(Secret).count() == 0
    assert db.session.query(GmailAuthCredentials).count() == 0
    assert db.session.query(ImapUid).count() == 0
示例#8
0
def delete_account_data(account_id, dry_run, yes, throttle):
    maybe_enable_rollbar()

    with session_scope(account_id) as db_session:
        account = db_session.query(Account).get(account_id)

        if not account:
            print("Account with id {} does NOT exist.".format(account_id))
            return

        email_address = account.email_address
        namespace_id = account.namespace.id

        if account.sync_should_run or not account.is_marked_for_deletion:
            print("Account with id {} NOT marked for deletion.\n"
                  "Will NOT delete, goodbye.".format(account_id))
            return -1

    if not yes:
        question = (
            "Are you sure you want to delete all data for account with "
            "id: {}, email_address: {} and namespace_id: {}? [yes / no]".
            format(account_id, email_address, namespace_id))

        answer = raw_input(question).strip().lower()

        if answer != "yes":
            print("Will NOT delete, goodbye.")
            return 0

    print("Deleting account with id: {}...".format(account_id))
    start = time.time()

    # Delete data in database
    try:
        print("Deleting database data")
        delete_namespace(namespace_id, dry_run=dry_run, throttle=throttle)
    except Exception as e:
        print("Database data deletion failed! Error: {}".format(str(e)))
        return -1

    database_end = time.time()
    print("Database data deleted. Time taken: {}".format(database_end - start))

    # Delete liveness data
    print("Deleting liveness data")
    clear_heartbeat_status(account_id)

    end = time.time()
    print("All data deleted successfully! TOTAL time taken: {}".format(end -
                                                                       start))
    return 0
def test_namespace_deletion(db):
    from inbox.models import (Namespace, Account, Thread, Message, Block,
                              Contact, Event, Transaction)
    from inbox.models.util import delete_namespace

    models = [Thread, Message, Block, Contact, Event, Transaction]

    namespace = db.session.query(Namespace).first()
    namespace_id = namespace.id
    account_id = namespace.account.id

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

    for m in models:
        assert db.session.query(m).filter(
            m.namespace_id == namespace_id).count() != 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
示例#10
0
def test_namespace_deletion(db):
    from inbox.models import (Namespace, Account, Thread, Message, Block,
                              Contact, Event, Transaction)
    from inbox.models.util import delete_namespace

    models = [Thread, Message, Block, Contact, Event, Transaction]

    namespace = db.session.query(Namespace).first()
    namespace_id = namespace.id
    account_id = namespace.account.id

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

    for m in models:
        assert db.session.query(m).filter(
            m.namespace_id == namespace_id).count() != 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