# In[ ]:

# download a remote text file into a local directory

Files.download(fileService, Files_RootVolumeName1, Files_UserVolumeName1, 
               Files_NewDirectoryName1 + "/" + Files_LocalFileName, 
               localFilePath=Files_LocalFileName);


# In[ ]:

# Delete remote file

Files.delete(fileService, Files_RootVolumeName1, Files_UserVolumeName1, Files_NewDirectoryName1 + "/" + Files_LocalFileName)


# In[ ]:

# upload a local file into a remote directory

Files.upload(fileService, Files_RootVolumeName1, Files_UserVolumeName1,
             Files_NewDirectoryName1 + "/" + Files_LocalFileName, localFilePath=Files_LocalFileName, quiet=False);


# In[ ]:

# copy remote file into remote directory

Files.move(fileService, Files_RootVolumeName1, Files_UserVolumeName1,Files_NewDirectoryName1 + "/" + Files_LocalFileName, 
    def test_Files_createDir_upload_dirList_download_download_shareUserVolume(
            self):

        try:
            fileService = Files.getFileServiceFromName(Files_FileServiceName)
            os.remove(Files_LocalFileName)
            Files.deleteUserVolume(fileService,
                                   Files_RootVolumeName1,
                                   Files_UserVolumeName1,
                                   quiet=True)
            Files.deleteUserVolume(fileService,
                                   Files_RootVolumeName1,
                                   Files_UserVolumeName2,
                                   quiet=True)
        except:
            pass

        try:
            fileService = Files.getFileServiceFromName(Files_FileServiceName)

            Files.createUserVolume(fileService,
                                   "/".join([
                                       Files_RootVolumeName1,
                                       Authentication_loginName,
                                       Files_UserVolumeName1
                                   ]),
                                   quiet=False)
            Files.createUserVolume(fileService,
                                   "/".join([
                                       Files_RootVolumeName1,
                                       Authentication_loginName,
                                       Files_UserVolumeName2
                                   ]),
                                   quiet=False)

            Files.createDir(
                fileService, "/".join([
                    Files_RootVolumeName1, Authentication_loginName,
                    Files_UserVolumeName1, Files_NewDirectoryName1
                ]))
            Files.createDir(
                fileService, "/".join([
                    Files_RootVolumeName2, Authentication_loginName,
                    Files_UserVolumeName2, Files_NewDirectoryName2
                ]))

            dirList = Files.dirList(fileService,
                                    "/".join([
                                        Files_RootVolumeName1,
                                        Authentication_loginName,
                                        Files_UserVolumeName1,
                                        Files_NewDirectoryName1
                                    ]),
                                    level=2)
            self.assertTrue(
                dirList.get('root').get('name') == Files_NewDirectoryName1)

            Files.upload(fileService,
                         "/".join([
                             Files_RootVolumeName1, Authentication_loginName,
                             Files_UserVolumeName1, Files_NewDirectoryName1,
                             Files_LocalFileName
                         ]),
                         data=Files_LocalFileContent)

            dirList = Files.dirList(fileService,
                                    "/".join([
                                        Files_RootVolumeName1,
                                        Authentication_loginName,
                                        Files_UserVolumeName1,
                                        Files_NewDirectoryName1
                                    ]),
                                    level=2)
            self.assertTrue(
                dirList.get('root').get('files')[0].get('name') ==
                Files_LocalFileName)

            Files.download(fileService,
                           "/".join([
                               Files_RootVolumeName1, Authentication_loginName,
                               Files_UserVolumeName1, Files_NewDirectoryName1,
                               Files_LocalFileName
                           ]),
                           localFilePath=Files_LocalFileName)

            with open(Files_LocalFileName, 'r') as myfile:
                downloadedFileContent = myfile.read()
                assert (downloadedFileContent == Files_LocalFileContent)

            Files.delete(
                fileService, "/".join([
                    Files_RootVolumeName1, Authentication_loginName,
                    Files_UserVolumeName1, Files_NewDirectoryName1,
                    Files_LocalFileName
                ]))

            dirList = Files.dirList(fileService,
                                    "/".join([
                                        Files_RootVolumeName1,
                                        Authentication_loginName,
                                        Files_UserVolumeName1,
                                        Files_NewDirectoryName1
                                    ]),
                                    level=2)
            self.assertIsNone(dirList.get('root').get('files'))

            Files.upload(fileService,
                         "/".join([
                             Files_RootVolumeName1, Authentication_loginName,
                             Files_UserVolumeName1, Files_NewDirectoryName1,
                             Files_LocalFileName
                         ]),
                         localFilePath=Files_LocalFileName,
                         quiet=False)

            Files.move(
                fileService, "/".join([
                    Files_RootVolumeName1, Authentication_loginName,
                    Files_UserVolumeName1, Files_NewDirectoryName1,
                    Files_LocalFileName
                ]), fileService, "/".join([
                    Files_RootVolumeName2, Authentication_loginName,
                    Files_UserVolumeName2, Files_NewDirectoryName2,
                    Files_LocalFileName
                ]))

            Files.shareUserVolume(
                fileService,
                "/".join([
                    Files_RootVolumeName2, Authentication_loginName,
                    Files_UserVolumeName2
                ]),
                sharedWith=Authentication_login_sharedWithName,
                type="USER",
                allowedActions=["read"])

            token1 = Authentication.login(
                Authentication_login_sharedWithName,
                Authentication_login_sharedWithPassword)

            string = Files.download(fileService,
                                    "/".join([
                                        Files_RootVolumeName2,
                                        Authentication_loginName,
                                        Files_UserVolumeName2,
                                        Files_NewDirectoryName2,
                                        Files_LocalFileName
                                    ]),
                                    format="txt")

            self.assertTrue(string, Files_LocalFileContent)

            token1 = Authentication.login(Authentication_loginName,
                                          Authentication_loginPassword)

        finally:
            try:
                os.remove(Files_LocalFileName)
                Files.deleteUserVolume(fileService,
                                       "/".join([
                                           Files_RootVolumeName1,
                                           Authentication_loginName,
                                           Files_UserVolumeName1
                                       ]),
                                       quiet=True)
                Files.deleteUserVolume(fileService,
                                       "/".join([
                                           Files_RootVolumeName1,
                                           Authentication_loginName,
                                           Files_UserVolumeName2
                                       ]),
                                       quiet=True)
            except:
                pass