Esempio n. 1
0
    def test_file_in_folder(self):
        rtype = 'GenericResource'
        title = 'My Test resource'
        params = {'resource_type': rtype,
                  'title': title,
                  'file': ('cea.tif',
                           open('hs_core/tests/data/cea.tif'),
                           'image/tiff')}
        url = '/hsapi/resource/'
        response = self.client.post(url, params)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        content = json.loads(response.content)
        res_id = content['resource_id']
        self.resources_to_delete.append(res_id)

        # create a folder 'foo'
        url2 = str.format('/hsapi/resource/{}/folders/foo/', res_id)
        response = self.client.put(url2, {})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # create a folder 'foo/bar'
        url3 = url2 + 'bar/'
        response = self.client.put(url3, {})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # put a file 'test.txt' into folder 'foo'
        url4 = str.format('/hsapi/resource/{}/files/foo/', res_id)
        params = {'file': ('test.txt',
                           open('hs_core/tests/data/test.txt'),
                           'text/plain')}
        response = self.client.post(url4, params)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        resfile = ResourceFile.objects.get(file_folder='foo')
        path = get_path(resfile, 'test.txt')
        self.assertEqual(path,
                         str.format("{}/data/contents/foo/test.txt",
                                    res_id))

        # list that folder: should contain one file and one folder
        response = self.client.get(url2, {})
        content = json.loads(response.content)
        self.assertEqual(len(content['folders']), 1)
        self.assertEqual(content['folders'][0], 'bar')
        self.assertEqual(len(content['files']), 1)
        self.assertEqual(content['files'][0], 'test.txt')
    def test_federated_root_path_logic(self):
        """ a federated file path in the root folder has the proper state after state changes """
        # resource should not have any files at this point
        self.assertEqual(self.res.files.all().count(), 0,
                         msg="resource file count didn't match")

        # add one file to the resource
        hydroshare.add_resource_files(self.res.short_id, self.test_file_1)

        # resource should has only one file at this point
        self.assertEqual(self.res.files.all().count(), 1,
                         msg="resource file count didn't match")

        # get the handle of the file created above
        resfile = self.res.files.all()[0]

        # cheat: set a fake federated path to test path logic
        oldfedpath = self.res.resource_federation_path
        oldpath = resfile.storage_path

        # intentionally break path logic by setting an unused federation path
        fedpath = "/myzone/home/myuser"
        self.res.resource_federation_path = fedpath
        self.res.save()
        # must load changes into resfile from self.res before setting storage path
        resfile.content_object.refresh_from_db()
        resfile.set_storage_path('file1.txt', test_exists=False)

        self.assertEqual(self.res.resource_federation_path, fedpath)
        self.assertEqual(resfile.storage_path, get_path(resfile, 'file1.txt'))

        # determine where that file should live; THIS IS FAKE
        shortpath = os.path.join(fedpath, self.res.short_id, "data",
                                 "contents", "file1.txt")

        # intentionally break the resource file path
        resfile.set_storage_path(shortpath, test_exists=False)
        self.assertEqual(shortpath, resfile.storage_path)

        self.assertEqual(resfile.file_folder, None)
        self.assertEqual(resfile.storage_path, shortpath)

        self.assertTrue(resfile.path_is_acceptable(shortpath, test_exists=False))

        otherpath = os.path.join(fedpath, self.res.short_id, "data", "contents", "file2.txt")
        resfile.path_is_acceptable(otherpath, test_exists=False)

        # non-existent files should raise error
        # This won't work because federation path is fake
        # with self.assertRaises(ValidationError):
        #     resfile.path_is_acceptable(otherpath, test_exists=True)

        # try setting to an unqualified name; should qualify it
        resfile.set_storage_path("file1.txt", test_exists=False)
        # should match computed path
        self.assertEqual(resfile.file_folder, None)
        self.assertEqual(resfile.storage_path, shortpath)

        # now try to change that path to what it is already
        resfile.set_storage_path(shortpath, test_exists=False)
        # should match computed path
        self.assertEqual(resfile.file_folder, None)
        self.assertEqual(resfile.storage_path, shortpath)

        # now try to change that path to a good path to a non-existent object
        resfile.set_storage_path(otherpath, test_exists=False)

        # conclusion: strip off federation path
        self.res.resource_federation_path = oldfedpath
        self.res.save()
        # must load changes into resfile from self.res before setting storage path
        resfile.content_object.refresh_from_db()
        resfile.set_storage_path(oldpath, test_exists=False)

        # delete resources to clean up
        hydroshare.delete_resource(self.res.short_id)