Exemplo n.º 1
0
    def test_basic_folder_ops(self):
        from girder.plugins.virtual_resources.rest import VirtualObject

        resp = self.request(
            path="/folder",
            method="POST",
            user=self.users["admin"],
            params={
                "parentType": "folder",
                "parentId": self.public_folder["_id"],
                "name": "test_folder",
            },
        )
        self.assertStatusOk(resp)
        folder = resp.json

        actual_folder_path = pathlib.Path(self.public_root) / folder["name"]
        self.assertTrue(actual_folder_path.is_dir())

        decoded_path, decoded_root_id = VirtualObject.path_from_id(
            folder["_id"])
        self.assertEqual(decoded_path, actual_folder_path)
        self.assertEqual(decoded_root_id, str(self.public_folder["_id"]))

        resp = self.request(
            path="/folder",
            method="GET",
            user=self.users["admin"],
            params={
                "parentType": "folder",
                "parentId": str(self.public_folder["_id"])
            },
        )
        self.assertStatusOk(resp)
        get_folders = resp.json
        self.assertEqual(len(get_folders), 1)
        self.assertEqual(get_folders[0], folder)

        resp = self.request(
            path="/folder/{_id}".format(**folder),
            method="GET",
            user=self.users["admin"],
        )
        self.assertStatusOk(resp)
        self.assertEqual(resp.json, get_folders[0])

        resp = self.request(
            path="/folder/{_id}".format(**folder),
            method="PUT",
            user=self.users["admin"],
            params={"name": "renamed"},
        )
        self.assertStatusOk(resp)
        folder = resp.json
        self.assertFalse(actual_folder_path.exists())
        actual_folder_path = pathlib.Path(self.public_root) / folder["name"]
        self.assertTrue(actual_folder_path.is_dir())

        resp = self.request(
            path="/folder/{_id}".format(**folder),
            method="DELETE",
            user=self.users["admin"],
        )
        self.assertStatusOk(resp)
        self.assertFalse(actual_folder_path.exists())
Exemplo n.º 2
0
    def test_basic_item_ops(self):
        from girder.plugins.virtual_resources.rest import VirtualObject

        root_path = pathlib.Path(self.public_folder["fsPath"])
        nested_dir = root_path / "level0"
        nested_dir.mkdir(parents=True)
        parentId = VirtualObject.generate_id(nested_dir.as_posix(),
                                             self.public_folder["_id"])

        resp = self.request(
            path="/item",
            method="POST",
            user=self.users["admin"],
            params={
                "folderId": parentId,
                "name": "test_item"
            },
        )
        self.assertStatusOk(resp)
        item = resp.json

        actual_item_path = nested_dir / item["name"]
        self.assertTrue(actual_item_path.is_file())

        decoded_path, decoded_root_id = VirtualObject.path_from_id(item["_id"])
        self.assertEqual(decoded_path, actual_item_path)
        self.assertEqual(decoded_root_id, str(self.public_folder["_id"]))

        resp = self.request(
            path="/item",
            method="GET",
            user=self.users["admin"],
            params={
                "parentType": "folder",
                "parentId": str(parentId),
                "name": "nope"
            },
        )
        self.assertStatusOk(resp)
        self.assertEqual(len(resp.json), 0)

        resp = self.request(
            path="/item",
            method="GET",
            user=self.users["admin"],
            params={
                "parentType": "folder",
                "parentId": str(parentId),
                "name": item["name"]
            },
        )
        self.assertStatusOk(resp)
        self.assertEqual(len(resp.json), 1)

        resp = self.request(
            path="/item",
            method="GET",
            user=self.users["admin"],
            params={
                "parentType": "folder",
                "parentId": str(parentId)
            },
        )
        self.assertStatusOk(resp)
        get_items = resp.json
        self.assertEqual(len(get_items), 1)
        self.assertEqual(get_items[0], item)

        resp = self.request(path="/item/{_id}".format(**item),
                            method="GET",
                            user=self.users["admin"])
        self.assertStatusOk(resp)
        self.assertEqual(resp.json, get_items[0])

        resp = self.request(
            path="/item/{_id}".format(**item),
            method="PUT",
            user=self.users["admin"],
            params={"name": "renamed"},
        )
        self.assertStatusOk(resp)
        item = resp.json
        self.assertFalse(actual_item_path.exists())
        actual_item_path = actual_item_path.with_name(item["name"])
        self.assertTrue(actual_item_path.is_file())

        resp = self.request(
            path="/item/{_id}/files".format(**item),
            method="GET",
            user=self.users["admin"],
        )
        self.assertStatusOk(resp)
        files = resp.json
        self.assertEqual(len(files), 1)
        self.assertEqual(files[0]["_id"], item["_id"])

        resp = self.request(path="/item/{_id}".format(**item),
                            method="DELETE",
                            user=self.users["admin"])
        self.assertStatusOk(resp)
        self.assertFalse(actual_item_path.exists())
        shutil.rmtree(nested_dir.as_posix())