def create_mock_dbclient_with_master_and_password_manager_collection( master_password, master_key, domain, username): """Creates a mock DbClient with a mock master collection and mock password manager collection. The master collection is returned as a result of first find()/find_one(). The password manager collection is returned as a result of second find()/find_one(). Args: master_password (str): Master password whose hash is stored in the master collection mock. master_key (str): Master key whose hash is stored in the master collection mock. Returns: Mock: Mock DbClient object. """ master_collection = create_mock_cursor(cursor_values=[{ MASTER_PASSWORD_COLLECTION_MASTER_PASSWORD_FIELD: generate_hash(master_password), MASTER_PASSWORD_COLLECTION_MASTER_KEY_FIELD: generate_hash(master_key) }]) password_manager_collection = None if domain and username: password_manager_collection = create_mock_cursor( cursor_values=[{ PASSWORD_MANAGER_COLLECTION_DOMAIN_FIELD: domain, PASSWORD_MANAGER_COLLECTION_USERNAME_FIELD: username }]) else: password_manager_collection = create_mock_cursor() return create_mock_dbclient(collection=create_mock_collection( find_return_value1=master_collection, find_return_value2=password_manager_collection))
def __assertMasterPasswordAndKeyValid(self): """Asserts that the master password and key matches the one present in database. Master password and key is stored as SHA2 in a single row in the master collection. """ cursor = self.master_password_collection.find() assert cursor.count() == 1, ERROR_MULTIPLE_MASTER_ROWS document = cursor.next() assert document[ MASTER_PASSWORD_COLLECTION_MASTER_PASSWORD_FIELD] == generate_hash( self.request.form.get( BODY_MASTER_PASSWORD_PARAM)), ERROR_MASTER_PASSWORD assert document[ MASTER_PASSWORD_COLLECTION_MASTER_KEY_FIELD] == generate_hash( self.request.form.get(BODY_MASTER_KEY_PARAM)), ERROR_MASTER_KEY
def create_mock_dbclient_with_master_collection(master_password, master_key): """Creates a mock DbClient with a mock master collection that is returned as a return of get_collection. Args: master_password (str): Master password whose hash is stored in the master collection mock. master_key (str): Master key whose hash is stored in the master collection mock. Returns: Mock: Mock DbClient object. """ master_collection = create_mock_cursor(cursor_values=[{ MASTER_PASSWORD_COLLECTION_MASTER_PASSWORD_FIELD: generate_hash(master_password), MASTER_PASSWORD_COLLECTION_MASTER_KEY_FIELD: generate_hash(master_key) }]) return create_mock_dbclient(collection=create_mock_collection( find_return_value1=master_collection))
def create_mock_dbclient_with_master_and_multiple_password_manager_collection( master_password, master_key, domain1, username1, domain2, username2): """Creates a mock DbClient with a mock master collection and two mock password manager collection. The master collection is returned as a result of first find()/find_one(). The 1st password manager collection is returned as a result of second find()/find_one(). The 2nd password manager collection is returned as a result of third find()/find_one(). Args: master_password (str): Master password whose hash is stored in the master collection mock. master_key (str): Master key whose hash is stored in the master collection mock. domain1 (str): Domain for a record returned by 1st password manager collection mock. username1 (str): Username for the same record returned by 1st password manager collection mock. domain2 (str): Domain for a record returned by 2nd password manager collection mock. username2 (str): Username for the same record returned by 2nd password manager collection mock. Returns: Mock: Mock DbClient object. """ master_collection = create_mock_cursor(cursor_values=[{ MASTER_PASSWORD_COLLECTION_MASTER_PASSWORD_FIELD: generate_hash(master_password), MASTER_PASSWORD_COLLECTION_MASTER_KEY_FIELD: generate_hash(master_key) }]) password_manager_collection1 = None if domain1 and username1: password_manager_collection1 = create_mock_cursor(cursor_values=[{ PASSWORD_MANAGER_COLLECTION_DOMAIN_FIELD: domain1, PASSWORD_MANAGER_COLLECTION_USERNAME_FIELD: username1 }]) else: password_manager_collection1 = create_mock_cursor() password_manager_collection2 = None if domain2 and username2: password_manager_collection2 = create_mock_cursor(cursor_values=[{ PASSWORD_MANAGER_COLLECTION_DOMAIN_FIELD: domain2, PASSWORD_MANAGER_COLLECTION_USERNAME_FIELD: username2 }]) else: password_manager_collection2 = create_mock_cursor() return create_mock_dbclient(collection=create_mock_collection( find_return_value1=master_collection, find_return_value2=password_manager_collection1, find_return_value3=password_manager_collection2))
def authenticate(self, key): """ Authenticates a key. Args: key (str): The authentication key whose hash is present in the auth collection. Returns: bool: True of the hash of the auth key was found, False otherwise. """ cursor = self.auth_collection.find({ AUTH_COLLECTION_KEY_FIELD: generate_hash(key) }) return cursor != None and cursor.count() != 0
def create_mock_dbclient_with_auth_collection(key): """Creates a mock DbClient with a mock auth collection. The auth collection is returned as a result of find()/find_one(). Args: key (str): The key which is hashed as stored in auth collection. Returns: Mock: Mock DbClient object. """ auth_collection = None if key != None: auth_collection = create_mock_cursor( cursor_values=[{ AUTH_COLLECTION_KEY_FIELD: generate_hash(key) }]) else: auth_collection = create_mock_cursor() return create_mock_dbclient(collection=create_mock_collection( find_return_value1=auth_collection))