예제 #1
0
    def get_file_guids(cls, materialized_path, provider, node=None):
        guids = []
        path = materialized_path.strip('/')
        file_obj = cls.load(path)
        if not file_obj:
            file_obj = TrashedFileNode.load(path)

        # At this point, file_obj may be an OsfStorageFile, an OsfStorageFolder, or a
        # TrashedFileNode. TrashedFileNodes do not have *File and *Folder subclasses, since
        # only osfstorage trashes folders. To search for children of TrashFileNodes
        # representing ex-OsfStorageFolders, we will reimplement the `children` method of the
        # Folder class here.
        if not file_obj.is_file:
            children = []
            if isinstance(file_obj, TrashedFileNode):
                children = TrashedFileNode.find(Q('parent', 'eq', file_obj._id))
            else:
                children = file_obj.children

            for item in children:
                guids.extend(cls.get_file_guids(item.path, provider, node=node))
        else:
            try:
                guid = Guid.find(Q('referent', 'eq', file_obj))[0]
            except IndexError:
                guid = None
            if guid:
                guids.append(guid._id)

        return guids
예제 #2
0
 def test_delete_action_creates_trashed_file_node(self):
     file_node = self.get_test_file()
     payload = {"provider": file_node.provider, "metadata": {"path": "/test/Test", "materialized": "/test/Test"}}
     views.addon_delete_file_node(
         self=None, node=self.project, user=self.user, event_type="file_removed", payload=payload
     )
     assert_false(StoredFileNode.load(file_node._id))
     assert_true(TrashedFileNode.load(file_node._id))
예제 #3
0
 def test_delete_action_creates_trashed_file_node(self):
     file_node = self.get_test_file()
     payload = {
         'provider': file_node.provider,
         'metadata': {
             'path': '/test/Test',
             'materialized': '/test/Test'
         }
     }
     views.addon_delete_file_node(self=None, node=self.project, user=self.user, event_type='file_removed', payload=payload)
     assert_false(StoredFileNode.load(file_node._id))
     assert_true(TrashedFileNode.load(file_node._id))
예제 #4
0
 def test_delete_action_for_folder_deletes_subfolders_and_creates_trashed_file_nodes(self):
     file_node = self.get_test_file()
     subfolder = TestFolder(
         name="folder", node=self.project, path="/test/folder/", materialized_path="/test/folder/", versions=[]
     )
     subfolder.save()
     payload = {"provider": file_node.provider, "metadata": {"path": "/test/", "materialized": "/test/"}}
     views.addon_delete_file_node(
         self=None, node=self.project, user=self.user, event_type="file_removed", payload=payload
     )
     assert_false(StoredFileNode.load(file_node._id))
     assert_true(TrashedFileNode.load(file_node._id))
     assert_false(StoredFileNode.load(subfolder._id))
예제 #5
0
파일: osfstorage.py 프로젝트: exmo/osf.io
    def get_file_guids(cls, materialized_path, provider, node=None, guids=None):
        guids = guids or []
        path = materialized_path.strip('/')
        file_obj = cls.load(path)
        if not file_obj:
            file_obj = TrashedFileNode.load(path)

        if not file_obj.is_file:
            for item in file_obj.children:
                cls.get_file_guids(item.path, provider, node=node, guids=guids)
        else:
            try:
                guid = Guid.find(Q('referent', 'eq', file_obj))[0]
            except IndexError:
                guid = None
            if guid:
                guids.append(guid._id)
        return guids
예제 #6
0
    def test_view_file_does_not_delete_file_when_requesting_invalid_version(self):
        with mock.patch(
            "website.addons.github.model.GitHubNodeSettings.is_private", new_callable=mock.PropertyMock
        ) as mock_is_private:
            mock_is_private.return_value = False

            file_node = self.get_test_file()
            assert_is(file_node.get_guid(), None)

            url = self.project.web_url_for(
                "addon_view_or_download_file", path=file_node.path.strip("/"), provider="github"
            )
            # First view generated GUID
            self.app.get(url, auth=self.user.auth)

            self.app.get(url + "?version=invalid", auth=self.user.auth, expect_errors=True)

            assert_is_not_none(StoredFileNode.load(file_node._id))
            assert_is_none(TrashedFileNode.load(file_node._id))
예제 #7
0
 def test_delete_action_for_folder_deletes_subfolders_and_creates_trashed_file_nodes(self):
     file_node = self.get_test_file()
     subfolder = TestFolder(
         name='folder',
         node=self.project,
         path='/test/folder/',
         materialized_path='/test/folder/',
         versions=[]
     )
     subfolder.save()
     payload = {
         'provider': file_node.provider,
         'metadata': {
             'path': '/test/',
             'materialized': '/test/'
         }
     }
     views.addon_delete_file_node(self=None, node=self.project, user=self.user, event_type='file_removed', payload=payload)
     assert_false(StoredFileNode.load(file_node._id))
     assert_true(TrashedFileNode.load(file_node._id))
     assert_false(StoredFileNode.load(subfolder._id))
예제 #8
0
    def test_view_file_does_not_delete_file_when_requesting_invalid_version(
            self):
        with mock.patch(
                'website.addons.github.model.GitHubNodeSettings.is_private',
                new_callable=mock.PropertyMock) as mock_is_private:
            mock_is_private.return_value = False

            file_node = self.get_test_file()
            assert_is(file_node.get_guid(), None)

            url = self.project.web_url_for(
                'addon_view_or_download_file',
                path=file_node.path.strip('/'),
                provider='github',
            )
            # First view generated GUID
            self.app.get(url, auth=self.user.auth)

            self.app.get(url + '?version=invalid',
                         auth=self.user.auth,
                         expect_errors=True)

            assert_is_not_none(StoredFileNode.load(file_node._id))
            assert_is_none(TrashedFileNode.load(file_node._id))
예제 #9
0
 def test_delete_action_for_folder_deletes_subfolders_and_creates_trashed_file_nodes(
         self):
     file_node = self.get_test_file()
     subfolder = TestFolder(name='folder',
                            node=self.project,
                            path='/test/folder/',
                            materialized_path='/test/folder/',
                            versions=[])
     subfolder.save()
     payload = {
         'provider': file_node.provider,
         'metadata': {
             'path': '/test/',
             'materialized': '/test/'
         }
     }
     views.addon_delete_file_node(self=None,
                                  node=self.project,
                                  user=self.user,
                                  event_type='file_removed',
                                  payload=payload)
     assert_false(StoredFileNode.load(file_node._id))
     assert_true(TrashedFileNode.load(file_node._id))
     assert_false(StoredFileNode.load(subfolder._id))
예제 #10
0
 def tearDownClass(cls):
     super(TestAddonFileViews, cls).tearDownClass()
     PROVIDER_MAP['github'] = [models.GithubFolder, models.GithubFile, models.GithubFileNode]
     del PROVIDER_MAP['test_addons']
     TrashedFileNode.remove()
예제 #11
0
 def tearDownClass(cls):
     super(TestAddonFileViews, cls).tearDownClass()
     PROVIDER_MAP['github'] = [models.GithubFolder, models.GithubFile, models.GithubFileNode]
     del PROVIDER_MAP['test_addons']
     TrashedFileNode.remove()