Beispiel #1
0
def test_get_fake(OSFCore_get):
    osf = OSF()
    with pytest.raises(OSFException) as exc:
        osf.project('f3szh')

    assert exc.value.args[
        0] == 'f3szh is unrecognized type fakes. Clone supports projects and registrations'
    OSFCore_get.assert_called_once_with('https://api.osf.io/v2//guids/f3szh/')
Beispiel #2
0
def test_failed_get_project(OSFCore_get):
    osf = OSF()
    with pytest.raises(RuntimeError):
        osf.project('f3szh')

    OSFCore_get.assert_called_once_with(
        'https://api.osf.io/v2//nodes/f3szh/'
        )
Beispiel #3
0
def test_get_registration(OSFCore_get):
    osf = OSF()
    project = osf.project('f3szh')

    calls = [call('https://api.osf.io/v2//guids/f3szh/'), call('https://api.osf.io/v2//registrations/f3szh/')]
    OSFCore_get.assert_has_calls(calls)
    assert isinstance(project, Project)
Beispiel #4
0
def test_get_project_with_endpoint(OSFCore_get):
    osf = OSF(base_url='https://api.test.osf.io/v2/')
    project = osf.project('f3szh')

    calls = [call('https://api.test.osf.io/v2//guids/f3szh/'), call('https://api.test.osf.io/v2//nodes/f3szh/')]
    OSFCore_get.assert_has_calls(calls)
    assert isinstance(project, Project)
Beispiel #5
0
class OSFClient:

    def __init__(self, username, password, project):
        self.osf = OSF(username=username, password=password)
        self.project = self.osf.project(project)

    def uploadFile(self, folder, path):
        try:
            storage = self.project.storage()
            storage.create_folder(folder, exist_ok=True)

            with open(path, 'rb') as f:
                storage.create_file(folder + '/' + os.path.basename(path), f)
        except Exception as ex:
            logging.error(ex)

    def uploadWorker(self, buf):
        while True:
            item = buf.get()
            folder = item.get('query')
            path = item.get('path')
            logging.info("Uploading from {0} to {1}...".format(path, folder))
            self.uploadFile(folder, path)
            buf.task_done()
            logging.info('Done.')
Beispiel #6
0
def test_get_project(OSFCore_get):
    osf = OSF()
    project = osf.project('f3szh')

    OSFCore_get.assert_called_once_with(
        'https://api.osf.io/v2//nodes/f3szh/'
        )
    assert isinstance(project, Project)
Beispiel #7
0
def test_get_project(OSFCore_get):
    osf = OSF()
    project = osf.project("f3szh")

    calls = [
        call("https://api.osf.io/v2/guids/f3szh/"),
        call("https://api.osf.io/v2/nodes/f3szh/"),
    ]
    OSFCore_get.assert_has_calls(calls)
    assert isinstance(project, Project)
Beispiel #8
0
def test_delete_project(OSFCore_delete, OSFCore_get):
    osf = OSF()

    project = osf.project("f3szh")

    calls = [call("https://api.osf.io/v2/nodes/f3szh/")]
    OSFCore_get.assert_has_calls(calls)

    project.delete()
    calls = [call("https://api.osf.io/v2/nodes/f3szh/")]
    OSFCore_delete.assert_has_calls(calls)
Beispiel #9
0
def test_project_metadata_only_mutable(OSFCore_get):
    osf = OSF()
    project = osf.project("f3szh")

    md = project.metadata(only_mutable=True)
    data = project_node["data"]["attributes"]

    assert data["title"] == md["title"]
    assert data["description"] == md["description"]
    assert data["category"] == md["category"]
    assert data["tags"] == md["tags"]
    assert data["public"] == md["public"]
Beispiel #10
0
def test_project_metadata(OSFCore_get):
    osf = OSF()
    project = osf.project("f3szh")

    md = project.metadata()
    data = project_node["data"]["attributes"]

    assert data["title"] == md["title"]
    assert data["date_created"] == md["date_created"]
    assert data["date_modified"] == md["date_modified"]
    assert data["description"] == md["description"]
    assert data["category"] == md["category"]
    assert data["tags"] == md["tags"]
    assert data["public"] == md["public"]
    def prepare(self):
        """"""
        node_id = self.annex.getconfig('node')
        if not node_id:
            # fall back on outdated 'project' parameter, which could be
            # just the node ID or a full URL to a project
            node_id = posixpath.basename(
                urlparse(self.annex.getconfig('project')).path.strip(
                    posixpath.sep))

        if not node_id:
            raise RemoteError('Could not determine OSF node ID')

        try:
            # make use of DataLad's credential manager for a more convenient
            # out-of-the-box behavior
            from datalad_osf.utils import get_credentials
            # we must stay non-interactive, because this is running inside
            # git-annex's special remote protocal
            creds = get_credentials(allow_interactive=False)
        except ImportError as e:
            # whenever anything goes wrong here, stay clam and fall back
            # on envvars.
            # we want this special remote to be fully functional without
            # datalad
            creds = dict(
                username=os.environ.get('OSF_USERNAME', None),
                password=os.environ.get('OSF_PASSWORD', None),
                token=os.environ.get('OSF_TOKEN', None),
            )
        # next one just sets up the stage, no requests performed yet, hence
        # no error checking needed
        # supply both auth credentials, so osfclient can fall back on user/pass
        # if needed
        osf = OSF(**creds)
        # next one performs initial auth
        try:
            self.node = osf.project(node_id)
        except Exception as e:
            # we need to raise RemoteError() such that PREPARE-FAILURE
            # is reported, sadly that doesn't give users any clue
            # TODO support datalad logging here
            raise RemoteError('Failed to obtain OSF node handle: {}'.format(e))
        # which storage to use, defaults to 'osfstorage'
        # TODO a node could have more than one? Make parameter to select?
        self.storage = self.node.storage()
Beispiel #12
0
def test_project_metadata_jsonld(OSFCore_get):
    osf = OSF()
    project = osf.project("f3szh")

    md = project.metadata(jsonld=True)
    data = project_node["data"]["attributes"]

    assert data["title"] == md[osf_to_jsonld["title"]]
    assert data["description"] == md[osf_to_jsonld["description"]]
    assert data["category"] == md[osf_to_jsonld["category"]]
    assert data["tags"] == md[osf_to_jsonld["tags"]]
    assert data["public"] == md[osf_to_jsonld["public"]]
    assert data["date_created"] == md[osf_to_jsonld["date_created"]]
    assert data["date_modified"] == md[osf_to_jsonld["date_modified"]]

    with pytest.raises(KeyError):
        md["title"]
        md["description"]
Beispiel #13
0
def test_update_project(OSFCore_put, OSFCore_get):
    osf = OSF()

    attr = project_node["data"]["attributes"]

    attr["title"] = "Long long title"
    project = osf.project("f3szh")

    calls = [call("https://api.osf.io/v2/nodes/f3szh/")]
    OSFCore_get.assert_has_calls(calls)

    project.title = attr["title"]
    project.update()

    calls = [
        call(
            "https://api.osf.io/v2/nodes/f3szh/",
            data=
            '{"data": {"type": "nodes", "id": "f3szh", "attributes": {"category": "project", "description": "this is a test for preprint citations", "title": "Long long title", "public": true, "tags": ["qatest"]}}}',
        )
    ]
    OSFCore_put.assert_has_calls(calls)
    assert isinstance(project, Project)
Beispiel #14
0
def test_failed_get_project(OSFCore_get):
    osf = OSF()
    with pytest.raises(RuntimeError):
        osf.project("f3szh")

    OSFCore_get.assert_called_once_with("https://api.osf.io/v2/guids/f3szh/")