Beispiel #1
0
    def setUp(self):
        super(TestSetPreprintFile, self).setUp()

        self.user = AuthUserFactory()
        self.auth = Auth(user=self.user)
        self.read_write_user = AuthUserFactory()
        self.read_write_user_auth = Auth(user=self.read_write_user)

        self.project = ProjectFactory(creator=self.user)
        self.file = OsfStorageFile.create(node=self.project,
                                          path='/panda.txt',
                                          name='panda.txt',
                                          materialized_path='/panda.txt')
        self.file.save()

        self.file_two = OsfStorageFile.create(
            node=self.project,
            path='/pandapanda.txt',
            name='pandapanda.txt',
            materialized_path='/pandapanda.txt')
        self.file_two.save()

        self.project.add_contributor(self.read_write_user,
                                     permissions=[permissions.WRITE])
        self.project.save()

        self.preprint = PreprintFactory(project=self.project, finish=False)
Beispiel #2
0
 def test_get_file_guids_trashed_file_wo_guid(self):
     node = self.node_settings.owner
     file = OsfStorageFile(name='foo', node=node)
     file.save()
     file.delete()
     assert [] == OsfStorageFileNode.get_file_guids(
         '/' + file._id, provider='osfstorage', node=node)
Beispiel #3
0
 def test_get_file_guids_live_file_wo_guid(self):
     node = self.node_settings.owner
     file = OsfStorageFile(name='foo', target=node)
     file.save()
     assert [] == OsfStorageFileNode.get_file_guids('/' + file._id,
                                                    provider='osfstorage',
                                                    target=node)
Beispiel #4
0
    def setUp(self):
        super(TestSetPreprintFile, self).setUp()

        self.user = AuthUserFactory()
        self.auth = Auth(user=self.user)
        self.read_write_user = AuthUserFactory()
        self.read_write_user_auth = Auth(user=self.read_write_user)

        self.project = ProjectFactory(creator=self.user)
        self.file = OsfStorageFile.create(
            node=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        self.file.save()

        self.file_two = OsfStorageFile.create(
            node=self.project,
            path='/pandapanda.txt',
            name='pandapanda.txt',
            materialized_path='/pandapanda.txt')
        self.file_two.save()

        self.project.add_contributor(self.read_write_user, permissions=[permissions.WRITE])
        self.project.save()

        self.preprint = PreprintFactory(project=self.project, finish=False)
Beispiel #5
0
    def test_get_file_guids_for_trashed_file(self):
        node = self.node_settings.owner
        file = OsfStorageFile(name='foo', target=node)
        file.save()

        file.get_guid(create=True)
        guid = file.get_guid()._id

        file.delete()
        assert guid is not None
        assert guid in OsfStorageFileNode.get_file_guids(
            '/' + file._id, provider='osfstorage', target=node)
Beispiel #6
0
    def test_not_just_primary_file_returned(self):
        filename = 'my second file'
        second_file = OsfStorageFile.create(
            target_object_id=self.preprint.id,
            target_content_type=ContentType.objects.get_for_model(
                self.preprint),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        second_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        second_file.create_version(
            self.user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': 1337,
                'contentType': 'img/png'
            }).save()
        second_file.parent = self.preprint.root_folder
        second_file.save()

        assert len(self.preprint.files.all()) == 2
        res = self.app.get(self.url, auth=self.user.auth)
        assert res.status_code == 200

        data = res.json['data']
        assert len(data) == 2
        assert data[0]['id'] == self.preprint.primary_file._id
    def test_not_just_primary_file_returned(self):
        filename = 'my second file'
        second_file = OsfStorageFile.create(
            target_object_id=self.preprint.id,
            target_content_type=ContentType.objects.get_for_model(self.preprint),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        second_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        second_file.create_version(self.user, {
            'object': '06d80e',
            'service': 'cloud',
            osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
        }, {
            'size': 1337,
            'contentType': 'img/png'
        }).save()
        second_file.parent = self.preprint.root_folder
        second_file.save()

        assert len(self.preprint.files.all()) == 2
        res = self.app.get(self.url, auth=self.user.auth)
        assert res.status_code == 200

        data = res.json['data']
        assert len(data) == 2
        assert data[0]['id'] == self.preprint.primary_file._id
Beispiel #8
0
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch(
            'website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        set_doi = kwargs.pop('set_doi', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)
        file_size = kwargs.pop('file_size', 1337)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.article_doi = doi

        user = kwargs.pop('creator', None) or instance.creator
        instance.save()

        preprint_file = OsfStorageFile.create(
            target_object_id=instance.id,
            target_content_type=ContentType.objects.get_for_model(instance),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        instance.machine_state = kwargs.pop('machine_state', 'initial')
        preprint_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(
            user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': file_size,
                'contentType': 'img/png'
            }).save()
        update_task_patcher.stop()
        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)
            instance.set_published(is_published, auth=auth)
            create_task_patcher = mock.patch(
                'website.identifiers.utils.request_identifiers')
            mock_create_identifier = create_task_patcher.start()
            if is_published and set_doi:
                mock_create_identifier.side_effect = sync_set_identifiers(
                    instance)
            create_task_patcher.stop()

        instance.save()
        return instance
Beispiel #9
0
 def test_file_download_url_no_guid(self):
     file_ = self.root.append_file('Timber.mp3')
     path = OsfStorageFile.find_one( Q('node', 'eq', file_.node_id)).path
     deep_url = '/' + file_.node._id + '/files/osfstorage' + path + '/'
     find = query_file('Timber.mp3')['results']
     assert_not_equal(file_.path, '')
     assert_equal(file_.path, path)
     assert_equal(find[0]['guid_url'], None)
     assert_equal(find[0]['deep_url'], deep_url)
 def test_file_download_url_no_guid(self):
     file_ = self.root.append_file('Timber.mp3')
     path = OsfStorageFile.find_one( Q('node', 'eq', file_.node_id)).wrapped().path
     deep_url = '/' + file_.node._id + '/files/osfstorage' + path + '/'
     find = query_file('Timber.mp3')['results']
     assert_not_equal(file_.path, '')
     assert_equal(file_.path, path)
     assert_equal(find[0]['guid_url'], None)
     assert_equal(find[0]['deep_url'], deep_url)
Beispiel #11
0
def addon_view_or_download_quickfile(**kwargs):
    fid = kwargs.get('fid', 'NOT_AN_FID')
    file_ = OsfStorageFile.load(fid)
    if not file_:
        raise HTTPError(httplib.NOT_FOUND, data={
            'message_short': 'File Not Found',
            'message_long': 'The requested file could not be found.'
        })
    return proxy_url('/project/{}/files/osfstorage/{}/'.format(file_.target._id, fid))
Beispiel #12
0
    def test_redirects(self, app, project, user):
        # test_redirect_to_node_view
        url = '/{}guids/{}/'.format(API_BASE, project._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/'.format(API_DOMAIN, API_BASE,
                                              project._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_registration_view
        registration = RegistrationFactory()
        url = '/{}guids/{}/'.format(API_BASE, registration._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}registrations/{}/'.format(API_DOMAIN, API_BASE,
                                                      registration._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_collections_view
        collection = CollectionFactory()
        url = '/{}guids/{}/'.format(API_BASE, collection._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}collections/{}/'.format(API_DOMAIN, API_BASE,
                                                    collection._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_file_view
        test_file = OsfStorageFile.create(
            node=ProjectFactory(),
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/'.format(API_BASE, guid._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/'.format(API_DOMAIN, API_BASE,
                                              test_file._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_comment_view
        comment = CommentFactory()
        url = '/{}guids/{}/'.format(API_BASE, comment._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}comments/{}/'.format(API_DOMAIN, API_BASE,
                                                 comment._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_throws_404_for_invalid_guids
        url = '/{}guids/{}/'.format(API_BASE, 'fakeguid')
        res = app.get(url, auth=user.auth, expect_errors=True)
        assert res.status_code == 404
Beispiel #13
0
    def test_get_file_guids_for_trashed_file(self):
        node = self.node_settings.owner
        file = OsfStorageFile(name='foo', node=node)
        file.save()

        file.get_guid(create=True)
        guid = file.get_guid()._id

        file.delete()
        assert guid is not None
        assert guid in OsfStorageFileNode.get_file_guids(
            '/' + file._id, provider='osfstorage', node=node)
Beispiel #14
0
    def _create(cls,
                target_class,
                project=None,
                is_public=True,
                filename='preprint_file.txt',
                provider=None,
                doi=None,
                external_url=None,
                is_published=True,
                subjects=None,
                finish=True,
                *args,
                **kwargs):
        save_kwargs(**kwargs)
        user = None
        if project:
            user = project.creator
        user = kwargs.get('user') or kwargs.get(
            'creator') or user or UserFactory()
        kwargs['creator'] = user
        # Original project to be converted to a preprint
        project = project or AbstractNodeFactory(*args, **kwargs)
        if user._id not in project.permissions:
            project.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=False)
        project.save()
        project.reload()

        file = OsfStorageFile.create(node=project,
                                     path='/{}'.format(filename),
                                     name=filename,
                                     materialized_path='/{}'.format(filename))
        file.save()

        preprint = target_class(node=project, provider=provider)

        auth = Auth(project.creator)

        if finish:
            preprint.set_primary_file(file, auth=auth)
            subjects = subjects or [[SubjectFactory()._id]]
            preprint.set_subjects(subjects, auth=auth)
            preprint.set_published(is_published, auth=auth)

        if not preprint.is_published:
            project._has_abandoned_preprint = True

        project.preprint_article_doi = doi
        project.save()
        preprint.save()

        return preprint
Beispiel #15
0
    def test_redirects(self, app, project, user):
        # test_redirect_to_node_view
        url = '/{}guids/{}/'.format(API_BASE, project._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/'.format(
            API_DOMAIN, API_BASE, project._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_registration_view
        registration = RegistrationFactory()
        url = '/{}guids/{}/'.format(API_BASE, registration._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}registrations/{}/'.format(
            API_DOMAIN, API_BASE, registration._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_collections_view
        collection = CollectionFactory()
        url = '/{}guids/{}/'.format(API_BASE, collection._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}collections/{}/'.format(
            API_DOMAIN, API_BASE, collection._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_file_view
        test_file = OsfStorageFile.create(
            node=ProjectFactory(),
            path='/test', name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/'.format(API_BASE, guid._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/'.format(
            API_DOMAIN, API_BASE, test_file._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_comment_view
        comment = CommentFactory()
        url = '/{}guids/{}/'.format(API_BASE, comment._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}comments/{}/'.format(
            API_DOMAIN, API_BASE, comment._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_throws_404_for_invalid_guids
        url = '/{}guids/{}/'.format(API_BASE, 'fakeguid')
        res = app.get(url, auth=user.auth, expect_errors=True)
        assert res.status_code == 404
Beispiel #16
0
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch('website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        set_doi = kwargs.pop('set_doi', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.article_doi = doi

        instance.machine_state = kwargs.pop('machine_state', 'initial')
        user = kwargs.pop('creator', None) or instance.creator
        instance.save()

        preprint_file = OsfStorageFile.create(
            target_object_id=instance.id,
            target_content_type=ContentType.objects.get_for_model(instance),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        preprint_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(user, {
            'object': '06d80e',
            'service': 'cloud',
            osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
        }, {
            'size': 1337,
            'contentType': 'img/png'
        }).save()
        update_task_patcher.stop()
        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)
            instance.set_published(is_published, auth=auth)
            create_task_patcher = mock.patch('website.identifiers.utils.request_identifiers')
            mock_create_identifier = create_task_patcher.start()
            if is_published and set_doi:
                mock_create_identifier.side_effect = sync_set_identifiers(instance)
            create_task_patcher.stop()

        instance.save()
        return instance
Beispiel #17
0
 def test_redirect_to_file_view(self):
     test_file = OsfStorageFile.create(
         node=ProjectFactory(),
         path='/test',
         name='test',
         materialized_path='/test',
     )
     test_file.save()
     guid = test_file.get_guid(create=True)
     url = '/{}guids/{}/'.format(API_BASE, guid._id)
     res = self.app.get(url, auth=self.user.auth)
     redirect_url = '{}{}files/{}/'.format(API_DOMAIN, API_BASE, test_file._id)
     assert_equal(res.status_code, 302)
     assert_equal(res.location, redirect_url)
Beispiel #18
0
    def test_admin_can_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(node=self.project,
                                     path='/panda.txt',
                                     name='panda.txt',
                                     materialized_path='/panda.txt')
        file.save()

        self.preprint.set_primary_file(file, auth=Auth(self.user), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_not_equal(initial_file._id, self.preprint.primary_file._id)
        assert_equal(file._id, self.preprint.primary_file._id)
Beispiel #19
0
    def test_nonadmin_cannot_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(
            node=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        file.save()

        with assert_raises(PermissionsError):
            self.preprint.set_primary_file(file, auth=Auth(self.write_contrib), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_equal(initial_file._id, self.preprint.primary_file._id)
Beispiel #20
0
    def test_nonadmin_cannot_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(
            target=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        file.save()

        with assert_raises(PermissionsError):
            self.preprint.set_primary_file(file, auth=Auth(self.write_contrib), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_equal(initial_file._id, self.preprint.primary_file._id)
Beispiel #21
0
    def test_admin_can_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(
            node=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        file.save()

        self.preprint.set_primary_file(file, auth=Auth(self.user), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_not_equal(initial_file._id, self.preprint.primary_file._id)
        assert_equal(file._id, self.preprint.primary_file._id)
Beispiel #22
0
    def test_redirect_when_viewing_private_project_file_through_view_only_link(self):
        project = ProjectFactory()
        test_file = OsfStorageFile.create(
            node=project,
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        view_only_link = self._add_private_link(project)

        url = '/{}guids/{}/?view_only={}'.format(API_BASE, guid._id, view_only_link.key)
        res = self.app.get(url, auth=AuthUserFactory().auth)
        redirect_url = '{}{}files/{}/?view_only={}'.format(API_DOMAIN, API_BASE, test_file._id, view_only_link.key)
        assert_equal(res.status_code, 302)
        assert_equal(res.location, redirect_url)
Beispiel #23
0
    def test_serialize(self):
        file = OsfStorageFile(name='MOAR PYLONS',
                              target=self.node_settings.owner)
        file.save()

        assert_equals(
            file.serialize(), {
                u'id': file._id,
                u'path': file.path,
                u'created': None,
                u'name': u'MOAR PYLONS',
                u'kind': 'file',
                u'version': 0,
                u'downloads': 0,
                u'size': None,
                u'modified': None,
                u'contentType': None,
                u'checkout': None,
                u'md5': None,
                u'sha256': None,
            })

        version = file.create_version(
            self.user, {
                u'service': u'cloud',
                settings.WATERBUTLER_RESOURCE: u'osf',
                u'object': u'06d80e',
            }, {
                u'size': 1234,
                u'contentType': u'text/plain'
            })

        assert_equals(
            file.serialize(), {
                u'id': file._id,
                u'path': file.path,
                u'created': version.created.isoformat(),
                u'name': u'MOAR PYLONS',
                u'kind': u'file',
                u'version': 1,
                u'downloads': 0,
                u'size': 1234L,
                u'modified': version.created.isoformat(),
                u'contentType': u'text/plain',
                u'checkout': None,
                u'md5': None,
                u'sha256': None,
            })
Beispiel #24
0
    def _create(cls, target_class, project=None, is_public=True, filename='preprint_file.txt', provider=None, 
                doi=None, external_url=None, is_published=True, subjects=None, finish=True, *args, **kwargs):
        save_kwargs(**kwargs)
        user = None
        if project:
            user = project.creator
        user = kwargs.get('user') or kwargs.get('creator') or user or UserFactory()
        kwargs['creator'] = user
        # Original project to be converted to a preprint
        project = project or AbstractNodeFactory(*args, **kwargs)
        if user._id not in project.permissions:
            project.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=False
            )
        project.save()
        project.reload()

        file = OsfStorageFile.create(
            node=project,
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))
        file.save()

        preprint = target_class(node=project, provider=provider)

        auth = Auth(project.creator)

        if finish:
            preprint.set_primary_file(file, auth=auth)
            subjects = subjects or [[SubjectFactory()._id]]
            preprint.set_subjects(subjects, auth=auth)
            preprint.set_published(is_published, auth=auth)
        
        if not preprint.is_published:
            project._has_abandoned_preprint = True

        project.preprint_article_doi = doi
        project.save()
        preprint.save()

        return preprint
Beispiel #25
0
    def test_redirects_through_view_only_link(self, app, project, user):

        # test_redirect_when_viewing_private_project_through_view_only_link
        view_only_link = PrivateLinkFactory(anonymous=False)
        view_only_link.nodes.add(project)
        view_only_link.save()

        url = '/{}guids/{}/?view_only={}'.format(API_BASE, project._id,
                                                 view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, project._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_file_through_view_only_link
        test_file = OsfStorageFile.create(
            node=project,
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/?view_only={}'.format(API_BASE, guid._id,
                                                 view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, test_file._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_comment_through_view_only_link
        comment = CommentFactory(node=project)
        url = '/{}guids/{}/?view_only={}'.format(API_BASE, comment._id,
                                                 view_only_link.key)
        res = app.get(url, auth=AuthUserFactory().auth)
        redirect_url = '{}{}comments/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, comment._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url
Beispiel #26
0
    def test_redirects_through_view_only_link(self, app, project, user):

        # test_redirect_when_viewing_private_project_through_view_only_link
        view_only_link = PrivateLinkFactory(anonymous=False)
        view_only_link.nodes.add(project)
        view_only_link.save()

        url = '/{}guids/{}/?view_only={}'.format(
            API_BASE, project._id, view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, project._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_file_through_view_only_link
        test_file = OsfStorageFile.create(
            node=project,
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/?view_only={}'.format(
            API_BASE, guid._id, view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, test_file._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_comment_through_view_only_link
        comment = CommentFactory(node=project)
        url = '/{}guids/{}/?view_only={}'.format(
            API_BASE, comment._id, view_only_link.key)
        res = app.get(url, auth=AuthUserFactory().auth)
        redirect_url = '{}{}comments/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, comment._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url
Beispiel #27
0
    def test_serialize(self):
        file = OsfStorageFile(name='MOAR PYLONS', node=self.node_settings.owner)
        file.save()

        assert_equals(file.serialize(), {
            u'id': file._id,
            u'path': file.path,
            u'created': None,
            u'name': u'MOAR PYLONS',
            u'kind': 'file',
            u'version': 0,
            u'downloads': 0,
            u'size': None,
            u'modified': None,
            u'contentType': None,
            u'checkout': None,
            u'md5': None,
            u'sha256': None,
        })

        version = file.create_version(
            self.user,
            {
                u'service': u'cloud',
                settings.WATERBUTLER_RESOURCE: u'osf',
                u'object': u'06d80e',
            }, {
                u'size': 1234,
                u'contentType': u'text/plain'
            })

        assert_equals(file.serialize(), {
            u'id': file._id,
            u'path': file.path,
            u'created': version.date_created.isoformat(),
            u'name': u'MOAR PYLONS',
            u'kind': u'file',
            u'version': 1,
            u'downloads': 0,
            u'size': 1234L,
            u'modified': version.date_created.isoformat(),
            u'contentType': u'text/plain',
            u'checkout': None,
            u'md5': None,
            u'sha256': None,
        })
Beispiel #28
0
    def _build(cls,
               target_class,
               project=None,
               filename='preprint_file.txt',
               provider=None,
               doi=None,
               external_url=None,
               is_published=True,
               subjects=None,
               finish=True,
               *args,
               **kwargs):
        user = None
        if project:
            user = project.creator
        user = kwargs.get('user') or kwargs.get(
            'creator') or user or UserFactory()
        kwargs['creator'] = user
        # Original project to be converted to a preprint
        project = project or ProjectFactory(*args, **kwargs)
        project.save()
        if not project.is_contributor(user):
            project.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=True)

        file = OsfStorageFile.create(node=project,
                                     path='/{}'.format(filename),
                                     name=filename,
                                     materialized_path='/{}'.format(filename))
        file.save()

        from addons.osfstorage import settings as osfstorage_settings

        file.create_version(
            user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': 1337,
                'contentType': 'img/png'
            }).save()

        preprint = target_class(node=project, provider=provider)

        auth = Auth(project.creator)

        if finish:
            preprint.set_primary_file(file, auth=auth)
            subjects = subjects or [[SubjectFactory()._id]]
            preprint.save()
            preprint.set_subjects(subjects, auth=auth)
            preprint.set_published(is_published, auth=auth)

        if not preprint.is_published:
            project._has_abandoned_preprint = True

        project.preprint_article_doi = doi
        project.save()
        return preprint
Beispiel #29
0
    def test_serialize(self):
        file = OsfStorageFile(name='MOAR PYLONS',
                              target=self.node_settings.owner)
        file.save()

        assert_equals(
            file.serialize(), {
                u'id': file._id,
                u'path': file.path,
                u'created': None,
                u'name': u'MOAR PYLONS',
                u'kind': 'file',
                u'version': 0,
                u'downloads': 0,
                u'size': None,
                u'modified': None,
                u'contentType': None,
                u'checkout': None,
                u'md5': None,
                u'sha256': None,
            })

        version = file.create_version(
            self.user, {
                u'service': u'cloud',
                settings.WATERBUTLER_RESOURCE: u'osf',
                u'object': u'06d80e',
            }, {
                u'size': 1234,
                u'contentType': u'text/plain'
            })

        assert_equals(
            file.serialize(), {
                u'id': file._id,
                u'path': file.path,
                u'created': version.created.isoformat(),
                u'name': u'MOAR PYLONS',
                u'kind': u'file',
                u'version': 1,
                u'downloads': 0,
                u'size': 1234,
                u'modified': version.created.isoformat(),
                u'contentType': u'text/plain',
                u'checkout': None,
                u'md5': None,
                u'sha256': None,
            })

        date = timezone.now()
        version.update_metadata({u'modified': date.isoformat()})

        assert_equals(
            file.serialize(),
            {
                u'id': file._id,
                u'path': file.path,
                u'created': version.created.isoformat(),
                u'name': u'MOAR PYLONS',
                u'kind': u'file',
                u'version': 1,
                u'downloads': 0,
                u'size': 1234,
                # modified date is the creation date of latest version
                # see https://github.com/CenterForOpenScience/osf.io/pull/7155
                u'modified': version.created.isoformat(),
                u'contentType': u'text/plain',
                u'checkout': None,
                u'md5': None,
                u'sha256': None,
            })
Beispiel #30
0
def file_node(project):
    file_node = OsfStorageFile(name='test', target=project)
    file_node.save()
    return file_node
Beispiel #31
0
 def test_file_path(self):
     file = OsfStorageFile(name='MOAR PYLONS', target=self.node)
     file.save()
     assert_equal(file.name, 'MOAR PYLONS')
     assert_equal(file.path, '/{}'.format(file._id))
Beispiel #32
0
    def setUp(self):
        super(TestPreregFiles, self).setUp()
        self.prereg_user = AuthUserFactory()
        self.user = AuthUserFactory()
        self.node = ProjectFactory(creator=self.user)

        prereg_schema = get_prereg_schema()
        self.d_of_qs = {
            'q7': OsfStorageFile(target=self.node, name='7'),
            'q11': OsfStorageFile(target=self.node, name='11'),
            'q16': OsfStorageFile(target=self.node, name='16'),
            'q12': OsfStorageFile(target=self.node, name='12'),
            'q13': OsfStorageFile(target=self.node, name='13'),
            'q19': OsfStorageFile(target=self.node, name='19'),
            'q26': OsfStorageFile(target=self.node, name='26')
        }
        data = {}
        for q, f in self.d_of_qs.items():
            guid = f.get_guid(create=True)._id
            f.save()
            if q == 'q26':
                data[q] = {
                    'comments': [],
                    'value':
                    '26',
                    'extra': [{
                        'data': {
                            'provider': 'osfstorage',
                            'path': f.path,
                        },
                        'fileId': guid,
                        'nodeId': self.node._id,
                    }]
                }
                continue
            data[q] = {
                'value': {
                    'uploader': {
                        'extra': [{
                            'data': {
                                'provider': 'osfstorage',
                                'path': f.path,
                            },
                            'fileId': guid,
                            'nodeId': self.node._id,
                        }]
                    }
                }
            }
        self.draft = DraftRegistrationFactory(
            initiator=self.user,
            branched_from=self.node,
            registration_schema=prereg_schema,
            registration_metadata=data)

        self.prereg_user.save()
        self.admin_user = UserFactory()
        self.admin_user.is_superuser = True
        self.admin_user.groups.add(Group.objects.get(name='prereg_admin'))
        self.admin_user.groups.add(Group.objects.get(name='prereg_view'))
        self.admin_user.save()
Beispiel #33
0
def file_node2(project):
    file_node2 = OsfStorageFile(name='test2', target=project)
    file_node2.save()
    return file_node2
Beispiel #34
0
def file_node3(project):
    file_node3 = OsfStorageFile(name='test3', target=project)
    file_node3.save()
    return file_node3
Beispiel #35
0
def file_node3(project):
    file_node3 = OsfStorageFile(name='test3', target=project)
    file_node3.save()
    return file_node3
Beispiel #36
0
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch(
            'website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.node.preprint_article_doi = doi

        instance.machine_state = kwargs.pop('machine_state', 'initial')

        user = kwargs.pop('creator', None) or instance.node.creator
        if not instance.node.is_contributor(user):
            instance.node.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=True)

        preprint_file = OsfStorageFile.create(
            node=instance.node,
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))
        preprint_file.save()

        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(
            user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': 1337,
                'contentType': 'img/png'
            }).save()

        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)

            create_task_patcher = mock.patch(
                'website.preprints.tasks.get_and_set_preprint_identifiers.si')
            mock_create_identifier = create_task_patcher.start()
            if is_published:
                mock_create_identifier.side_effect = sync_set_identifiers(
                    instance)

            instance.set_published(is_published, auth=auth)
            create_task_patcher.stop()

        if not instance.is_published:
            instance.node._has_abandoned_preprint = True

        instance.node.save()
        instance.save()
        return instance
Beispiel #37
0
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch('website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.node.preprint_article_doi = doi

        instance.machine_state = kwargs.pop('machine_state', 'initial')

        user = kwargs.pop('creator', None) or instance.node.creator
        if not instance.node.is_contributor(user):
            instance.node.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=True
            )

        preprint_file = OsfStorageFile.create(
            node=instance.node,
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))
        preprint_file.save()

        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(user, {
            'object': '06d80e',
            'service': 'cloud',
            osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
        }, {
            'size': 1337,
            'contentType': 'img/png'
        }).save()

        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)

            create_task_patcher = mock.patch('website.preprints.tasks.get_and_set_preprint_identifiers.si')
            mock_create_identifier = create_task_patcher.start()
            if is_published:
                mock_create_identifier.side_effect = sync_set_identifiers(instance)

            instance.set_published(is_published, auth=auth)
            create_task_patcher.stop()

        if not instance.is_published:
            instance.node._has_abandoned_preprint = True

        instance.node.save()
        instance.save()
        return instance
Beispiel #38
0
def file_node(project):
    file_node = OsfStorageFile(name='test', target=project)
    file_node.save()
    return file_node
Beispiel #39
0
 def test_file_path(self):
     file = OsfStorageFile(name='MOAR PYLONS', node=self.node)
     file.save()
     assert_equal(file.name, 'MOAR PYLONS')
     assert_equal(file.path, '/{}'.format(file._id))
Beispiel #40
0
def file_node2(project):
    file_node2 = OsfStorageFile(name='test2', target=project)
    file_node2.save()
    return file_node2