def test_scandir_mlst_propagate_error(self, mocked_ftp_conn): client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.side_effect = Exception("any other exception") with pytest.raises(Exception, match="any other exception"): client.scandir()
def test_scandir_mlst_not_supported(self, mocked_ftp_conn): client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.side_effect = ftplib.error_perm("501 'MLST type;'") client.scandir() # assert we do a simple dir in the client client.conn.dir.assert_called()
def test_scandir_folder(self, mocked_ftp_conn): fake_entries = [("another_dir", {"type": "dir"})] client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.return_value = fake_entries entries = list(client.scandir()) assert entries[0].url == URL("ftp://localhost:9999/mydir/another_dir") assert entries[0].is_dir
def test_scandir_file(self, mocked_ftp_conn): fake_entries = [("my_file.txt", {"type": "file"})] client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.return_value = fake_entries entries = list(client.scandir()) assert entries[0].url == URL("ftp://localhost:9999/mydir/my_file.txt") assert entries[0].is_file
def test_parsing_ftp_url(self, url, username, password, hostname, port, path): parsed_url = ftp_client.FTPClient(url).url assert parsed_url.scheme == "ftp" assert parsed_url.hostname == hostname assert parsed_url.username == username assert parsed_url.password == password assert parsed_url.port == port assert parsed_url.path == path
def test_get(self, mocked_ftp_conn): expected = bytes("hello", "utf-8") client = ftp_client.FTPClient("ftp://*****:*****@localhost/myfile.txt") client.connect().retrbinary = lambda _, f: f(expected) buff = io.BytesIO() with client: client.get(buff) assert buff.getvalue() == expected
def test_scandir_dir_folder(self, mocked_ftp_conn): fake_entry = "drwxrwxrwx 1 owner group 0 Feb 17 17:54 nested" client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.side_effect = ftplib.error_perm("501 'MLST type;'") # mock ftplib.dir behaviour client.conn.dir = lambda url, parser: parser(fake_entry) entries = list(client.scandir()) assert entries[0].url == URL("ftp://localhost:9999/mydir/nested") assert entries[0].is_dir
def test_put(self, mocked_ftp_conn): expected = bytes("hello", "utf-8") result = io.BytesIO() client = ftp_client.FTPClient("ftp://*****:*****@localhost/myfile.txt") client.connect().storbinary = lambda _, f: result.write(f.read()) with client: reader = io.BytesIO(expected) client.put(reader) result.seek(0) assert result.getvalue() == expected
def test_get_invalid_path(self, url, path, mocked_ftp_conn): with ftp_client.FTPClient(url) as client: with pytest.raises(exceptions.FTPError): client.get(io.StringIO(), file_path=path)
def test_invalid_scheme(self, url): with pytest.raises(ValueError): ftp_client.FTPClient(url)