Exemple #1
0
 def test_put_wrong_callback(self):
     with pytest.raises(AssertionError) as e_info:
         with FTPClient(
                 self.host, self.user,
                 self.password) as ftp, tempfile.NamedTemporaryFile() as f:
             remotepath = os.path.join(self.dir, os.path.basename(f.name))
             ftp.put(f.name, remotepath, callback='error')
Exemple #2
0
    def test_pwd_chdir(self):
        with FTPClient(self.host, self.user, self.password) as ftp:
            assert ftp.pwd() == '/'

            ftp.chdir(self.dir)

            assert ftp.pwd() == self.dir
Exemple #3
0
    def test_pwd_chdir(self):
        with FTPClient(self.host,
                       self.user,
                       self.password,
                       port=self.port,
                       protocol='sftp') as sftp:
            sftp.chdir(self.dir)

            assert sftp.pwd() == f'/home/{self.user}/{self.dir}'
Exemple #4
0
    def test_listdir(self):
        with FTPClient(self.host,
                       self.user,
                       self.password,
                       port=self.port,
                       protocol='sftp') as sftp:
            directory_files = sftp.listdir(self.dir)

            assert len(directory_files) > 0
Exemple #5
0
 def test_name(self):
     with FTPClient(self.host,
                    self.user,
                    self.password,
                    port=self.port,
                    protocol='sftp') as sftp:
         assert str(
             sftp
         ) == f'<FTPClient({self.host}, {self.user}, xxxx, port = {self.port}, protocol = sftp)>'
Exemple #6
0
    def test_rename(self):
        with FTPClient(self.host, self.user, self.password) as ftp:
            ftp.chdir(self.dir)
            new_dir = str(int(time.time()))
            ftp.mkdir(new_dir)

            ftp.rename(new_dir, f'{new_dir}_renamed')

            files = ftp.listdir(self.dir)

            assert f'{self.dir}/{new_dir}_renamed' in files

            ftp.rmdir(f'{new_dir}_renamed')
Exemple #7
0
    def test_get_wrong_callback(self):
        with pytest.raises(AssertionError) as e_info:
            with FTPClient(self.host, self.user,
                           self.password) as ftp, tempfile.NamedTemporaryFile(
                               mode='w') as f:
                files = ftp.listdir(self.dir)

                remotepath = next(
                    (f for f in files
                     if f.endswith('.csv') or f.endswith('.txt')), None)

                ftp.get(remotepath, f.name, callback='error')

                assert os.path.exists(f.name)
                assert os.path.isfile(f.name)
Exemple #8
0
    def test_put_delete(self):
        with FTPClient(
                self.host,
                self.user,
                self.password,
                port=self.port,
                protocol='sftp') as sftp, tempfile.NamedTemporaryFile() as f:
            remotepath = os.path.join(self.dir, os.path.basename(f.name))
            sftp.put(f.name, remotepath)

            sftp.delete(remotepath)

            files = sftp.listdir(self.dir)

            assert remotepath not in files
Exemple #9
0
    def test_put_delete(self):
        with FTPClient(
                self.host, self.user,
                self.password) as ftp, tempfile.NamedTemporaryFile() as f:
            remotepath = os.path.join(self.dir, os.path.basename(f.name))

            def file_downloaded():
                print("File downloaded successfully")

            ftp.put(f.name, remotepath, callback=file_downloaded)

            ftp.delete(remotepath)

            files = ftp.listdir(self.dir)

            assert remotepath not in files
Exemple #10
0
    def test_get(self):
        with FTPClient(self.host,
                       self.user,
                       self.password,
                       port=self.port,
                       protocol='sftp') as sftp, tempfile.NamedTemporaryFile(
                           mode='w') as f:
            files = sftp.listdir(self.dir)

            filename = next(
                (f for f in files if f.endswith('.csv') or f.endswith('.txt')),
                None)

            sftp.get(f'/home/{self.user}/{self.dir}/{filename}', f.name)

            assert os.path.exists(f.name)
            assert os.path.isfile(f.name)
Exemple #11
0
    def test_rename(self):
        with FTPClient(self.host,
                       self.user,
                       self.password,
                       port=self.port,
                       protocol='sftp') as sftp:
            sftp.chdir(self.dir)
            new_dir = str(int(time.time()))
            sftp.mkdir(new_dir)

            sftp.rename(f'/home/{self.user}/{self.dir}/{new_dir}',
                        f'/home/{self.user}/{self.dir}/{new_dir}_renamed')

            files = sftp.listdir('.')

            assert f'{new_dir}_renamed' in files

            sftp.rmdir(f'/home/{self.user}/{self.dir}/{new_dir}_renamed')
Exemple #12
0
    def test_get(self):
        with FTPClient(self.host, self.user,
                       self.password) as ftp, tempfile.NamedTemporaryFile(
                           mode='w') as f:
            files = ftp.listdir(self.dir)

            remotepath = next(
                (f for f in files if f.endswith('.csv') or f.endswith('.txt')),
                None)

            def file_uploaded():
                print("File uploaded successfully")

            localpath = os.path.join('/tmp', 'newdir',
                                     os.path.basename(f.name))

            ftp.get(remotepath, localpath, callback=file_uploaded)

            assert os.path.exists(localpath)
            assert os.path.isfile(localpath)
Exemple #13
0
    def test_listdir(self):
        with FTPClient(self.host, self.user, self.password) as ftp:
            directory_files = ftp.listdir(self.dir)

            assert len(directory_files) > 0
Exemple #14
0
 def test_invalid_connection(self):
     with pytest.raises(InvalidConnection) as e_info:
         with FTPClient(self.host, self.user, '1111') as ftp:
             pass
Exemple #15
0
 def test_name(self):
     with FTPClient(self.host, self.user, self.password) as ftp:
         assert str(
             ftp
         ) == f'<FTPClient({self.host}, {self.user}, xxxx, port = 21, protocol = ftp)>'
Exemple #16
0
 def test_put_invalid_file(self):
     with pytest.raises(Exception) as e_info:
         with FTPClient(self.host, self.user, self.password) as ftp:
             remotepath = os.path.join(self.dir, 'foofiledoesnotexist')
             ftp.put('foofiledoesnotexist', remotepath)