def test_categories_basic(self): # Verify category list empty initially and insert a category with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(categories, []) category = models.Category(name="name", user=self.u) api.category_create(self.s, [category]) # Retrieve and verify inserted category with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "name") # Update the category with self.s.begin(): category.name = 'new name' api.category_update(self.s, [category]) # Check the updated category with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "new name") # Clean up with self.s.begin(): api.category_delete(self.s, categories, True) # Verify clean up successful with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 0)
def test_categories_get_filter(self): # Insert several categories with self.s.begin(): categories = [ models.Category(name="name0", user=self.u), models.Category(name="name1", user=self.u), models.Category(name="name2", user=self.u) ] api.category_create(self.s, categories) # Retrieve first and last categories only with self.s.begin(): ids = [1, 3] categories = api.category_getall(self.s, self.u, filter_ids=ids) self.assertEqual(len(categories), 2) self.assertEqual(categories[0].name, "name0") self.assertEqual(categories[1].name, "name2") # Clean up with self.s.begin(): categories = api.category_getall(self.s, self.u) api.category_delete(self.s, categories, True) # Verify clean up successful with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 0)
def test_categories_delete_by_id(self): # Insert several categories with self.s.begin(): categories = [ models.Category(name="name3", user=self.u), models.Category(name="name4", user=self.u), models.Category(name="name5", user=self.u) ] api.category_create(self.s, categories) # Delete first and last categories only with self.s.begin(): ids = [1, 3] api.category_delete_by_id(self.s, self.u, ids, cascade=False) # Verify only second added category remains with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "name4") # Clean up with self.s.begin(): categories = api.category_getall(self.s, self.u) api.category_delete(self.s, categories, True) # Verify clean up successful with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 0)
def test_items_basic(self): # Verify item list empty initially and insert an item with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(items, []) item = models.Item(blob="blob", category_id=None, user=self.u) api.item_create(self.s, [item]) # Retrieve and verify inserted item with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 1) self.assertEqual(items[0].blob, "blob") # Update the item with self.s.begin(): item.blob = "new blob" api.item_update(self.s, [item]) # Check the updated item with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 1) self.assertEqual(items[0].blob, "new blob") self.assertEqual(items[0].category_id, None) # Update item with valid category with self.s.begin(): category = models.Category(name="blah", user=self.u) api.category_create(self.s, [category]) item.category_id = 1 api.item_update(self.s, [item]) # Check the updated item with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 1) self.assertEqual(items[0].blob, "new blob") self.assertEqual(items[0].category_id, 1) self.assertIsNotNone(items[0].category) # Clean up with self.s.begin(): categories = api.category_getall(self.s, self.u) api.category_delete(self.s, categories, True) # Verify clean up successful with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 0) items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 0)
def _do_get(self, phrase): """ Fetch all categories and items data for a particular user. :param phrase: decryption passphrase :returns: success result along with categories and items arrays """ cat_array = [] item_array = [] cipher = aescipher.AESCipher(phrase) try: categories = api.category_getall(self.session, self.user) for category in categories: cat_array.append(category.extract(cipher, with_items=False)) items = api.item_getall(self.session, self.user) for item in items: item_array.append(item.extract(cipher, with_category=False)) except UnicodeDecodeError: raise bh.OppError("Unable to decrypt data!") except Exception: raise bh.OppError("Unable to fetch from the database!") return { 'result': 'success', 'categories': cat_array, 'items': item_array }
def _do_get(self, phrase): response = [] cipher = aescipher.AESCipher(phrase) categories = api.category_getall(session=self.session) for category in categories: response.append(category.extract(cipher)) return {'result': "success", 'categories': response}
def test_categories_get_filter(self): # Insert several categories categories = [ models.Category(name="name0"), models.Category(name="name1"), models.Category(name="name2") ] api.category_create(categories, session=self.session) # Retrieve first and last categories only ids = [1, 3] categories = api.category_getall(filter_ids=ids, session=self.session) self.assertEqual(len(categories), 2) self.assertEqual(categories[0].name, "name0") self.assertEqual(categories[1].name, "name2") # Clean up and verify categories = api.category_getall(session=self.session) api.category_delete(categories, True, session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 0)
def test_categories_access_by_user(self): # Verify category list empty initially, add a category and some items with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(categories, []) category = models.Category(name="name", user=self.u) api.category_create(self.s, [category]) items = [ models.Item(blob="item1", category_id=1, user=self.u), models.Item(blob="item2", category_id=1, user=self.u) ] api.item_create(self.s, items) # Retrieve and verify inserted category with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "name") self.assertEqual(len(categories[0].items), 2) # Add another user and retrieve it utils.execute("opp-db --config_file %s add-user -uu2 -pp " "--phrase=123456" % self.conf_filepath) new_u = api.user_get_by_username(self.s, "u2") self.assertEqual(new_u.username, "u2") # Attempt to retrieve categories with new user with self.s.begin(): new_categories = api.category_getall(self.s, new_u) self.assertEqual(len(new_categories), 0) # Clean up with self.s.begin(): api.category_delete(self.s, categories, True) # Verify clean up successful with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 0)
def test_categories_delete_by_id(self): # Insert several categories categories = [ models.Category(name="name3"), models.Category(name="name4"), models.Category(name="name5") ] api.category_create(categories, session=self.session) # Delete first and last categories only ids = [1, 3] api.category_delete_by_id(ids, cascade=False, session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "name4") # Clean up and verify categories = api.category_getall(session=self.session) api.category_delete(categories, True, session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 0)
def test_categories_basic(self): # Expect empty category list initially categories = api.category_getall(session=self.session) self.assertEqual(categories, []) # Insert and retrieve a category category = models.Category(name="name") api.category_create([category], session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 1) # Update and check the category category.name = 'new name' api.category_update([category], session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "new name") # Clean up and verify api.category_delete(categories, True, session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 0)
def test_items_basic(self): # Expect empty item list initially items = api.item_getall(session=self.session) self.assertEqual(items, []) # Insert and retrieve an item item = models.Item(blob="blob", category_id=None) api.item_create([item], session=self.session) items = api.item_getall(session=self.session) self.assertEqual(len(items), 1) # Update and check the item item.blob = "new blob" item.category_id = 999 api.item_update([item], session=self.session) items = api.item_getall(session=self.session) self.assertEqual(len(items), 1) self.assertEqual(items[0].blob, "new blob") self.assertEqual(items[0].category_id, 999) # Update item with valid category category = models.Category(name="blah") api.category_create([category], session=self.session) item.category_id = 1 api.item_update([item], session=self.session) items = api.item_getall(session=self.session) self.assertEqual(len(items), 1) self.assertEqual(items[0].blob, "new blob") self.assertEqual(items[0].category_id, 1) self.assertIsNotNone(items[0].category) # Clean up and verify categories = api.category_getall(session=self.session) api.category_delete(categories, True, session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 0) items = api.item_getall(session=self.session) self.assertEqual(len(items), 0)
def test_update_phrase(self): config = opp_config.OppConfig(self.conf_filepath) # Add user utils.execute("opp-db --config_file %s add-user -uu -pp " "--phrase=123456" % self.conf_filepath) old = aescipher.AESCipher("123456") new = aescipher.AESCipher("654321") # Add category and item using old passphrase session = api.get_scoped_session(config) category = models.Category(name=old.encrypt("cat1")) item = models.Item(name=old.encrypt("item1"), url=old.encrypt("url1"), account=old.encrypt("account1"), username=old.encrypt("username1"), password=old.encrypt("password1"), blob=old.encrypt("blob1")) with session.begin(): user = api.user_get_by_username(session, 'u') category.user = user item.user = user api.category_create(session, [category]) api.item_create(session, [item]) # Update passphrase utils.execute("opp-db --config_file %s update-phrase -uu -pp " "--old_phrase=123456 --new_phrase=654321" % self.conf_filepath) # Check data using new passphrase session = api.get_scoped_session(config) with session.begin(): user = api.user_get_by_username(session, 'u') category = api.category_getall(session, user)[0] self.assertEqual(new.decrypt(category.name), "cat1") item = api.item_getall(session, user)[0] self.assertEqual(new.decrypt(item.name), "item1") self.assertEqual(new.decrypt(item.url), "url1") self.assertEqual(new.decrypt(item.account), "account1") self.assertEqual(new.decrypt(item.username), "username1") self.assertEqual(new.decrypt(item.password), "password1") self.assertEqual(new.decrypt(item.blob), "blob1") # Cleanup utils.execute("opp-db --config_file %s del-user -uu -pp" " --remove_data" % self.conf_filepath) self._assert_user_does_not_exist('u')
def update_phrase(config, u, p, old_phrase, new_phrase): if len(new_phrase) < 6: sys.exit("Error: passphrase must be at least 6 characters long!") try: old_cipher = aescipher.AESCipher(old_phrase) new_cipher = aescipher.AESCipher(new_phrase) s = api.get_scoped_session(config.conf) with s.begin(): user = api.user_get_by_username(s, u) if not user: sys.exit("Error: user does not exist!") if not utils.checkpw(p, user.password): sys.exit("Error: incorrect password!") try: if old_cipher.decrypt(user.phrase_check) != "OK": sys.exit("Error: incorrect old passphrase supplied!") except UnicodeDecodeError: sys.exit("Error: incorrect old passphrase supplied!") printv(config, "Updating user information") user.phrase_check = new_cipher.encrypt("OK") api.user_update(s, user) printv(config, "Updating user's categories") categories = api.category_getall(s, user) for category in categories: category.recrypt(old_cipher, new_cipher) api.category_update(s, categories) printv(config, "Updating user's items") items = api.item_getall(s, user) for item in items: item.recrypt(old_cipher, new_cipher) api.item_update(s, items) print("All of user's data has been successfuly " "re-encrypted with the new passphrase.") except Exception as e: sys.exit("Error: %s" % str(e))
def test_categories_delete_cascade(self): # Create two categories with self.s.begin(): categories = [ models.Category(name="cat1", user=self.u), models.Category(name="cat2", user=self.u) ] api.category_create(self.s, categories) # Verify created categories with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 2) self.assertEqual(categories[0].name, "cat1") self.assertEqual(categories[1].name, "cat2") # Create 4 items with self.s.begin(): items = [ models.Item(blob="item1", category_id=1, user=self.u), models.Item(blob="item2", category_id=1, user=self.u), models.Item(blob="item3", category_id=2, user=self.u), models.Item(blob="item4", category_id=2, user=self.u) ] api.item_create(self.s, items) # Verify created items with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 4) i1, i2, i3, i4 = items self.assertEqual(i1.blob, "item1") self.assertEqual(i1.category_id, 1) self.assertIsNotNone(i1.category) self.assertEqual(i1.category.id, 1) self.assertEqual(i2.blob, "item2") self.assertEqual(i2.category_id, 1) self.assertIsNotNone(i2.category) self.assertEqual(i1.category.id, 1) self.assertEqual(i3.blob, "item3") self.assertEqual(i3.category_id, 2) self.assertIsNotNone(i3.category) self.assertEqual(i3.category.id, 2) self.assertEqual(i4.blob, "item4") self.assertEqual(i4.category_id, 2) self.assertIsNotNone(i4.category) self.assertEqual(i4.category.id, 2) # Delete category 1 with cascade with self.s.begin(): categories = api.category_getall(self.s, self.u) api.category_delete(self.s, categories[:1], cascade=True) # Verify only 1 category remains with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "cat2") # Verify items 1 & 2 were deleted through cascade action # and that items 3 & 4 remain unchanged with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 2) i3, i4 = items self.assertEqual(i3.blob, "item3") self.assertEqual(i3.category_id, 2) self.assertIsNotNone(i3.category) self.assertEqual(i3.category.id, 2) self.assertEqual(i4.blob, "item4") self.assertEqual(i4.category_id, 2) self.assertIsNotNone(i4.category) self.assertEqual(i4.category.id, 2) # Delete category 2 without cascade with self.s.begin(): api.category_delete(self.s, categories, cascade=False) # Verify categories list is now empty with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 0) # Verify that items 3 & 4 have no category association with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 2) i3, i4 = items self.assertEqual(i3.blob, "item3") self.assertEqual(i3.category_id, None) self.assertIsNone(i3.category) self.assertEqual(i4.blob, "item4") self.assertEqual(i4.category_id, None) self.assertIsNone(i4.category) # Remove remaining category with self.s.begin(): categories = api.category_getall(self.s, self.u) api.category_delete(self.s, categories, True) # Verify categories list is now empty with self.s.begin(): categories = api.category_getall(self.s, self.u) self.assertEqual(len(categories), 0) # Make sure items were NOT deleted despite cascade=True # because they have no category association with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 2) i3, i4 = items self.assertEqual(i3.blob, "item3") self.assertEqual(i3.category_id, None) self.assertIsNone(i3.category) self.assertEqual(i4.blob, "item4") self.assertEqual(i4.category_id, None) self.assertIsNone(i4.category) # Clean up remaining items with self.s.begin(): api.item_delete(self.s, items) # Verify clean up successful with self.s.begin(): items = api.item_getall(self.s, self.u) self.assertEqual(len(items), 0)
def test_categories_delete_cascade(self): # Insert categories categories = [ models.Category(name="cat1"), models.Category(name="cat2") ] api.category_create(categories, session=self.session) # Verify categories categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 2) self.assertEqual(categories[0].name, "cat1") self.assertEqual(categories[1].name, "cat2") # Insert items items = [ models.Item(blob="item1", category_id=1), models.Item(blob="item2", category_id=1), models.Item(blob="item3", category_id=2), models.Item(blob="item4", category_id=2) ] api.item_create(items, session=self.session) # Verify items items = api.item_getall(session=self.session) self.assertEqual(len(items), 4) i1, i2, i3, i4 = items self.assertEqual(i1.blob, "item1") self.assertEqual(i1.category_id, 1) self.assertIsNotNone(i1.category) self.assertEqual(i1.category.id, 1) self.assertEqual(i2.blob, "item2") self.assertEqual(i2.category_id, 1) self.assertIsNotNone(i2.category) self.assertEqual(i1.category.id, 1) self.assertEqual(i3.blob, "item3") self.assertEqual(i3.category_id, 2) self.assertIsNotNone(i3.category) self.assertEqual(i3.category.id, 2) self.assertEqual(i4.blob, "item4") self.assertEqual(i4.category_id, 2) self.assertIsNotNone(i4.category) self.assertEqual(i4.category.id, 2) # Delete category 1 with cascade api.category_delete(categories[:1], cascade=True, session=self.session) # Verify only 1 category remains categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 1) self.assertEqual(categories[0].name, "cat2") # Verify items 1 & 2 were deleted through cascade action # and that items 3 & 4 remain unchanged items = api.item_getall(session=self.session) self.assertEqual(len(items), 2) i3, i4 = items self.assertEqual(i3.blob, "item3") self.assertEqual(i3.category_id, 2) self.assertIsNotNone(i3.category) self.assertEqual(i3.category.id, 2) self.assertEqual(i4.blob, "item4") self.assertEqual(i4.category_id, 2) self.assertIsNotNone(i4.category) self.assertEqual(i4.category.id, 2) # Delete category 2 without cascade api.category_delete(categories, cascade=False, session=self.session) # Verify categories list is now empty categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 0) # Verify that items 3 & 4 have no category association items = api.item_getall(session=self.session) self.assertEqual(len(items), 2) i3, i4 = items self.assertEqual(i3.blob, "item3") self.assertEqual(i3.category_id, None) self.assertIsNone(i3.category) self.assertEqual(i4.blob, "item4") self.assertEqual(i4.category_id, None) self.assertIsNone(i4.category) # Clean up and verify categories = api.category_getall(session=self.session) api.category_delete(categories, True, session=self.session) categories = api.category_getall(session=self.session) self.assertEqual(len(categories), 0) items = api.item_getall(session=self.session) api.item_delete(items, session=self.session) items = api.item_getall(session=self.session) self.assertEqual(len(items), 0)