def test_get_accounts_to_delete(db): from inbox.models import Account from inbox.models.util import get_accounts_to_delete existing_account_count = db.session.query(Account.id).count() accounts = [] email = "test{}@nylas.com" for i in range(1, 6): account = add_completely_fake_account(db, email.format(i)) accounts.append(account) # Ensure all of the accounts have been created successfully assert db.session.query(Account.id).count() == (existing_account_count + 5) # get_accounts_to_delete() with no accounts marked as deleted accounts_to_delete = get_accounts_to_delete(0) assert len(accounts_to_delete) == 0 # get_accounts_to_delete() with one account marked as deleted accounts[0].mark_for_deletion() db.session.commit() accounts_to_delete = get_accounts_to_delete(0) assert len(accounts_to_delete) == 1 # get_accounts_to_delete() with more than one account marked as deleted for i in range(1, 4): accounts[i].mark_for_deletion() db.session.commit() accounts_to_delete = get_accounts_to_delete(0) assert len(accounts_to_delete) == 4
def test_get_accounts_to_delete(db): from inbox.models import Account from inbox.models.util import get_accounts_to_delete existing_account_count = db.session.query(Account.id).count() accounts = [] email = 'test{}@nylas.com' for i in range(1, 6): account = add_completely_fake_account(db, email.format(i)) accounts.append(account) # Ensure all of the accounts have been created successfully assert db.session.query(Account.id).count() == (existing_account_count + 5) # get_accounts_to_delete() with no accounts marked as deleted accounts_to_delete = get_accounts_to_delete(0) assert len(accounts_to_delete) == 0 # get_accounts_to_delete() with one account marked as deleted accounts[0].mark_deleted() db.session.commit() accounts_to_delete = get_accounts_to_delete(0) assert len(accounts_to_delete) == 1 # get_accounts_to_delete() with more than one account marked as deleted for i in range(1, 4): accounts[i].mark_deleted() db.session.commit() accounts_to_delete = get_accounts_to_delete(0) assert len(accounts_to_delete) == 4
def test_bulk_namespace_deletion(db): from inbox.models import Account from inbox.models.util import batch_delete_namespaces, get_accounts_to_delete db.session.query(Account).delete(synchronize_session=False) db.session.commit() assert db.session.query(Account.id).count() == 0 # Add 5 accounts account_1 = add_completely_fake_account(db) account_1_id = account_1.id account_2 = add_completely_fake_account(db, "*****@*****.**") account_2_id = account_2.id account_3 = add_completely_fake_account(db, "*****@*****.**") account_3_id = account_3.id account_4 = add_completely_fake_account(db, "*****@*****.**") account_4_id = account_4.id add_completely_fake_account(db, "*****@*****.**") # Ensure all of the accounts have been created successfully assert db.session.query(Account).count() == 5 # batch_delete_namespaces() with no accounts marked as deleted to_delete = get_accounts_to_delete(0) batch_delete_namespaces(to_delete) assert len(db.session.query(Account.id).all()) == 5 # batch_delete_namespaces() with one account marked as deleted account_1.mark_for_deletion() db.session.commit() to_delete = get_accounts_to_delete(0) batch_delete_namespaces(to_delete) alive_accounts = db.session.query(Account.id).all() assert len(alive_accounts) == 4 assert account_1_id not in alive_accounts # batch_delete_namespaces() with more than one account marked as deleted account_2.mark_for_deletion() account_3.mark_for_deletion() account_4.mark_for_deletion() db.session.commit() to_delete = get_accounts_to_delete(0) batch_delete_namespaces(to_delete) alive_accounts = db.session.query(Account.id).all() assert len(alive_accounts) == 1 assert account_4_id not in alive_accounts assert account_3_id not in alive_accounts assert account_2_id not in alive_accounts
def test_bulk_namespace_deletion(db): from inbox.models import Account from inbox.models.util import get_accounts_to_delete, delete_marked_accounts db.session.query(Account).delete(synchronize_session=False) db.session.commit() assert db.session.query(Account.id).count() == 0 # Add 5 accounts account_1 = add_completely_fake_account(db) account_1_id = account_1.id account_2 = add_completely_fake_account(db, "*****@*****.**") account_2_id = account_2.id account_3 = add_completely_fake_account(db, "*****@*****.**") account_3_id = account_3.id account_4 = add_completely_fake_account(db, "*****@*****.**") account_4_id = account_4.id add_completely_fake_account(db, "*****@*****.**") # Ensure all of the accounts have been created successfully assert db.session.query(Account).count() == 5 # delete_marked_accounts() with no accounts marked as deleted to_delete = get_accounts_to_delete(0) delete_marked_accounts(0, to_delete) assert len(db.session.query(Account.id).all()) == 5 # delete_marked_accounts() with one account marked as deleted account_1.mark_deleted() db.session.commit() to_delete = get_accounts_to_delete(0) delete_marked_accounts(0, to_delete) alive_accounts = db.session.query(Account.id).all() assert len(alive_accounts) == 4 assert account_1_id not in alive_accounts # delete_marked_accounts() with more than one account marked as deleted account_2.mark_deleted() account_3.mark_deleted() account_4.mark_deleted() db.session.commit() to_delete = get_accounts_to_delete(0) delete_marked_accounts(0, to_delete) alive_accounts = db.session.query(Account.id).all() assert len(alive_accounts) == 1 assert account_4_id not in alive_accounts assert account_3_id not in alive_accounts assert account_2_id not in alive_accounts
def test_deletion_time_throttle(db, patch_requests_no_throttle): from inbox.models import Account from inbox.models.util import get_accounts_to_delete, delete_marked_accounts account_1 = add_completely_fake_account(db, "*****@*****.**") account_1_id = account_1.id account_2 = add_completely_fake_account(db, "*****@*****.**") account_2_id = account_2.id account_1.mark_deleted() account_2.mark_deleted() db.session.commit() to_delete = get_accounts_to_delete(0) greenlet = gevent.spawn(delete_marked_accounts, 0, to_delete, throttle=True) greenlet.join() alive_accounts = [acc.id for acc in db.session.query(Account).all()] # Ensure the two accounts we added are still present assert account_1_id in alive_accounts assert account_2_id in alive_accounts
def test_deletion_no_throttle(db, patch_requests_no_throttle): from inbox.models import Account from inbox.models.util import get_accounts_to_delete, delete_marked_accounts new_accounts = set() account_1 = add_completely_fake_account(db) new_accounts.add(account_1.id) account_2 = add_completely_fake_account(db, "*****@*****.**") new_accounts.add(account_2.id) account_1.mark_deleted() account_2.mark_deleted() db.session.commit() to_delete = get_accounts_to_delete(0) greenlet = gevent.spawn(delete_marked_accounts, 0, to_delete, throttle=True) greenlet.join() alive_accounts = db.session.query(Account.id).all() # Ensure the two accounts we added were deleted assert new_accounts - set(alive_accounts) == new_accounts
def delete_account_data(host, throttle, dry_run): while True: for shard in host["SHARDS"]: # Ensure shard is explicitly not marked as disabled if "DISABLED" in shard and not shard["DISABLED"]: namespace_ids = get_accounts_to_delete(shard["ID"]) batch_delete_namespaces(namespace_ids, throttle, dry_run) gevent.sleep(600)
def test_deletion_metric_throttle(db, patch_requests_throttle): from inbox.models import Account from inbox.models.util import batch_delete_namespaces, get_accounts_to_delete account_1 = add_completely_fake_account(db) account_1_id = account_1.id account_2 = add_completely_fake_account(db, "*****@*****.**") account_2_id = account_2.id account_1.mark_for_deletion() account_2.mark_for_deletion() db.session.commit() to_delete = get_accounts_to_delete(0) greenlet = gevent.spawn(batch_delete_namespaces, to_delete, throttle=True) greenlet.join() alive_accounts = [acc.id for acc in db.session.query(Account).all()] # Ensure the two accounts we added are still present assert account_1_id in alive_accounts assert account_2_id in alive_accounts