Ejemplo n.º 1
0
 def __init__(self):
     """
     Setup a provider manager with a new default CredentialManager
     """
     self.credential_manager = CredentialManager()
     self.credential_manager.load()
     self.tmp_oauth_providers = {
     }  # Stores data for in-flight OAuth transactions
Ejemplo n.º 2
0
def test_app_cred_missing():
    cm = CredentialManager()
    # To test this, we don't want to remove the example app credential file
    # So we change the file location to an invalid one
    cm.app_creds_file = "foobarfile"

    with pytest.raises(ValueError):
        cm.load()
Ejemplo n.º 3
0
def test_user_cred_mising():
    cm = CredentialManager()
    if os.path.exists(cm.user_creds_file):
        os.remove(cm.user_creds_file)

    cm.load()

    assert cm.user_creds == {}
Ejemplo n.º 4
0
def test_roundtrip_providers():
    cm = CredentialManager()
    cm.load()

    def make_local(cm, path):
        provider = LocalFilesystemProvider(cm)
        provider.connect(path)
        return provider

    providers = [make_local(cm, "tmp/" + str(i)) for i in xrange(5)]

    manifest = Manifest()
    manifest.set_providers(providers)

    provider_strings = map(lambda provider: provider.uuid, providers)

    assert Manifest.deserialize(
        manifest.serialize()).get_provider_strings() == provider_strings
Ejemplo n.º 5
0
def main(args):
    provider_manager = ProviderManager()
    cm = CredentialManager()
    cm.load()

    providers, errors = provider_manager.load_all_providers_from_credentials()

    def wipe(provider):
        try:
            provider.wipe()
        except Exception:
            print "failed to wipe provider: " + provider.provider_name()

    run_parallel(wipe, map(lambda provider: [provider], providers))

    behavior = args[0]
    cm.clear_user_credentials(provider_class=None,
                              account_identifier=None)  # clear the file

    if behavior == "setup":
        # copy in default credentials (Dropbox, GoogleDrive, OneDrive)
        default_credentials = os.path.join(cm.config_dir,
                                           "default_credentials.json")
        current_credentials = os.path.join(cm.config_dir,
                                           "user_credentials.json")
        try:
            copyfile(default_credentials, current_credentials)
        except IOError:
            print "credential reset to setup state failed, see default_credentials.json"

        if len(args) == 2:  # write credentials for the demo server
            cm.load()  # reload the credential manager with updated store

            ip_block = args[1]
            url = ''.join("http://158.130.108." + ip_block + ":5000")
            demo_server = TestServerProvider(cm)

            try:
                demo_server.connect(url)
            except exceptions.ConnectionFailure:
                print "please start a demo server at: " + url
Ejemplo n.º 6
0
class ProviderManager():
    """
    A manager for constructing providers and performing provider-related operations
    """
    # the classes of all available providers
    PROVIDER_CLASSES = [
        DropboxProvider, GoogleDriveProvider, BoxProvider, OneDriveProvider,
        LocalFilesystemProvider, TestServerProvider
    ]

    def __init__(self):
        """
        Setup a provider manager with a new default CredentialManager
        """
        self.credential_manager = CredentialManager()
        self.credential_manager.load()
        self.tmp_oauth_providers = {
        }  # Stores data for in-flight OAuth transactions

    def get_provider_classes(self):
        """
        Returns all available provider classes in a list.
        """
        return self.PROVIDER_CLASSES[:]

    def get_provider_classes_by_kind(self):
        """
        Get all available provider classes
        Returns a tuple (oauth_providers, unauth_providers)
            oauth_providers: a map from provider_identifier to provider class that follows the oauth flow
            unauth_providers: a map from provider_identifier to provider class that follows the unauth flow
        """
        provider_classes = self.get_provider_classes()
        oauth_providers = {
            cls.provider_identifier(): cls
            for cls in provider_classes if issubclass(cls, OAuthProvider)
        }
        unauth_providers = {
            cls.provider_identifier(): cls
            for cls in provider_classes
            if issubclass(cls, UnauthenticatedProvider)
        }
        return oauth_providers, unauth_providers

    def load_all_providers_from_credentials(self):
        """
        Get all providers of exposed types that can be loaded from the underlying credential_manager
        Returns (cached_providers, failed_ids)
            cached_providers: a list of loaded provider objects
            failed_ids: the uuids of providers that failed to load
        """
        def flatten(list_of_lists):
            return [item for sublist in list_of_lists for item in sublist]

        providers_and_errors = []

        def load_cached_by_class(provider_class):
            providers_and_errors.append(
                provider_class.load_cached_providers(self.credential_manager))

        provider_classes = self.get_provider_classes()
        run_parallel(
            load_cached_by_class,
            map(lambda provider_class: [provider_class], provider_classes))

        return tuple(map(flatten, zip(*providers_and_errors)))

    def start_oauth_connection(self, provider_class):
        """
        Start a connection for the OAuthProvider of the specified class
        Args: provider_class: the class of the provider to be created
        Returns the login url for the provider
        Calling this additional times invalidates any previous unfinished flows for this class
        """
        provider = provider_class(self.credential_manager)
        self.tmp_oauth_providers[provider_class] = provider
        return provider.start_connection()

    def finish_oauth_connection(self, provider_class, localhost_url):
        """
        Finish the connection for the OAuthProvider of the specified class
        Args:
            provider_class: the class of the provider to be created
            localhost_url, the url resulting from redirect after start_oauth_connection(provider_class)
        Returns: a functional provider
        Raises ProviderOperationFailure
        """
        provider = self.tmp_oauth_providers.get(provider_class)
        if provider is None:
            raise ValueError(
                "Call start_oauth_connection with this class first!")
        self.tmp_oauth_providers[provider_class] = None

        provider.finish_connection(localhost_url)
        return provider

    def make_unauth_provider(self, provider_class, provider_id):
        """
        Args:
            provider_class: the class of the provider to be created
            provider_id: the identifier of the provider to be created
        Make an Unauthenticated provider with the specified class and provider_id
        Raises ProviderOperationFailure
        """
        provider = provider_class(self.credential_manager)
        provider.connect(provider_id)
        return provider
Ejemplo n.º 7
0
from providers.LocalFilesystemProvider import LocalFilesystemProvider
from managers.CredentialManager import CredentialManager
from custom_exceptions import exceptions
import pytest
import os

cm = CredentialManager()
cm.load()


def teardown_function(function):
    cm.clear_user_credentials()


def test_wipe():
    FS = LocalFilesystemProvider(cm)
    FS.connect("tmp")
    FS.put("file1", "abc")
    FS.put("file2", "def")
    FS.wipe()

    assert len(os.listdir("tmp/" + FS.ROOT_DIR)) == 0


def test_roundtrip():
    FS = LocalFilesystemProvider(cm)
    FS.connect("tmp")
    FS.put("file1", "abc")
    assert FS.get("file1") == "abc"

Ejemplo n.º 8
0
def test_get_app_credentials():
    cm = CredentialManager()
    # Assume that the packaging includes an app credential file with at least "Dropbox"
    cm.load()
    assert cm.get_app_credentials(DropboxProvider) is not None
Ejemplo n.º 9
0
def test_clear_entire_provider():
    cm = CredentialManager()
    cm.load()
    cm.set_user_credentials(DropboxProvider, "foo", 0)
    cm.set_user_credentials(DropboxProvider, "bar", 1)
    cm.clear_user_credentials(DropboxProvider)

    cm.load()
    assert cm.get_user_credentials(DropboxProvider) == {}
Ejemplo n.º 10
0
def test_roundtrip_user():
    cm = CredentialManager()
    cm.load()
    foo_creds = {"a": 1}
    bar_creds = "a_string"
    cm.set_user_credentials(DropboxProvider, "foo", foo_creds)
    cm.set_user_credentials(DropboxProvider, "bar", bar_creds)

    cm.load()
    assert cm.get_user_credentials(DropboxProvider) == {"foo": foo_creds, "bar": bar_creds}

    cm.clear_user_credentials(DropboxProvider, "foo")
    cm.load()
    assert cm.get_user_credentials(DropboxProvider) == {"bar": bar_creds}

    cm.clear_user_credentials(DropboxProvider, "bar")

    cm.load()
    assert cm.get_user_credentials(DropboxProvider) == {}
Ejemplo n.º 11
0
def test_get_not_exists():
    cm = CredentialManager()
    cm.load()
    cm.clear_user_credentials(DropboxProvider)
    assert cm.get_app_credentials(TestProvider) is None
    assert cm.get_user_credentials(DropboxProvider) == {}
Ejemplo n.º 12
0
from managers.ProviderManager import ProviderManager
from managers.CredentialManager import CredentialManager
from providers.LocalFilesystemProvider import LocalFilesystemProvider

cm = CredentialManager()


def setup_function(function):
    cm.clear_user_credentials()
    cm.load()


def teardown_function(function):
    cm.clear_user_credentials()


def test_load_providers():
    pm = ProviderManager()
    # put a new provider in the credentials
    fs = pm.make_unauth_provider(LocalFilesystemProvider, "tmp/1")
    cached_providers, errors = pm.load_all_providers_from_credentials()

    assert map(lambda provider: provider.uuid, cached_providers) == [fs.uuid]