コード例 #1
0
    def __init__(self, config_path=None, verbose=1):

        if config_path is None:
            config_path = local.CONFIG_FILE

        # Load local config if not provided
        self.config_path = config_path
        self.config = local.load_config(config_path)
        self.verbose = verbose

        # Get reference to remote client
        if self.config['backend_name'] == 'dropbox':
            from vimbox.remote.dropbox_backend import (install_backend,
                                                       StorageBackEnd)
            # Install if necessary
            if self.config.get('DROPBOX_TOKEN', None) is None:
                install_backend(self.config_path, local.DEFAULT_CONFIG)
            self.client = StorageBackEnd(self.config['DROPBOX_TOKEN'])

        elif self.config['backend_name'] in ['fake', 'fake-offline']:
            from vimbox.remote.fake_backend import (install_backend,
                                                    StorageBackEnd)
            if self.config is None:
                install_backend(self.config_path, local.DEFAULT_CONFIG)
            if self.config['backend_name'] == 'fake-offline':
                online = False
            else:
                online = True
            self.client = StorageBackEnd(online=online)

        else:
            raise Exception("Unknown backend %s" % self.config['backend_name'])
コード例 #2
0
def start_environment(**config_delta):
    """
    Create a folder for tests, store a copy of the config
    """

    # Overload some config fields if solicited
    original_config = local.load_config()
    test_config = {
        'DROPBOX_TOKEN': original_config['DROPBOX_TOKEN'],
        'backend_name': original_config['backend_name'],
        'cache': {},
        'path_hashes': {},
        'remove_local': False
    }
    if config_delta:
        for key, value in config_delta.items():
            test_config[key] = value
    # Save new config
    unit_test_config = "%s/unit_test_%s" % (os.path.dirname(
        local.CONFIG_FILE), os.path.basename(local.CONFIG_FILE))
    local.write_config(unit_test_config, test_config)
    local.CONFIG_FILE = unit_test_config
    print("Using config %s" % unit_test_config)

    # Remote storage
    backend_name = config_delta.get('backend_name', None)
    assert REMOTE_UNIT_TEST_FOLDER[:-1] == '/.vimbox_unit_test', \
        "CHANGING UNIT TEST FOLDER CAN LEAD TO DATA LOSS"
    reset_folder(REMOTE_UNIT_TEST_FOLDER)

    return unit_test_config
コード例 #3
0
def edit_paper_url(url):

    from vimbox.remote.paper_backend import StorageBackEnd, DOC_REGEX
    title, did, doc_id = DOC_REGEX.match(url).groups()
    paper_token = local.load_config()['DROPBOX_TOKEN']
    client = StorageBackEnd(paper_token)
    response = client.file_download(url)
    local_folder = local.get_local_file('.paper/')
    if not os.path.isdir(local_folder):
        os.mkdir(local_folder)
    local_file = "%s/%s--%s-%s.md" % (local_folder, title, did, doc_id)
    content = local.local_edit(local_file, response['content'])
    # Update remote if there are changes
    if content != response['content']:
        response = client.files_upload(content, url, response['revision'])
        if response['status'] == 'api-error':
            print(response['alert'])
    os.remove(local_file)
コード例 #4
0
def reset_environment(sucess=False):
    """
    - Remove folder simulating storage in remote
    - Remove folder in the local vimbox cache (THIS IS THE ACTUAL CACHE)
    - keep original config to restore it later
    """
    # Local storage
    test_config = local.load_config()
    reset_folder(REMOTE_UNIT_TEST_FOLDER, delete=True)
    # Extra check
    assert os.path.basename(local.CONFIG_FILE) == 'unit_test_config.yml', \
        "unit test configurstion has unexpected name, not deleting it"
    os.remove(local.CONFIG_FILE)
    print("Removing config %s" % local.CONFIG_FILE)

    # Inform user
    if sucess:
        print("Test was %s" % green("OK"))
コード例 #5
0
def hash_is_registered(encrypted_file):
    key = (get_path_hash(encrypted_file), encrypted_file)
    return key in load_config()['path_hashes'].items()
コード例 #6
0
def test_main(backend_name):

    # SIMPLE COLLISIONS

    # Collision create plain, create encrypted
    plain_file = '%sfolder1/plain' % REMOTE_UNIT_TEST_FOLDER
    assert main(['-f', plain_file, 'Cosa mariposa'])
    assert main(['ls', plain_file], verbose=0), "Creating file failed"
    # Try to overwrite it with an encrypted one of same name
    assert not main(['-e', plain_file, 'Otra cosa mariposa'],
                    password='******'), "Collision with plain file"
    print("Collision create plain, create encrypted %s" % green("OK"))

    # Collision create encrypted, create plain
    encrypted_file = '%sfolder1/folder2/encrypted' % REMOTE_UNIT_TEST_FOLDER
    assert main(['-e', encrypted_file, 'This is secret'], password='******')
    assert main(['ls', encrypted_file], verbose=0), \
        "Creating encrypted file failed"
    # Try to overwrite it with an encrypted one of same name
    assert not main(['-f', encrypted_file, 'Other secret']), \
        "Collision with encrypted filefile"
    print("Collision create encrypted, create plain %s" % green("OK"))

    # Collision create plain, create folder
    failed = False
    try:
        main(['mkdir', plain_file + '/']), "mkdir collision"
    except AssertionError:
        failed = True
    assert failed, "Collision create plain, create folder did not happen"
    print("Collision create plain, create folder %s" % green("OK"))

    # Collision create encrypted, create folder
    failed = False
    try:
        main(['mkdir', encrypted_file + '/']), "mkdir collision"
    except AssertionError:
        failed = True
    assert failed, "Collision create encrypted, create folder"
    print("Collision create encrypted, create folder %s" % green("OK"))

    # MOVES

    # Move folder with encrypted file
    source_folder = '%sfolder1/folder2/' % REMOTE_UNIT_TEST_FOLDER
    target_folder = '%sfolder3/' % REMOTE_UNIT_TEST_FOLDER
    moved_encrypted_file = \
        '%sfolder3/folder2/encrypted' % REMOTE_UNIT_TEST_FOLDER
    assert main(['mkdir', target_folder])
    assert main(['mv', source_folder, target_folder])
    assert main(['ls', moved_encrypted_file], verbose=0), \
        "Moving folder with encrypted file failed"
    assert not hash_is_registered(source_folder), \
        "Unregister file hash when removing folder failed"
    assert hash_is_registered(moved_encrypted_file), \
        "Register file hash when removing folder failed"
    assert target_folder in load_config()['cache'], \
        "Adding to cache failed"
    print("Move encrypted %s" % green("OK"))

    # Create an encrypted file and root and try to overwrite it as plain
    encrypted_file2 = '%sencrypted2' % REMOTE_UNIT_TEST_FOLDER
    assert main(['-e', encrypted_file2, 'Some encrypted data'],
                password='******')
    assert main(['ls', encrypted_file2], verbose=0)
    assert not main(['-f', encrypted_file2, 'Here is some more content'])
    print("encrypted file plain text collision %s" % green("OK"))

    # Fail at moving a file overwriting an encrypted file
    assert not main(['mv', encrypted_file2, moved_encrypted_file]), \
        "move collision"
    print("Encrypted file moved file collision %s" % green("OK"))
コード例 #7
0
def test_main(backend_name):
    '''
    Test StorageBackEnd primitives
    '''

    assert backend_name == 'dropbox', "Must use dropbox back-end"

    assert REMOTE_UNIT_TEST_FOLDER == '/.vimbox_unit_test/', \
        "CHANGING INTEGRATION TEST FOLDER CAN LEAD TO DATA LOSS"
    assert REMOTE_UNIT_TEST_FOLDER[-1] == '/', "Folder paths end in /"

    TMP_FILE = '%splain' % REMOTE_UNIT_TEST_FOLDER
    TMP_CONTENT = 'This is some text'
    TMP_FILE2 = '%splain2' % REMOTE_UNIT_TEST_FOLDER
    TMP_FOLDER = '%stest_folder/' % REMOTE_UNIT_TEST_FOLDER
    TMP_FOLDER2 = '%stest_folder2/' % REMOTE_UNIT_TEST_FOLDER

    client = StorageBackEnd(load_config()['DROPBOX_TOKEN'])

    client.get_user_account()
    # Create
    if sys.version_info[0] > 2:
        # Encoding for Python3
        TMP_CONTENT = str.encode(TMP_CONTENT)
    client.files_upload(TMP_CONTENT, TMP_FILE)
    print("File upload %s" % green("OK"))

    # Check existance file
    remote = client.file_type(TMP_FILE)
    assert remote['status'] == 'online', "Status not online on file_type"
    assert remote['content'] == 'file', "Dropbox file creation failed"
    print("File existance %s" % green("OK"))

    # Check content
    remote = client.file_download(TMP_FILE)
    assert remote['status'] == 'online', "Status not online on file_download"
    if sys.version_info[0] > 2:
        # Encoding for Python3
        remote['content'] = str.encode(remote['content'])
    assert TMP_CONTENT == remote['content'], \
        "Retrieval of remote content failed"
    print("Content check %s" % green("OK"))

    # List folders does not die
    remote = client.list_folders(REMOTE_UNIT_TEST_FOLDER[:-1])
    assert remote['status'] == 'online', "Status not online on list_folders"
    print("Listing files %s" % green("OK"))

    # Move
    client.files_copy(TMP_FILE, TMP_FILE2)
    client.files_delete(TMP_FILE)
    # Check non existance file
    remote = client.file_type(TMP_FILE)
    assert remote['status'] == 'online' and remote['content'] is None, \
        "File removal failed"
    print("Move files %s" % green("OK"))

    # Make folder
    client.make_directory(TMP_FOLDER[:-1])
    remote = client.file_type(TMP_FOLDER[:-1])
    assert remote['status'] == 'online' and remote['content'] == 'dir', \
        "Folder creation failed"
    print("Make folder %s" % green("OK"))

    # Move folder
    client.files_copy(TMP_FOLDER[:-1], TMP_FOLDER2[:-1])
    remote = client.file_type(TMP_FOLDER2[:-1])
    assert remote['status'] == 'online' and remote['content'] == 'dir', \
        "Folder creation failed"
    client.files_delete(TMP_FOLDER[:-1])
    remote = client.file_type(TMP_FOLDER[:-1])
    assert remote['status'] == 'online' and remote['content'] is None, \
        "Folder removal failed"
    print("Move folder %s" % green("OK"))

    # Clean-up
    client.files_delete(REMOTE_UNIT_TEST_FOLDER[:-1])
    remote = client.file_type(REMOTE_UNIT_TEST_FOLDER[:-1])
    assert remote['status'] == 'online' and remote['content'] is None, \
        "Folder removal failed"
コード例 #8
0
import requests
import sys
import re
import json
from vimbox.local import load_config


if __name__ == '__main__':

    doc_url = sys.argv[1]

    from vimbox.remote.paper_backend import StorageBackEnd
    client = StorageBackEnd(load_config()['paper_token'])
    reponse = client.file_download(doc_url)
    reponse['content'] += '# New Section\n[ ] New item\n[ ] Some other item'
    import ipdb; ipdb.set_trace(context=30)
    client.files_upload(reponse['content'], doc_url)
コード例 #9
0
def test_main(backend_name):

    # NOTE: start_environment() has overloaded local.CONFIG_FILE

    encrypted_file = '%sencrypted' % REMOTE_UNIT_TEST_FOLDER
    encrypted_content = 'This is some text'

    # EDIT
    # Basic file creation local, cache, remote and has list
    assert main(['-e', encrypted_file, encrypted_content], password='******')
    assert is_local_file(encrypted_file), "Local file not created"
    assert "%s/" % os.path.dirname(encrypted_file) in load_config()['cache'], \
        "Register in cache failed"
    print("Basic file creation local, cache and remote %s" % green("OK"))
    assert hash_is_registered(encrypted_file), "Register in hash list failed"
    if backend_name == 'fake':
        assert encrypted_content == \
            read_remote_content(encrypted_file, password='******'), \
            "Local and remote content do not match"
        assert is_fake_remote_file(encrypted_file, password='******'), \
            "Remote file not created"

    # MOVE
    # Move files
    folder_at_root = '%sfolder1/' % REMOTE_UNIT_TEST_FOLDER
    encrypted_file2 = '%sencrypted2' % folder_at_root
    assert main(['mkdir', folder_at_root]), "Could not create folder"
    assert main(['mv', encrypted_file, encrypted_file2])
    assert not is_local_file(encrypted_file), "Removal of local file failed"
    assert is_local_file(encrypted_file2), "creation of local file failed"
    assert hash_is_registered(encrypted_file2), \
        "Register moved target file in hash list failed"
    print("Move file %s" % green("OK"))
    if backend_name == 'fake':
        assert not is_fake_remote_file(encrypted_file, password='******'), \
            "Removal of encrypted file in remote failed"
        assert is_fake_remote_file(encrypted_file2, password='******'), \
            "creation of encrypted file in remote failed"

    # Rename folder
    folder2 = '%sfolder2/' % REMOTE_UNIT_TEST_FOLDER
    moved_encrypted_file2 = "%s%s" % \
        (folder2, os.path.basename(encrypted_file2))
    assert main(['mv', folder_at_root, folder2])
    assert is_local_file(moved_encrypted_file2), \
        "File inside folder was not moved in local"
    assert not is_local_file(encrypted_file2), \
        "File inside folder was not removed in local"
    assert hash_is_registered(moved_encrypted_file2), \
        "Register moved target file in hash list failed"
    if backend_name == 'fake':
        assert not is_fake_remote_file(encrypted_file2, password='******'), \
            "File inside folder was not removed in remote"
        assert is_fake_remote_file(moved_encrypted_file2, password='******'), \
            "File inside folder was not moved in remote"

    # REMOVE
    # remove folder (cache, local, remote)
    assert main(['rm', '-R', folder2])
    assert not is_local_dir(folder2), "Removal of local folder failed"
    assert folder2 not in load_config()['cache'], \
        "Removal from cache failed"
    assert not hash_is_registered(moved_encrypted_file2), \
        "Unregister file when removing folder failed"
    if backend_name == 'fake':
        assert not is_fake_remote_dir(folder2), \
            "Removal of remote folder failed"
    print("Remove folder %s" % green("OK"))